/
util.go
48 lines (43 loc) · 1.23 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package astut
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
)
func NodeFilename(fset *token.FileSet, node ast.Node) (string, error) {
f := fset.File(node.Pos())
if f == nil {
return "", fmt.Errorf("not found")
}
return f.Name(), nil
}
//----------
// print ast notes
// TODO: without tabwidth set, it won't output the source correctly
// Fail: has struct fields without spaces "field int"->"fieldint"
//cfg := &printer.Config{Mode: printer.SourcePos | printer.TabIndent}
// Fail: has stmts split with comments in the middle
//cfg := &printer.Config{Mode: printer.SourcePos | printer.TabIndent | printer.UseSpaces}
// debug
//cfg := &printer.Config{Tabwidth: 4}
//cfg := &printer.Config{Mode: printer.SourcePos, Tabwidth: 4}
func PrintNode(fset *token.FileSet, node any) {
fmt.Println(SprintNode(fset, node))
}
func SprintNode(fset *token.FileSet, node any) string {
s, err := SprintNode2(fset, node)
if err != nil {
return fmt.Sprintf("<sprintnodeerr:%v>", err)
}
return s
}
func SprintNode2(fset *token.FileSet, node any) (string, error) {
buf := &bytes.Buffer{}
cfg := &printer.Config{Mode: printer.RawFormat}
if err := cfg.Fprint(buf, fset, node); err != nil {
return "", err
}
return string(buf.Bytes()), nil
}