diff options
author | Riley Bruins <ribru17@hotmail.com> | 2024-08-17 21:05:09 -0700 |
---|---|---|
committer | Riley Bruins <ribru17@hotmail.com> | 2024-12-06 08:36:08 -0800 |
commit | b8c75a31e6f4716f542cd2000e4a7c19c1ae9d70 (patch) | |
tree | ad6d600790a0ee1ddab5ec367729689f73a29c44 /test/functional/treesitter/parser_spec.lua | |
parent | 1077843b9bee550fe2ae1e3e700a9c135005d593 (diff) | |
download | rneovim-b8c75a31e6f4716f542cd2000e4a7c19c1ae9d70.tar.gz rneovim-b8c75a31e6f4716f542cd2000e4a7c19c1ae9d70.tar.bz2 rneovim-b8c75a31e6f4716f542cd2000e4a7c19c1ae9d70.zip |
feat(treesitter): #trim! can trim all whitespace
This commit also implements more generic trimming, acting on all
whitespace (charwise) rather than just empty lines.
It will unblock
https://github.com/nvim-treesitter/nvim-treesitter/pull/3442 and allow
for properly concealing markdown bullet markers regardless of indent
width, e.g.
Diffstat (limited to 'test/functional/treesitter/parser_spec.lua')
-rw-r--r-- | test/functional/treesitter/parser_spec.lua | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/test/functional/treesitter/parser_spec.lua b/test/functional/treesitter/parser_spec.lua index 2f8d204d36..c0f11e75dc 100644 --- a/test/functional/treesitter/parser_spec.lua +++ b/test/functional/treesitter/parser_spec.lua @@ -644,6 +644,108 @@ print() end) end) + describe('trim! directive', function() + it('can trim all whitespace', function() + -- luacheck: push ignore 611 613 + insert([=[ + print([[ + + f + helllo + there + asdf + asdfassd + + + + ]]) + print([[ + + + + ]]) + + print([[]]) + + print([[ + ]]) + + print([[ hello 😃 ]]) + ]=]) + -- luacheck: pop + + local query_text = [[ + ; query + ((string_content) @str + (#trim! @str 1 1 1 1)) + ]] + + exec_lua(function() + vim.treesitter.start(0, 'lua') + end) + + local function run_query() + return exec_lua(function(query_str) + local query = vim.treesitter.query.parse('lua', query_str) + local parser = vim.treesitter.get_parser() + local tree = parser:parse()[1] + local res = {} + for id, _, metadata in query:iter_captures(tree:root(), 0) do + table.insert(res, { query.captures[id], metadata[id].range }) + end + return res + end, query_text) + end + + eq({ + { 'str', { 2, 12, 6, 10 } }, + { 'str', { 11, 10, 11, 10 } }, + { 'str', { 17, 10, 17, 10 } }, + { 'str', { 19, 10, 19, 10 } }, + { 'str', { 22, 15, 22, 25 } }, + }, run_query()) + end) + + it('trims only empty lines by default (backwards compatible)', function() + insert [[ + ## Heading + + With some text + + ## And another + + With some more]] + + local query_text = [[ + ; query + ((section) @fold + (#trim! @fold)) + ]] + + exec_lua(function() + vim.treesitter.start(0, 'markdown') + end) + + local function run_query() + return exec_lua(function(query_str) + local query = vim.treesitter.query.parse('markdown', query_str) + local parser = vim.treesitter.get_parser() + local tree = parser:parse()[1] + local res = {} + for id, _, metadata in query:iter_captures(tree:root(), 0) do + table.insert(res, { query.captures[id], metadata[id].range }) + end + return res + end, query_text) + end + + eq({ + { 'fold', { 0, 0, 3, 0 } }, + { 'fold', { 4, 0, 7, 0 } }, + }, run_query()) + end) + end) + it('tracks the root range properly (#22911)', function() insert([[ int main() { |