Skip to content

Commit 1a15e87

Browse files
committed
feat: lookahead/lookbehind setting local to a keymap setting
1 parent 0b826f8 commit 1a15e87

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ require'nvim-treesitter.configs'.setup {
5454
["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
5555
-- You can also use captures from other query groups like `locals.scm`
5656
["as"] = { query = "@scope", query_group = "locals", desc = "Select language scope" },
57+
58+
-- You can specify lookahead or lookbehind and it will take precedence over the global setting just for that keymapping
59+
["ibc"] = { query = "@assignment.outer", desc = "Select outer part of an assignment looking behind the cursor instead of ahead", lookbehind = true },
5760
},
5861
-- You can choose the select mode (default is charwise 'v')
5962
--

lua/nvim-treesitter/textobjects/select.lua

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,18 @@ local val_or_return = function(val, opts)
9494
end
9595
end
9696

97-
function M.select_textobject(query_string, query_group, keymap_mode)
97+
function M.select_textobject(query_string, query_group, keymap_mode, local_lookahead, local_lookbehind)
9898
query_group = query_group or "textobjects"
99-
local lookahead = configs.get_module("textobjects.select").lookahead
100-
local lookbehind = configs.get_module("textobjects.select").lookbehind
99+
if local_lookahead == "nil" then
100+
local_lookahead = nil
101+
end
102+
if local_lookbehind == "nil" then
103+
local_lookbehind = nil
104+
end
105+
local global_lookahead = configs.get_module("textobjects.select").lookahead
106+
local global_lookbehind = configs.get_module("textobjects.select").lookbehind
107+
local lookahead = (global_lookahead and not local_lookbehind) or local_lookahead
108+
local lookbehind = (global_lookbehind and not local_lookahead) or local_lookbehind
101109
local surrounding_whitespace = configs.get_module("textobjects.select").include_surrounding_whitespace
102110
local bufnr, textobject =
103111
shared.textobject_at_point(query_string, query_group, nil, nil, { lookahead = lookahead, lookbehind = lookbehind })
@@ -157,11 +165,13 @@ function M.attach(bufnr, lang)
157165
lang = lang or parsers.get_buf_lang(bufnr)
158166

159167
for mapping, query in pairs(config.keymaps) do
160-
local desc, query_string, query_group
168+
local desc, query_string, query_group, lookbehind, lookahead
161169
if type(query) == "table" then
162170
desc = query.desc
163171
query_string = query.query
164172
query_group = query.query_group or "textobjects"
173+
lookbehind = query.lookbehind
174+
lookahead = query.lookahead
165175
else
166176
query_string = query
167177
query_group = "textobjects"
@@ -190,10 +200,12 @@ function M.attach(bufnr, lang)
190200
{ keymap_mode },
191201
mapping,
192202
string.format(
193-
"<cmd>lua require'nvim-treesitter.textobjects.select'.select_textobject('%s','%s','%s')<cr>",
203+
"<cmd>lua require'nvim-treesitter.textobjects.select'.select_textobject('%s','%s','%s','%s','%s')<cr>",
194204
query_string,
195205
query_group,
196-
keymap_mode
206+
keymap_mode,
207+
lookahead,
208+
lookbehind
197209
),
198210
{ buffer = bufnr, silent = true, remap = false, desc = desc }
199211
)

0 commit comments

Comments
 (0)