go.tools/imports: fix case where output not gofmt-compatible

Fixes 7866. Adds a test case.

When trying to add newlines before certain imports via text manipulation,
a regex is used to iterate over all imports. The regex failed to match
dot imports because \w doesn't match a literal dot. This changes the regex
to accept a dot as well.

LGTM=bradfitz
R=bradfitz
CC=golang-codereviews
https://golang.org/cl/99400043
This commit is contained in:
Dmitri Shuralyov 2014-05-20 14:02:16 -07:00 committed by Brad Fitzpatrick
parent 33097bf3ed
commit 1154a04eb0
2 changed files with 44 additions and 1 deletions

View File

@ -573,6 +573,49 @@ func main() {
//var _ = bar.A
var _ = qux.B
}
`,
},
// Blank line can be added before all types of import declarations.
// golang.org/issue/7866
{
name: "issue 7866",
in: `package main
import (
"fmt"
renamed_bar "github.com/foo/bar"
. "github.com/foo/baz"
"io"
_ "github.com/foo/qux"
"strings"
)
func main() {
_, _, _, _, _ = fmt.Errorf, io.Copy, strings.Contains, renamed_bar.A, B
}
`,
out: `package main
import (
"fmt"
renamed_bar "github.com/foo/bar"
"io"
. "github.com/foo/baz"
"strings"
_ "github.com/foo/qux"
)
func main() {
_, _, _, _, _ = fmt.Errorf, io.Copy, strings.Contains, renamed_bar.A, B
}
`,
},
}

View File

@ -238,7 +238,7 @@ func matchSpace(orig []byte, src []byte) []byte {
return b.Bytes()
}
var impLine = regexp.MustCompile(`^\s+(?:\w+\s+)?"(.+)"`)
var impLine = regexp.MustCompile(`^\s+(?:[\w\.]+\s+)?"(.+)"`)
func addImportSpaces(r io.Reader, breaks []string) []byte {
var out bytes.Buffer