Skip to content

Instantly share code, notes, and snippets.

@arachsys
Created Jul 21, 2021
Embed
What would you like to do?
Restore support for space indentation in go fmt
diff --git a/src/cmd/gofmt/doc.go b/src/cmd/gofmt/doc.go
index e340665..5cd3c6d 100644
--- a/src/cmd/gofmt/doc.go
+++ b/src/cmd/gofmt/doc.go
@@ -22,6 +22,8 @@ The flags are:
to standard output.
-e
Print all (including spurious) errors.
+ -i width
+ Indent with spaces instead of tabs.
-l
Do not print reformatted sources to standard output.
If a file's formatting is different from gofmt's, print its name
diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go
index 8c56af7..022141f 100644
--- a/src/cmd/gofmt/gofmt.go
+++ b/src/cmd/gofmt/gofmt.go
@@ -32,6 +32,7 @@ var (
simplifyAST = flag.Bool("s", false, "simplify code")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
allErrors = flag.Bool("e", false, "report all errors (not just the first 10 on different lines)")
+ indent = flag.Int("i", 0, "indent with spaces instead of tabs")
// debugging
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
@@ -39,9 +40,6 @@ var (
// Keep these in sync with go/format/format.go.
const (
- tabWidth = 8
- printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
-
// printerNormalizeNumbers means to canonicalize number literal prefixes
// and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
//
@@ -50,6 +48,8 @@ const (
)
var (
+ tabWidth = 8
+ printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
fileSet = token.NewFileSet() // per process FileSet
exitCode = 0
rewrite func(*ast.File) *ast.File
@@ -191,6 +191,11 @@ func gofmtMain() {
flag.Usage = usage
flag.Parse()
+ if *indent > 0 {
+ tabWidth = *indent
+ printerMode = printer.UseSpaces | printerNormalizeNumbers
+ }
+
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
@arachsys
Copy link
Author

arachsys commented Jul 21, 2021

Patch extracted from the Arachsys Linux go package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment