mirror of
https://github.com/golang/go.git
synced 2024-09-22 19:08:30 +00:00
go/printer, gofmt: trim trailing whitespace in comments
Also: updated go fix testcases to pass tests. Fixes #4310. R=r CC=golang-dev https://golang.org/cl/6810055
This commit is contained in:
parent
640d818f2a
commit
db2b6ed854
@ -14,6 +14,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -405,6 +406,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, prev, comment *as
|
||||
// Split comment text into lines
|
||||
// (using strings.Split(text, "\n") is significantly slower for
|
||||
// this specific purpose, as measured with: go test -bench=Print)
|
||||
//
|
||||
func split(text string) []string {
|
||||
// count lines (comment text never ends in a newline)
|
||||
n := 1
|
||||
@ -432,6 +434,7 @@ func split(text string) []string {
|
||||
|
||||
// Returns true if s contains only white space
|
||||
// (only tabs and blanks can appear in the printer's context).
|
||||
//
|
||||
func isBlank(s string) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] > ' ' {
|
||||
@ -441,6 +444,7 @@ func isBlank(s string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// commonPrefix returns the common prefix of a and b.
|
||||
func commonPrefix(a, b string) string {
|
||||
i := 0
|
||||
for i < len(a) && i < len(b) && a[i] == b[i] && (a[i] <= ' ' || a[i] == '*') {
|
||||
@ -449,6 +453,11 @@ func commonPrefix(a, b string) string {
|
||||
return a[0:i]
|
||||
}
|
||||
|
||||
// trimRight returns s with trailing whitespace removed.
|
||||
func trimRight(s string) string {
|
||||
return strings.TrimRightFunc(s, unicode.IsSpace)
|
||||
}
|
||||
|
||||
// stripCommonPrefix removes a common prefix from /*-style comment lines (unless no
|
||||
// comment line is indented, all but the first line have some form of space prefix).
|
||||
// The prefix is computed using heuristics such that is is likely that the comment
|
||||
@ -611,7 +620,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
|
||||
|
||||
// shortcut common case of //-style comments
|
||||
if text[1] == '/' {
|
||||
p.writeString(pos, text, true)
|
||||
p.writeString(pos, trimRight(text), true)
|
||||
return
|
||||
}
|
||||
|
||||
@ -641,7 +650,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
|
||||
pos = p.pos
|
||||
}
|
||||
if len(line) > 0 {
|
||||
p.writeString(pos, line, true)
|
||||
p.writeString(pos, trimRight(line), true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
9
src/pkg/go/printer/testdata/comments.golden
vendored
9
src/pkg/go/printer/testdata/comments.golden
vendored
@ -626,4 +626,13 @@ func _() {
|
||||
var lflag bool // -l - disable line directives
|
||||
}
|
||||
|
||||
// Trailing white space in comments should be trimmed
|
||||
func _() {
|
||||
// This comment has 4 blanks following that should be trimmed:
|
||||
/* Each line of this comment has blanks or tabs following that should be trimmed:
|
||||
line 2:
|
||||
line 3:
|
||||
*/
|
||||
}
|
||||
|
||||
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */
|
||||
|
8
src/pkg/go/printer/testdata/comments.input
vendored
8
src/pkg/go/printer/testdata/comments.input
vendored
@ -630,5 +630,13 @@ var vflag string // -v [y.output] - y.output file
|
||||
var lflag bool // -l - disable line directives
|
||||
}
|
||||
|
||||
// Trailing white space in comments should be trimmed
|
||||
func _() {
|
||||
// This comment has 4 blanks following that should be trimmed:
|
||||
/* Each line of this comment has blanks or tabs following that should be trimmed:
|
||||
line 2:
|
||||
line 3:
|
||||
*/
|
||||
}
|
||||
|
||||
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */
|
||||
|
Loading…
Reference in New Issue
Block a user