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 @@
// Use of this source code is governed by a BSD-style
// 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
......@@ -53,7 +53,7 @@ type I1 interface {
}
// The I2 interface; all methods are exported.
type I1 interface {
type I2 interface {
I0;
F(x float) float; // exported method
G(x float) float; // exported method
......@@ -91,6 +91,11 @@ func f1() {
}
func _() {
// this comment should be properly indented
}
func abs(x int) int {
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
return -x; // this statement should be properly indented
......@@ -114,9 +119,26 @@ func typeswitch(x interface{}) {
switch v0, ok := x.(int); x.(type) {
case bool, int, float:
// this comment should be indented
case string:
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.
......@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// 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
......@@ -53,7 +53,7 @@ type I1 interface {
}
// The I2 interface; all methods are exported.
type I1 interface {
type I2 interface {
I0;
F(x float) float; // exported method
G(x float) float; // exported method
......@@ -91,6 +91,11 @@ func f1() {
}
func _() {
// this comment should be properly indented
}
func abs(x int) int {
if x < 0 { // the tab printed before this comment's // must not affect the remaining lines
return -x; // this statement should be properly indented
......@@ -112,9 +117,26 @@ func typeswitch(x interface{}) {
switch v0, ok := x.(int); x.(type) {
case bool, int, float:
// this comment should be indented
case string:
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 is a package for testing purposes.
// This is a package for testing comment placement by go/printer.
//
package main
......@@ -48,7 +48,7 @@ type I1 interface {
// The I2 interface; all methods are exported.
type I1 interface {
type I2 interface {
I0;
F(x float) float;
G(x float) float;
......
......@@ -175,6 +175,14 @@ func _() {
y = 20; // 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 _() {
......@@ -194,7 +202,13 @@ func _() {
// 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
f int;
......
......@@ -19,10 +19,10 @@ import (
)
import (
"io";
aLongRename "io";
b "io";
c "i" "o";
"io";
aLongRename "io";
b "io";
c "i" "o";
)
// no newlines between consecutive single imports, but
......@@ -173,6 +173,11 @@ func _() {
y = 20; // 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 _() {
......@@ -192,7 +197,13 @@ func _() {
// 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
f int;
......@@ -235,7 +246,7 @@ type _ struct {
bool;
a, b, c int;
int "tag";
ES; // comment
ES; // comment
float "tag"; // comment
f int; // comment
f, ff, fff, ffff int; // comment
......@@ -246,7 +257,7 @@ type _ struct {
// difficult cases
type _ struct {
bool; // comment
bool; // comment
text []byte; // comment
}
......
......@@ -165,3 +165,14 @@ func (p *parser) charClass() {
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 _() {
// do not modify `` strings
_ = ``;
_ = `
`;
// TODO(gri): fix line breaks here
`; // TODO(gri): fix line breaks here
_ = `foo
bar`;
}
......@@ -140,8 +138,8 @@ func _() {
b < a;
_ = "1234567890"
"1234567890";
// TODO(gri): add more test cases
// TODO(gri): these comments should be indented
// TODO(gri): add more test cases
// TODO(gri): these comments should be indented
}
......@@ -167,3 +165,15 @@ func (p *parser) charClass() {
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,7 +181,9 @@ var facts = map[int]string{
func usage() {
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"
" godoc -http=:6060\n");
flag.PrintDefaults();
......
......@@ -123,6 +123,18 @@ func _() {
func _() {
// this comment should be indented
L:
}
func _() {
L: _ = 0;
}
func _() {
// this comment should be indented
L: _ = 0;
}
......@@ -134,3 +146,13 @@ func _() {
_ = 0;
}
}
func _() {
// this comment should be indented
for {
L1: _ = 0;
L2:
_ = 0;
}
}
......@@ -139,6 +139,19 @@ L:
func _() {
// this comment should be indented
L:
;
}
func _() {
L: _ = 0;
}
func _() {
// this comment should be indented
L: _ = 0;
}
......@@ -150,3 +163,13 @@ func _() {
_ = 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