Reorganize code into individual tool files
This commit is contained in:
57
tool_find_references.go
Normal file
57
tool_find_references.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
type Reference struct {
|
||||
Context string `json:"context"`
|
||||
Kind string `json:"kind"`
|
||||
Position Position `json:"position"`
|
||||
}
|
||||
|
||||
func findReferences(dir string, symbol string) ([]Reference, error) {
|
||||
var refs []Reference
|
||||
|
||||
err := walkGoFiles(dir, func(path string, src []byte, file *ast.File, fset *token.FileSet) error {
|
||||
|
||||
ast.Inspect(file, func(n ast.Node) bool {
|
||||
switch node := n.(type) {
|
||||
case *ast.Ident:
|
||||
if node.Name == symbol {
|
||||
pos := fset.Position(node.Pos())
|
||||
kind := identifyReferenceKind(node)
|
||||
context := extractContext(src, pos)
|
||||
|
||||
refs = append(refs, Reference{
|
||||
Context: context,
|
||||
Kind: kind,
|
||||
Position: newPosition(pos),
|
||||
})
|
||||
}
|
||||
|
||||
case *ast.SelectorExpr:
|
||||
if node.Sel.Name == symbol {
|
||||
pos := fset.Position(node.Sel.Pos())
|
||||
context := extractContext(src, pos)
|
||||
|
||||
refs = append(refs, Reference{
|
||||
Context: context,
|
||||
Kind: "selector",
|
||||
Position: newPosition(pos),
|
||||
})
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return refs, err
|
||||
}
|
||||
|
||||
func identifyReferenceKind(ident *ast.Ident) string {
|
||||
return "identifier"
|
||||
}
|
||||
Reference in New Issue
Block a user