mirror of
https://github.com/chenasraf/nvim-treesitter.git
synced 2026-05-18 01:39:00 +00:00
fix!: indents now rely on treesitter highlight
- Apply suggestions from Lewis to only parse visible lines - Fix failed tests
This commit is contained in:
committed by
Christian Clason
parent
abcbe65b6e
commit
3966b8808b
@@ -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)
|
||||
assert.True(#assertions > 0, 'No assertions detected!')
|
||||
|
||||
local parser = ts.get_parser(buf)
|
||||
|
||||
parser:parse(true)
|
||||
|
||||
local self = highlighter.new(parser, {})
|
||||
|
||||
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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user