From b9a2e4cb14eb42e0631dba2a4bc906c18372c109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Dosko=C4=8Dil?= <30779172+Dook97@users.noreply.github.com> Date: Fri, 26 Dec 2025 11:21:45 +0100 Subject: [PATCH] config: set_jumps option for select_textobject fixes #842 --- README.md | 2 ++ lua/nvim-treesitter-textobjects/config.lua | 2 ++ lua/nvim-treesitter-textobjects/select.lua | 9 +++++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 84ab950d..1b6eae8e 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,8 @@ require("nvim-treesitter-textobjects").setup { -- * selection_mode: eg 'v' -- and should return true of false include_surrounding_whitespace = false, + -- whether to set jumps in the jumplist + set_jumps = false, }, } diff --git a/lua/nvim-treesitter-textobjects/config.lua b/lua/nvim-treesitter-textobjects/config.lua index 6c7fef07..3463ae4d 100644 --- a/lua/nvim-treesitter-textobjects/config.lua +++ b/lua/nvim-treesitter-textobjects/config.lua @@ -7,6 +7,7 @@ ---@field lookbehind? boolean ---@field selection_modes? table|fun(opts: TSTextObjects.ConfigFunctionArgs): TSTextObjects.SelectionMode|table ---@field include_surrounding_whitespace? boolean|fun(opts: TSTextObjects.ConfigFunctionArgs): boolean +---@field set_jumps? boolean ---@class (exact) TSTextObjects.Config.Move ---@field set_jumps? boolean @@ -29,6 +30,7 @@ local default_config = { lookbehind = false, selection_modes = {}, include_surrounding_whitespace = false, + set_jumps = false, }, move = { set_jumps = true, diff --git a/lua/nvim-treesitter-textobjects/select.lua b/lua/nvim-treesitter-textobjects/select.lua index ea8b4ae9..3c311fd4 100644 --- a/lua/nvim-treesitter-textobjects/select.lua +++ b/lua/nvim-treesitter-textobjects/select.lua @@ -4,7 +4,7 @@ local shared = require('nvim-treesitter-textobjects.shared') ---@param range Range4 ---@param selection_mode TSTextObjects.SelectionMode -local function update_selection(range, selection_mode) +local function update_selection(range, selection_mode, set_jumps) ---@type integer, integer, integer, integer local start_row, start_col, end_row, end_col = unpack(range) selection_mode = selection_mode or 'v' @@ -33,6 +33,10 @@ local function update_selection(range, selection_mode) end end_col = end_col - end_col_offset + if set_jumps then + vim.cmd("normal! m'") + end + -- Position is 1, 0 indexed. api.nvim_win_set_cursor(0, { start_row + 1, start_col }) vim.cmd('normal! o') @@ -158,6 +162,7 @@ function M.select_textobject(query_string, query_group) local lookahead = config.lookahead local lookbehind = config.lookbehind local surrounding_whitespace = config.include_surrounding_whitespace + local set_jumps = config.set_jumps local range6 = shared.textobject_at_point( query_string, query_group, @@ -178,7 +183,7 @@ function M.select_textobject(query_string, query_group) range4 = include_surrounding_whitespace(bufnr, range4, selection_mode) end if range4 then - update_selection(range4, selection_mode) + update_selection(range4, selection_mode, set_jumps) end end end