]> Cypherpunks repositories - gostls13.git/commitdiff
Revert "go/ast: fix SortImports to handle block comments"
authorJoe Tsai <thebrokentoaster@gmail.com>
Thu, 8 Aug 2019 06:01:24 +0000 (06:01 +0000)
committerJoe Tsai <joetsai@google.com>
Thu, 8 Aug 2019 19:18:56 +0000 (19:18 +0000)
This reverts CL 162337.

Reason for revert: this introduces a regression

Fixes #33538
Updates #18929

Change-Id: Ib2320a840c6d3ec7912e8f414e933d04fbf11ab4
Reviewed-on: https://go-review.googlesource.com/c/go/+/189379
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/gofmt/testdata/import.golden
src/cmd/gofmt/testdata/import.input
src/go/ast/import.go

index f7d742e3e8ded1b6a00980fc36c83abe8b9cb0d7..51d7be79dfab7aea7bec5c32fd02b6fdefebafec 100644 (file)
@@ -1,4 +1,3 @@
-// package comment
 package main
 
 import (
@@ -21,10 +20,6 @@ import (
        "io"
 )
 
-// We reset the line numbering to test that
-// the formatting works independent of line directives
-//line :19
-
 import (
        "errors"
        "fmt"
@@ -129,58 +124,3 @@ import (
 
        "dedup_by_group"
 )
-
-import (
-       "fmt" // for Printf
-       /* comment */ io1 "io"
-       /* comment */ io2 "io"
-       /* comment */ "log"
-)
-
-import (
-       "fmt"
-       /* comment */ io1 "io"
-       /* comment */ io2 "io" // hello
-       "math"                 /* right side */
-       // end
-)
-
-import (
-       "errors" // for New
-       "fmt"
-       /* comment */ io1 "io" /* before */ // after
-       io2 "io"               // another
-       // end
-)
-
-import (
-       "errors" // for New
-       /* left */ "fmt" /* right */
-       "log"            // for Fatal
-       /* left */ "math" /* right */
-)
-
-import /* why */ /* comment here? */ (
-       /* comment */ "fmt"
-       "math"
-)
-
-// Reset it again
-//line :100
-
-// Dedup with different import styles
-import (
-       "path"
-       . "path"
-       _ "path"
-       pathpkg "path"
-)
-
-/* comment */
-import (
-       "fmt"
-       "math" // for Abs
-       // This is a new run
-       "errors"
-       "fmt"
-)
index 6e3a3a3bed8b5e3fa82b9c9b46f6e9b6b339ebc1..9a4b09dbf9108ca8cd642e122a340887dee79b46 100644 (file)
@@ -1,4 +1,3 @@
-// package comment
 package main
 
 import (
@@ -21,10 +20,6 @@ import (
        "io"
 )
 
-// We reset the line numbering to test that
-// the formatting works independent of line directives
-//line :19
-
 import (
        "fmt"
        "math"
@@ -134,60 +129,3 @@ import (
 
        "dedup_by_group"
 )
-
-import (
-       /* comment */ io1 "io"
-       "fmt" // for Printf
-       /* comment */ "log"
-       /* comment */ io2 "io"
-)
-
-import (
-       /* comment */ io2 "io" // hello
-       /* comment */ io1 "io"
-       "math" /* right side */
-       "fmt"
-       // end
-)
-
-import (
-       /* comment */ io1 "io" /* before */ // after
-       "fmt"
-       "errors" // for New
-       io2 "io" // another
-       // end
-)
-
-import (
-       /* left */ "fmt" /* right */
-       "errors" // for New
-       /* left */ "math" /* right */
-       "log" // for Fatal
-)
-
-import /* why */ /* comment here? */ (
-       /* comment */ "fmt"
-       "math"
-)
-
-// Reset it again
-//line :100
-
-// Dedup with different import styles
-import (
-       "path"
-       . "path"
-       _ "path"
-       "path"
-       pathpkg "path"
-)
-
-/* comment */
-import (
-       "math" // for Abs
-       "fmt"
-       // This is a new run
-       "errors"
-       "fmt"
-       "errors"
-)
index 7102884c8596444653bd35972c6e6fa737eaba1f..be23c7fc43207f87af33626b9b676a6450736a66 100644 (file)
@@ -30,7 +30,7 @@ func SortImports(fset *token.FileSet, f *File) {
                i := 0
                specs := d.Specs[:0]
                for j, s := range d.Specs {
-                       if j > i && lineAt(fset, s.Pos()) > 1+lineAt(fset, d.Specs[j-1].End()) {
+                       if j > i && fset.Position(s.Pos()).Line > 1+fset.Position(d.Specs[j-1].End()).Line {
                                // j begins a new run. End this one.
                                specs = append(specs, sortSpecs(fset, f, d.Specs[i:j])...)
                                i = j
@@ -42,8 +42,8 @@ func SortImports(fset *token.FileSet, f *File) {
                // Deduping can leave a blank line before the rparen; clean that up.
                if len(d.Specs) > 0 {
                        lastSpec := d.Specs[len(d.Specs)-1]
-                       lastLine := lineAt(fset, lastSpec.Pos())
-                       rParenLine := lineAt(fset, d.Rparen)
+                       lastLine := fset.Position(lastSpec.Pos()).Line
+                       rParenLine := fset.Position(d.Rparen).Line
                        for rParenLine > lastLine+1 {
                                rParenLine--
                                fset.File(d.Rparen).MergeLine(rParenLine)
@@ -52,10 +52,6 @@ func SortImports(fset *token.FileSet, f *File) {
        }
 }
 
-func lineAt(fset *token.FileSet, pos token.Pos) int {
-       return fset.PositionFor(pos, false).Line
-}
-
 func importPath(s Spec) string {
        t, err := strconv.Unquote(s.(*ImportSpec).Path.Value)
        if err == nil {
@@ -93,11 +89,6 @@ type posSpan struct {
        End   token.Pos
 }
 
-type cgPos struct {
-       left bool // true if comment is to the left of the spec, false otherwise.
-       cg   *CommentGroup
-}
-
 func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
        // Can't short-circuit here even if specs are already sorted,
        // since they might yet need deduplication.
@@ -113,57 +104,39 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
        }
 
        // Identify comments in this range.
-       begSpecs := pos[0].Start
-       endSpecs := pos[len(pos)-1].End
-       beg := fset.File(begSpecs).LineStart(lineAt(fset, begSpecs))
-       end := fset.File(endSpecs).LineStart(lineAt(fset, endSpecs) + 1) // beginning of next line
-       first := len(f.Comments)
-       last := -1
+       // Any comment from pos[0].Start to the final line counts.
+       lastLine := fset.Position(pos[len(pos)-1].End).Line
+       cstart := len(f.Comments)
+       cend := len(f.Comments)
        for i, g := range f.Comments {
-               if g.End() >= end {
-                       break
+               if g.Pos() < pos[0].Start {
+                       continue
                }
-               // g.End() < end
-               if beg <= g.Pos() {
-                       // comment is within the range [beg, end[ of import declarations
-                       if i < first {
-                               first = i
-                       }
-                       if i > last {
-                               last = i
-                       }
+               if i < cstart {
+                       cstart = i
+               }
+               if fset.Position(g.End()).Line > lastLine {
+                       cend = i
+                       break
                }
        }
+       comments := f.Comments[cstart:cend]
 
-       var comments []*CommentGroup
-       if last >= 0 {
-               comments = f.Comments[first : last+1]
-       }
-
-       // Assign each comment to the import spec on the same line.
-       importComments := map[*ImportSpec][]cgPos{}
+       // Assign each comment to the import spec preceding it.
+       importComments := map[*ImportSpec][]*CommentGroup{}
        specIndex := 0
        for _, g := range comments {
                for specIndex+1 < len(specs) && pos[specIndex+1].Start <= g.Pos() {
                        specIndex++
                }
-               var left bool
-               // A block comment can appear before the first import spec.
-               if specIndex == 0 && pos[specIndex].Start > g.Pos() {
-                       left = true
-               } else if specIndex+1 < len(specs) && // Or it can appear on the left of an import spec.
-                       lineAt(fset, pos[specIndex].Start)+1 == lineAt(fset, g.Pos()) {
-                       specIndex++
-                       left = true
-               }
                s := specs[specIndex].(*ImportSpec)
-               importComments[s] = append(importComments[s], cgPos{left: left, cg: g})
+               importComments[s] = append(importComments[s], g)
        }
 
        // Sort the import specs by import path.
        // Remove duplicates, when possible without data loss.
        // Reassign the import paths to have the same position sequence.
-       // Reassign each comment to the spec on the same line.
+       // Reassign each comment to abut the end of its spec.
        // Sort the comments by new position.
        sort.Slice(specs, func(i, j int) bool {
                ipath := importPath(specs[i])
@@ -187,7 +160,7 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
                        deduped = append(deduped, s)
                } else {
                        p := s.Pos()
-                       fset.File(p).MergeLine(lineAt(fset, p))
+                       fset.File(p).MergeLine(fset.Position(p).Line)
                }
        }
        specs = deduped
@@ -201,16 +174,8 @@ func sortSpecs(fset *token.FileSet, f *File, specs []Spec) []Spec {
                s.Path.ValuePos = pos[i].Start
                s.EndPos = pos[i].End
                for _, g := range importComments[s] {
-                       for _, c := range g.cg.List {
-                               if g.left {
-                                       c.Slash = pos[i].Start - 1
-                               } else {
-                                       // An import spec can have both block comment and a line comment
-                                       // to its right. In that case, both of them will have the same pos.
-                                       // But while formatting the AST, the line comment gets moved to
-                                       // after the block comment.
-                                       c.Slash = pos[i].End
-                               }
+                       for _, c := range g.List {
+                               c.Slash = pos[i].End
                        }
                }
        }