diff options
| author | Santos Gallegos <stsewd@protonmail.com> | 2021-04-05 20:02:58 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-05 20:02:58 -0500 |
| commit | 79040e539d4351d7f972e6120c8f958185ee4c84 (patch) | |
| tree | 7480b16619a7af9f80583e2b0b82a65bc3e9431c /lua | |
| parent | Small update to README (diff) | |
| download | nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar.gz nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar.bz2 nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar.lz nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar.xz nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.tar.zst nvim-treesitter-79040e539d4351d7f972e6120c8f958185ee4c84.zip | |
Fix incremental selection (#1135)
So, there are two problems:
- There was an infinite loop when inc selection was initiated
from an injection.
- The comparison was wrong when the whole file was selected,
this is because ts matches the EOF.
This is an extra line with one char (EOF).
I put a workaround to try to select the node from the main
tree, but we should try to select the node from the parent tree of the
injection, but I wasn't able to get the parent tree from the node.
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/nvim-treesitter/incremental_selection.lua | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/lua/nvim-treesitter/incremental_selection.lua b/lua/nvim-treesitter/incremental_selection.lua index fa833f90a..ac2c98386 100644 --- a/lua/nvim-treesitter/incremental_selection.lua +++ b/lua/nvim-treesitter/incremental_selection.lua @@ -22,11 +22,32 @@ end local function visual_selection_range() local _, csrow, cscol, _ = unpack(vim.fn.getpos("'<")) local _, cerow, cecol, _ = unpack(vim.fn.getpos("'>")) + + local start_row, start_col, end_row, end_col + if csrow < cerow or (csrow == cerow and cscol <= cecol) then - return csrow - 1, cscol - 1, cerow - 1, cecol + start_row = csrow - 1 + start_col = cscol - 1 + end_row = cerow - 1 + end_col = cecol else - return cerow - 1, cecol - 1, csrow - 1, cscol + start_row = cerow - 1 + start_col = cecol - 1 + end_row = csrow - 1 + end_col = cscol + end + + -- The last char in ts is equivalent to the EOF in another line. + local last_row = vim.fn.line("$") + local last_col = vim.fn.col({last_row, "$"}) + last_row = last_row - 1 + last_col = last_col - 1 + if end_row == last_row and end_col == last_col then + end_row = end_row + 1 + end_col = 0 end + + return start_row, start_col, end_row, end_col end local function range_matches(node) @@ -57,8 +78,18 @@ local function select_incremental(get_parent) -- Find a node that changes the current selection. local node = nodes[#nodes] while true do - node = get_parent(node) - if not node then return end + local parent = get_parent(node) + if not parent or parent == node then + -- Keep searching in the main tree + -- TODO: we should search on the parent tree of the current node. + local root = parsers.get_parser():parse()[1]:root() + parent = root:named_descendant_for_range(csrow, cscol, cerow, cecol) + if not parent or parent == node then + ts_utils.update_selection(buf, node) + return + end + end + node = parent local srow, scol, erow, ecol = node:range() local same_range = ( srow == csrow |
