summaryrefslogtreecommitdiff
path: root/src/go/format
Commit message (Collapse)AuthorAgeFilesLines
* go/printer: canonicalize //go:build and // +build lines while formattingRuss Cox2021-02-201-0/+4
| | | | | | | | | | | | | | | | | | | | | Part of //go:build change (#41184). See https://golang.org/design/draft-gobuild Gofmt and any other go/printer-using program will now: - move //go:build and //+build lines to the appropriate file location - if there's no //go:build line, add one derived from the // +build lines - if there is a //go:build line, recompute and replace any // +build lines to match what the //go:build line says For Go 1.17. Change-Id: Ide5cc3b4a07507ba9ed6f8b0de846e840876f49f Reviewed-on: https://go-review.googlesource.com/c/go/+/240608 Trust: Russ Cox <rsc@golang.org> Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
* all: update to use os.ReadFile, os.WriteFile, os.CreateTemp, os.MkdirTempRuss Cox2020-12-092-5/+5
| | | | | | | | | | | | | | | | | | | | | | 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>
* go/printer: remove exported StdFormat flagDmitri Shuralyov2020-07-172-3/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* cmd/gofmt, go/format, go/printer: move number normalization to printerDmitri Shuralyov2020-05-012-1/+45
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* cmd/gofmt, go/format: sync internal.goDmitri Shuralyov2020-02-281-1/+1
| | | | | | | | | | | | | | | | 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>
* go/format: add simple benchmark framework and basic benchmarkRobert Griesemer2018-12-121-0/+91
| | | | | | | | | | | | | | | | | | | | | | | | For now, this CL adds as a first benchmark the formatting of a 10,000 element array literal. It is easy to add additional test cases as we see fit. name time/op Format/array1-10000-4 26.7ms ± 7% name speed Format/array1-10000-4 2.43MB/s ± 6% name alloc/op Format/array1-10000-4 5.52MB ± 0% name allocs/op Format/array1-10000-4 119k ± 0% Updates #26528. Change-Id: Ic8ec8f70160d122b877740412d4d4406f5f4b345 Reviewed-on: https://go-review.googlesource.com/c/153642 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* doc: explain minor change to gofmt in go1.11Daniel Martí2018-07-201-4/+9
| | | | | | | | | | | | | | | | Add the tools section with a Gofmt sub-section, just like in go1.10.html. Instead of copying the two last paragraphs from 1.10, which warn users about the hidden complexity of enforcing gofmt, move that to go/format and link to it. While at it, remove a duplicate "Tools" header that was likely added by accident. Fixes #26228. Change-Id: Ic511c44b2b86f82a41f2b78dd7e7482d694b6c62 Reviewed-on: https://go-review.googlesource.com/122295 Reviewed-by: Ian Lance Taylor <iant@golang.org>
* go/format: move example to external test filejimmyfrasche2018-03-192-27/+39
| | | | | | | | | | | | Per #11257 all examples should be in external test files. Additionally, doing so makes this example playable. Updates #24352. (Albeit tangentially). Change-Id: I77ab4655107f61db2e9d21a608b73ace3a230fb2 Reviewed-on: https://go-review.googlesource.com/101285 Reviewed-by: Robert Griesemer <gri@golang.org>
* go/format: document use of Source betterRobert Griesemer2017-11-291-2/+6
| | | | | | | | | For #22695. Change-Id: Idcda3294070aeaeaf940aaf2014b573732fd60a4 Reviewed-on: https://go-review.googlesource.com/80696 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Reviewed-by: Dmitri Shuralyov <shurcool@gmail.com>
* go/format: quote bytes in comments for readabilityxufei_Alex2017-04-191-4/+4
| | | | | | Change-Id: I8e90012b3498c51aaeb2f70182debab52549afb4 Reviewed-on: https://go-review.googlesource.com/40930 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
* go/format: add format.Node exampleAlberto Donizetti2016-09-081-0/+27
| | | | | | | | | Updates #16360 Change-Id: I5927cffa961cd85539a3ba9606b116c5996d1096 Reviewed-on: https://go-review.googlesource.com/26696 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
* all: single space after period.Brad Fitzpatrick2016-03-021-3/+3
| | | | | | | | | | | | | | | | | | | | 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>
* go/format: handle whitespace-only input correctlyRobert Griesemer2015-09-302-6/+29
| | | | | | | | | | 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>
* go/format, cmd/gofmt: avoid dependency on internal package formatRobert Griesemer2015-09-302-3/+168
| | | | | | | | Fixes #11844. Change-Id: I32edd39e79f7c9bdc132c49bd06081f35dac245d Reviewed-on: https://go-review.googlesource.com/15114 Reviewed-by: Alan Donovan <adonovan@google.com>
* go/format: fix //line corner case when formatting statementsDidier Spezia2015-06-241-1/+5
| | | | | | | | | | | | | | | | | | | | | The code formatting mechanism can be applied to partial Go code, such as a list of statements. The statements are wrapped into a function definition (to be parsed fine), and unwrapped after formatting. When the statements contain //line annotations, it may fail, because not all comments are flushed by the printer before the final '}'. Formatting "\ta()\n//line :1" results in "\ta() }\n\n//line", which is wrong. Tweaked the wrapping/unwrapping code to make sure comments are flushed before the '}'. Fixes #11276 Change-Id: Id15c80279b0382ee9ed939cca1647f525c4929f5 Reviewed-on: https://go-review.googlesource.com/11282 Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
* cmd/gofmt, go/format: refactor common pieces into internal/formatSebastien Binet2015-04-011-154/+3
| | | | | | | | | | | | | | cmd/gofmt and go/format had 3 functions (parse, format and isSpace) that had to be kept in-sync. This CL extracts these 3 functions and refactors them into a new internal/format package. This CL is just code reorganization with no behavior nor semantic change. Change-Id: I593f24e9d3cadbbd9559a67e3b1d2ff190b4fd90 Reviewed-on: https://go-review.googlesource.com/6760 Reviewed-by: Robert Griesemer <gri@golang.org>
* go/format, cmd/gofmt: added missing comments, minor internal cleanupRobert Griesemer2014-09-301-7/+29
| | | | | | | | | | | | | | | | | | This is a minor cleanup following CL 142360043: The internal parse and format functions in both packages were almost identical - made them identical by adding an extra parameter, and documented them as identical. Eventually we should find a nice way to factor these functions out, but we cannot do this now while in prep for 1.4. No functionality change. LGTM=adonovan R=adonovan CC=golang-codereviews https://golang.org/cl/146520043
* go/format, cmd/gofmt: fix issues with partial Go code with indentDmitri Shuralyov2014-09-292-82/+131
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixes #5551. Fixes #4449. Adds tests for both issues. Note that the two issues occur only when formatting partial Go code with indent. The best way to understand the change is as follows: I took the code of cmd/gofmt and go/format, combined it into one unified code that does not suffer from either 4449 nor 5551, and then applied that code to both cmd/gofmt and go/format. As a result, there is now much more identical code between the two packages, making future code deduplication easier (it was not possible to do that now without adding public APIs, which I was advised not to do at this time). More specifically, I took the parse() of cmd/gofmt which correctly preserves comments (issue 5551) and modified it to fix issue where it would sometimes modify literal values (issue 4449). I ended up removing the matchSpace() function because it no longer needed to do some of its work (insert indent), and a part of its work had to be done in advance (determining the indentation of first code line), because that calculation is required for cfg.Fprint() to run. adjustIndent is used to adjust the indent of cfg.Fprint() to compensate for the body of wrapper func being indented by one level. This allows to get rid of the bytes.Replace text manipulation of inner content, which was problematic and sometimes altered raw string literals (issue 4449). This means that sometimes the value of cfg.Indent is negative, but that works as expected. So now the algorithm for formatting partial Go code is: 1. Determine and prepend leading space of original source. 2. Determine and prepend indentation of first code line. 3. Format and write partial Go code (with all of its leading & trailing space trimmed). 4. Determine and append trailing space of original source. LGTM=gri R=golang-codereviews, bradfitz, gri CC=golang-codereviews https://golang.org/cl/142360043
* build: move package sources from src/pkg to srcRuss Cox2014-09-082-0/+323
Preparation was in CL 134570043. This CL contains only the effect of 'hg mv src/pkg/* src'. For more about the move, see golang.org/s/go14nopkg.