mirror of
https://github.com/neovim/neovim.git
synced 2025-10-26 12:27:24 +00:00
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.
This commit is contained in:
committed by
Christian Clason
parent
59c328bc88
commit
65c7033cbe
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user