-
Notifications
You must be signed in to change notification settings - Fork 7
/
highlights.lua
221 lines (168 loc) · 4.23 KB
/
highlights.lua
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
local parser = require 'plugins.evergreen.parser'
local languages = require 'plugins.evergreen.languages'
local ts = require 'libraries.tree_sitter'
local M = {}
local function localPath()
local str = debug.getinfo(2, 'S').source:sub(2)
return str:match '(.*[/\\])'
end
local function predicatesFor(doc)
local function getSource(n)
local startPt = n:start_point()
local endPt = n:end_point()
local startRow, startCol = startPt:row() + 1, startPt:column() + 1
local endRow, endCol = endPt:row() + 1, endPt:column() + 1
return doc:get_text(startRow, startCol, endRow, endCol)
end
local function coerceToStr(n)
if type(n) ~= 'string' then
return getSource(n:one_node())
else
return n
end
end
local predicates = {
['eq?'] = function(ns, m)
local str = coerceToStr(m)
for _, n in ipairs(ns:nodes()) do
if getSource(n) ~= str then return false end
end
return true
end,
['any-eq?'] = function(ns, m)
local str = coerceToStr(m)
for _, n in ipairs(ns:nodes()) do
if getSource(n) == str then return true end
end
return false
end,
['match?'] = function(ns, s)
local r = regex.compile(s)
for _, n in ipairs(ns:nodes()) do
if not r:cmatch(getSource(n), 0, 0) then return false end
end
return true
end,
['any-match?'] = function(ns, s)
local r = regex.compile(s)
for _, n in ipairs(ns:nodes()) do
if r:cmatch(getSource(n), 0, 0) then return true end
end
return false
end,
['lua-match?'] = function(ns, p)
for _, n in ipairs(ns:nodes()) do
if not getSource(n):match(p) then return false end
end
return true
end,
['any-lua-match?'] = function(ns, p)
for _, n in ipairs(ns:nodes()) do
if getSource(n):match(p) then return true end
end
return false
end,
['contains?'] = function(ns, ...)
local ts = {...}
for _, n in ipairs(ns:nodes()) do
local s = getSource(n)
for _, t in ipairs(ts) do
if not s:find(t, 1, true) then return false end
end
end
return true
end,
['any-contains?'] = function(ns, ...)
local ts = {...}
for _, n in ipairs(ns:nodes()) do
local s = getSource(n)
for _, t in ipairs(ts) do
if s:find(t, 1, true) then return true end
end
end
return false
end,
['any-of?'] = function(ns, ...)
local ts = {}
for _, t in ipairs {...} do
ts[t] = true
end
for _, n in ipairs(ns:nodes()) do
if not ts[getSource(n)] then return false end
end
return true
end,
['has-ancestor?'] = function(n, ...)
local ts = {}
for _, t in ipairs {...} do
ts[t] = true
end
local a = n:one_node()
while a do
a = a:parent()
if ts[a:type()] then return true end
end
return false
end,
['has-parent?'] = function(n, ...)
local p = n:one_node():parent():type()
for _, t in ipairs {...} do
if p == t then return true end
end
return false
end,
['set!'] = function()
-- dummy
end,
}
local ret = {}
for name, fn in pairs(predicates) do
ret[name] = fn
if name:sub(-1) == '?' then
ret['not-' .. name] = function(...)
return not fn(...)
end
end
end
return ret
end
function M.query(ftype)
local ff = io.open(string.format('%s/queries/%s/highlights.scm', localPath(), ftype))
if not ff then
return ""
end
local highlights = ff:read '*a'
ff:close()
return highlights
end
--- @param doc core.doc
function M.init(doc)
local function getSource(n)
local startPt = n:start_point()
local endPt = n:end_point()
local startRow, startCol = startPt:row() + 1, startPt:column() + 1
local endRow, endCol = endPt:row() + 1, endPt:column()
if startRow == endRow then
return doc.lines[startRow]:sub(startCol, endCol)
end
local lns = {}
lns[1] = doc.lines[startRow]:sub(startCol)
for i = startRow + 1, endRow - 1 do
lns[#lns + 1] = doc.lines[i]
end
lns[#lns + 1] = doc.lines[endRow]:sub(1, endCol)
return table.concat(lns)
end
if not doc.filename then return end
local p = parser.get(languages.fromDoc(doc))
if p then
doc.treesit = true
doc.ts = {
parser = p,
tree = p:parse(nil, parser.input(doc.lines)),
query = ts.Query.new(p:language(), M.query(languages.fromDoc(doc))),
runner = ts.Query.Runner.new(predicatesFor(doc)),
}
end
end
return M