aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJongwook Choi <wookayin@gmail.com>2024-01-06 07:18:13 -0500
committerLewis Russell <me@lewisr.dev>2024-01-08 09:27:03 +0000
commitfbe40caa7cc1786dc58210a82901307417ba0654 (patch)
treec577b7efd70ca2e13519c1fa1540dd5525a90271
parent367e52cc79a786bbee4456b30f9ec5db7e28d6a5 (diff)
downloadrneovim-fbe40caa7cc1786dc58210a82901307417ba0654.tar.gz
rneovim-fbe40caa7cc1786dc58210a82901307417ba0654.tar.bz2
rneovim-fbe40caa7cc1786dc58210a82901307417ba0654.zip
docs(treesitter): improve 'no parser' error message for InspectTree
Improve error messages for `:InspectTree`, when no parsers are available for the current buffer and filetype. We can show more informative and helpful error message for users (e.g., which lang was searched for): ``` ... No parser available for the given buffer: +... no parser for 'custom_ft' language, see :help treesitter-parsers ``` Also improve the relevant docs for *treesitter-parsers*.
-rw-r--r--runtime/doc/treesitter.txt21
-rw-r--r--runtime/lua/vim/treesitter/dev.lua3
-rw-r--r--runtime/lua/vim/treesitter/language.lua4
3 files changed, 22 insertions, 6 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt
index 44d644f945..b4c444bbfc 100644
--- a/runtime/doc/treesitter.txt
+++ b/runtime/doc/treesitter.txt
@@ -20,15 +20,23 @@ PARSER FILES *treesitter-parsers*
Parsers are the heart of tree-sitter. They are libraries that tree-sitter will
search for in the `parser` runtime directory. By default, Nvim bundles parsers
for C, Lua, Vimscript, Vimdoc and Treesitter query files, but parsers can be
-installed manually or via a plugin like
-https://github.com/nvim-treesitter/nvim-treesitter. Parsers are searched for
-as `parser/{lang}.*` in any 'runtimepath' directory. If multiple parsers for
-the same language are found, the first one is used. (This typically implies
-the priority "user config > plugins > bundled".
+installed via a plugin like https://github.com/nvim-treesitter/nvim-treesitter
+or even manually.
+
+Parsers are searched for as `parser/{lang}.*` in any 'runtimepath' directory.
+If multiple parsers for the same language are found, the first one is used.
+(NOTE: This typically implies the priority "user config > plugins > bundled".)
A parser can also be loaded manually using a full path: >lua
vim.treesitter.language.add('python', { path = "/path/to/python.so" })
<
+To associate certain |filetypes| with a treesitter language (name of parser),
+use |vim.treesitter.language.register()|. For example, to use the `xml`
+treesitter parser for buffers with filetype `svg` or `xslt`, use: >lua
+
+ vim.treesitter.language.register('xml', { 'svg', 'xslt' })
+<
+
==============================================================================
TREESITTER TREES *treesitter-tree*
*TSTree*
@@ -833,6 +841,9 @@ inspect({lang}) *vim.treesitter.language.inspect()*
register({lang}, {filetype}) *vim.treesitter.language.register()*
Register a parser named {lang} to be used for {filetype}(s).
+ Note: this adds or overrides the mapping for {filetype}, any existing
+ mappings from other filetypes to {lang} will be preserved.
+
Parameters: ~
• {lang} (string) Name of parser
• {filetype} string|string[] Filetype(s) to associate with lang
diff --git a/runtime/lua/vim/treesitter/dev.lua b/runtime/lua/vim/treesitter/dev.lua
index 870761b7c7..399d0ef03e 100644
--- a/runtime/lua/vim/treesitter/dev.lua
+++ b/runtime/lua/vim/treesitter/dev.lua
@@ -75,7 +75,8 @@ end
function TSTreeView:new(bufnr, lang)
local ok, parser = pcall(vim.treesitter.get_parser, bufnr or 0, lang)
if not ok then
- return nil, 'No parser available for the given buffer'
+ local err = parser --[[ @as string ]]
+ return nil, 'No parser available for the given buffer:\n' .. err
end
-- For each child tree (injected language), find the root of the tree and locate the node within
diff --git a/runtime/lua/vim/treesitter/language.lua b/runtime/lua/vim/treesitter/language.lua
index 2056c337c8..2105a1d992 100644
--- a/runtime/lua/vim/treesitter/language.lua
+++ b/runtime/lua/vim/treesitter/language.lua
@@ -119,6 +119,10 @@ local function ensure_list(x)
end
--- Register a parser named {lang} to be used for {filetype}(s).
+---
+--- Note: this adds or overrides the mapping for {filetype}, any existing mappings from other
+--- filetypes to {lang} will be preserved.
+---
--- @param lang string Name of parser
--- @param filetype string|string[] Filetype(s) to associate with lang
function M.register(lang, filetype)