-
Notifications
You must be signed in to change notification settings - Fork 38
/
ast_node.go
56 lines (49 loc) · 1.07 KB
/
ast_node.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
49
50
51
52
53
54
55
56
package core
import (
"fmt"
"math"
"strings"
sitter "github.com/smacker/go-tree-sitter"
)
type (
NodeEndpoints interface {
StartPoint() sitter.Point
EndPoint() sitter.Point
}
NodeContent struct {
Endpoints NodeEndpoints
Content string
}
)
func (n NodeContent) Format(s fmt.State, verb rune) {
start := n.Endpoints.StartPoint()
end := n.Endpoints.EndPoint()
if s.Flag('+') || s.Flag('#') {
lineNum := start.Row
lineNumWidth := 1
if end.Row > 0 {
lineNumWidth = int(math.Ceil(math.Log10(float64(end.Row))))
}
lineNumFmt := fmt.Sprintf("%%0%dd", lineNumWidth)
content := lineIndentRE.ReplaceAllStringFunc(n.Content, func(s string) string {
res := fmt.Sprintf(lineNumFmt+"| %s", lineNum, s)
lineNum++
return res
})
fmt.Fprint(s, content)
} else {
content := n.Content
if firstNl := strings.Index(content, "\n"); firstNl >= 0 {
content = content[:firstNl]
}
if end.Row > start.Row {
fmt.Fprintf(s, "%d-%d| %s ...",
start.Row,
end.Row,
content,
)
} else {
fmt.Fprintf(s, "%d| %s", start.Row, content)
}
}
}