aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/gofmt
AgeCommit message (Collapse)Author
2021-06-22[dev.typeparams] cmd/gofmt: remove typeparams guardsRob Findley
Remove logic related to guarding against allowing type parameters from cmd/gofmt. At this point, it was only restricting tests. Change-Id: Idd198389aaa422636d61af547a37be49f3be6c97 Reviewed-on: https://go-review.googlesource.com/c/go/+/329931 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-04-30cmd/gofmt: always format non-directory arguments againDaniel Martí
golang.org/cl/284138 introduced a regression: running "gofmt foo" would silently ignore the file due to its lack of a ".go" extension, whereas the tool is documented otherwise: Given a file, it operates on that file; given a directory, it operates on all .go files in that directory, recursively. This wasn't caught as there were no tests for these edge cases. gofmt's own tests are regular Go tests, so it's hard to test it properly without adding an abstraction layer on top of func main. Luckily, this kind of test is a great fit for cmd/go's own script tests, and it just takes a few straightforward lines. Finally, add the relevant logic back, with documentation to clarify its intentional purpose. Fixes #45859. Change-Id: Ic5bf5937b8f95fcdad2b6933227c8b504ef38a82 Reviewed-on: https://go-review.googlesource.com/c/go/+/315270 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Trust: Daniel Martí <mvdan@mvdan.cc> Trust: Robert Griesemer <gri@golang.org> Trust: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
2021-04-28cmd/gofmt: simplify arg handlingDaniel Martí
First, we can use flag.Args instead of flag.NArg and flag.Arg. Second, just call filepath.WalkDir directly on each argument. We don't need to check if each argument is a directory or not, since the function will still work on regular files as expected. To continue giving an error in the "gofmt does-not-exist.go" case, we now need to return and handle errors from filepath.WalkDir, too. Arguably, that should have always been the case. While at it, I noticed that the printinf of the "diff" command did not obey the "out" parameter. Fix that. Finally, remove the code to ignore IsNotExist errors. It was added in CL 19301, though it didn't include tests and its reasoning is dubious. Using gofmt on a directory treewhile another program is concurrently editing or removing files is inherently racy. Hiding errors can hide valid problems from the user, and such racy usages aren't supported. Change-Id: I2e74cc04c53eeefb25231d804752b53562b97371 Reviewed-on: https://go-review.googlesource.com/c/go/+/284138 Run-TryBot: Daniel Martí <mvdan@mvdan.cc> Trust: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Bryan C. Mills <bcmills@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2021-04-13go/*,cmd/gofmt: guard AST changes with the typeparams build tagRob Findley
This CL changes our approach to guarding type parameter functionality and API. Previously, we guarded type parameter functionality with the parser.parseTypeParams parser mode, and were in the process of hiding the type parameter API behind the go1.18 build constraint. These mechanisms had several limitations: + Requiring the parser.parseTypeParams mode to be set meant that existing tooling would have to opt-in to type parameters in all places where it parses Go files. + The parseTypeParams mode value had to be copied in several places. + go1.18 is not specific to typeparams, making it difficult to set up the builders to run typeparams tests. This CL addresses the above limitations, and completes the task of hiding the AST API, by switching to a new 'typeparams' build constraint and adding a new go/internal/typeparams helper package. The typeparams build constraint is used to conditionally compile the new AST changes. The typeparams package provides utilities for accessing and writing the new AST data, so that we don't have to fragment our parser or type checker logic across build constraints. The typeparams.Enabled const is used to guard tests that require type parameter support. The parseTypeParams parser mode is gone, replaced by a new typeparams.DisableParsing mode with the opposite sense. Now, type parameters are only parsed if go/parser is compiled with the typeparams build constraint set AND typeparams.DisableParsing not set. This new parser mode allows opting out of type parameter parsing for tests. How exactly to run tests on builders is left to a follow-up CL. Updates #44933 Change-Id: I3091e42a2e5e2f23e8b2ae584f415a784b9fbd65 Reviewed-on: https://go-review.googlesource.com/c/go/+/300649 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-10cmd/gofmt: fix const association to avoid inaccurate commentRob Findley
The const parseTypeParams was grouped with printer-related consts in gofmt.go, implicitly suggesting that it must be kept in sync with go/format/format.go. Change-Id: Ia65dc15c27fef2c389f963071252adee32ec6bd6 Reviewed-on: https://go-review.googlesource.com/c/go/+/300451 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-03-02go/parser,go/types: hide API changes related to type parametersRob Findley
While the dev.typeparams branch was merged, the type parameter API is slated for go1.18. Hide these changes to the go/parser and go/types API. This was done as follows: + For APIs that will probably not be needed for go1.18, simply unexport them. + For APIs that we expect to export in go1.18, prefix symbols with '_', so that the planned (upper-cased) symbol name is apparent. + For APIs that must be exported for testing, move both API and tests to files guarded by the go1.18 build constraint. + parser.ParseTypeParams is unexported and copied wherever it is needed. + The -G flag is removed from gofmt, replaced by enabling type parameters if built with the go1.18 build constraint. Notably, changes related to type parameters in go/ast are currently left exported. We're looking at the AST API separately. The new API diff from 1.16 is: +pkg go/ast, method (*FuncDecl) IsMethod() bool +pkg go/ast, method (*ListExpr) End() token.Pos +pkg go/ast, method (*ListExpr) Pos() token.Pos +pkg go/ast, type FuncType struct, TParams *FieldList +pkg go/ast, type ListExpr struct +pkg go/ast, type ListExpr struct, ElemList []Expr +pkg go/ast, type TypeSpec struct, TParams *FieldList +pkg go/types, type Config struct, GoVersion string Change-Id: I1baf67e26279b49092e774309a836c460979774a Reviewed-on: https://go-review.googlesource.com/c/go/+/295929 Trust: Robert Findley <rfindley@google.com> Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2021-02-10[dev.typeparams] cmd/gofmt: add the -G flag to allow generic codeRob Findley
Add support for type parameters to cmd/gofmt, gated behind the -G flag. The test was based on a test from go/printer, slightly modified to exercise more formatting. Change-Id: I489bcb3ad06e1ed4e6d9f5bc79825e60dcfe9953 Reviewed-on: https://go-review.googlesource.com/c/go/+/291011 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Go Bot <gobot@golang.org>
2020-12-09all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.MkdirTempRuss Cox
As part of #42026, these helpers from io/ioutil were moved to os. (ioutil.TempFile and TempDir became os.CreateTemp and MkdirTemp.) Update the Go tree to use the preferred names. As usual, code compiled with the Go 1.4 bootstrap toolchain and code vendored from other sources is excluded. ReadDir changes are in a separate CL, because they are not a simple search and replace. For #42026. Change-Id: If318df0216d57e95ea0c4093b89f65e5b0ababb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/266365 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2020-12-02all: update to use filepath.WalkDir instead of filepath.WalkRuss Cox
Now that filepath.WalkDir is available, it is more efficient and should be used in place of filepath.Walk. Update the tree to reflect best practices. As usual, the code compiled with Go 1.4 during bootstrap is excluded. (In this CL, that's only cmd/dist.) For #42027. Change-Id: Ib0f7b1e43e50b789052f9835a63ced701d8c411c Reviewed-on: https://go-review.googlesource.com/c/go/+/267719 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2020-10-20all: update references to symbols moved from io/ioutil to ioRuss Cox
The old ioutil references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. Also excluded vendored code. For #41190. Change-Id: I6d86f2bf7bc37a9d904b6cee3fe0c7af6d94d5b1 Reviewed-on: https://go-review.googlesource.com/c/go/+/263142 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2020-10-20all: update references to symbols moved from os to io/fsRuss Cox
The old os references are still valid, but update our code to reflect best practices and get used to the new locations. Code compiled with the bootstrap toolchain (cmd/asm, cmd/dist, cmd/compile, debug/elf) must remain Go 1.4-compatible and is excluded. For #41190. Change-Id: I8f9526977867c10a221e2f392f78d7dec073f1bd Reviewed-on: https://go-review.googlesource.com/c/go/+/243907 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
2020-07-17go/printer: remove exported StdFormat flagDmitri Shuralyov
The StdFormat flag was added as part of CL 231461, where the primary aim was to fix the bug #37476. It's expected that the existing printer modes only adjust spacing but do not change any of the code text itself. A new printing flag served as a way for cmd/gofmt and go/format to delegate a part of formatting work to the printer—where it's more more convenient and efficient to perform—while maintaining current low-level printing behavior of go/printer unmodified. We already have cmd/gofmt and the go/format API that implement standard formatting of Go source code, so there isn't a need to expose StdFormat flag to the world, as it can only cause confusion. Consider that to format source in canonical gofmt style completely it may require tasks A, B, C to be done. In one version of Go, the printer may do both A and B, while cmd/gofmt and go/format will do the remaining task C. In another version, the printer may take on doing just A, while cmd/gofmt and go/format will perform B and C. This makes it hard to add a gofmt-like mode to the printer without compromising on above fluidity. This change prefers to shift back some complexity to the implementation of the standard library, allowing us to avoid creating the new exported printing flag just for the internal needs of gofmt and go/format today. We may still want to re-think the API and consider if something better should be added, but unfortunately there isn't time for Go 1.15. We are not adding new APIs now, so we can defer this decision until Go 1.16 or later, when there is more time. For #37476. For #37453. For #39489. For #37419. Change-Id: I0bb07156dca852b043487099dcf05c5350b29e20 Reviewed-on: https://go-review.googlesource.com/c/go/+/240683 Reviewed-by: Robert Griesemer <gri@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2020-05-01cmd/gofmt, go/format, go/printer: move number normalization to printerDmitri Shuralyov
Normalization of number prefixes and exponents was added in CL 160184 directly in cmd/gofmt. The same behavior change needs to be applied in the go/format package. This is done by moving the normalization code into go/printer, behind a new StdFormat mode, which is then re-used by both cmd/gofmt and go/format. Note that formatting of Go source code changes over time, so the exact byte output produced by go/printer may change between versions of Go when using StdFormat mode. What is guaranteed is that the new formatting is equivalent Go code. Clients looking to format Go code with standard formatting consistent with cmd/gofmt and go/format would need to start using this flag, but a better alternative is to use the go/format package instead. Benchstat numbers on go test go/printer -bench=BenchmarkPrint: name old time/op new time/op delta Print-8 4.56ms ± 1% 4.57ms ± 0% ~ (p=0.700 n=3+3) name old alloc/op new alloc/op delta Print-8 467kB ± 0% 467kB ± 0% ~ (p=1.000 n=3+3) name old allocs/op new allocs/op delta Print-8 17.2k ± 0% 17.2k ± 0% ~ (all equal) That benchmark data doesn't contain any numbers that need to be normalized. More work needs to be performed when formatting Go code with numbers, but it is unavoidable to produce standard formatting. Fixes #37476. For #37453. Change-Id: If50bde4035c3ee6e6ff0ece5691f6d3566ffe8d5 Reviewed-on: https://go-review.googlesource.com/c/go/+/231461 Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2020-02-28cmd/gofmt, go/format: sync internal.goDmitri Shuralyov
Apply CL 40930 to src/cmd/gofmt/internal.go to bring it into sync with src/go/format/internal.go. Also revert '\n' back to "\n\n" in one of the comments, because the previous text was more accurate. Gofmt replaces the "; " part of "package p; func _() {" input with two newline characters, not one. Updates #11844 Change-Id: I6bb8155a931b793311991d3cd8e006a2931b167a Reviewed-on: https://go-review.googlesource.com/c/go/+/221497 Reviewed-by: Robert Griesemer <gri@golang.org>
2019-10-28cmd/fix, cmd/go, cmd/gofmt: refactor common code into new internal diff packageMikhail Fesenko
Change-Id: Idac8473d1752059bf2f617fd7a781000ee2c3af4 GitHub-Last-Rev: 02a3aa1a3241d3ed4422518f1c954cd54bbe858e GitHub-Pull-Request: golang/go#35141 Reviewed-on: https://go-review.googlesource.com/c/go/+/203218 Run-TryBot: Ian Lance Taylor <iant@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-10-28go/ast: fix SortImports to handle block comments (take 2)Agniva De Sarker
This is a 2nd attempt at fixing CL 162337 which had an off-by-one error. We were unconditionally getting the position of the start of the next line from the last import without checking whether it is the end of the file or not. Fix the code to check for that and move the testcase added in CL 190523 to the end of the file for it to trigger the issue properly. Fixes #18929 Change-Id: I59e77256e256570b160fea6a17bce9ef49e810df Reviewed-on: https://go-review.googlesource.com/c/go/+/190480 Run-TryBot: Agniva De Sarker <agniva.quicksilver@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-09-24cmd/gofmt: fix computation of function header sizeEli Bendersky
Function sizes are computed to determine whether a function can be kept on one line or should be split to several lines. Part of the computation is the function header from the FUNC token and until the opening { token. Prior to this change, the function header size used distance from the original source position of the current token, which led to issues when the source between FUNC and the original source position was rewritten (such as whitespace being collapsed). Now we take the current output position into account, so that header size represents the reformatted source rather than the original source. The following files in the Go repository are reformatted with this change: * strings/strings_test.go * cmd/compile/internal/gc/fmt.go In both cases the reformatting is minor and seems to be correct given the heuristic to single-line functions longer than 100 columns to multiple lines. Fixes #28082 Change-Id: Ib737f6933e09b79e83715211421d5262b366ec93 Reviewed-on: https://go-review.googlesource.com/c/go/+/188818 Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-11cmd/gofmt: don't turn nil slices into empty slices during rewritingDominik Honnef
The go/ast package uses and guarantees nil slices for optional elements that weren't present in the parsed source code, such as the list of return values of a function. Packages using go/ast rely on this attribute and check for nils explicitly. One such package is go/printer. In the presence of empty slices instead of nil slices, it generates invalid code, such as "case :" instead of "default:". The issues that this CL fixes are all manifestations of that problem, each for a different syntactic element. Fixes #33103 Fixes #33104 Fixes #33105 Change-Id: I219f95a7da820eaf697a4ee227d458ab6e4a80bd Reviewed-on: https://go-review.googlesource.com/c/go/+/187917 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-16cmd/gofmt: update TestRewrite to avoid future regressionsJoe Tsai
CL 162337 changed go/ast to better handle block comments, but was reverted because it introduced an off-by-one bug. This CL adds a test case to enforce the correct behavior so that future changes do not break this again. Updates #18929 Updates #33538 Change-Id: I2d25c139d007f8db1091b7a48b1dd20c584e2699 Reviewed-on: https://go-review.googlesource.com/c/go/+/190523 Run-TryBot: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-08-08Revert "go/ast: fix SortImports to handle block comments"Joe Tsai
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>
2019-06-21cmd/gofmt: fix normalization of imaginary number literalsRobert Griesemer
The old code only normalized decimal integer imaginary number literals. But with the generalized imaginary number syntax, the number value may be decimal, binary, octal, or hexadecimal, integer or floating-point. The new code only looks at the number pattern. Only for decimal integer imaginary literals do we need to strip leading zeroes. The remaining normalization code simply ignore the 'i' suffix. As a result, the new code is both simpler and shorter. Fixes #32718. Change-Id: If43fc962a48ed62002e65d5c81fddbb9bd283984 Reviewed-on: https://go-review.googlesource.com/c/go/+/183378 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-03-28go/ast: fix SortImports to handle block commentsAgniva De Sarker
The current algorithm only assumed line comments which always appear at the end of an import spec. This caused block comments which can appear before a spec to be attached to the previous spec. So while mapping a comment to an import spec, we maintain additional information on whether the comment is supposed to appear on the left or right of the spec. And we also take into account the possibility of "//line" comments in the source. So we use unadjusted line numbers. While at it, added some more testcases from tools/go/ast/astutil/imports_test.go Fixes #18929 Change-Id: If920426641702a8a93904b2ec1d3455749169f69 Reviewed-on: https://go-review.googlesource.com/c/go/+/162337 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2019-02-19cmd/gofmt: normalize integer imaginary literals starting with 0Robert Griesemer
An 'i' suffix on an integer literal marks the integer literal as a decimal integer imaginary value, even if the literal without the suffix starts with a 0 and thus looks like an octal value: 0123i == 123i // != 0123 * 1i This is at best confusing, and at worst a potential source of bugs. It is always safe to rewrite such literals into the equivalent literal without the leading 0. This CL implements this normalization. Change-Id: Ib77ad535f98b5be912ecbdec20ca1b472c1b4973 Reviewed-on: https://go-review.googlesource.com/c/162538 Run-TryBot: Robert Griesemer <gri@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-02-11cmd/gofmt: normalize number prefixes and exponentsRobert Griesemer
Rewrite non-decimal number prefixes to always use a lower-case base ("0X" -> "0x", etc.), and rewrite exponents to use a lower-case 'e' or 'p'. Leave hexadecimal digits and 0-octals alone. Comparing the best time of 3 runs of `time go test -run All` with the time for a gofmt that doesn't do the rewrite shows no increase in runtime for this bulk gofmt application (in fact on my machine I see a small decline, probably due to cache effects). R=Go1.13 Updates #12711. Updates #19308. Updates #29008. Change-Id: I9c6ebed2ffa0a6a001c59412a73382090955f5a9 Reviewed-on: https://go-review.googlesource.com/c/160184 Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-02-11cmd/gofmt: test that Go 2 number literals can be formattedRobert Griesemer
R=Go1.13 Updates #12711. Updates #19308. Updates #28493. Updates #29008. Change-Id: Icd25aa7f6e18ed671ea6cf2b1b292899daf4b1a5 Reviewed-on: https://go-review.googlesource.com/c/160018 Reviewed-by: Russ Cox <rsc@golang.org>
2018-09-26all: use strings.ReplaceAll and bytes.ReplaceAll where applicableBrad Fitzpatrick
I omitted vendor directories and anything necessary for bootstrapping. (Tested by bootstrapping with Go 1.4) Updates #27864 Change-Id: I7d9b68d0372d3a34dee22966cca323513ece7e8a Reviewed-on: https://go-review.googlesource.com/137856 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-08-22cmd/gofmt: update error handling when writing to backup fileShivansh Rai
As per commit aa0ae75, handling of io.ErrShortWrite is done in *File.Write() itself. Change-Id: I92924b51e8df2ae88e6e50318348f44973addba8 Reviewed-on: https://go-review.googlesource.com/113696 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-08-21cmd/gofmt: skip gofmt idempotency check on known issueAlberto Donizetti
gofmt's TestAll runs gofmt on all the go files in the tree and checks, among other things, that gofmt is idempotent (i.e. that a second invocation does not change the input again). There's a known bug of gofmt not being idempotent (Issue #24472), and unfortunately the fixedbugs/issue22662.go file triggers it. We can't just gofmt the file, because it tests the effect of various line directives inside weirdly-placed comments, and gofmt moves those comments, making the test useless. Instead, just skip the idempotency check when gofmt-ing the problematic file. This fixes go test on the cmd/gofmt package, and a failure seen on the longtest builder. Updates #24472 Change-Id: Ib06300977cd8fce6c609e688b222e9b2186f5aa7 Reviewed-on: https://go-review.googlesource.com/130377 Reviewed-by: Daniel Martí <mvdan@mvdan.cc> Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Daniel Martí <mvdan@mvdan.cc> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-03-30cmd/fix,cmd/gofmt: flush to disk before diffingEgon Elbre
Flush file content to disk before diffing files, may cause unpredictable results on Windows. Convert from \r\n to \n when comparing diff result. Change-Id: Ibcd6154a2382dba1338ee5674333611aea16bb65 Reviewed-on: https://go-review.googlesource.com/36750 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org>
2017-03-20cmd/gofmt: clarify doc string even moreRobert Griesemer
Since "columns of alignment" are terminated whenever indentation changes from one line to the next, alignment with spaces will work independent of the actually chosen tab width. Don't mention tab width anymore. Follow-up on https://golang.org/cl/38374/. For #19618. Change-Id: I58e47dfde57834f56a98d9119670757a12fb9c41 Reviewed-on: https://go-review.googlesource.com/38379 Reviewed-by: Rob Pike <r@golang.org>
2017-03-20cmd/gofmt: clarify documentation re: tab widthRobert Griesemer
Fixes #19618. Change-Id: I0ac450ff717ec1f16eb12758c6bf5e98b5de20e8 Reviewed-on: https://go-review.googlesource.com/38374 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-03-18cmd/gofmt: unindent the second line of a BUG noteDamien Lespiau
Currently, this second line is treated a pre-formatted text as it's indented relatively to the BUG() line. The current state can be seen at: https://golang.org/cmd/gofmt/#pkg-note-BUG Unindenting makes the rest of the sentence part of the same paragraph. Change-Id: I6dee55c9c321b1a03b41c7124c6a1ea15772c878 Reviewed-on: https://go-review.googlesource.com/38353 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-02-08cmd/gofmt: fix diff on Plan 9David du Colombier
On Plan 9, GNU diff is called ape/diff. Fixes #18999. Change-Id: I7cf6c23c97bcc47172bbf838fd9dd72aefa4c18b Reviewed-on: https://go-review.googlesource.com/36650 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: David du Colombier <0intro@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-02-08cmd/gofmt: use actual filename in gofmt -d outputhaya14busa
By using actual filename, diff output of "gofmt -d" can be used with other commands like "diffstat" and "patch". Example: $ gofmt -d path/to/file.go | diffstat $ gofmt -d path/to/file.go > gofmt.patch $ patch -u -p0 < gofmt.patch Fixes #18932 Change-Id: I21ce15eb77870d72f2c14bfd5e7c21e2c77dc9ab Reviewed-on: https://go-review.googlesource.com/36374 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2017-02-08cmd/gofmt: clear pattern match map at the correct timeMatthew Dempsky
We need to clear the pattern match map after the recursive rewrite applications, otherwise there might be lingering entries that cause match to fail. Fixes #18987. Change-Id: I7913951c455c98932bda790861db6a860ebad032 Reviewed-on: https://go-review.googlesource.com/36546 Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2017-01-10[dev.typealias] cmd/gofmt: added test cases for alias type declarationsRobert Griesemer
For #18130. Change-Id: I95e84130df40db5241e0cc25c36873c3281199ff Reviewed-on: https://go-review.googlesource.com/34987 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
2016-11-23cmd/gofmt: don't call Chmod on windowsBrad Fitzpatrick
Fixes #18026 Change-Id: Id510f427ceffb2441c3d6f5bb5c93244e46c6497 Reviewed-on: https://go-review.googlesource.com/33477 TryBot-Result: Gobot Gobot <gobot@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
2016-11-11cmd/gofmt, crypto/tls: fix typosKevin Burke
Fix spelling of "original" and "occurred" in new gofmt docs. The same misspelling of "occurred" was also present in crypto/tls, I fixed it there as well. Change-Id: I67b4f1c09bd1a2eb1844207d5514f08a9f525ff9 Reviewed-on: https://go-review.googlesource.com/33138 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-11-10cmd/gofmt: don't overwrite read-only filesRobert Griesemer
This reverts the changes from https://golang.org/cl/33018: Instead of writing the result of gofmt to a tmp file and then rename that to the original (which doesn't preserve the original file's perm bits, uid, gid, and possibly other properties because it is hard to do in a platform-independent way - see #17869), use the original code that simply overwrites the processed file if gofmt was able to create a backup first. Upon success, the backup is removed, otherwise it remains. Fixes #17873. For #8984. Change-Id: Ifcf2bf1f84f730e6060f3517d63b45eb16215ae1 Reviewed-on: https://go-review.googlesource.com/33098 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-11-10cmd/gofmt: don't leave tmp file if -w failedRobert Griesemer
Follow-up on https://golang.org/cl/33018. For #8984. Change-Id: I6655a5537a60d4ea3ee13029a56a75b150f8c8f8 Reviewed-on: https://go-review.googlesource.com/33020 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-11-10cmd/gofmt: don't eat source if -w failsRobert Griesemer
Write output to a temp file first and only upon success rename that file to source file name. Fixes #8984. Change-Id: Ie40e49d2a4eb3c9462fe769ccbf055b4366eceb0 Reviewed-on: https://go-review.googlesource.com/33018 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2016-10-12cmd/gofmt: simplify map key literalsGustav Westling
Simplify map key literals in "gofmt -s" Fixes #16461. Change-Id: Ia61739b34a30ac27f6696f94a98809109a8a7b61 Reviewed-on: https://go-review.googlesource.com/25530 Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
2016-07-11gofmt: remove unneeded call to os.ExitFrancesc Campoy
PrintDefaults already calls os.Exit(2). Change-Id: I0d783a6476f42b6157853cdb34ba69618e3f3fcb Reviewed-on: https://go-review.googlesource.com/24844 Reviewed-by: Andrew Gerrand <adg@golang.org>
2016-04-06cmd/gofmt: make gofmt -s simplify slices in presence of dot-importsRobert Griesemer
A dot-import cannot possibly introduce a `len` function since that function would not be exported (it's lowercase). Furthermore, the existing code already (incorrectly) assumed that there was no other `len` function in another file of the package. Since this has been an ok assumption for years, let's leave it, but remove the dot-import restriction. Fixes #15153. Change-Id: I18fbb27acc5a5668833b4b4aead0cca540862b52 Reviewed-on: https://go-review.googlesource.com/21613 Reviewed-by: Alan Donovan <adonovan@google.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-04-01all: use bytes.Equal, bytes.Contains and strings.ContainsDominik Honnef
Change-Id: Iba82a5bd3846f7ab038cc10ec72ff6bcd2c0b484 Reviewed-on: https://go-review.googlesource.com/21377 Run-TryBot: Dave Cheney <dave@cheney.net> Reviewed-by: Dave Cheney <dave@cheney.net>
2016-03-02all: single space after period.Brad Fitzpatrick
The tree's pretty inconsistent about single space vs double space after a period in documentation. Make it consistently a single space, per earlier decisions. This means contributors won't be confused by misleading precedence. This CL doesn't use go/doc to parse. It only addresses // comments. It was generated with: $ perl -i -npe 's,^(\s*// .+[a-z]\.) +([A-Z]),$1 $2,' $(git grep -l -E '^\s*//(.+\.) +([A-Z])') $ go test go/doc -update Change-Id: Iccdb99c37c797ef1f804a94b22ba5ee4b500c4f7 Reviewed-on: https://go-review.googlesource.com/20022 Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Dave Day <djd@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-03-01all: make copyright headers consistent with one space after periodBrad Fitzpatrick
This is a subset of https://golang.org/cl/20022 with only the copyright header lines, so the next CL will be smaller and more reviewable. Go policy has been single space after periods in comments for some time. The copyright header template at: https://golang.org/doc/contribute.html#copyright also uses a single space. Make them all consistent. Change-Id: Icc26c6b8495c3820da6b171ca96a74701b4a01b0 Reviewed-on: https://go-review.googlesource.com/20111 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Reviewed-by: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
2016-02-19cmd/gofmt: Ignore file not found errors.Benoit Sigoure
gofmt prints an error to stderr when a file is deleted during its `filepath.Walk()', which can happen in builds that change the tree concurrently with gofmt running. Change-Id: Ia1aa4804f6bc2172baf061c093e16fe56a3ee50c Reviewed-on: https://go-review.googlesource.com/19301 Reviewed-by: Robert Griesemer <gri@golang.org>
2015-12-07go/parser, go/types: report invalid else branch in if statementsRobert Griesemer
- Only accept valid if statement syntax in go/parser. - Check AST again in go/types since it may have been modified and the AST doesn't preclude other statements in the else branch of an if statement. - Removed a test from gofmt which verified that old-style if statements permitting any statement in the else branch were correctly reformatted. It's been years since we switched to the current syntax; no need to support this anymore. - Added a comment to go/printer. Fixes #13475. Change-Id: Id2c8fbcc68b719cd511027d0412a37266cceed6b Reviewed-on: https://go-review.googlesource.com/17408 Reviewed-by: Russ Cox <rsc@golang.org>
2015-09-30go/format: handle whitespace-only input correctlyRobert Griesemer
Applied identical change to cmd/gofmt/internal.go. Fixes #11275. Change-Id: Icb4bf0460c94c9e2830dd0d62c69376774cbda30 Reviewed-on: https://go-review.googlesource.com/15154 Reviewed-by: Alan Donovan <adonovan@google.com>