go/format, cmd/gofmt: avoid dependency on internal package format

Fixes #11844.

Change-Id: I32edd39e79f7c9bdc132c49bd06081f35dac245d
Reviewed-on: https://go-review.googlesource.com/15114
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index b2805ac..cfebeff 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -13,7 +13,6 @@
 	"go/printer"
 	"go/scanner"
 	"go/token"
-	"internal/format"
 	"io"
 	"io/ioutil"
 	"os"
@@ -88,7 +87,7 @@
 		return err
 	}
 
-	file, sourceAdj, indentAdj, err := format.Parse(fileSet, filename, src, stdin)
+	file, sourceAdj, indentAdj, err := parse(fileSet, filename, src, stdin)
 	if err != nil {
 		return err
 	}
@@ -107,7 +106,7 @@
 		simplify(file)
 	}
 
-	res, err := format.Format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
+	res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
 	if err != nil {
 		return err
 	}
diff --git a/src/internal/format/format.go b/src/cmd/gofmt/internal.go
similarity index 85%
copy from src/internal/format/format.go
copy to src/cmd/gofmt/internal.go
index a8270ba..fc7f976 100644
--- a/src/internal/format/format.go
+++ b/src/cmd/gofmt/internal.go
@@ -2,7 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package format
+// TODO(gri): This file and the file src/go/format/internal.go are
+// the same (but for this comment and the package name). Do not modify
+// one without the other. Determine if we can factor out functionality
+// in a public API. See also #11844 for context.
+
+package main
 
 import (
 	"bytes"
@@ -13,11 +18,9 @@
 	"strings"
 )
 
-const parserMode = parser.ParseComments
-
-// Parse parses src, which was read from the named file,
+// parse parses src, which was read from the named file,
 // as a Go source file, declaration, or statement list.
-func Parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (
+func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (
 	file *ast.File,
 	sourceAdj func(src []byte, indent int) []byte,
 	indentAdj int,
@@ -85,10 +88,10 @@
 	return
 }
 
-// Format formats the given package file originally obtained from src
+// format formats the given package file originally obtained from src
 // and adjusts the result based on the original source via sourceAdj
 // and indentAdj.
-func Format(
+func format(
 	fset *token.FileSet,
 	file *ast.File,
 	sourceAdj func(src []byte, indent int) []byte,
@@ -109,7 +112,7 @@
 	// Partial source file.
 	// Determine and prepend leading space.
 	i, j := 0, 0
-	for j < len(src) && IsSpace(src[j]) {
+	for j < len(src) && isSpace(src[j]) {
 		if src[j] == '\n' {
 			i = j + 1 // byte offset of last line in leading space
 		}
@@ -150,14 +153,14 @@
 
 	// Determine and append trailing space.
 	i = len(src)
-	for i > 0 && IsSpace(src[i-1]) {
+	for i > 0 && isSpace(src[i-1]) {
 		i--
 	}
 	return append(res, src[i:]...), nil
 }
 
-// IsSpace reports whether the byte is a space character.
-// IsSpace defines a space as being among the following bytes: ' ', '\t', '\n' and '\r'.
-func IsSpace(b byte) bool {
+// isSpace reports whether the byte is a space character.
+// isSpace defines a space as being among the following bytes: ' ', '\t', '\n' and '\r'.
+func isSpace(b byte) bool {
 	return b == ' ' || b == '\t' || b == '\n' || b == '\r'
 }
diff --git a/src/cmd/gofmt/long_test.go b/src/cmd/gofmt/long_test.go
index df9a878..237b860 100644
--- a/src/cmd/gofmt/long_test.go
+++ b/src/cmd/gofmt/long_test.go
@@ -15,7 +15,6 @@
 	"go/ast"
 	"go/printer"
 	"go/token"
-	"internal/format"
 	"io"
 	"os"
 	"path/filepath"
@@ -33,7 +32,7 @@
 )
 
 func gofmt(fset *token.FileSet, filename string, src *bytes.Buffer) error {
-	f, _, _, err := format.Parse(fset, filename, src.Bytes(), false)
+	f, _, _, err := parse(fset, filename, src.Bytes(), false)
 	if err != nil {
 		return err
 	}
@@ -61,7 +60,7 @@
 
 	// exclude files w/ syntax errors (typically test cases)
 	fset := token.NewFileSet()
-	if _, _, _, err = format.Parse(fset, filename, b1.Bytes(), false); err != nil {
+	if _, _, _, err = parse(fset, filename, b1.Bytes(), false); err != nil {
 		if *verbose {
 			fmt.Fprintf(os.Stderr, "ignoring %s\n", err)
 		}
diff --git a/src/go/format/format.go b/src/go/format/format.go
index 1adfd7d..b9cacfe 100644
--- a/src/go/format/format.go
+++ b/src/go/format/format.go
@@ -12,7 +12,6 @@
 	"go/parser"
 	"go/printer"
 	"go/token"
-	"internal/format"
 	"io"
 )
 
@@ -82,7 +81,7 @@
 //
 func Source(src []byte) ([]byte, error) {
 	fset := token.NewFileSet()
-	file, sourceAdj, indentAdj, err := format.Parse(fset, "", src, true)
+	file, sourceAdj, indentAdj, err := parse(fset, "", src, true)
 	if err != nil {
 		return nil, err
 	}
@@ -93,7 +92,7 @@
 		ast.SortImports(fset, file)
 	}
 
-	return format.Format(fset, file, sourceAdj, indentAdj, src, config)
+	return format(fset, file, sourceAdj, indentAdj, src, config)
 }
 
 func hasUnsortedImports(file *ast.File) bool {
diff --git a/src/internal/format/format.go b/src/go/format/internal.go
similarity index 85%
rename from src/internal/format/format.go
rename to src/go/format/internal.go
index a8270ba..2850a43 100644
--- a/src/internal/format/format.go
+++ b/src/go/format/internal.go
@@ -2,6 +2,11 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
+// TODO(gri): This file and the file src/cmd/gofmt/internal.go are
+// the same (but for this comment and the package name). Do not modify
+// one without the other. Determine if we can factor out functionality
+// in a public API. See also #11844 for context.
+
 package format
 
 import (
@@ -13,11 +18,9 @@
 	"strings"
 )
 
-const parserMode = parser.ParseComments
-
-// Parse parses src, which was read from the named file,
+// parse parses src, which was read from the named file,
 // as a Go source file, declaration, or statement list.
-func Parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (
+func parse(fset *token.FileSet, filename string, src []byte, fragmentOk bool) (
 	file *ast.File,
 	sourceAdj func(src []byte, indent int) []byte,
 	indentAdj int,
@@ -85,10 +88,10 @@
 	return
 }
 
-// Format formats the given package file originally obtained from src
+// format formats the given package file originally obtained from src
 // and adjusts the result based on the original source via sourceAdj
 // and indentAdj.
-func Format(
+func format(
 	fset *token.FileSet,
 	file *ast.File,
 	sourceAdj func(src []byte, indent int) []byte,
@@ -109,7 +112,7 @@
 	// Partial source file.
 	// Determine and prepend leading space.
 	i, j := 0, 0
-	for j < len(src) && IsSpace(src[j]) {
+	for j < len(src) && isSpace(src[j]) {
 		if src[j] == '\n' {
 			i = j + 1 // byte offset of last line in leading space
 		}
@@ -150,14 +153,14 @@
 
 	// Determine and append trailing space.
 	i = len(src)
-	for i > 0 && IsSpace(src[i-1]) {
+	for i > 0 && isSpace(src[i-1]) {
 		i--
 	}
 	return append(res, src[i:]...), nil
 }
 
-// IsSpace reports whether the byte is a space character.
-// IsSpace defines a space as being among the following bytes: ' ', '\t', '\n' and '\r'.
-func IsSpace(b byte) bool {
+// isSpace reports whether the byte is a space character.
+// isSpace defines a space as being among the following bytes: ' ', '\t', '\n' and '\r'.
+func isSpace(b byte) bool {
 	return b == ' ' || b == '\t' || b == '\n' || b == '\r'
 }