aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPham Huy Hoang <hoangtun0810@gmail.com>2023-09-04 16:19:33 +0900
committerChristian Clason <c.clason@uni-graz.at>2025-05-12 18:43:40 +0200
commit3966b8808b36ce263111c7e177b4364d32e67a2d (patch)
tree9f418bf636767f5ec4d375dddfab476e11a39933
parentdocs(readme): document 'location' key (diff)
downloadnvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar.gz
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar.bz2
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar.lz
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar.xz
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.tar.zst
nvim-treesitter-3966b8808b36ce263111c7e177b4364d32e67a2d.zip
fix!: indents now rely on treesitter highlight
- Apply suggestions from Lewis to only parse visible lines - Fix failed tests
-rw-r--r--tests/query/highlights_spec.lua63
-rw-r--r--tests/query/injection_spec.lua9
2 files changed, 27 insertions, 45 deletions
diff --git a/tests/query/highlights_spec.lua b/tests/query/highlights_spec.lua
index b6a52124a..0529af4a4 100644
--- a/tests/query/highlights_spec.lua
+++ b/tests/query/highlights_spec.lua
@@ -1,4 +1,3 @@
-local highlighter = require('vim.treesitter.highlighter')
local ts = vim.treesitter
local COMMENT_NODES = {
@@ -7,16 +6,16 @@ local COMMENT_NODES = {
}
local function check_assertions(file)
- local buf = vim.fn.bufadd(file)
- vim.fn.bufload(file)
- local ft = vim.bo[buf].filetype
- local lang = vim.treesitter.language.get_lang(ft) or ft
assert.same(
1,
vim.fn.executable('highlight-assertions'),
'"highlight-assertions" not executable!'
.. ' Get it via "cargo install --git https://github.com/theHamsta/highlight-assertions"'
)
+ local buf = vim.fn.bufadd(file)
+ vim.fn.bufload(file)
+ local ft = vim.bo[buf].filetype
+ local lang = vim.treesitter.language.get_lang(ft) or ft
local comment_node = COMMENT_NODES[lang] or 'comment'
local assertions = vim.fn.json_decode(
vim.fn.system(
@@ -28,21 +27,21 @@ local function check_assertions(file)
.. comment_node
)
)
- local parser = ts.get_parser(buf, lang)
- parser:parse(true)
+ assert.True(#assertions > 0, 'No assertions detected!')
- local self = highlighter.new(parser, {})
+ local parser = ts.get_parser(buf)
+
+ parser:parse(true)
- assert.True(#assertions > 0, 'No assertions detected!')
for _, assertion in ipairs(assertions) do
local row = assertion.position.row
local col = assertion.position.column
+ assert.is.number(row)
+ assert.is.number(col)
local captures = {}
- local highlights = {}
- self:prepare_highlight_states(row, row + 1)
- self:for_each_highlight_state(function(state)
- if not state.tstree then
+ parser:for_each_tree(function(tstree, tree)
+ if not tstree then
return
end
@@ -54,36 +53,24 @@ local function check_assertions(file)
return
end
- local query = state.highlighter_query
-
- -- Some injected languages may not have highlight queries.
- if not query:query() then
+ local query = ts.query.get(tree:lang(), 'highlights')
+ if not query then
return
end
- local iter = query:query():iter_captures(root, self.bufnr, row, row + 1)
-
- for capture, node, _ in iter do
- local hl = query:get_hl_from_capture(capture)
- assert.is.truthy(hl)
-
- assert.Truthy(node)
- assert.is.number(row)
- assert.is.number(col)
- if hl and ts.is_in_node_range(node, row, col) then
- local c = query._query.captures[capture] -- name of the capture in the query
- if c ~= nil and c ~= 'spell' and c ~= 'conceal' then
- captures[c] = true
- highlights[c] = true
+ for id, node, _ in query:iter_captures(root, buf, row, row + 1) do
+ if ts.is_in_node_range(node, row, col) then
+ local capture = query.captures[id]
+ if capture ~= nil and capture ~= 'conceal' and capture ~= 'spell' then
+ captures[capture] = true
end
end
end
end)
if assertion.expected_capture_name:match('^!') then
assert.Falsy(
- captures[assertion.expected_capture_name:sub(2)]
- or highlights[assertion.expected_capture_name:sub(2)],
- 'Error in at '
+ captures[assertion.expected_capture_name:sub(2)],
+ 'Error in '
.. file
.. ':'
.. (row + 1)
@@ -93,13 +80,11 @@ local function check_assertions(file)
.. assertion.expected_capture_name
.. '", captures: '
.. vim.inspect(vim.tbl_keys(captures))
- .. '", highlights: '
- .. vim.inspect(vim.tbl_keys(highlights))
)
else
assert.True(
- captures[assertion.expected_capture_name] or highlights[assertion.expected_capture_name],
- 'Error in at '
+ captures[assertion.expected_capture_name],
+ 'Error in '
.. file
.. ':'
.. (row + 1)
@@ -109,8 +94,6 @@ local function check_assertions(file)
.. assertion.expected_capture_name
.. '", captures: '
.. vim.inspect(vim.tbl_keys(captures))
- .. '", highlights: '
- .. vim.inspect(vim.tbl_keys(highlights))
)
end
end
diff --git a/tests/query/injection_spec.lua b/tests/query/injection_spec.lua
index 91096fee4..e90925e20 100644
--- a/tests/query/injection_spec.lua
+++ b/tests/query/injection_spec.lua
@@ -26,7 +26,6 @@ local function check_assertions(file)
)
local parser = ts.get_parser(buf, lang)
- local self = parser
local top_level_root = parser:parse(true)[1]:root()
for _, assertion in ipairs(assertions) do
@@ -37,7 +36,7 @@ local function check_assertions(file)
assertion.expected_capture_name = neg_assert and assertion.expected_capture_name:sub(2)
or assertion.expected_capture_name
local found = false
- self:for_each_tree(function(tstree, tree)
+ parser:for_each_tree(function(tstree, tree)
if not tstree then
return
end
@@ -50,11 +49,11 @@ local function check_assertions(file)
if assertion.expected_capture_name == tree:lang() then
found = true
end
- end, true)
+ end)
if neg_assert then
assert.False(
found,
- 'Error in at '
+ 'Error in '
.. file
.. ':'
.. (row + 1)
@@ -67,7 +66,7 @@ local function check_assertions(file)
else
assert.True(
found,
- 'Error in at '
+ 'Error in '
.. file
.. ':'
.. (row + 1)