diff options
author | altermo <107814000+altermo@users.noreply.github.com> | 2024-04-10 10:52:51 +0200 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-04-10 15:54:52 +0100 |
commit | 00e6651880c32a9878797eeeaef7018c3d5d99b7 (patch) | |
tree | 627c1afad5a2ec85d1f03acf0ca4906942b72cc5 | |
parent | 81fc27124b9e1b375e0ce9605ae69c3c2a2d9222 (diff) | |
download | rneovim-00e6651880c32a9878797eeeaef7018c3d5d99b7.tar.gz rneovim-00e6651880c32a9878797eeeaef7018c3d5d99b7.tar.bz2 rneovim-00e6651880c32a9878797eeeaef7018c3d5d99b7.zip |
fix(treesitter): use tree range instead of tree root node range
-rw-r--r-- | runtime/lua/vim/treesitter/languagetree.lua | 9 | ||||
-rw-r--r-- | test/functional/lua/comment_spec.lua | 11 | ||||
-rw-r--r-- | test/functional/treesitter/language_spec.lua | 14 |
3 files changed, 27 insertions, 7 deletions
diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index 8f65cb57c3..990debc77b 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -1100,7 +1100,14 @@ end ---@param range Range ---@return boolean local function tree_contains(tree, range) - return Range.contains({ tree:root():range() }, range) + local tree_ranges = tree:included_ranges(false) + + return Range.contains({ + tree_ranges[1][1], + tree_ranges[1][2], + tree_ranges[#tree_ranges][3], + tree_ranges[#tree_ranges][4], + }, range) end --- Determines whether {range} is contained in the |LanguageTree|. diff --git a/test/functional/lua/comment_spec.lua b/test/functional/lua/comment_spec.lua index 48e23fd7c7..df7776d928 100644 --- a/test/functional/lua/comment_spec.lua +++ b/test/functional/lua/comment_spec.lua @@ -495,22 +495,21 @@ describe('commenting', function() it("recomputes local 'commentstring' based on cursor position", function() setup_treesitter() local lines = { + ' print(1)', 'lua << EOF', ' print(1)', 'EOF', } set_lines(lines) - -- Vimscript's tree-sitter grammar is (currently) written in a way that Lua's - -- injection really starts at the first non-blank character - set_cursor(2, 1) + set_cursor(1, 1) feed('gc_') - eq(get_lines()[2], ' "print(1)') + eq(get_lines()[1], ' "print(1)') set_lines(lines) - set_cursor(2, 2) + set_cursor(3, 2) feed('.') - eq(get_lines()[2], ' -- print(1)') + eq(get_lines()[3], ' -- print(1)') end) it('preserves marks', function() diff --git a/test/functional/treesitter/language_spec.lua b/test/functional/treesitter/language_spec.lua index 0e18a36352..365e2a697a 100644 --- a/test/functional/treesitter/language_spec.lua +++ b/test/functional/treesitter/language_spec.lua @@ -120,6 +120,20 @@ describe('treesitter language API', function() eq('<node translation_unit>', exec_lua('return tostring(tree:root())')) end) + it('retrieve the tree given a range when range is out of bounds relative to buffer', function() + insert([[ + int main() { + int x = 3; + }]]) + + exec_lua([[ + langtree = vim.treesitter.get_parser(0, "c") + tree = langtree:tree_for_range({10, 10, 10, 10}) + ]]) + + eq('<node translation_unit>', exec_lua('return tostring(tree:root())')) + end) + it('retrieve the node given a range', function() insert([[ int main() { |