diff options
| author | Kiyoon Kim <yoonkr33@gmail.com> | 2023-02-12 00:39:21 +0000 |
|---|---|---|
| committer | Stephan Seitz <stephan.seitz@fau.de> | 2023-02-12 02:38:17 -0800 |
| commit | 2c2430d42f30d1aef8715aed933272c9a6898f4e (patch) | |
| tree | 895ccd83705f22332e8bcf61ad68fc3d11b389dc | |
| parent | Update parsers: c_sharp, capnp, fortran, func, glimmer, haskell, php, ron, sq... (diff) | |
| download | nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar.gz nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar.bz2 nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar.lz nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar.xz nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.tar.zst nvim-treesitter-2c2430d42f30d1aef8715aed933272c9a6898f4e.zip | |
deprecate get_node_range and is_in_node_range
| -rw-r--r-- | lua/nvim-treesitter/ts_utils.lua | 27 | ||||
| -rw-r--r-- | tests/unit/ts_utils_spec.lua | 40 |
2 files changed, 17 insertions, 50 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 775094bd5..560b49cf2 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -2,6 +2,7 @@ local api = vim.api local parsers = require "nvim-treesitter.parsers" local utils = require "nvim-treesitter.utils" +local ts = vim.treesitter local M = {} @@ -12,7 +13,7 @@ local function get_node_text(node, bufnr) end -- We have to remember that end_col is end-exclusive - local start_row, start_col, end_row, end_col = M.get_node_range(node) + local start_row, start_col, end_row, end_col = ts.get_node_range(node) if start_row ~= end_row then local lines = api.nvim_buf_get_lines(bufnr, start_row, end_row + 1, false) @@ -169,7 +170,7 @@ function M.get_node_at_cursor(winnr, ignore_injected_langs) if ignore_injected_langs then for _, tree in ipairs(root_lang_tree:trees()) do local tree_root = tree:root() - if tree_root and M.is_in_node_range(tree_root, cursor_range[1], cursor_range[2]) then + if tree_root and ts.is_in_node_range(tree_root, cursor_range[1], cursor_range[2]) then root = tree_root break end @@ -199,7 +200,7 @@ function M.get_root_for_position(line, col, root_lang_tree) for _, tree in ipairs(lang_tree:trees()) do local root = tree:root() - if root and M.is_in_node_range(root, line, col) then + if root and ts.is_in_node_range(root, line, col) then return root, tree, lang_tree end end @@ -260,7 +261,7 @@ end -- @param selection_mode One of "charwise" (default) or "v", "linewise" or "V", -- "blockwise" or "<C-v>" (as a string with 5 characters or a single character) function M.update_selection(buf, node, selection_mode) - local start_row, start_col, end_row, end_col = M.get_vim_range({ M.get_node_range(node) }, buf) + local start_row, start_col, end_row, end_col = M.get_vim_range({ ts.get_node_range(node) }, buf) local v_table = { charwise = "v", linewise = "V", blockwise = "<C-v>" } selection_mode = selection_mode or "charwise" @@ -294,10 +295,15 @@ function M.node_length(node) end --- Determines whether (line, col) position is in node range +--- @deprecated Use `vim.treesitter.is_in_node_range()` instead -- @param node Node defining the range -- @param line A line (0-based) -- @param col A column (0-based) function M.is_in_node_range(node, line, col) + vim.notify_once( + "nvim-treesitter.ts_utils.is_in_node_range is deprecated: use vim.treesitter.is_in_node_range", + vim.log.levels.WARN + ) local start_line, start_col, end_line, end_col = node:range() if line >= start_line and line <= end_line then if line == start_line and line == end_line then @@ -314,16 +320,17 @@ function M.is_in_node_range(node, line, col) end end +--- @deprecated Use `vim.treesitter.get_node_range()` instead function M.get_node_range(node_or_range) - if type(node_or_range) == "table" then - return unpack(node_or_range) - else - return node_or_range:range() - end + vim.notify_once( + "nvim-treesitter.ts_utils.get_node_range is deprecated: use vim.treesitter.get_node_range", + vim.log.levels.WARN + ) + return ts.get_node_range(node_or_range) end function M.node_to_lsp_range(node) - local start_line, start_col, end_line, end_col = M.get_node_range(node) + local start_line, start_col, end_line, end_col = ts.get_node_range(node) local rtn = {} rtn.start = { line = start_line, character = start_col } rtn["end"] = { line = end_line, character = end_col } diff --git a/tests/unit/ts_utils_spec.lua b/tests/unit/ts_utils_spec.lua index 3cc7d4dc8..a56755134 100644 --- a/tests/unit/ts_utils_spec.lua +++ b/tests/unit/ts_utils_spec.lua @@ -1,45 +1,5 @@ local tsutils = require "nvim-treesitter.ts_utils" -describe("is_in_node_range", function() - local function test_is_in_node_range(line, col) - local node = { - range = function() - return unpack { 0, 3, 2, 5 } - end, - } - return tsutils.is_in_node_range(node, line, col) - end - - it("returns false before node start", function() - assert.is_false(test_is_in_node_range(0, 0)) - assert.is_false(test_is_in_node_range(0, 1)) - assert.is_false(test_is_in_node_range(0, 2)) - end) - - it("returns true at node start", function() - assert.is_true(test_is_in_node_range(0, 3)) - end) - - it("returns true on first line of the node", function() - assert.is_true(test_is_in_node_range(0, 4)) - end) - - it("returns true between node lines", function() - assert.is_true(test_is_in_node_range(1, 2)) - assert.is_true(test_is_in_node_range(1, 20)) - end) - - it("returns false on node end", function() - -- Ranges are end-exclusive - assert.is_false(test_is_in_node_range(2, 5)) - end) - - it("returns false after node end", function() - assert.is_false(test_is_in_node_range(2, 6)) - assert.is_false(test_is_in_node_range(3, 0)) - end) -end) - describe("update_selection", function() local function get_updated_selection(case) vim.api.nvim_buf_set_lines(0, 0, -1, false, case.lines) |
