mirror of
https://github.com/neovim/neovim.git
synced 2025-12-10 08:32:42 +00:00
fix(help): retry once if async parsing fails #36592
Problem: If the buffer changes during iteration of the query matches, `get_node_text()` may fail, showing an error to the user. Solution: Use `pcall` to not propogate the error to the user. Retry once if an error occurred.
This commit is contained in:
committed by
GitHub
parent
ea70d2ad85
commit
e49a592595
@@ -67,10 +67,8 @@ vim.keymap.set('n', '[[', function()
|
|||||||
end, { buffer = 0, silent = false, desc = 'Jump to previous section' })
|
end, { buffer = 0, silent = false, desc = 'Jump to previous section' })
|
||||||
|
|
||||||
local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
|
local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
|
||||||
local root = parser:parse()[1]:root()
|
|
||||||
|
|
||||||
-- Add "runnables" for Lua/Vimscript code examples.
|
local function runnables()
|
||||||
do
|
|
||||||
---@type table<integer, { lang: string, code: string }>
|
---@type table<integer, { lang: string, code: string }>
|
||||||
local code_blocks = {}
|
local code_blocks = {}
|
||||||
local query = vim.treesitter.query.parse(
|
local query = vim.treesitter.query.parse(
|
||||||
@@ -85,6 +83,7 @@ do
|
|||||||
]]
|
]]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
local root = parser:parse()[1]:root()
|
||||||
for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
|
for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
|
||||||
for id, nodes in pairs(match) do
|
for id, nodes in pairs(match) do
|
||||||
local name = query.captures[id]
|
local name = query.captures[id]
|
||||||
@@ -115,39 +114,55 @@ do
|
|||||||
end, { buffer = true })
|
end, { buffer = true })
|
||||||
end
|
end
|
||||||
|
|
||||||
local url_ns = vim.api.nvim_create_namespace('nvim.help.urls')
|
-- Retry once if the buffer has changed during the iteration of the code
|
||||||
local filepath = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
|
-- blocks. See #36574.
|
||||||
|
if not pcall(runnables) then
|
||||||
|
runnables()
|
||||||
|
end
|
||||||
|
|
||||||
if vim.fs.relpath(vim.env.VIMRUNTIME, filepath) ~= nil then
|
local url_ns = vim.api.nvim_create_namespace('nvim.help.urls')
|
||||||
local base = 'https://neovim.io/doc/user/helptag.html?tag='
|
local function urls()
|
||||||
local query = vim.treesitter.query.parse(
|
local filepath = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
|
||||||
'vimdoc',
|
|
||||||
[[
|
if vim.fs.relpath(vim.env.VIMRUNTIME, filepath) ~= nil then
|
||||||
|
local base = 'https://neovim.io/doc/user/helptag.html?tag='
|
||||||
|
local query = vim.treesitter.query.parse(
|
||||||
|
'vimdoc',
|
||||||
|
[[
|
||||||
((optionlink) @helplink)
|
((optionlink) @helplink)
|
||||||
(taglink
|
(taglink
|
||||||
text: (_) @helplink)
|
text: (_) @helplink)
|
||||||
(tag
|
(tag
|
||||||
text: (_) @helplink)
|
text: (_) @helplink)
|
||||||
]]
|
]]
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, match, _ in query:iter_matches(root, 0, 0, -1) do
|
vim.api.nvim_buf_clear_namespace(0, url_ns, 0, -1)
|
||||||
for id, nodes in pairs(match) do
|
local root = parser:parse()[1]:root()
|
||||||
if query.captures[id] == 'helplink' then
|
for _, match, _ in query:iter_matches(root, 0, 0, -1) do
|
||||||
for _, node in ipairs(nodes) do
|
for id, nodes in pairs(match) do
|
||||||
local start_line, start_col, end_line, end_col = node:range()
|
if query.captures[id] == 'helplink' then
|
||||||
local tag = vim.treesitter.get_node_text(node, 0)
|
for _, node in ipairs(nodes) do
|
||||||
vim.api.nvim_buf_set_extmark(0, url_ns, start_line, start_col, {
|
local start_line, start_col, end_line, end_col = node:range()
|
||||||
end_line = end_line,
|
local tag = vim.treesitter.get_node_text(node, 0)
|
||||||
end_col = end_col,
|
vim.api.nvim_buf_set_extmark(0, url_ns, start_line, start_col, {
|
||||||
url = base .. vim.uri_encode(tag),
|
end_line = end_line,
|
||||||
})
|
end_col = end_col,
|
||||||
|
url = base .. vim.uri_encode(tag),
|
||||||
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Retry once if the buffer has changed during the iteration of the code
|
||||||
|
-- blocks. See #36574.
|
||||||
|
if not pcall(urls) then
|
||||||
|
urls()
|
||||||
|
end
|
||||||
|
|
||||||
vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
|
vim.b.undo_ftplugin = (vim.b.undo_ftplugin or '')
|
||||||
.. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
|
.. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="'
|
||||||
.. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
|
.. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["'
|
||||||
|
|||||||
Reference in New Issue
Block a user