fix(treesitter): don't use [1] and ipairs on trees()

|LanguageTree:trees()| no longer guarantees that the returned table is
list-like even after a full parse.

NOTE: Actually, they should work fine without modification in these
specific cases.
* dev.lua: For the root tree, the returned table is always `{ tree }`
  unless the private method `LanguageTree:set_included_regions` is used.
* gen_help_html.lua: `trees()` is initially list-like in the very first
  parse. So `ipairs` should be fine if source is not modified.

However, it seems better to "fix" them for consistency.
This commit is contained in:
Jaehwang Jung 2024-05-21 01:29:59 +09:00
parent 2a56b563e5
commit d15d002bf4
2 changed files with 4 additions and 3 deletions

View File

@ -556,7 +556,8 @@ local function update_editor_highlights(query_win, base_win, lang)
-- Remove the '@' from the cursor word
cursor_word = cursor_word:sub(2)
local topline, botline = vim.fn.line('w0', base_win), vim.fn.line('w$', base_win)
for id, node in query:iter_captures(parser:trees()[1]:root(), base_buf, topline - 1, botline) do
local _, tree = next(parser:trees())
for id, node in query:iter_captures(tree:root(), base_buf, topline - 1, botline) do
local capture_name = query.captures[id]
if capture_name == cursor_word then
local lnum, col, end_lnum, end_col = node:range()

View File

@ -802,7 +802,7 @@ local function validate_one(fname, parser_path)
parse_errors = {},
}
local lang_tree, buf = parse_buf(fname, parser_path)
for _, tree in ipairs(lang_tree:trees()) do
for _, tree in pairs(lang_tree:trees()) do
visit_validate(tree:root(), 0, tree, { buf = buf, fname = fname }, stats)
end
lang_tree:destroy()
@ -909,7 +909,7 @@ local function gen_one(fname, to_fname, old, commit, parser_path)
]]
local main = ''
for _, tree in ipairs(lang_tree:trees()) do
for _, tree in pairs(lang_tree:trees()) do
main = main
.. (
visit_node(