aboutsummaryrefslogtreecommitdiff
path: root/test/functional/lua/comment_spec.lua
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 /test/functional/lua/comment_spec.lua
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 'test/functional/lua/comment_spec.lua')
-rw-r--r--test/functional/lua/comment_spec.lua134
1 files changed, 134 insertions, 0 deletions
diff --git a/test/functional/lua/comment_spec.lua b/test/functional/lua/comment_spec.lua
index bbf061a2ab..9ae9ce84d0 100644
--- a/test/functional/lua/comment_spec.lua
+++ b/test/functional/lua/comment_spec.lua
@@ -586,6 +586,140 @@ describe('commenting', function()
feed('.')
eq(get_lines(), { '"set background=dark', 'lua << EOF', '-- print(1)', 'EOF' })
end)
+
+ it('respects tree-sitter commentstring metadata', function()
+ exec_lua [=[
+ vim.treesitter.query.set('vim', 'highlights', [[
+ ((list) @_list (#set! @_list bo.commentstring "!! %s"))
+ ]])
+ ]=]
+ setup_treesitter()
+
+ local lines = {
+ 'set background=dark',
+ 'let mylist = [',
+ [[ \"a",]],
+ [[ \"b",]],
+ [[ \"c",]],
+ ' \\]',
+ }
+ set_lines(lines)
+
+ set_cursor(1, 0)
+ feed('gcc')
+ eq(
+ { '"set background=dark', 'let mylist = [', [[ \"a",]], [[ \"b",]], [[ \"c",]], ' \\]' },
+ get_lines()
+ )
+
+ -- Should work with dot-repeat
+ set_cursor(4, 0)
+ feed('.')
+ eq({
+ '"set background=dark',
+ 'let mylist = [',
+ [[ \"a",]],
+ [[ !! \"b",]],
+ [[ \"c",]],
+ ' \\]',
+ }, get_lines())
+ end)
+
+ it('only applies the innermost tree-sitter commentstring metadata', function()
+ exec_lua [=[
+ vim.treesitter.query.set('vim', 'highlights', [[
+ ((list) @_list (#gsub! @_list "(.*)" "%1") (#set! bo.commentstring "!! %s"))
+ ((script_file) @_src (#set! @_src bo.commentstring "## %s"))
+ ]])
+ ]=]
+ setup_treesitter()
+
+ local lines = {
+ 'set background=dark',
+ 'let mylist = [',
+ [[ \"a",]],
+ [[ \"b",]],
+ [[ \"c",]],
+ ' \\]',
+ }
+ set_lines(lines)
+
+ set_cursor(1, 0)
+ feed('gcc')
+ eq({
+ '## set background=dark',
+ 'let mylist = [',
+ [[ \"a",]],
+ [[ \"b",]],
+ [[ \"c",]],
+ ' \\]',
+ }, get_lines())
+
+ -- Should work with dot-repeat
+ set_cursor(4, 0)
+ feed('.')
+ eq({
+ '## set background=dark',
+ 'let mylist = [',
+ [[ \"a",]],
+ [[ !! \"b",]],
+ [[ \"c",]],
+ ' \\]',
+ }, get_lines())
+ end)
+
+ it('respects injected tree-sitter commentstring metadata', function()
+ exec_lua [=[
+ vim.treesitter.query.set('lua', 'highlights', [[
+ ((string) @string (#set! @string bo.commentstring "; %s"))
+ ]])
+ ]=]
+ setup_treesitter()
+
+ local lines = {
+ 'set background=dark',
+ 'lua << EOF',
+ 'print[[',
+ 'Inside string',
+ ']]',
+ 'EOF',
+ }
+ set_lines(lines)
+
+ set_cursor(1, 0)
+ feed('gcc')
+ eq({
+ '"set background=dark',
+ 'lua << EOF',
+ 'print[[',
+ 'Inside string',
+ ']]',
+ 'EOF',
+ }, get_lines())
+
+ -- Should work with dot-repeat
+ set_cursor(4, 0)
+ feed('.')
+ eq({
+ '"set background=dark',
+ 'lua << EOF',
+ 'print[[',
+ '; Inside string',
+ ']]',
+ 'EOF',
+ }, get_lines())
+
+ set_cursor(3, 0)
+ feed('.')
+ eq({
+ '"set background=dark',
+ 'lua << EOF',
+ '-- print[[',
+ '; Inside string',
+ ']]',
+ 'EOF',
+ }, get_lines())
+ end)
end)
describe('Textobject', function()