Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lookbehind local keymap setting #588

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the name of the args could be lookahead and lookbehind. If they are not defined just use the global. Or I would name it like override_lookahead etc.

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
Loading