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
947c43052c
commit
ac9e755942
3 changed files with 29 additions and 46 deletions
|
|
@ -123,7 +123,7 @@ function M.get_indent(lnum)
|
||||||
-- some languages like Python will actually have worse results when re-parsing at opened new line
|
-- some languages like Python will actually have worse results when re-parsing at opened new line
|
||||||
if not M.avoid_force_reparsing[root_lang] then
|
if not M.avoid_force_reparsing[root_lang] then
|
||||||
-- Reparse in case we got triggered by ":h indentkeys"
|
-- Reparse in case we got triggered by ":h indentkeys"
|
||||||
parser:parse()
|
parser:parse({ vim.fn.line('w0') - 1, vim.fn.line('w$') - 1 })
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Get language tree with smallest range around node that's not a comment parser
|
-- Get language tree with smallest range around node that's not a comment parser
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
local highlighter = require('vim.treesitter.highlighter')
|
|
||||||
local ts = vim.treesitter
|
local ts = vim.treesitter
|
||||||
|
|
||||||
local COMMENT_NODES = {
|
local COMMENT_NODES = {
|
||||||
|
|
@ -6,16 +5,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(
|
||||||
|
|
@ -27,18 +26,20 @@ local function check_assertions(file)
|
||||||
.. comment_node
|
.. comment_node
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
local parser = ts.get_parser(buf, lang)
|
|
||||||
|
|
||||||
local self = highlighter.new(parser, {})
|
|
||||||
|
|
||||||
assert.True(#assertions > 0, 'No assertions detected!')
|
assert.True(#assertions > 0, 'No assertions detected!')
|
||||||
|
|
||||||
|
local parser = ts.get_parser(buf)
|
||||||
|
|
||||||
|
parser:parse(true)
|
||||||
|
|
||||||
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.tree:for_each_tree(function(tstree, tree)
|
|
||||||
if not tstree then
|
if not tstree then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -51,36 +52,24 @@ local function check_assertions(file)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local query = self:get_query(tree:lang())
|
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.hl_cache[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)
|
||||||
|
|
@ -90,13 +79,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)
|
||||||
|
|
@ -106,8 +93,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
|
||||||
|
|
|
||||||
|
|
@ -25,9 +25,7 @@ local function check_assertions(file)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
local parser = ts.get_parser(buf, lang)
|
local parser = ts.get_parser(buf, lang)
|
||||||
|
local top_level_root = parser:parse(true)[1]:root()
|
||||||
local self = ts.highlighter.new(parser, {})
|
|
||||||
local top_level_root = parser:parse()[1]:root()
|
|
||||||
|
|
||||||
for _, assertion in ipairs(assertions) do
|
for _, assertion in ipairs(assertions) do
|
||||||
local row = assertion.position.row
|
local row = assertion.position.row
|
||||||
|
|
@ -37,7 +35,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.tree: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 +48,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 +65,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