Skip to content

Commit

Permalink
Preserve top level comments on update of imports (#55)
Browse files Browse the repository at this point in the history
Fixes #54
  • Loading branch information
breml authored Jun 8, 2022
1 parent 4439f10 commit d2ac57b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (r *patchRunner) Apply(filename string, f *ast.File) (*ast.File, bool) {
func cleanupFilePos(tfile *token.File, cl engine.Changelog, comments []*ast.CommentGroup) {
linesToDelete := make(map[int]struct{})
for _, dr := range cl.ChangedIntervals() {
if dr.Start == token.NoPos {
continue
}

for i := tfile.Line(dr.Start); i < tfile.Line(dr.End); i++ {
if i > 0 {
linesToDelete[i] = struct{}{}
Expand Down
32 changes: 32 additions & 0 deletions testdata/replace_import_with_top_level_comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- in.patch --
@@
var randomIdentifier identifier
@@
-import "cmd/internal/edit"
+import "github.com/foo/bar/internal/edit"

edit.randomIdentifier

-- comment.in.go --
// Copyright 2022 ...

package main

import (
"cmd/internal/edit"
)

func main() {
_ = edit.Buffer
}

-- comment.out.go --
// Copyright 2022 ...

package main

import "github.com/foo/bar/internal/edit"

func main() {
_ = edit.Buffer
}

0 comments on commit d2ac57b

Please sign in to comment.