Skip to content

Commit

Permalink
feat: lookahead/lookbehind setting local to a keymap setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-schemoul committed Apr 3, 2024
1 parent 0b826f8 commit 1a15e87
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ require'nvim-treesitter.configs'.setup {
["ic"] = { query = "@class.inner", desc = "Select inner part of a class region" },
-- You can also use captures from other query groups like `locals.scm`
["as"] = { query = "@scope", query_group = "locals", desc = "Select language scope" },

-- You can specify lookahead or lookbehind and it will take precedence over the global setting just for that keymapping
["ibc"] = { query = "@assignment.outer", desc = "Select outer part of an assignment looking behind the cursor instead of ahead", lookbehind = true },
},
-- You can choose the select mode (default is charwise 'v')
--
Expand Down
24 changes: 18 additions & 6 deletions lua/nvim-treesitter/textobjects/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,18 @@ local val_or_return = function(val, opts)
end
end

function M.select_textobject(query_string, query_group, keymap_mode)
function M.select_textobject(query_string, query_group, keymap_mode, local_lookahead, local_lookbehind)
query_group = query_group or "textobjects"
local lookahead = configs.get_module("textobjects.select").lookahead
local lookbehind = configs.get_module("textobjects.select").lookbehind
if local_lookahead == "nil" then
local_lookahead = nil
end
if local_lookbehind == "nil" then
local_lookbehind = nil
end
local global_lookahead = configs.get_module("textobjects.select").lookahead
local global_lookbehind = configs.get_module("textobjects.select").lookbehind
local lookahead = (global_lookahead and not local_lookbehind) or local_lookahead
local lookbehind = (global_lookbehind and not local_lookahead) or local_lookbehind
local surrounding_whitespace = configs.get_module("textobjects.select").include_surrounding_whitespace
local bufnr, textobject =
shared.textobject_at_point(query_string, query_group, nil, nil, { lookahead = lookahead, lookbehind = lookbehind })
Expand Down Expand Up @@ -157,11 +165,13 @@ function M.attach(bufnr, lang)
lang = lang or parsers.get_buf_lang(bufnr)

for mapping, query in pairs(config.keymaps) do
local desc, query_string, query_group
local desc, query_string, query_group, lookbehind, lookahead
if type(query) == "table" then
desc = query.desc
query_string = query.query
query_group = query.query_group or "textobjects"
lookbehind = query.lookbehind
lookahead = query.lookahead
else
query_string = query
query_group = "textobjects"
Expand Down Expand Up @@ -190,10 +200,12 @@ function M.attach(bufnr, lang)
{ keymap_mode },
mapping,
string.format(
"<cmd>lua require'nvim-treesitter.textobjects.select'.select_textobject('%s','%s','%s')<cr>",
"<cmd>lua require'nvim-treesitter.textobjects.select'.select_textobject('%s','%s','%s','%s','%s')<cr>",
query_string,
query_group,
keymap_mode
keymap_mode,
lookahead,
lookbehind
),
{ buffer = bufnr, silent = true, remap = false, desc = desc }
)
Expand Down

0 comments on commit 1a15e87

Please sign in to comment.