-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtreesitter.go
More file actions
203 lines (171 loc) · 5.24 KB
/
Copy pathtreesitter.go
File metadata and controls
203 lines (171 loc) · 5.24 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package wig
import (
"context"
"os"
"strings"
"sync"
"unicode/utf8"
"unsafe"
odin "github.com/firstrow/tree-sitter-odin/bindings/go"
sitter "github.com/tree-sitter/go-tree-sitter"
clang "github.com/tree-sitter/tree-sitter-c/bindings/go"
golang "github.com/tree-sitter/tree-sitter-go/bindings/go"
python "github.com/tree-sitter/tree-sitter-python/bindings/go"
)
// TODO: rewrite treesitter to use channel and scheduled parsing. some day.
var tslock sync.Mutex
var _ Highlighter = &TreeSitterHighlighter{}
type TreeSitterHighlighter struct {
e *Editor
buf *Buffer
parser *sitter.Parser
q *sitter.Query
tree *sitter.Tree
sourceCode []byte
}
func TreeSitterHighlighterGo(e *Editor) {
go func() {
for event := range e.Events.Subscribe() {
switch e := event.Msg.(type) {
case EventTextChange:
if e.Buf.Highlighter != nil {
e.Buf.Highlighter.TextChanged(e)
}
}
event.Wg.Done()
}
}()
}
func (h *TreeSitterHighlighter) TextChanged(event EventTextChange) {
if event.Buf == nil {
return
}
tslock.Lock()
defer tslock.Unlock()
ll := h.editEditInput(event)
h.tree.Edit(&ll)
h.sourceCode = []byte(event.Buf.String())
tree := h.parser.ParseCtx(context.Background(), h.sourceCode, h.tree)
h.tree.Close()
h.tree = tree
}
func TreeSitterHighlighterInitBuffer(e *Editor, buf *Buffer) *TreeSitterHighlighter {
var treeSitterLang unsafe.Pointer
qpath := ""
switch {
case strings.HasSuffix(buf.FilePath, ".go"):
treeSitterLang = golang.Language()
qpath = "go"
case strings.HasSuffix(buf.FilePath, ".odin"):
treeSitterLang = odin.Language()
qpath = "odin"
case strings.HasSuffix(buf.FilePath, ".c"):
treeSitterLang = clang.Language()
qpath = "c"
case strings.HasSuffix(buf.FilePath, ".h"):
treeSitterLang = clang.Language()
qpath = "c"
case strings.HasSuffix(buf.FilePath, ".py"):
treeSitterLang = python.Language()
qpath = "python"
default:
return nil
}
h := &TreeSitterHighlighter{
e: e,
buf: buf,
}
h.parser = sitter.NewParser()
h.parser.SetLanguage(sitter.NewLanguage(treeSitterLang))
var err error
hgFile := e.RuntimeDir("queries", qpath, "highlights.scm")
highlightQ, err := os.ReadFile(hgFile)
if err != nil {
EditorInst.LogError(err, true)
return nil
}
// TODO: check that weird error. geeeeeee.
h.q, _ = sitter.NewQuery(sitter.NewLanguage(treeSitterLang), string(highlightQ))
h.Build()
return h
}
func (h *TreeSitterHighlighter) Build() {
tslock.Lock()
defer tslock.Unlock()
if h == nil {
return
}
if h.tree != nil {
h.tree.Close()
}
h.sourceCode = []byte(h.buf.String())
tree := h.parser.Parse(h.sourceCode, nil)
h.tree = tree
}
func (h *TreeSitterHighlighter) ForRange(lineStart, lineEnd uint32) *HighlighterCursor {
tslock.Lock()
defer tslock.Unlock()
qc := sitter.NewQueryCursor()
qc.SetPointRange(
sitter.Point{Row: uint(lineStart), Column: 0},
sitter.Point{Row: uint(lineEnd), Column: 0},
)
defer qc.Close()
matches := qc.Matches(h.q, h.tree.RootNode(), h.sourceCode)
nodes := List[HighlighterNode]{}
for match := matches.Next(); match != nil; match = matches.Next() {
for _, capture := range match.Captures {
row := capture.Node.StartPosition().Row
col := capture.Node.StartPosition().Column
erow := capture.Node.EndPosition().Row
ecol := capture.Node.EndPosition().Column
nodes.PushBack(HighlighterNode{
NodeName: h.q.CaptureNames()[capture.Index],
StartLine: uint32(row),
StartChar: uint32(col),
EndLine: uint32(erow),
EndChar: uint32(ecol),
})
}
}
return &HighlighterCursor{
Cursor: nodes.First(),
}
}
func (h *TreeSitterHighlighter) editEditInput(event EventTextChange) (r sitter.InputEdit) {
pointToByte := func(buf *Buffer, line, char int) int {
size := 0
lineNum := 0
currentLine := buf.Lines.First()
for currentLine != nil {
if lineNum == line {
v := currentLine.Value.Range(0, char)
return size + utf8.RuneCountInString(string(v))
}
size += currentLine.Value.Bytes()
currentLine = currentLine.Next()
lineNum++
}
return size
}
// deletion
if len(event.Text) == 0 {
return sitter.InputEdit{
StartPosition: sitter.Point{Row: uint(event.Start.Line), Column: uint(event.Start.Char)},
OldEndPosition: sitter.Point{Row: uint(event.End.Line), Column: uint(event.End.Char)},
NewEndPosition: sitter.Point{Row: uint(event.Start.Line), Column: uint(event.Start.Char)},
StartByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char)),
OldEndByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char) + len(event.OldText)),
NewEndByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char)),
}
}
// insertion
return sitter.InputEdit{
StartPosition: sitter.Point{Row: uint(event.Start.Line), Column: uint(event.Start.Char)},
OldEndPosition: sitter.Point{Row: uint(event.Start.Line), Column: uint(event.Start.Char)},
NewEndPosition: sitter.Point{Row: uint(event.NewEnd.Line), Column: uint(event.NewEnd.Char)},
StartByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char)),
OldEndByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char)),
NewEndByte: uint(pointToByte(event.Buf, event.Start.Line, event.Start.Char) + utf8.RuneCountInString(event.Text)),
}
}