cmd/gofmt: reformats block comments #5128
Comments
Comment 1 by borman@google.com: Another indentation fail with /* */: Start with: /* This is a comment It is indented with 8 spaces. And has several lines */ Gofmt changes this to: /* This is a comment It is indented with 8 spaces. And has several lines */ It has changed my 8 spaces into 3. |
Owner changed to @griesemer. |
The first issue you reported seems a expected output for issue #1835. |
There is a tension between gofmt leaving comments alone and "adjusting" them to match surrounding code. There is a know bug where formatting of /* comments is not idempotent (as witnessed in the first example above), and where formatting doesn't change anymore after two applications of gofmt (as is also the case in the first example above). Being completely idempotent is a very hard problem (short of simply running gofmt twice internally). Independent of idempotency, the question is what gofmt should be doing with /* comments. You are suggesting that it should leave it alone if the first line is indented correctly. Perhaps that is the right answer. But I suspect there are counter examples. Leaving alone for now. Status changed to Thinking. |
Another use for leaving /* comments */ completely untouched is for example output with whitespace: https://golang.org/issue/6416 |
I ran into this issue recently, with an even simpler reproduce case: http://play.golang.org/p/Pwn4cOHtMN |
I've done some investigating here.
That particular issue is due to an unhandled edge case in It happens when a comment block consists of a first line with Handling it correctly will fix that particular sample I provided (http://play.golang.org/p/Pwn4cOHtMN), but it seems like it's not the same issue as what's described in the original post. Should I break http://play.golang.org/p/Pwn4cOHtMN out into a separate issue then? Or just consider this current issue to be it? |
Okay, I've created failing test cases in shurcooL/play@bfc69a3 and a simple fix for the issue in shurcooL/play@f416cec. I didn't try to refactor it in any way. I can submit a CL for review as soon as I'm sure if I should say "fixes #5128" or create a separate issue... |
After re-reading the original issue and the responses it received, I feel that I should move http://play.golang.org/p/Pwn4cOHtMN into a separate, much smaller issue. That way I can proceed to make a CL that completely fixes and closes it. So, I've created #9751. It has a simple and direct fix. I've made a CL for it. |
I have an Example test case: func ExampleDecode() {
call-my-test-case it print some output
// Output:
/*
here is the output in a block comment
in multi lines with different indentation
*/
} The test case is passing, it's no problem but when I run go fmt, it constantly change the block comment to this style /*
here is the output in a block comment
in multi lines with different indentation
*/ and causing test case to FAIL; wonder is it the same case here, is there any way to let go fmt not touching my block comment? |
@c0b There's no way to let A future |
It is a real pain to work with multi-line
Currently I have to paste expected content, add I would appreciate that |
how about to change there might be other keywords should be recognized as well, if
@griesemer are you aware of any ongoing effort of this future gofmt? can connect related PR to here? |
I just spent 1 hour trying to get gofmt to leave alone the I feel like gofmt re-formatting block comments in the specific case of |
It seems impossible, at least for me, to get the following reproducer test to pass; if you run package main
import (
"fmt"
)
func Hello() {
fmt.Printf("Hello \n\nWorld")
}
// 1. https://golang.org/pkg/testing/#hdr-Examples
// 2. https://blog.golang.org/examples
func ExampleHello() {
Hello()
// Output:
// Hello
//
// World
} |
What: - Add example test. That test example is kind of blocked on; 1. golang/go#41980 2. golang/go#5128 (comment)
by borman@google.com:
The text was updated successfully, but these errors were encountered: