diff --git a/lua/nvim-treesitter-textobjects/move.lua b/lua/nvim-treesitter-textobjects/move.lua index ef4a9b62..9721547b 100644 --- a/lua/nvim-treesitter-textobjects/move.lua +++ b/lua/nvim-treesitter-textobjects/move.lua @@ -25,11 +25,19 @@ local function goto_node(range, goto_end, avoid_set_jump) vim.cmd('normal! v') end + -- end positions with `col=0` mean "up to the end of the previous line, including the newline character" + if end_col == 0 then + end_row = end_row - 1 + end_col = #api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1] + else + end_col = end_col - 1 + end + -- Position is 1, 0 indexed. if not goto_end then api.nvim_win_set_cursor(0, { start_row + 1, start_col }) else - api.nvim_win_set_cursor(0, { end_row + 1, end_col - 1 }) + api.nvim_win_set_cursor(0, { end_row + 1, end_col }) end end diff --git a/lua/nvim-treesitter-textobjects/select.lua b/lua/nvim-treesitter-textobjects/select.lua index 713c2cb1..ea8b4ae9 100644 --- a/lua/nvim-treesitter-textobjects/select.lua +++ b/lua/nvim-treesitter-textobjects/select.lua @@ -20,16 +20,23 @@ local function update_selection(range, selection_mode) vim.cmd.normal({ selection_mode, bang = true }) end - local end_col_offset = 1 + -- end positions with `col=0` mean "up to the end of the previous line, including the newline character" + if end_col == 0 then + end_row = end_row - 1 + -- +1 is needed because we are interpreting `end_col` to be exclusive afterwards + end_col = #api.nvim_buf_get_lines(0, end_row, end_row + 1, true)[1] + 1 + end + local end_col_offset = 1 if selection_mode == 'v' and vim.o.selection == 'exclusive' then end_col_offset = 0 end + end_col = end_col - end_col_offset -- Position is 1, 0 indexed. api.nvim_win_set_cursor(0, { start_row + 1, start_col }) vim.cmd('normal! o') - api.nvim_win_set_cursor(0, { end_row + 1, end_col - end_col_offset }) + api.nvim_win_set_cursor(0, { end_row + 1, end_col }) end local M = {}