Skip to content

Commit

Permalink
Fixed sort dropping head comment #1181
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Apr 14, 2022
1 parent 6f9f80c commit ea66a73
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/yqlib/candidate_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ func (n *CandidateNode) CreateChildInArray(index int, node *yaml.Node) *Candidat

func (n *CandidateNode) CreateReplacement(node *yaml.Node) *CandidateNode {
return &CandidateNode{
Node: node,
Path: n.createChildPath(nil),
Parent: n.Parent,
Key: n.Key,
IsMapKey: n.IsMapKey,
Document: n.Document,
Filename: n.Filename,
FileIndex: n.FileIndex,
Node: node,
Path: n.createChildPath(nil),
LeadingContent: n.LeadingContent,
Parent: n.Parent,
Key: n.Key,
IsMapKey: n.IsMapKey,
Document: n.Document,
Filename: n.Filename,
FileIndex: n.FileIndex,
}
}

Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/operator_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func getCommentsOperator(d *dataTreeNavigator, context Context, expressionNode *

node := &yaml.Node{Kind: yaml.ScalarNode, Value: comment, Tag: "!!str"}
result := candidate.CreateReplacement(node)
result.LeadingContent = "" // don't include the leading yaml content when retrieving a comment
results.PushBack(result)
}
return context.ChildContext(results), nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[], (!!seq)::[{a: false, b: 1}, {a: false, b: 3}, {a: true, b: 2}]\n",
},
},
{
skipDoc: true,
description: "head comment",
document: "# abc\n- def\n# ghi",
expression: `sort`,
expected: []string{
"D0, P[], (!!seq)::# abc\n- def\n# ghi\n",
},
},
}

func TestSortByOperatorScenarios(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/stream_evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, p

for index, filename := range filenames {
reader, leadingContent, err := readStream(filename, leadingContentPreProcessing)
log.Debug("leadingContent: %v", leadingContent)

if index == 0 {
firstFileLeadingContent = leadingContent
Expand Down

0 comments on commit ea66a73

Please sign in to comment.