summaryrefslogtreecommitdiff
path: root/src/cmd/gofmt/rewrite.go
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-11-23 15:44:27 -0800
committerRuss Cox <rsc@golang.org>2009-11-23 15:44:27 -0800
commit02c8b926e12ec16c2beedc269cc553fae9360244 (patch)
treea116632e27d91cee1a844490a31bf1f75a83bc3a /src/cmd/gofmt/rewrite.go
parentae77f3c8ef2f6e1f0d51562323c3ec3b8929521f (diff)
downloadgolang-02c8b926e12ec16c2beedc269cc553fae9360244.tar.gz
gofmt -r: documentation and minor fixes
fix a few paren insertion bugs in the printer too. R=gri, r CC=golang-dev http://codereview.appspot.com/157119
Diffstat (limited to 'src/cmd/gofmt/rewrite.go')
-rw-r--r--src/cmd/gofmt/rewrite.go33
1 files changed, 17 insertions, 16 deletions
diff --git a/src/cmd/gofmt/rewrite.go b/src/cmd/gofmt/rewrite.go
index 9399bcd49..ccbfe1d7f 100644
--- a/src/cmd/gofmt/rewrite.go
+++ b/src/cmd/gofmt/rewrite.go
@@ -65,23 +65,23 @@ func rewriteFile(pattern, replace ast.Expr, p *ast.File) *ast.File {
for k := range m {
m[k] = nil, false
}
+ val = apply(f, val);
if match(m, pat, val) {
- return subst(m, repl)
+ val = subst(m, repl, reflect.NewValue(val.Interface().(ast.Node).Pos()))
}
- return apply(f, val);
+ return val;
};
return apply(f, reflect.NewValue(p)).Interface().(*ast.File);
}
var positionType = reflect.Typeof(token.Position{})
-var zeroPosition = reflect.NewValue(token.Position{})
var identType = reflect.Typeof((*ast.Ident)(nil))
func isWildcard(s string) bool {
- rune, _ := utf8.DecodeRuneInString(s);
- return unicode.Is(unicode.Greek, rune) && unicode.IsLower(rune);
+ rune, size := utf8.DecodeRuneInString(s);
+ return size == len(s) && unicode.IsLower(rune);
}
@@ -173,10 +173,11 @@ func match(m map[string]reflect.Value, pattern, val reflect.Value) bool {
}
-// subst returns a copy of pattern with values from m substituted in place of wildcards.
-// if m == nil, subst returns a copy of pattern.
-// Either way, the returned value has no valid line number information.
-func subst(m map[string]reflect.Value, pattern reflect.Value) reflect.Value {
+// subst returns a copy of pattern with values from m substituted in place
+// of wildcards and pos used as the position of tokens from the pattern.
+// if m == nil, subst returns a copy of pattern and doesn't change the line
+// number information.
+func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value) reflect.Value {
if pattern == nil {
return nil
}
@@ -186,13 +187,13 @@ func subst(m map[string]reflect.Value, pattern reflect.Value) reflect.Value {
name := pattern.Interface().(*ast.Ident).Value;
if isWildcard(name) {
if old, ok := m[name]; ok {
- return subst(nil, old)
+ return subst(nil, old, nil)
}
}
}
- if pattern.Type() == positionType {
- return zeroPosition
+ if pos != nil && pattern.Type() == positionType {
+ return pos
}
// Otherwise copy.
@@ -200,25 +201,25 @@ func subst(m map[string]reflect.Value, pattern reflect.Value) reflect.Value {
case *reflect.SliceValue:
v := reflect.MakeSlice(p.Type().(*reflect.SliceType), p.Len(), p.Len());
for i := 0; i < p.Len(); i++ {
- v.Elem(i).SetValue(subst(m, p.Elem(i)))
+ v.Elem(i).SetValue(subst(m, p.Elem(i), pos))
}
return v;
case *reflect.StructValue:
v := reflect.MakeZero(p.Type()).(*reflect.StructValue);
for i := 0; i < p.NumField(); i++ {
- v.Field(i).SetValue(subst(m, p.Field(i)))
+ v.Field(i).SetValue(subst(m, p.Field(i), pos))
}
return v;
case *reflect.PtrValue:
v := reflect.MakeZero(p.Type()).(*reflect.PtrValue);
- v.PointTo(subst(m, p.Elem()));
+ v.PointTo(subst(m, p.Elem(), pos));
return v;
case *reflect.InterfaceValue:
v := reflect.MakeZero(p.Type()).(*reflect.InterfaceValue);
- v.SetValue(subst(m, p.Elem()));
+ v.SetValue(subst(m, p.Elem(), pos));
return v;
}