mirror of
https://github.com/golang/go
synced 2024-11-22 01:44:40 -07:00
go/printer, gofmt: correct indentation after certain /*-style comments
- applied gofmt to src and misc Note: This fix improved formatting of src/pkg/math/all_test.go but leads to a degradation in src/pkg/exp/4s/xs.go. The latter happened to "work" before accidentally. Fixing the alignment in that case in general will be a separate CL. Fixes #628. R=rsc CC=golang-dev https://golang.org/cl/223054
This commit is contained in:
parent
e0d5177dd7
commit
dbe0b57012
@ -145,13 +145,13 @@ var txbits = [NCOL][32]byte{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var txpix = [NCOL]draw.Color{
|
var txpix = [NCOL]draw.Color{
|
||||||
draw.Yellow, /* yellow */
|
draw.Yellow, /* yellow */
|
||||||
draw.Cyan, /* cyan */
|
draw.Cyan, /* cyan */
|
||||||
draw.Green, /* lime green */
|
draw.Green, /* lime green */
|
||||||
draw.GreyBlue, /* slate */
|
draw.GreyBlue, /* slate */
|
||||||
draw.Red, /* red */
|
draw.Red, /* red */
|
||||||
draw.GreyGreen, /* olive green */
|
draw.GreyGreen, /* olive green */
|
||||||
draw.Blue, /* blue */
|
draw.Blue, /* blue */
|
||||||
draw.Color(0xFF55AAFF), /* pink */
|
draw.Color(0xFF55AAFF), /* pink */
|
||||||
draw.Color(0xFFAAFFFF), /* lavender */
|
draw.Color(0xFFAAFFFF), /* lavender */
|
||||||
draw.Color(0xBB005DFF), /* maroon */
|
draw.Color(0xBB005DFF), /* maroon */
|
||||||
|
@ -205,22 +205,16 @@ func (p *printer) write(data []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (p *printer) writeNewlines(n int) {
|
func (p *printer) writeNewlines(n int, useFF bool) {
|
||||||
if n > 0 {
|
if n > 0 {
|
||||||
if n > maxNewlines {
|
if n > maxNewlines {
|
||||||
n = maxNewlines
|
n = maxNewlines
|
||||||
}
|
}
|
||||||
p.write(newlines[0:n])
|
if useFF {
|
||||||
}
|
p.write(formfeeds[0:n])
|
||||||
}
|
} else {
|
||||||
|
p.write(newlines[0:n])
|
||||||
|
|
||||||
func (p *printer) writeFormfeeds(n int) {
|
|
||||||
if n > 0 {
|
|
||||||
if n > maxNewlines {
|
|
||||||
n = maxNewlines
|
|
||||||
}
|
}
|
||||||
p.write(formfeeds[0:n])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +354,7 @@ func (p *printer) writeCommentPrefix(pos, next token.Position, isFirst, isKeywor
|
|||||||
// use formfeeds to break columns before a comment;
|
// use formfeeds to break columns before a comment;
|
||||||
// this is analogous to using formfeeds to separate
|
// this is analogous to using formfeeds to separate
|
||||||
// individual lines of /*-style comments
|
// individual lines of /*-style comments
|
||||||
p.writeFormfeeds(pos.Line - p.last.Line)
|
p.writeNewlines(pos.Line-p.last.Line, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -591,9 +585,10 @@ func (p *printer) writeComment(comment *ast.Comment) {
|
|||||||
// writeCommentSuffix writes a line break after a comment if indicated
|
// writeCommentSuffix writes a line break after a comment if indicated
|
||||||
// and processes any leftover indentation information. If a line break
|
// and processes any leftover indentation information. If a line break
|
||||||
// is needed, the kind of break (newline vs formfeed) depends on the
|
// is needed, the kind of break (newline vs formfeed) depends on the
|
||||||
// pending whitespace.
|
// pending whitespace. writeCommentSuffix returns true if a pending
|
||||||
|
// formfeed was dropped from the whitespace buffer.
|
||||||
//
|
//
|
||||||
func (p *printer) writeCommentSuffix(needsLinebreak bool) {
|
func (p *printer) writeCommentSuffix(needsLinebreak bool) (droppedFF bool) {
|
||||||
for i, ch := range p.buffer {
|
for i, ch := range p.buffer {
|
||||||
switch ch {
|
switch ch {
|
||||||
case blank, vtab:
|
case blank, vtab:
|
||||||
@ -603,9 +598,13 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) {
|
|||||||
// don't loose indentation information
|
// don't loose indentation information
|
||||||
case newline, formfeed:
|
case newline, formfeed:
|
||||||
// if we need a line break, keep exactly one
|
// if we need a line break, keep exactly one
|
||||||
|
// but remember if we dropped any formfeeds
|
||||||
if needsLinebreak {
|
if needsLinebreak {
|
||||||
needsLinebreak = false
|
needsLinebreak = false
|
||||||
} else {
|
} else {
|
||||||
|
if ch == formfeed {
|
||||||
|
droppedFF = true
|
||||||
|
}
|
||||||
p.buffer[i] = ignore
|
p.buffer[i] = ignore
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -616,6 +615,8 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) {
|
|||||||
if needsLinebreak {
|
if needsLinebreak {
|
||||||
p.write([]byte{'\n'})
|
p.write([]byte{'\n'})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -623,9 +624,10 @@ func (p *printer) writeCommentSuffix(needsLinebreak bool) {
|
|||||||
// and prints it together with the buffered whitespace (i.e., the whitespace
|
// and prints it together with the buffered whitespace (i.e., the whitespace
|
||||||
// that needs to be written before the next token). A heuristic is used to mix
|
// that needs to be written before the next token). A heuristic is used to mix
|
||||||
// the comments and whitespace. The isKeyword parameter indicates if the next
|
// the comments and whitespace. The isKeyword parameter indicates if the next
|
||||||
// token is a keyword or not.
|
// token is a keyword or not. intersperseComments returns true if a pending
|
||||||
|
// formfeed was dropped from the whitespace buffer.
|
||||||
//
|
//
|
||||||
func (p *printer) intersperseComments(next token.Position, isKeyword bool) {
|
func (p *printer) intersperseComments(next token.Position, isKeyword bool) (droppedFF bool) {
|
||||||
isFirst := true
|
isFirst := true
|
||||||
needsLinebreak := false
|
needsLinebreak := false
|
||||||
var last *ast.Comment
|
var last *ast.Comment
|
||||||
@ -643,7 +645,7 @@ func (p *printer) intersperseComments(next token.Position, isKeyword bool) {
|
|||||||
// follows on the same line: separate with an extra blank
|
// follows on the same line: separate with an extra blank
|
||||||
p.write([]byte{' '})
|
p.write([]byte{' '})
|
||||||
}
|
}
|
||||||
p.writeCommentSuffix(needsLinebreak)
|
return p.writeCommentSuffix(needsLinebreak)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -772,12 +774,13 @@ func (p *printer) print(args ...) {
|
|||||||
p.pos = next
|
p.pos = next
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
p.flush(next, isKeyword)
|
droppedFF := p.flush(next, isKeyword)
|
||||||
|
|
||||||
// intersperse extra newlines if present in the source
|
// intersperse extra newlines if present in the source
|
||||||
// (don't do this in flush as it will cause extra newlines
|
// (don't do this in flush as it will cause extra newlines
|
||||||
// at the end of a file)
|
// at the end of a file) - use formfeeds if we dropped one
|
||||||
p.writeNewlines(next.Line - p.pos.Line)
|
// before
|
||||||
|
p.writeNewlines(next.Line-p.pos.Line, droppedFF)
|
||||||
|
|
||||||
p.writeItem(next, data, tag)
|
p.writeItem(next, data, tag)
|
||||||
}
|
}
|
||||||
@ -794,15 +797,19 @@ func (p *printer) commentBefore(next token.Position) bool {
|
|||||||
|
|
||||||
|
|
||||||
// Flush prints any pending comments and whitespace occuring
|
// Flush prints any pending comments and whitespace occuring
|
||||||
// textually before the position of the next item.
|
// textually before the position of the next item. Flush returns
|
||||||
|
// true if a pending formfeed character was dropped from the
|
||||||
|
// whitespace buffer as a result of interspersing comments.
|
||||||
//
|
//
|
||||||
func (p *printer) flush(next token.Position, isKeyword bool) {
|
func (p *printer) flush(next token.Position, isKeyword bool) (droppedFF bool) {
|
||||||
// if there are comments before the next item, intersperse them
|
|
||||||
if p.commentBefore(next) {
|
if p.commentBefore(next) {
|
||||||
p.intersperseComments(next, isKeyword)
|
// if there are comments before the next item, intersperse them
|
||||||
|
droppedFF = p.intersperseComments(next, isKeyword)
|
||||||
|
} else {
|
||||||
|
// otherwise, write any leftover whitespace
|
||||||
|
p.writeWhitespace(len(p.buffer))
|
||||||
}
|
}
|
||||||
// write any leftover whitespace
|
return
|
||||||
p.writeWhitespace(len(p.buffer))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
7
src/pkg/go/printer/testdata/comments.golden
vendored
7
src/pkg/go/printer/testdata/comments.golden
vendored
@ -105,10 +105,13 @@ func _() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func abs(x int) int {
|
func _(x int) int {
|
||||||
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
|
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
|
||||||
return -x // this statement should be properly indented
|
return -x // this statement should be properly indented
|
||||||
}
|
}
|
||||||
|
if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */
|
||||||
|
return -x // this statement should be properly indented
|
||||||
|
}
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -389,6 +392,8 @@ func _() {
|
|||||||
|
|
||||||
func ( /* comment1 */ T /* comment2 */ ) _() {}
|
func ( /* comment1 */ T /* comment2 */ ) _() {}
|
||||||
|
|
||||||
|
func _() { /* one-liner */ }
|
||||||
|
|
||||||
|
|
||||||
// Line comments with tabs
|
// Line comments with tabs
|
||||||
func _() {
|
func _() {
|
||||||
|
7
src/pkg/go/printer/testdata/comments.input
vendored
7
src/pkg/go/printer/testdata/comments.input
vendored
@ -105,10 +105,13 @@ func _() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func abs(x int) int {
|
func _(x int) int {
|
||||||
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
|
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
|
||||||
return -x // this statement should be properly indented
|
return -x // this statement should be properly indented
|
||||||
}
|
}
|
||||||
|
if x < 0 { /* the tab printed before this comment's /* must not affect the remaining lines */
|
||||||
|
return -x // this statement should be properly indented
|
||||||
|
}
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -390,6 +393,8 @@ func _() {
|
|||||||
|
|
||||||
func (/* comment1 */ T /* comment2 */) _() {}
|
func (/* comment1 */ T /* comment2 */) _() {}
|
||||||
|
|
||||||
|
func _() { /* one-liner */ }
|
||||||
|
|
||||||
|
|
||||||
// Line comments with tabs
|
// Line comments with tabs
|
||||||
func _() {
|
func _() {
|
||||||
|
@ -1205,7 +1205,7 @@ func TestFmin(t *testing.T) {
|
|||||||
func TestFmod(t *testing.T) {
|
func TestFmod(t *testing.T) {
|
||||||
for i := 0; i < len(vf); i++ {
|
for i := 0; i < len(vf); i++ {
|
||||||
if f := Fmod(10, vf[i]); fmod[i] != f { /*!close(fmod[i], f)*/
|
if f := Fmod(10, vf[i]); fmod[i] != f { /*!close(fmod[i], f)*/
|
||||||
t.Errorf("Fmod(10, %g) = %g, want %g\n", vf[i], f, fmod[i])
|
t.Errorf("Fmod(10, %g) = %g, want %g\n", vf[i], f, fmod[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for i := 0; i < len(vffmodSC); i++ {
|
for i := 0; i < len(vffmodSC); i++ {
|
||||||
|
Loading…
Reference in New Issue
Block a user