Permalink
Browse files

fixed comment mis-placement issue

  • Loading branch information...
1 parent 03dd366 commit 62163a4f123037f972c5cc868d8f30c4f0e700f7 @skelterjohn committed Apr 8, 2011
Showing with 86 additions and 7 deletions.
  1. +63 −0 comments.go
  2. +6 −0 dscan.go
  3. +2 −0 move.go
  4. +8 −2 pkg.go
  5. +3 −4 scan.go
  6. +3 −0 singlemover.go
  7. +1 −1 source.go
View
@@ -0,0 +1,63 @@
+package main
+
+import (
+ //"fmt"
+ "go/ast"
+)
+
+type CommentTie struct {
+ C *ast.Comment
+ Before, After ast.Node
+}
+
+func GetCommentTies(file *ast.File) (ties []CommentTie) {
+ for _, cg := range file.Comments {
+ for _, c := range cg.List {
+ ties = append(ties, CommentTie{C:c})
+ }
+ }
+
+ if len(ties) == 0 {
+ return
+ }
+ tw := TieWalker{ties:ties, next:0}
+
+ ast.Walk(&tw, file)
+ /*
+ for _, ct := range tw.ties {
+ fmt.Printf("%v\n%v\n%v\n\n", ct.Before, ct.C, ct.After)
+ }
+ */
+
+ return
+}
+
+type TieWalker struct {
+ ties []CommentTie
+ next int
+}
+
+func (this *TieWalker) Visit(node ast.Node) ast.Visitor {
+ if node == nil {
+ return this
+ }
+ check := func() bool {
+ if this.next >= len(this.ties) {
+ return false
+ }
+ ch := this.ties[this.next]
+ //fmt.Printf("Checking (%d, %d) vs %d\n", node.Pos(), node.End(), ch.C.Slash)
+ if node.End() < ch.C.Slash {
+ this.ties[this.next].Before = node
+ } else if node.Pos() > ch.C.Slash {
+ this.ties[this.next].After = node
+ this.next++
+ return true
+ }
+ return false
+ }
+
+ for check() {}
+
+ return this
+}
View
@@ -15,6 +15,11 @@ func ScanCmd(args []string) (err os.Error) {
ScanAllForImports(LocalRoot)
for _, path := range args {
pkg := LocalImporter(path)
+
+ for _, file := range pkg.Files {
+ GetCommentTies(file)
+ }
+
ast.Walk(DepthWalker(0), pkg)
}
return
@@ -32,6 +37,7 @@ func (this DepthWalker) Visit(node ast.Node) ast.Visitor {
buffer += " "
}
+ fmt.Printf("%sPos: %d %s\n", buffer, node.Pos(), AllSources.Position(node.Pos()))
fmt.Printf("%s%T\n", buffer, node)
fmt.Printf("%s%v\n", buffer, node)
if e, ok := node.(ast.Expr); ok {
View
@@ -85,6 +85,7 @@ func MoveCmd(args []string) (err os.Error) {
for fpath := range pkg.Files {
_, base := filepath.Split(fpath)
npath := filepath.Join(LocalRoot, newpath, base)
+ _ = npath
err = MoveSource(fpath, npath)
if err != nil {
return
@@ -97,6 +98,7 @@ func MoveCmd(args []string) (err os.Error) {
pcw := PathChangeWalker{OldPath:oldpath, NewPath:newpath}
ast.Walk(&pcw, file)
if pcw.Updated {
+
err = RewriteSource(fpath, file)
if err != nil {
return
View
10 pkg.go
@@ -56,14 +56,17 @@ func PkgCmd(args []string) (err os.Error) {
for _, ip := range ImportedBy[QuotePath(path)] {
ipkg := LocalImporter(ip)
+ if ipkg == nil {
+ return MakeErr("Problem getting package in %s", ip)
+ }
for fpath, file := range ipkg.Files {
uniqueName := GetUniqueIdent([]*ast.File{file}, newname)
if uniqueName != newname {
fmt.Printf("In %s: possible conflict with %s, using %s instead\n", fpath, newname, uniqueName)
}
- pc := PkgChanger{
+ pc := PkgChanger {
path:path,
oldname:oldname,
newname:uniqueName,
@@ -106,7 +109,10 @@ func (this *PkgChanger) Visit(node ast.Node) ast.Visitor {
}
if n.Name == nil && this.newname != this.pkgname {
- n.Name = &ast.Ident{Name:this.newname}
+ n.Name = &ast.Ident {
+ Name : this.newname,
+ NamePos : n.Pos(),
+ }
this.Updated = true
}
}
View
@@ -6,11 +6,10 @@ package main
import (
"os"
- //"fmt"
+ "fmt"
"go/token"
"go/ast"
"path/filepath"
- "log"
"strings"
"rog-go.googlecode.com/hg/exp/go/types"
"rog-go.googlecode.com/hg/exp/go/parser"
@@ -45,7 +44,7 @@ func LocalImporter(path string) (pkg *ast.Package) {
//fmt.Printf("Parsing %v\n", sourcefiles)
dirpkgs, err := parser.ParseFiles(AllSources, sourcefiles, parser.ParseComments)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
return
}
@@ -96,7 +95,7 @@ func ScanForImports(path string) (err os.Error) {
dirpkgs, err := parser.ParseFiles(AllSourceTops, sourcefiles, parser.ImportsOnly)
if err != nil {
- log.Println(err)
+ fmt.Println(err)
}
//take the first non-main. otherwise, main is ok.
View
@@ -226,6 +226,7 @@ func (this *SingleMover) CreateNewSource() (err os.Error) {
return &ast.SelectorExpr {
X : &ast.Ident {
Name : oldPkgNewName,
+ NamePos : idn.NamePos,
},
Sel : idn,
}
@@ -394,6 +395,7 @@ func (this *SingleMover) RemoveUpdatePkg() (err os.Error) {
return &ast.SelectorExpr {
X : &ast.Ident {
Name : newpkgname,
+ NamePos : idn.NamePos,
},
Sel : idn,
}
@@ -488,6 +490,7 @@ func (this *SingleMover) UpdateOther() (err os.Error) {
return &ast.SelectorExpr {
X : &ast.Ident {
Name : newpkgname,
+ NamePos : sel.X.Pos(),
},
Sel : sel.Sel,
}
View
@@ -128,7 +128,7 @@ func RewriteSource(fpath string, file *ast.File) (err os.Error) {
return
}
- err = printer.Fprint(out, token.NewFileSet(), file)
+ err = printer.Fprint(out, AllSources, file)
return
}

0 comments on commit 62163a4

Please sign in to comment.