-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/gofmt: incorrect comment alignment with double-width runes #7481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Vim does a great job with calculating widths, though it is not perfect in dealing with ambiguous characters. I just retrieved the source from [1] and found the relevant code in "src/mbyte.c" /^utf_char2cells/. Intervals are used to identify double-width and ambiguous characters, so not too much data is required. [1] http://www.vim.org/sources.php |
The problem is not so much in gofmt (or go/printer) but in text/tabwriter, which is the package dealing with alignment. As has been pointed out in #1, doing this correctly assumes detailed knowledge of font sizes which was an explicit non-goal. There may be heuristics that work reasonably well (#2), but it won't solve the problem for people using proportional fonts for programming (which would be the right thing to do for readability). But even if this information were available, formatting it for one person using one editor and font does not help another person using a different editor and font. gofmt used to have a mode where all alignment was indicated with tabs (as one would do on a mechanical type writer, with the tabs properly set for each section), and the idea was that perhaps at some point an editor (or editor "plug-in") would come along that would do the job that text/tabwriter is doing now. Having such an editor would make code look properly aligned no matter what font or character one is using. Albeit, we're not yet in the 21st century proper when it comes to source code editing. Closing as unfortunate. Status changed to Unfortunate. |
Actually, the size of characters in a generally mono-spaced environment is independent of font and well-defined. There are half-width characters that occupy one column and full-width characters that occupy two columns. See this report for details. Issue #8273 mentions some strategies to implement. The corresponding tables aren't that large either as full-width characters appear in long, uninterrupted blocks. |
what about ambiguous characters? |
@minux Ambiguous characters have a size that depends on whether they appear in a full-width or half-width “context.” It isn't really clear to me what is meant with “context” here, but in all environments I tested, ambiguous characters always come out as occupying only one column. I suspect that some legacy encodings allowed to select whether these ambiguous characters should occupy one column or two. For the sake of supporting full-width characters, it seems sensible to make ambiguous characters behave as occupying only one column. |
Please let me notice, that |
At least vim has an option to set the default width of ambiguous [0] http://vimdoc.sourceforge.net/htmldoc/options.html#'ambiwidth' |
@minux I believe this setting is rarely used / provided for compatibility with old systems only. Perhaps someone with more experience with CJK terminal applications could chime in? |
No. Nearly every terminal has a similar setting for that. For example, this em-dash \u2014 character, is one of I think the option is quite popular with Japanese users |
@minux Hm... This sounds like a situation. Any way, the current situation of treating every character as being one column wide is not acceptable. This does things wrong for either setting. |
go-runewidth detect locale automatically. And switching width of the characters for ambiguous width. https://github.com/mattn/go-runewidth/blob/master/runewidth_posix.go#L13 |
This is essentially a duplicate of #8273 . |
by travis.cardwell:
The text was updated successfully, but these errors were encountered: