aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim
diff options
context:
space:
mode:
authorRiley Bruins <ribru17@hotmail.com>2024-09-24 08:41:46 -0700
committerChristian Clason <ch.clason+github@icloud.com>2025-03-02 18:38:13 +0100
commit65c7033cbe75825c4cc948b71aba9a6d7a14540d (patch)
tree864da0d8da2918f5e7abdc46f57f78cd5a1f244a /runtime/lua/vim
parent59c328bc88ccb1d331c3e63768bb11c3555b1f57 (diff)
downloadrneovim-65c7033cbe75825c4cc948b71aba9a6d7a14540d.tar.gz
rneovim-65c7033cbe75825c4cc948b71aba9a6d7a14540d.tar.bz2
rneovim-65c7033cbe75825c4cc948b71aba9a6d7a14540d.zip
feat(comment): allow commentstring to be determined from node metadata
**Problem:** Some weird languages have different comment syntax depending on the location in the code, and we do not have a way to determine the correct `commentstring` for these special cases. **Solution:** Allow queries to specify `commentstring` values in metadata, allowing users/`nvim-treesitter` to provide a better commenting experience without hugely increasing the scope of the code in core.
Diffstat (limited to 'runtime/lua/vim')
-rw-r--r--runtime/lua/vim/_comment.lua12
-rw-r--r--runtime/lua/vim/treesitter/query.lua1
2 files changed, 13 insertions, 0 deletions
diff --git a/runtime/lua/vim/_comment.lua b/runtime/lua/vim/_comment.lua
index de7f62632c..57fd3d73d6 100644
--- a/runtime/lua/vim/_comment.lua
+++ b/runtime/lua/vim/_comment.lua
@@ -19,6 +19,18 @@ local function get_commentstring(ref_position)
local row, col = ref_position[1] - 1, ref_position[2]
local ref_range = { row, col, row, col + 1 }
+ -- Get 'commentstring' from tree-sitter captures' metadata.
+ -- Traverse backwards to prefer narrower captures.
+ local caps = vim.treesitter.get_captures_at_pos(0, row, col)
+ for i = #caps, 1, -1 do
+ local id, metadata = caps[i].id, caps[i].metadata
+ local md_cms = metadata['bo.commentstring'] or metadata[id] and metadata[id]['bo.commentstring']
+
+ if md_cms then
+ return md_cms
+ end
+ end
+
-- - Get 'commentstring' from the deepest LanguageTree which both contains
-- reference range and has valid 'commentstring' (meaning it has at least
-- one associated 'filetype' with valid 'commentstring').
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index 1b72151aac..17088ac0eb 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -608,6 +608,7 @@ predicate_handlers['any-vim-match?'] = predicate_handlers['any-match?']
---@class vim.treesitter.query.TSMetadata
---@field range? Range
---@field conceal? string
+---@field bo.commentstring? string
---@field [integer]? vim.treesitter.query.TSMetadata
---@field [string]? integer|string