1
0
mirror of https://github.com/golang/go synced 2024-11-21 20:44:39 -07:00

go/printer/gofmt: remove special case for multi-line raw strings

As a special case, multi-line raw strings (i.e., strings in `` quotes)
were not indented if they were the only token on a line. This heuristic
was meant to improve formatting for multi-line raw strings where sub-
sequent lines are not indented at the level of the surrounding code.
Multiple people have complained about this. Removing the heuristic
again because it makes the formatting more regular, easier to under-
stand, and simplifies the implementation.

- manual changes to ebnf/ebnf_test.go for readability
- gofmt -w src misc

Fixes #1643.

R=r, rsc
CC=golang-dev
https://golang.org/cl/4307045
This commit is contained in:
Robert Griesemer 2011-03-29 18:30:59 -07:00
parent d54c4ecc32
commit 1afc37fa7e
8 changed files with 57 additions and 103 deletions

View File

@ -12,7 +12,7 @@ import (
var httpserverFix = fix{
"httpserver",
httpserver,
`Adapt http server methods and functions to changes
`Adapt http server methods and functions to changes
made to the http ResponseWriter interface.
http://codereview.appspot.com/4245064 Hijacker

View File

@ -11,7 +11,7 @@ import (
var netdialFix = fix{
"netdial",
netdial,
`Adapt 3-argument calls of net.Dial to use 2-argument form.
`Adapt 3-argument calls of net.Dial to use 2-argument form.
http://codereview.appspot.com/4244055
`,
@ -20,7 +20,7 @@ http://codereview.appspot.com/4244055
var tlsdialFix = fix{
"tlsdial",
tlsdial,
`Adapt 4-argument calls of tls.Dial to use 3-argument form.
`Adapt 4-argument calls of tls.Dial to use 3-argument form.
http://codereview.appspot.com/4244055
`,
@ -29,7 +29,7 @@ http://codereview.appspot.com/4244055
var netlookupFix = fix{
"netlookup",
netlookup,
`Adapt 3-result calls to net.LookupHost to use 2-result form.
`Adapt 3-result calls to net.LookupHost to use 2-result form.
http://codereview.appspot.com/4244055
`,

View File

@ -12,7 +12,7 @@ import (
var procattrFix = fix{
"procattr",
procattr,
`Adapt calls to os.StartProcess to use new ProcAttr type.
`Adapt calls to os.StartProcess to use new ProcAttr type.
http://codereview.appspot.com/4253052
`,

View File

@ -15,31 +15,26 @@ var fset = token.NewFileSet()
var grammars = []string{
`Program = .
`,
`Program = .`,
`Program = foo .
foo = "foo" .
`,
`Program = foo .
foo = "foo" .`,
`Program = "a" | "b" "c" .
`,
`Program = "a" | "b" "c" .`,
`Program = "a" ... "z" .
`,
`Program = "a" ... "z" .`,
`Program = Song .
Song = { Note } .
Note = Do | (Re | Mi | Fa | So | La) | Ti .
Do = "c" .
Re = "d" .
Mi = "e" .
Fa = "f" .
So = "g" .
La = "a" .
Ti = ti .
ti = "b" .
`,
`Program = Song .
Song = { Note } .
Note = Do | (Re | Mi | Fa | So | La) | Ti .
Do = "c" .
Re = "d" .
Mi = "e" .
Fa = "f" .
So = "g" .
La = "a" .
Ti = ti .
ti = "b" .`,
}

View File

@ -14,7 +14,6 @@ import (
"os"
"path/filepath"
"runtime"
"strings"
"tabwriter"
)
@ -35,13 +34,6 @@ const (
)
const (
esc2 = '\xfe' // an escape byte that cannot occur in regular UTF-8
_ = 1 / (esc2 - tabwriter.Escape) // cause compiler error if esc2 == tabwriter.Escape
esc2str = "\xfe"
)
var (
esc = []byte{tabwriter.Escape}
htab = []byte{'\t'}
@ -775,21 +767,7 @@ func (p *printer) print(args ...interface{}) {
data = x.Name
tok = token.IDENT
case *ast.BasicLit:
// If we have a raw string that spans multiple lines and
// the opening quote (`) is on a line preceded only by
// indentation, we don't want to write that indentation
// because the following lines of the raw string are not
// indented. It's easiest to correct the output at the end
// via the trimmer (because of the complex handling of
// white space).
// Mark multi-line raw strings by replacing the opening
// quote with esc2 and have the trimmer take care of fixing
// it up.
if x.Value[0] == '`' && strings.Index(x.Value, "\n") > 0 {
data = p.escape(esc2str + x.Value[1:])
} else {
data = p.escape(x.Value)
}
data = p.escape(x.Value)
tok = x.Kind
case token.Token:
s := x.String()
@ -871,10 +849,9 @@ func (p *printer) flush(next token.Position, tok token.Token) (droppedFF bool) {
// through unchanged.
//
type trimmer struct {
output io.Writer
state int
space bytes.Buffer
hasText bool
output io.Writer
state int
space bytes.Buffer
}
@ -882,15 +859,11 @@ type trimmer struct {
// It can be in one of the following states:
const (
inSpace = iota // inside space
atEscape // inside space and the last char was an opening tabwriter.Escape
inEscape // inside text bracketed by tabwriter.Escapes
inText // inside text
)
var backquote = []byte{'`'}
// Design note: It is tempting to eliminate extra blanks occurring in
// whitespace in this function as it could simplify some
// of the blanks logic in the node printing functions.
@ -899,9 +872,8 @@ var backquote = []byte{'`'}
func (p *trimmer) Write(data []byte) (n int, err os.Error) {
// invariants:
// p.state == inSpace, atEscape:
// p.state == inSpace:
// p.space is unwritten
// p.hasText indicates if there is any text on this line
// p.state == inEscape, inText:
// data[m:n] is unwritten
m := 0
@ -918,32 +890,20 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
case '\n', '\f':
p.space.Reset() // discard trailing space
_, err = p.output.Write(newlines[0:1]) // write newline
p.hasText = false
case tabwriter.Escape:
p.state = atEscape
_, err = p.output.Write(p.space.Bytes())
p.state = inEscape
m = n + 1 // +1: skip tabwriter.Escape
default:
_, err = p.output.Write(p.space.Bytes())
p.state = inText
m = n
}
case atEscape:
// discard indentation if we have a multi-line raw string
// (see printer.print for details)
if b != esc2 || p.hasText {
_, err = p.output.Write(p.space.Bytes())
}
p.state = inEscape
m = n
if b == esc2 {
_, err = p.output.Write(backquote) // convert back
m++
}
case inEscape:
if b == tabwriter.Escape {
_, err = p.output.Write(data[m:n])
p.state = inSpace
p.space.Reset()
p.hasText = true
}
case inText:
switch b {
@ -952,19 +912,18 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
p.state = inSpace
p.space.Reset()
p.space.WriteByte(b) // WriteByte returns no errors
p.hasText = true
case '\n', '\f':
_, err = p.output.Write(data[m:n])
p.state = inSpace
p.space.Reset()
_, err = p.output.Write(newlines[0:1]) // write newline
p.hasText = false
case tabwriter.Escape:
_, err = p.output.Write(data[m:n])
p.state = atEscape
p.space.Reset()
p.hasText = true
p.state = inEscape
m = n + 1 // +1: skip tabwriter.Escape
}
default:
panic("unreachable")
}
if err != nil {
return
@ -977,7 +936,6 @@ func (p *trimmer) Write(data []byte) (n int, err os.Error) {
_, err = p.output.Write(data[m:n])
p.state = inSpace
p.space.Reset()
p.hasText = true
}
return

View File

@ -253,8 +253,8 @@ bar`
var _ = ``
var _ = `foo`
var _ =
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var _ = // comment
@ -262,8 +262,8 @@ bar`
var _ = // comment
`foo`
var _ = // comment
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var _ = /* comment */ ``
@ -276,12 +276,12 @@ bar`
var _ = /* comment */
`foo`
var _ = /* comment */
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var board = []int(
`...........
`...........
...........
....●●●....
....●●●....
@ -296,8 +296,8 @@ bar`
var state = S{
"foo",
// the next line should not be indented
`...........
// the next line should remain indented
`...........
...........
....●●●....
....●●●....

View File

@ -256,7 +256,7 @@ var _ =
var _ =
`foo`
var _ =
// the next line should not be indented
// the next line should remain indented
`foo
bar`
@ -266,7 +266,7 @@ bar`
var _ = // comment
`foo`
var _ = // comment
// the next line should not be indented
// the next line should remain indented
`foo
bar`
@ -282,7 +282,7 @@ bar`
var _ = /* comment */
`foo`
var _ = /* comment */
// the next line should not be indented
// the next line should remain indented
`foo
bar`
@ -304,7 +304,7 @@ var board = []int(
var state = S{
"foo",
// the next line should not be indented
// the next line should remain indented
`...........
...........
....●●●....

View File

@ -239,7 +239,8 @@ func _() {
_ = `foo
bar`
_ = `three spaces before the end of the line starting here:
they must not be removed` }
they must not be removed`
}
func _() {
@ -252,8 +253,8 @@ bar`
var _ = ``
var _ = `foo`
var _ =
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var _ = // comment
@ -261,8 +262,8 @@ bar`
var _ = // comment
`foo`
var _ = // comment
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var _ = /* comment */ ``
@ -275,12 +276,12 @@ bar`
var _ = /* comment */
`foo`
var _ = /* comment */
// the next line should not be indented
`foo
// the next line should remain indented
`foo
bar`
var board = []int(
`...........
`...........
...........
....●●●....
....●●●....
@ -295,8 +296,8 @@ bar`
var state = S{
"foo",
// the next line should not be indented
`...........
// the next line should remain indented
`...........
...........
....●●●....
....●●●....