2020-11-06 00:23:26 +00:00
|
|
|
package yqlib
|
|
|
|
|
2020-11-06 00:45:18 +00:00
|
|
|
import (
|
2021-11-12 04:02:28 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2020-11-06 00:45:18 +00:00
|
|
|
"container/list"
|
2021-11-12 04:02:28 +00:00
|
|
|
"regexp"
|
2020-11-06 00:45:18 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
type commentOpPreferences struct {
|
2020-11-06 00:23:26 +00:00
|
|
|
LineComment bool
|
|
|
|
HeadComment bool
|
|
|
|
FootComment bool
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func assignCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-11-06 00:23:26 +00:00
|
|
|
|
|
|
|
log.Debugf("AssignComments operator!")
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2020-11-06 00:23:26 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 05:56:24 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(commentOpPreferences)
|
2020-11-06 00:23:26 +00:00
|
|
|
|
2021-01-06 09:22:50 +00:00
|
|
|
comment := ""
|
2021-01-12 23:18:53 +00:00
|
|
|
if !expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
|
2021-01-06 09:22:50 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:22:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
2023-10-18 01:11:53 +00:00
|
|
|
comment = rhs.MatchingNodes.Front().Value.(*CandidateNode).Value
|
2021-01-06 09:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debugf("AssignComments comment is %v", comment)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-06 00:23:26 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
2021-01-06 09:22:50 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debugf("AssignComments lhs %v", NodeToString(candidate))
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
if expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
|
2021-01-06 09:22:50 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:22:50 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
2023-10-18 01:11:53 +00:00
|
|
|
comment = rhs.MatchingNodes.Front().Value.(*CandidateNode).Value
|
2021-01-06 09:22:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 00:23:26 +00:00
|
|
|
log.Debugf("Setting comment of : %v", candidate.GetKey())
|
|
|
|
if preferences.LineComment {
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debugf("Setting line comment of : %v to %v", candidate.GetKey(), comment)
|
|
|
|
candidate.LineComment = comment
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
|
|
|
if preferences.HeadComment {
|
2023-10-18 01:11:53 +00:00
|
|
|
candidate.HeadComment = comment
|
2021-11-12 04:02:28 +00:00
|
|
|
candidate.LeadingContent = "" // clobber the leading content, if there was any.
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
if preferences.FootComment {
|
|
|
|
candidate.FootComment = comment
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-11-06 00:23:26 +00:00
|
|
|
}
|
2020-11-06 00:45:18 +00:00
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func getCommentsOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-01-13 05:56:24 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(commentOpPreferences)
|
2023-09-18 23:52:36 +00:00
|
|
|
var startCommentCharacterRegExp = regexp.MustCompile(`^# `)
|
|
|
|
var subsequentCommentCharacterRegExp = regexp.MustCompile(`\n# `)
|
2021-11-12 04:02:28 +00:00
|
|
|
|
2020-11-06 00:45:18 +00:00
|
|
|
log.Debugf("GetComments operator!")
|
|
|
|
var results = list.New()
|
|
|
|
|
2024-02-24 04:36:16 +00:00
|
|
|
yamlPrefs := ConfiguredYamlPreferences.Copy()
|
2022-10-28 03:16:46 +00:00
|
|
|
yamlPrefs.PrintDocSeparators = false
|
|
|
|
yamlPrefs.UnwrapScalar = false
|
2024-02-24 04:36:16 +00:00
|
|
|
yamlPrefs.ColorsEnabled = false
|
2022-10-28 03:16:46 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-06 00:45:18 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
comment := ""
|
|
|
|
if preferences.LineComment {
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debugf("Reading line comment of : %v to %v", candidate.GetKey(), candidate.LineComment)
|
|
|
|
comment = candidate.LineComment
|
2021-11-12 04:02:28 +00:00
|
|
|
} else if preferences.HeadComment && candidate.LeadingContent != "" {
|
|
|
|
var chompRegexp = regexp.MustCompile(`\n$`)
|
|
|
|
var output bytes.Buffer
|
|
|
|
var writer = bufio.NewWriter(&output)
|
2024-02-24 04:36:16 +00:00
|
|
|
var encoder = NewYamlEncoder(yamlPrefs)
|
2022-01-15 00:57:59 +00:00
|
|
|
if err := encoder.PrintLeadingContent(writer, candidate.LeadingContent); err != nil {
|
2021-11-12 04:02:28 +00:00
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
if err := writer.Flush(); err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
comment = output.String()
|
|
|
|
comment = chompRegexp.ReplaceAllString(comment, "")
|
2020-11-06 00:45:18 +00:00
|
|
|
} else if preferences.HeadComment {
|
2023-10-18 01:11:53 +00:00
|
|
|
comment = candidate.HeadComment
|
2020-11-06 00:45:18 +00:00
|
|
|
} else if preferences.FootComment {
|
2023-10-18 01:11:53 +00:00
|
|
|
comment = candidate.FootComment
|
2020-11-06 00:45:18 +00:00
|
|
|
}
|
2023-09-18 23:52:36 +00:00
|
|
|
comment = startCommentCharacterRegExp.ReplaceAllString(comment, "")
|
|
|
|
comment = subsequentCommentCharacterRegExp.ReplaceAllString(comment, "\n")
|
2020-11-06 00:45:18 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
result := candidate.CreateReplacement(ScalarNode, "!!str", comment)
|
|
|
|
if candidate.IsMapKey {
|
|
|
|
result.IsMapKey = false
|
|
|
|
result.Key = candidate
|
|
|
|
}
|
2021-01-12 08:36:28 +00:00
|
|
|
results.PushBack(result)
|
2020-11-06 00:45:18 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-11-06 00:45:18 +00:00
|
|
|
}
|