Commit df7efaf9 authored by Robert Griesemer's avatar Robert Griesemer

- improved comment intersperse heuristic:

  comments should now be indented properly in corner cases
  (at the end of statement lists, for instance)

- changed import decl. formatting as suggested by Russ (no "global"
  indentation of imports if there are renames present)

- better field list handling

- better documentation

R=rsc
DELTA=534  (324 added, 35 deleted, 175 changed)
OCL=35557
CL=35630
parent e2854875
This diff is collapsed.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// This is a package for testing purposes. // This is a package for testing comment placement by go/printer.
// //
package main package main
...@@ -53,7 +53,7 @@ type I1 interface { ...@@ -53,7 +53,7 @@ type I1 interface {
} }
// The I2 interface; all methods are exported. // The I2 interface; all methods are exported.
type I1 interface { type I2 interface {
I0; I0;
F(x float) float; // exported method F(x float) float; // exported method
G(x float) float; // exported method G(x float) float; // exported method
...@@ -91,6 +91,11 @@ func f1() { ...@@ -91,6 +91,11 @@ func f1() {
} }
func _() {
// this comment should be properly indented
}
func abs(x int) int { func abs(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
...@@ -114,9 +119,26 @@ func typeswitch(x interface{}) { ...@@ -114,9 +119,26 @@ func typeswitch(x interface{}) {
switch v0, ok := x.(int); x.(type) { switch v0, ok := x.(int); x.(type) {
case bool, int, float: case bool, int, float:
// this comment should be indented
case string: case string:
default: default:
// this comment should be indented
} }
// this comment should be indented
} }
// Line comments with tabs
func _() {
var finput *bufio.Reader; // input file
var stderr *bufio.Writer;
var ftable *bufio.Writer; // y.go file
var foutput *bufio.Writer; // y.output file
var oflag string; // -o [y.go] - y.go file
var vflag string; // -v [y.output] - y.output file
var lflag bool; // -l - disable line directives
}
// This comment is the last entry in this file. It must be printed. // This comment is the last entry in this file. It must be printed.
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style // Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
// This is a package for testing purposes. // This is a package for testing comment placement by go/printer.
// //
package main package main
...@@ -53,7 +53,7 @@ type I1 interface { ...@@ -53,7 +53,7 @@ type I1 interface {
} }
// The I2 interface; all methods are exported. // The I2 interface; all methods are exported.
type I1 interface { type I2 interface {
I0; I0;
F(x float) float; // exported method F(x float) float; // exported method
G(x float) float; // exported method G(x float) float; // exported method
...@@ -91,6 +91,11 @@ func f1() { ...@@ -91,6 +91,11 @@ func f1() {
} }
func _() {
// this comment should be properly indented
}
func abs(x int) int { func abs(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
...@@ -112,9 +117,26 @@ func typeswitch(x interface{}) { ...@@ -112,9 +117,26 @@ func typeswitch(x interface{}) {
switch v0, ok := x.(int); x.(type) { switch v0, ok := x.(int); x.(type) {
case bool, int, float: case bool, int, float:
// this comment should be indented
case string: case string:
default: default:
// this comment should be indented
} }
// this comment should be indented
} }
// Line comments with tabs
func _() {
var finput *bufio.Reader; // input file
var stderr *bufio.Writer;
var ftable *bufio.Writer; // y.go file
var foutput *bufio.Writer; // y.output file
var oflag string; // -o [y.go] - y.go file
var vflag string; // -v [y.output] - y.output file
var lflag bool; // -l - disable line directives
}
// This comment is the last entry in this file. It must be printed. // This comment is the last entry in this file. It must be printed.
// This is a package for testing purposes. // This is a package for testing comment placement by go/printer.
// //
package main package main
...@@ -48,7 +48,7 @@ type I1 interface { ...@@ -48,7 +48,7 @@ type I1 interface {
// The I2 interface; all methods are exported. // The I2 interface; all methods are exported.
type I1 interface { type I2 interface {
I0; I0;
F(x float) float; F(x float) float;
G(x float) float; G(x float) float;
......
...@@ -175,6 +175,14 @@ func _() { ...@@ -175,6 +175,14 @@ func _() {
y = 20; // comment y = 20; // comment
f, ff, fff, ffff int = 0, 1, 2, 3; // comment f, ff, fff, ffff int = 0, 1, 2, 3; // comment
) )
// respect original line breaks
var _ = []T {
T{0x20, "Telugu"}
};
var _ = []T {
// respect original line breaks
T{0x20, "Telugu"}
};
} }
func _() { func _() {
...@@ -194,7 +202,13 @@ func _() { ...@@ -194,7 +202,13 @@ func _() {
// formatting of structs // formatting of structs
type ES struct{} type _ struct{}
type _ struct{ /* this comment should be visible */ }
type _ struct{
// this comment should be visible and properly indented
}
type _ struct { // this comment must not change indentation type _ struct { // this comment must not change indentation
f int; f int;
......
...@@ -173,6 +173,11 @@ func _() { ...@@ -173,6 +173,11 @@ func _() {
y = 20; // comment y = 20; // comment
f, ff, fff, ffff int = 0, 1, 2, 3; // comment f, ff, fff, ffff int = 0, 1, 2, 3; // comment
) )
// respect original line breaks
var _ = []T{T{0x20, "Telugu"}};
var _ = []T{
// respect original line breaks
T{0x20, "Telugu"}};
} }
func _() { func _() {
...@@ -192,7 +197,13 @@ func _() { ...@@ -192,7 +197,13 @@ func _() {
// formatting of structs // formatting of structs
type ES struct{} type _ struct{}
type _ struct { /* this comment should be visible */}
type _ struct {
// this comment should be visible and properly indented
}
type _ struct { // this comment must not change indentation type _ struct { // this comment must not change indentation
f int; f int;
......
...@@ -165,3 +165,14 @@ func (p *parser) charClass() { ...@@ -165,3 +165,14 @@ func (p *parser) charClass() {
p.re.add(nl); p.re.add(nl);
} }
} }
func addState(s []state, inst instr, match []int) {
// handle comments correctly in multi-line expressions
for i := 0; i < l; i++ {
if s[i].inst.index() == index && // same instruction
s[i].match[0] < pos { // earlier match already going; leftmost wins
return s
}
}
}
...@@ -108,11 +108,9 @@ func _() { ...@@ -108,11 +108,9 @@ func _() {
// do not modify `` strings // do not modify `` strings
_ = ``; _ = ``;
_ = ` _ = `
`; `; // TODO(gri): fix line breaks here
// TODO(gri): fix line breaks here
_ = `foo _ = `foo
bar`; bar`;
} }
...@@ -140,8 +138,8 @@ func _() { ...@@ -140,8 +138,8 @@ func _() {
b < a; b < a;
_ = "1234567890" _ = "1234567890"
"1234567890"; "1234567890";
// TODO(gri): add more test cases // TODO(gri): add more test cases
// TODO(gri): these comments should be indented // TODO(gri): these comments should be indented
} }
...@@ -167,3 +165,15 @@ func (p *parser) charClass() { ...@@ -167,3 +165,15 @@ func (p *parser) charClass() {
p.re.add(nl); p.re.add(nl);
} }
} }
func addState(s []state, inst instr, match []int) {
// handle comments correctly in multi-line expressions
for i := 0; i < l; i++ {
if s[i].inst.index() == index &&
// same instruction
s[i].match[0] < pos { // earlier match already going; leftmost wins
return s;
}
}
}
...@@ -181,6 +181,8 @@ var facts = map[int]string{ ...@@ -181,6 +181,8 @@ var facts = map[int]string{
func usage() { func usage() {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
// TODO(gri): the 2nd string of this string list should not be indented // TODO(gri): the 2nd string of this string list should not be indented
"usage: godoc package [name ...]\n" "usage: godoc package [name ...]\n"
" godoc -http=:6060\n"); " godoc -http=:6060\n");
......
...@@ -123,6 +123,18 @@ func _() { ...@@ -123,6 +123,18 @@ func _() {
func _() { func _() {
// this comment should be indented
L:
}
func _() {
L: _ = 0;
}
func _() {
// this comment should be indented
L: _ = 0; L: _ = 0;
} }
...@@ -134,3 +146,13 @@ func _() { ...@@ -134,3 +146,13 @@ func _() {
_ = 0; _ = 0;
} }
} }
func _() {
// this comment should be indented
for {
L1: _ = 0;
L2:
_ = 0;
}
}
...@@ -139,6 +139,19 @@ L: ...@@ -139,6 +139,19 @@ L:
func _() { func _() {
// this comment should be indented
L:
;
}
func _() {
L: _ = 0;
}
func _() {
// this comment should be indented
L: _ = 0; L: _ = 0;
} }
...@@ -150,3 +163,13 @@ func _() { ...@@ -150,3 +163,13 @@ func _() {
_ = 0; _ = 0;
} }
} }
func _() {
// this comment should be indented
for {
L1: _ = 0;
L2:
_ = 0;
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment