]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt: fix error on partial Go code ending with line comment.
authorDmitri Shuralyov <shurcooL@gmail.com>
Fri, 22 Aug 2014 17:18:00 +0000 (10:18 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 22 Aug 2014 17:18:00 +0000 (10:18 -0700)
Fix issue by always appending newline after user input, before
the closing curly bracket. The adjust func is modified to remove
this new newline.

Add test case (it fails before CL, passes after).

Fixes #8411.

LGTM=gri
R=golang-codereviews, bradfitz, josharian, gri
CC=golang-codereviews
https://golang.org/cl/124700043

src/cmd/gofmt/gofmt.go
src/cmd/gofmt/testdata/stdin5.golden [new file with mode: 0644]
src/cmd/gofmt/testdata/stdin5.input [new file with mode: 0644]

index 576cae5228e8dd6db916d7f3792283f5dc973736..f7d30d8405276423523f3546a05eedc4569998ea 100644 (file)
@@ -277,14 +277,14 @@ func parse(fset *token.FileSet, filename string, src []byte, stdin bool) (*ast.F
        // into a function body.  This handles expressions too.
        // Insert using a ;, not a newline, so that the line numbers
        // in fsrc match the ones in src.
-       fsrc := append(append([]byte("package p; func _() {"), src...), '}')
+       fsrc := append(append([]byte("package p; func _() {"), src...), '\n', '}')
        file, err = parser.ParseFile(fset, filename, fsrc, parserMode)
        if err == nil {
                adjust := func(orig, src []byte) []byte {
                        // Remove the wrapping.
                        // Gofmt has turned the ; into a \n\n.
                        src = src[len("package p\n\nfunc _() {"):]
-                       src = src[:len(src)-len("}\n")]
+                       src = src[:len(src)-len("\n}\n")]
                        // Gofmt has also indented the function body one level.
                        // Remove that indent.
                        src = bytes.Replace(src, []byte("\n\t"), []byte("\n"), -1)
diff --git a/src/cmd/gofmt/testdata/stdin5.golden b/src/cmd/gofmt/testdata/stdin5.golden
new file mode 100644 (file)
index 0000000..31ce6b2
--- /dev/null
@@ -0,0 +1,3 @@
+//gofmt -stdin
+
+i := 5 // Line comment without newline.
\ No newline at end of file
diff --git a/src/cmd/gofmt/testdata/stdin5.input b/src/cmd/gofmt/testdata/stdin5.input
new file mode 100644 (file)
index 0000000..0a7c97d
--- /dev/null
@@ -0,0 +1,3 @@
+//gofmt -stdin
+
+i  :=5// Line comment without newline.
\ No newline at end of file