+55
-10
lines changedFilter options
+55
-10
lines changed Original file line number Diff line number Diff line change
@@ -164,8 +164,6 @@ function M.parse_query(lang, query)
164
164
return self
165
165
end
166
166
167
-
-- TODO(vigoux): support multiline nodes too
168
-
169
167
--- Gets the text corresponding to a given node
170
168
---
171
169
---@param node the node
@@ -175,11 +173,26 @@ function M.get_node_text(node, source)
175
173
local end_row, end_col, end_byte = node:end_()
176
174
177
175
if type(source) == "number" then
178
-
if start_row ~= end_row then
176
+
local lines
177
+
local eof_row = vim.api.nvim_buf_line_count(source)
178
+
if start_row >= eof_row then
179
179
return nil
180
180
end
181
-
local line = a.nvim_buf_get_lines(source, start_row, start_row+1, true)[1]
182
-
return string.sub(line, start_col+1, end_col)
181
+
if end_col == 0 then
182
+
lines = a.nvim_buf_get_lines(source, start_row, end_row, true)
183
+
end_col = #lines[#lines]
184
+
else
185
+
lines = a.nvim_buf_get_lines(source, start_row, end_row + 1, true)
186
+
end
187
+
lines[1] = string.sub(lines[1], start_col + 1)
188
+
189
+
local end_index = end_col
190
+
if #lines == 1 then
191
+
end_index = end_col - start_col
192
+
end
193
+
lines[#lines] = string.sub(lines[#lines], 1, end_index)
194
+
195
+
return table.concat(lines, "\n")
183
196
elseif type(source) == "string" then
184
197
return source:sub(start_byte+1, end_byte)
185
198
end
@@ -211,11 +224,6 @@ local predicate_handlers = {
211
224
["lua-match?"] = function(match, _, source, predicate)
212
225
local node = match[predicate[2]]
213
226
local regex = predicate[3]
214
-
local start_row, _, end_row, _ = node:range()
215
-
if start_row ~= end_row then
216
-
return false
217
-
end
218
-
219
227
return string.find(M.get_node_text(node, source), regex)
220
228
end,
221
229
Original file line number Diff line number Diff line change
@@ -227,6 +227,43 @@ void ui_refresh(void)
227
227
}, res)
228
228
end)
229
229
230
+
it('supports getting text of multiline node', function()
231
+
if pending_c_parser(pending) then return end
232
+
insert(test_text)
233
+
local res = exec_lua([[
234
+
local parser = vim.treesitter.get_parser(0, "c")
235
+
local tree = parser:parse()[1]
236
+
return vim.treesitter.get_node_text(tree:root(), 0)
237
+
]])
238
+
eq(test_text, res)
239
+
240
+
local res2 = exec_lua([[
241
+
local parser = vim.treesitter.get_parser(0, "c")
242
+
local root = parser:parse()[1]:root()
243
+
return vim.treesitter.get_node_text(root:child(0):child(0), 0)
244
+
]])
245
+
eq('void', res2)
246
+
end)
247
+
248
+
it('support getting text where start of node is past EOF', function()
249
+
local text = [[
250
+
def run
251
+
a = <<~E
252
+
end]]
253
+
insert(text)
254
+
local result = exec_lua([[
255
+
local fake_node = {}
256
+
function fake_node:start()
257
+
return 3, 0, 23
258
+
end
259
+
function fake_node:end_()
260
+
return 3, 0, 23
261
+
end
262
+
return vim.treesitter.get_node_text(fake_node, 0) == nil
263
+
]])
264
+
eq(true, result)
265
+
end)
266
+
230
267
it('can match special regex characters like \\ * + ( with `vim-match?`', function()
231
268
insert('char* astring = "\\n"; (1 + 1) * 2 != 2;')
232
269
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4