1
0
Fork 0
mirror of https://gitlab.alpinelinux.org/alpine/aports.git synced 2025-07-12 18:59:50 +03:00
aports/community/neovim/make-tree-sitter-optional.patch
Sören Tempel abbe66b3a5 community/neovim: update make-tree-sitter-optional.patch for 0.11
The help.lua plguin needs to be updated to disable "runnables" for
Lua/Vimscript code examples, if tree-sitter-vimdoc is not installed.
Without this patch, opening vimdoc files emits an error message if
tree-sitter-vimdoc is not installed.

Same story for tree-sitter-query.
2025-05-19 14:07:22 +00:00

117 lines
3.8 KiB
Diff

From: Jakub Jirutka <jakub@jirutka.cz>
Date: Thu, 23 May 2024 20:07:15 +0200
Subject: Fallback to classic syntax highlighting if tree-sitter parser
is not available
See https://gitlab.alpinelinux.org/alpine/aports/-/issues/16132,
https://github.com/neovim/neovim/pull/26824
--- neovim-0.11.1.orig/runtime/ftplugin/help.lua
+++ neovim-0.11.1/runtime/ftplugin/help.lua
@@ -1,5 +1,8 @@
-- use treesitter over syntax (for highlighted code blocks)
-vim.treesitter.start()
+local ok, _ = pcall(vim.treesitter.start)
+if not ok then
+ print('Note: tree-sitter-vimdoc package is not installed, some features will not work')
+end
--- Apply current colorscheme to lists of default highlight groups
---
@@ -68,33 +71,35 @@
-- Add "runnables" for Lua/Vimscript code examples.
---@type table<integer, { lang: string, code: string }>
-local code_blocks = {}
-local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
-local query = vim.treesitter.query.parse(
- 'vimdoc',
- [[
- (codeblock
- (language) @_lang
- .
- (code) @code
- (#any-of? @_lang "lua" "vim")
- (#set! @code lang @_lang))
-]]
-)
-local root = parser:parse()[1]:root()
+if ok then
+ local code_blocks = {}
+ local parser = assert(vim.treesitter.get_parser(0, 'vimdoc', { error = false }))
+ local query = vim.treesitter.query.parse(
+ 'vimdoc',
+ [[
+ (codeblock
+ (language) @_lang
+ .
+ (code) @code
+ (#any-of? @_lang "lua" "vim")
+ (#set! @code lang @_lang))
+ ]]
+ )
+ local root = parser:parse()[1]:root()
-for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
- for id, nodes in pairs(match) do
- local name = query.captures[id]
- local node = nodes[1]
- local start, _, end_ = node:parent():range()
+ for _, match, metadata in query:iter_matches(root, 0, 0, -1) do
+ for id, nodes in pairs(match) do
+ local name = query.captures[id]
+ local node = nodes[1]
+ local start, _, end_ = node:parent():range()
- if name == 'code' then
- local code = vim.treesitter.get_node_text(node, 0)
- local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
- local lang = vim.treesitter.get_node_text(lang_node, 0)
- for i = start + 1, end_ do
- code_blocks[i] = { lang = lang, code = code }
+ if name == 'code' then
+ local code = vim.treesitter.get_node_text(node, 0)
+ local lang_node = match[metadata[id].lang][1] --[[@as TSNode]]
+ local lang = vim.treesitter.get_node_text(lang_node, 0)
+ for i = start + 1, end_ do
+ code_blocks[i] = { lang = lang, code = code }
+ end
end
end
end
--- neovim-0.11.1.orig/runtime/ftplugin/lua.lua
+++ neovim-0.11.1/runtime/ftplugin/lua.lua
@@ -1,5 +1,8 @@
-- use treesitter over syntax
-vim.treesitter.start()
+local ok, _ = pcall(vim.treesitter.start)
+if not ok then
+ print('Note: tree-sitter-lua package is not installed, some features will not work')
+end
vim.bo.includeexpr = [[v:lua.require'vim._ftplugin.lua'.includeexpr(v:fname)]]
vim.bo.omnifunc = 'v:lua.vim.lua_omnifunc'
--- neovim-0.11.1.orig/runtime/ftplugin/query.lua
+++ neovim-0.11.1/runtime/ftplugin/query.lua
@@ -8,7 +8,10 @@
-- Do not set vim.b.did_ftplugin = 1 to allow loading of ftplugin/lisp.vim
-- use treesitter over syntax
-vim.treesitter.start()
+local ok, _ = pcall(vim.treesitter.start)
+if not ok then
+ print('Note: tree-sitter-query package is not installed, some features will not work')
+end
-- set omnifunc
vim.bo.omnifunc = 'v:lua.vim.treesitter.query.omnifunc'
@@ -18,6 +21,9 @@
-- query linter
local buf = vim.api.nvim_get_current_buf()
local query_lint_on = vim.g.query_lint_on or { 'BufEnter', 'BufWrite' }
+if not ok then
+ vim.b[buf].disable_query_linter = true
+end
if not vim.b.disable_query_linter and #query_lint_on > 0 then
vim.api.nvim_create_autocmd(query_lint_on, {