gofmt: add -r flag to rewrite source code according to pattern
a little slow, but usable (speed unchanged when not using -r)
tweak go/printer to handle nodes without line numbers
more gracefully in a couple cases.
http://codereview.appspot.com/156103/diff/8/1011 File src/cmd/gofmt/rewrite.go (right): http://codereview.appspot.com/156103/diff/8/1011#newcode1 src/cmd/gofmt/rewrite.go:1: package main On 2009/11/20 18:22:51, gri wrote: > Missing ...
15 years, 4 months ago
(2009-11-20 21:43:25 UTC)
#4
http://codereview.appspot.com/156103/diff/8/1011
File src/cmd/gofmt/rewrite.go (right):
http://codereview.appspot.com/156103/diff/8/1011#newcode1
src/cmd/gofmt/rewrite.go:1: package main
On 2009/11/20 18:22:51, gri wrote:
> Missing copyright notice.
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode22
src/cmd/gofmt/rewrite.go:22: fmt.Fprintf(os.Stderr, "rewrite string should be of
the form 'pattern -> replacement'\n");
On 2009/11/20 18:22:51, gri wrote:
> s/should/must/
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode25
src/cmd/gofmt/rewrite.go:25: s1, err := parser.ParseStmtList("input", f[0]);
On 2009/11/20 18:22:51, gri wrote:
> Could factor this out by having a parse(s string) []ast.Stmt helper function.
> Same code appears twice.
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode41
src/cmd/gofmt/rewrite.go:41: var pattern, replace interface{}
On 2009/11/20 18:22:51, gri wrote:
> s/interface/ast.Node/
Done as ast.Expr for now.
http://codereview.appspot.com/156103/diff/8/1011#newcode56
src/cmd/gofmt/rewrite.go:56: func rewriteFile(pattern, replace interface{}, p
*ast.File) *ast.File {
On 2009/11/20 18:22:51, gri wrote:
> s/interface{}/ast.Node/
Done as ast.Expr for now.
http://codereview.appspot.com/156103/diff/8/1011#newcode61
src/cmd/gofmt/rewrite.go:61: f = func(val reflect.Value) reflect.Value {
On 2009/11/20 18:22:51, gri wrote:
> s/=/:=/ and get rid of the var f decl above
Can't - f is recursive. Move the decl down and added a comment.
http://codereview.appspot.com/156103/diff/8/1011#newcode72
src/cmd/gofmt/rewrite.go:72:
On 2009/11/20 18:22:51, gri wrote:
> please add an extra empty line for consistency
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode106
src/cmd/gofmt/rewrite.go:106:
On 2009/11/20 18:22:51, gri wrote:
> please add extra blank line for consistency.
>
> (Gofmt should do this eventually by looking at the prevalent style).
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode172
src/cmd/gofmt/rewrite.go:172: // Either way, the returned value has no line
number information.
On 2009/11/20 18:22:51, gri wrote:
> s/no line/no valid line/
Done.
http://codereview.appspot.com/156103/diff/8/1011#newcode189
src/cmd/gofmt/rewrite.go:189: return zeroPosition
On 2009/11/20 18:22:51, gri wrote:
> wouldn't it be better to keep the old position?
No, because the line number here (if m != nil)
is file "input" line 1.
http://codereview.appspot.com/156103/diff/8/1012
File src/pkg/go/printer/nodes.go (right):
http://codereview.appspot.com/156103/diff/8/1012#newcode49
src/pkg/go/printer/nodes.go:49: if line == 0 {
On 2009/11/20 18:22:51, gri wrote:
> This should not be necessary. It's always 0 <= min <= max (perhaps a comment
is
> needed) and p.pos.Line >= 0 and line >= 0. If line == 0, then n <= 0 <= min,
and
> the switch does the right thing.
Done.
http://codereview.appspot.com/156103/diff/8/1012#newcode58
src/pkg/go/printer/nodes.go:58: p.pos.Line += n;
On 2009/11/20 18:22:51, gri wrote:
> Please don't do this. Code in nodes.go shouldn't mess with p.pos, it should
only
> read it; otherwise it will become incomprehensible. If this is necessary,
there
> is a bug elsewhere.
The code is about the emit n newlines.
Shouldn't it update the line number?
The problem I am trying to solve is this:
linebreak gets called with a min of 1 printing
an expression statement where the expression
has no line number information. p.pos.Line == 1,
because that's the line we're on. Since there's no
line number information, the caller can't update
p.pos.Line, but linebreak is going to print a \n.
The next expression has line number information
and is on line 2. The output is also on line 2
but p.pos.Line still says 1, so you get a spurious
newline.
I figured if the caller is going to update p.pos.Line,
it will do so with an assignment (not a +=), so no
harm done here. On the other hand, if the caller
isn't going to update p.pos.Line, this gets closer
to the actual answer.
http://codereview.appspot.com/156103/diff/8/1012#newcode572
src/pkg/go/printer/nodes.go:572: //println(x.OpPos.String());
On 2009/11/20 18:22:51, gri wrote:
> remove this println
Done.
LGTM http://codereview.appspot.com/156103/diff/2002/2005 File src/cmd/gofmt/rewrite.go (right): http://codereview.appspot.com/156103/diff/2002/2005#newcode63 src/cmd/gofmt/rewrite.go:63: var f func(val reflect.Value) reflect.Value; // f is ...
15 years, 4 months ago
(2009-11-20 23:01:51 UTC)
#5
Issue 156103: code review 156103: gofmt: add -r flag to rewrite source code according to ...
(Closed)
Created 15 years, 4 months ago by rsc
Modified 15 years, 4 months ago
Reviewers: gri
Base URL:
Comments: 31