diff options
| author | Stephan Seitz <stephan.seitz@fau.de> | 2020-06-21 20:38:00 +0200 |
|---|---|---|
| committer | Thomas Vigouroux <39092278+vigoux@users.noreply.github.com> | 2020-07-14 22:34:43 +0200 |
| commit | 69cabc69be49bc37c8a9bbb7def1415504b489b2 (patch) | |
| tree | cc4925d6ce00654c181a7cb4d2eef3dc488a324b /lua/nvim-treesitter/ts_utils.lua | |
| parent | c42c38a8346dd9edda96477b774d3e086cd73650 (diff) | |
Add textobjects module
Diffstat (limited to 'lua/nvim-treesitter/ts_utils.lua')
| -rw-r--r-- | lua/nvim-treesitter/ts_utils.lua | 55 |
1 files changed, 54 insertions, 1 deletions
diff --git a/lua/nvim-treesitter/ts_utils.lua b/lua/nvim-treesitter/ts_utils.lua index 39c75365..f9c3de10 100644 --- a/lua/nvim-treesitter/ts_utils.lua +++ b/lua/nvim-treesitter/ts_utils.lua @@ -28,7 +28,7 @@ function M.get_node_text(node, bufnr) end end ---- Determines wether a node is the parent of another +--- Determines whether a node is the parent of another -- @param dest the possible parent -- @param source the possible child node function M.is_parent(dest, source) @@ -323,4 +323,57 @@ function M.highlight_range(range, buf, hl_namespace, hl_group) vim.highlight.range(buf, hl_namespace, hl_group, {start_row, start_col}, {end_row, end_col}) end +-- Set visual selection to node +function M.update_selection(buf, node) + local start_row, start_col, end_row, end_col = node:range() + + if end_row == vim.fn.line('$') then + end_col = #vim.fn.getline('$') + end + + -- Convert to 1-based indices + start_row = start_row + 1 + start_col = start_col + 1 + end_row = end_row + 1 + end_col = end_col + 1 + + vim.fn.setpos(".", { buf, start_row, start_col, 0 }) + vim.fn.nvim_exec("normal v", false) + + -- Convert exclusive end position to inclusive + if end_col == 1 then + vim.fn.setpos(".", { buf, end_row - 1, -1, 0 }) + else + vim.fn.setpos(".", { buf, end_row, end_col - 1, 0 }) + end +end + +-- Byte length of node range +function M.node_length(node) + local _, _, start_byte = node:start() + local _, _, end_byte = node:end_() + return end_byte - start_byte +end + +--- Determines whether (line, col) position is in node range +-- @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) + 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 + return col >= start_col and col < end_col + elseif line == start_line then + return col >= start_col + elseif line == end_line then + return col < end_col + else + return true + end + else + return false + end +end + return M |
