Skip to content

Retain comments #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ func handleObject() SectionHandler {
pathCommentAlreadyAdded := false
for _, m := range childSection.Value.(*jwcc.Object).Members {
newMember := &jwcc.Member{Key: m.Key, Value: m.Value}
newMember.Comments().Before = m.Comments().Before
newMember.Comments().Line = m.Comments().Line
newMember.Comments().End = m.Comments().End
newObj.Members = append(newObj.Members, newMember)

if !pathCommentAlreadyAdded {
Expand Down Expand Up @@ -204,15 +207,19 @@ func handleAutoApprovers() SectionHandler {
func upsertMember[V *jwcc.Object | *jwcc.Array](doc *jwcc.Object, key string, val V) {
keyAst := ast.String(key)
index := doc.IndexKey(ast.TextEqual(key))
newMember := &jwcc.Member{Key: keyAst.Quote(), Value: jwcc.Value(val)}
newMember.Comments().Before = jwcc.Value(val).Comments().Before
newMember.Comments().Line = jwcc.Value(val).Comments().Line
newMember.Comments().End = jwcc.Value(val).Comments().End
if index != -1 {
doc.Members[index] = &jwcc.Member{Key: keyAst.Quote(), Value: jwcc.Value(val)}
doc.Members[index] = newMember
} else {
doc.Members = append(doc.Members, &jwcc.Member{Key: keyAst.Quote(), Value: jwcc.Value(val)})
doc.Members = append(doc.Members, newMember)
}
}

func pathComment(val jwcc.Value, path string) {
val.Comments().Before = []string{fmt.Sprintf("from `%s`", path)}
val.Comments().Before = append([]string{fmt.Sprintf("from `%s`", path)}, val.Comments().Before...)
}

func mergeDocs(sections map[string]SectionHandler, parentDoc *ParsedDocument, childDocs []*ParsedDocument) error {
Expand Down