Skip to content

cmd/gofmt: incorrect comment alignment with double-width runes #7481

@gopherbot

Description

@gopherbot

by travis.cardwell:

The gofmt command does not take into account the width of runes when calculating the
alignment for comments.  I ran into the issue in a program that has rune constants, some
of which are single-width and some of which are double-width, but the issue is more
apparent in the following (contrived) example:

What does 'go version' print?

$ go version
go version go1.2.1 linux/amd64

What steps will reproduce the problem?

$ cat fmttest.go
package fmttest

const (
    msgEn = "This is a test."    // English
    msgJa = "これはテストです。" // Japanese
)

$ gofmt -d fmttest.go
diff fmttest.go gofmt/fmttest.go
--- /tmp/gofmt819271649 2014-03-06 14:13:28.000000000 +0900
+++ /tmp/gofmt002870220 2014-03-06 14:13:28.000000000 +0900
@@ -1,6 +1,6 @@
 package fmttest
 
 const (
-   msgEn = "This is a test."    // English
-   msgJa = "これはテストです。" // Japanese
+   msgEn = "This is a test." // English
+   msgJa = "これはテストです。"       // Japanese
 )

The issue is not clear in this web interface [with my font settings] because spaces do
not have the same constant width as non-spaces, but it is clear if you copy the above
example into a file and run the gofmt command in a terminal.

What happened?

The alignment for comments is calculated based on the maximum width of code in the
block.  Currently, all runes are assumed to have a width of 1.  With tabwidth=8, the
width of the msgEn code is 33 columns, and the width of the msgJa code is incorrectly
calculated as 27 columns.  Comments are therefore calculated to start on column 35.  The
msgEn comment is offset from the code by one space, and the msgJa comment is offset from
the code by 7 spaces.  The result is not aligned properly.  The original alignment was
expected to be correct.

What should have happened instead?

Instead, the width of runes should be taken into account when calculating alignment. 
With tabwidth=8, the width of the msgJa code should be calculated as 36 columns, as all
9 runes in the string are double-width.  Comments should then be calculated to start on
column 38, resulting in the original alignment.

Please provide any additional information below.

Note that the length of a rune (in bytes) is unrelated to the width of a rune (in
columns).  When testing, I highly recommend including troublesome cases.  For example,
the following rune is commonly used in Japanese yet causes issues in some software:

http://decodeunicode.org/u+203B

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions