Skip to content

Commit c9abe01

Browse files
committed
fix: properly handle ranges ending in newline
1 parent 7024f86 commit c9abe01

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lua/nvim-treesitter-textobjects/select.lua

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@ local function update_selection(range, selection_mode)
2020
vim.cmd.normal({ selection_mode, bang = true })
2121
end
2222

23-
local end_col_offset = 1
23+
-- end positions with `col=0` mean "up to the end of the previous line, including the newline character"
24+
if end_col == 0 then
25+
end_row = end_row - 1
26+
-- +1 is needed because we are interpreting `end_col` to be exclusive afterwards
27+
end_col = #api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1] + 1
28+
end
2429

30+
local end_col_offset = 1
2531
if selection_mode == 'v' and vim.o.selection == 'exclusive' then
2632
end_col_offset = 0
2733
end
34+
end_col = end_col - end_col_offset
2835

2936
-- Position is 1, 0 indexed.
3037
api.nvim_win_set_cursor(0, { start_row + 1, start_col })
3138
vim.cmd('normal! o')
32-
api.nvim_win_set_cursor(0, { end_row + 1, end_col - end_col_offset })
39+
api.nvim_win_set_cursor(0, { end_row + 1, end_col })
3340
end
3441

3542
local M = {}

0 commit comments

Comments
 (0)