mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04: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:
parent
abcbe65b6e
commit
3966b8808b
2 changed files with 27 additions and 45 deletions
|
|
@ -1,4 +1,3 @@
|
||||||
local highlighter = require('vim.treesitter.highlighter')
|
|
||||||
local ts = vim.treesitter
|
local ts = vim.treesitter
|
||||||
|
|
||||||
local COMMENT_NODES = {
|
local COMMENT_NODES = {
|
||||||
|
|
@ -7,16 +6,16 @@ local COMMENT_NODES = {
|
||||||
}
|
}
|
||||||
|
|
||||||
local function check_assertions(file)
|
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(
|
assert.same(
|
||||||
1,
|
1,
|
||||||
vim.fn.executable('highlight-assertions'),
|
vim.fn.executable('highlight-assertions'),
|
||||||
'"highlight-assertions" not executable!'
|
'"highlight-assertions" not executable!'
|
||||||
.. ' Get it via "cargo install --git https://github.com/theHamsta/highlight-assertions"'
|
.. ' 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 comment_node = COMMENT_NODES[lang] or 'comment'
|
||||||
local assertions = vim.fn.json_decode(
|
local assertions = vim.fn.json_decode(
|
||||||
vim.fn.system(
|
vim.fn.system(
|
||||||
|
|
@ -28,21 +27,21 @@ local function check_assertions(file)
|
||||||
.. comment_node
|
.. 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)
|
parser:parse(true)
|
||||||
|
|
||||||
local self = highlighter.new(parser, {})
|
|
||||||
|
|
||||||
assert.True(#assertions > 0, 'No assertions detected!')
|
|
||||||
for _, assertion in ipairs(assertions) do
|
for _, assertion in ipairs(assertions) do
|
||||||
local row = assertion.position.row
|
local row = assertion.position.row
|
||||||
local col = assertion.position.column
|
local col = assertion.position.column
|
||||||
|
assert.is.number(row)
|
||||||
|
assert.is.number(col)
|
||||||
|
|
||||||
local captures = {}
|
local captures = {}
|
||||||
local highlights = {}
|
parser:for_each_tree(function(tstree, tree)
|
||||||
self:prepare_highlight_states(row, row + 1)
|
if not tstree then
|
||||||
self:for_each_highlight_state(function(state)
|
|
||||||
if not state.tstree then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -54,36 +53,24 @@ local function check_assertions(file)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local query = state.highlighter_query
|
local query = ts.query.get(tree:lang(), 'highlights')
|
||||||
|
if not query then
|
||||||
-- Some injected languages may not have highlight queries.
|
|
||||||
if not query:query() then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local iter = query:query():iter_captures(root, self.bufnr, row, row + 1)
|
for id, node, _ in query:iter_captures(root, buf, row, row + 1) do
|
||||||
|
if ts.is_in_node_range(node, row, col) then
|
||||||
for capture, node, _ in iter do
|
local capture = query.captures[id]
|
||||||
local hl = query:get_hl_from_capture(capture)
|
if capture ~= nil and capture ~= 'conceal' and capture ~= 'spell' then
|
||||||
assert.is.truthy(hl)
|
captures[capture] = true
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
if assertion.expected_capture_name:match('^!') then
|
if assertion.expected_capture_name:match('^!') then
|
||||||
assert.Falsy(
|
assert.Falsy(
|
||||||
captures[assertion.expected_capture_name:sub(2)]
|
captures[assertion.expected_capture_name:sub(2)],
|
||||||
or highlights[assertion.expected_capture_name:sub(2)],
|
'Error in '
|
||||||
'Error in at '
|
|
||||||
.. file
|
.. file
|
||||||
.. ':'
|
.. ':'
|
||||||
.. (row + 1)
|
.. (row + 1)
|
||||||
|
|
@ -93,13 +80,11 @@ local function check_assertions(file)
|
||||||
.. assertion.expected_capture_name
|
.. assertion.expected_capture_name
|
||||||
.. '", captures: '
|
.. '", captures: '
|
||||||
.. vim.inspect(vim.tbl_keys(captures))
|
.. vim.inspect(vim.tbl_keys(captures))
|
||||||
.. '", highlights: '
|
|
||||||
.. vim.inspect(vim.tbl_keys(highlights))
|
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
assert.True(
|
assert.True(
|
||||||
captures[assertion.expected_capture_name] or highlights[assertion.expected_capture_name],
|
captures[assertion.expected_capture_name],
|
||||||
'Error in at '
|
'Error in '
|
||||||
.. file
|
.. file
|
||||||
.. ':'
|
.. ':'
|
||||||
.. (row + 1)
|
.. (row + 1)
|
||||||
|
|
@ -109,8 +94,6 @@ local function check_assertions(file)
|
||||||
.. assertion.expected_capture_name
|
.. assertion.expected_capture_name
|
||||||
.. '", captures: '
|
.. '", captures: '
|
||||||
.. vim.inspect(vim.tbl_keys(captures))
|
.. vim.inspect(vim.tbl_keys(captures))
|
||||||
.. '", highlights: '
|
|
||||||
.. vim.inspect(vim.tbl_keys(highlights))
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ local function check_assertions(file)
|
||||||
)
|
)
|
||||||
local parser = ts.get_parser(buf, lang)
|
local parser = ts.get_parser(buf, lang)
|
||||||
|
|
||||||
local self = parser
|
|
||||||
local top_level_root = parser:parse(true)[1]:root()
|
local top_level_root = parser:parse(true)[1]:root()
|
||||||
|
|
||||||
for _, assertion in ipairs(assertions) do
|
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)
|
assertion.expected_capture_name = neg_assert and assertion.expected_capture_name:sub(2)
|
||||||
or assertion.expected_capture_name
|
or assertion.expected_capture_name
|
||||||
local found = false
|
local found = false
|
||||||
self:for_each_tree(function(tstree, tree)
|
parser:for_each_tree(function(tstree, tree)
|
||||||
if not tstree then
|
if not tstree then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -50,11 +49,11 @@ local function check_assertions(file)
|
||||||
if assertion.expected_capture_name == tree:lang() then
|
if assertion.expected_capture_name == tree:lang() then
|
||||||
found = true
|
found = true
|
||||||
end
|
end
|
||||||
end, true)
|
end)
|
||||||
if neg_assert then
|
if neg_assert then
|
||||||
assert.False(
|
assert.False(
|
||||||
found,
|
found,
|
||||||
'Error in at '
|
'Error in '
|
||||||
.. file
|
.. file
|
||||||
.. ':'
|
.. ':'
|
||||||
.. (row + 1)
|
.. (row + 1)
|
||||||
|
|
@ -67,7 +66,7 @@ local function check_assertions(file)
|
||||||
else
|
else
|
||||||
assert.True(
|
assert.True(
|
||||||
found,
|
found,
|
||||||
'Error in at '
|
'Error in '
|
||||||
.. file
|
.. file
|
||||||
.. ':'
|
.. ':'
|
||||||
.. (row + 1)
|
.. (row + 1)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue