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

Repeat movements like ; and , #359

Merged
merged 8 commits into from
Jan 17, 2023
Merged
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
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,60 @@ require'nvim-treesitter.configs'.setup {
EOF
```

You can make the movements repeatable like `;` and `,`.

```lua
local ts_repeat_move = require "nvim-treesitter.textobjects.repeatable_move"

-- Repeat movement with ; and ,
-- ensure ; goes forward and , goes backward regardless of the last direction
vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move_next)
vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_previous)

-- vim way: ; goes to the direction you were moving.
-- vim.keymap.set({ "n", "x", "o" }, ";", ts_repeat_move.repeat_last_move)
-- vim.keymap.set({ "n", "x", "o" }, ",", ts_repeat_move.repeat_last_move_opposite)

-- Optionally, make builtin f, F, t, T also repeatable with ; and ,
vim.keymap.set({ "n", "x", "o" }, "f", ts_repeat_move.builtin_f)
vim.keymap.set({ "n", "x", "o" }, "F", ts_repeat_move.builtin_F)
vim.keymap.set({ "n", "x", "o" }, "t", ts_repeat_move.builtin_t)
vim.keymap.set({ "n", "x", "o" }, "T", ts_repeat_move.builtin_T)
```

You can even make a custom repeat behaviour.

````lua
local function repeat_last_move_next_end()
-- This repeats last query with always forward direction and end of the range.
return ts_repeat_move.repeat_last_move({forward = true, start = false})
end
vim.keymap.set({ "n", "x", "o" }, "<end>", repeat_last_move_next_end)
```

Furthermore, you can make any custom movements (e.g. from another plugin) repeatable with the same keys.
This doesn't need to be treesitter-related.

```lua
-- example: make l and h keys jump two characters.
-- and they are repeatable with ; and , keys.
local function right2()
vim.cmd [[normal! 2l]]
end
local function left2()
vim.cmd [[normal! 2h]]
end

-- make sure forward function comes first
local right2_repeat, left2_repeat = ts_repeat_move.make_repeatable_move_pair(right2, left2)
-- Or, use `make_repeatable_move` or `set_last_move` functions. See the code for instructions.

vim.keymap.set({ "n", "x", "o" }, "l", right2_repeat)
vim.keymap.set({ "n", "x", "o" }, "h", left2_repeat)
````

Alternative way is to use a repeatable movement managing plugin such as [nvim-next](https://github.com/ghostbuster91/nvim-next).
theHamsta marked this conversation as resolved.
Show resolved Hide resolved

## Textobjects: LSP interop

- peek_definition_code: show textobject surrounding definition as determined
Expand Down
4 changes: 3 additions & 1 deletion lua/nvim-treesitter-textobjects.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function M.init()
goto_next_end = {},
goto_previous_start = {},
goto_previous_end = {},
goto_next = {},
goto_previous = {},
},
swap = {
module_path = "nvim-treesitter.textobjects.swap",
Expand All @@ -64,7 +66,7 @@ function M.init()
},
},
}
for _, m in ipairs { "select", "move", "swap", "lsp_interop" } do
for _, m in ipairs { "select", "move", "repeatable_move", "swap", "lsp_interop" } do
utils.setup_commands("textobjects." .. m, require("nvim-treesitter.textobjects." .. m).commands)
end
end
Expand Down
14 changes: 7 additions & 7 deletions lua/nvim-treesitter/textobjects/attach.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ local parsers = require "nvim-treesitter.parsers"
local queries = require "nvim-treesitter.query"
local M = {}

local function make_repeatable(fn)
local function make_dot_repeatable(fn)
return function()
_G._nvim_treesitter_textobject_last_function = fn
vim.o.opfunc = "v:lua._nvim_treesitter_textobject_last_function"
vim.api.nvim_feedkeys("g@l", "n", false)
end
end

function M.make_attach(normal_mode_functions, submodule, keymap_modes, opts)
function M.make_attach(functions, submodule, keymap_modes, opts)
keymap_modes = keymap_modes or "n"
opts = opts or {}
return function(bufnr, lang)
Expand All @@ -22,7 +22,7 @@ function M.make_attach(normal_mode_functions, submodule, keymap_modes, opts)

local config = configs.get_module("textobjects." .. submodule)

for _, function_call in pairs(normal_mode_functions) do
for _, function_call in pairs(functions) do
local function_description = function_call:gsub("_", " "):gsub("^%l", string.upper)
for mapping, query_metadata in pairs(config[function_call] or {}) do
local mapping_description, query, query_group
Expand All @@ -40,8 +40,8 @@ function M.make_attach(normal_mode_functions, submodule, keymap_modes, opts)
local fn = function()
require("nvim-treesitter.textobjects." .. submodule)[function_call](query, query_group)
end
if opts.repeatable then
fn = make_repeatable(fn)
if opts.dot_repeatable then
fn = make_dot_repeatable(fn)
end
vim.keymap.set(
keymap_modes,
Expand All @@ -54,7 +54,7 @@ function M.make_attach(normal_mode_functions, submodule, keymap_modes, opts)
end
end

function M.make_detach(normal_mode_functions, submodule, keymap_modes)
function M.make_detach(functions, submodule, keymap_modes)
keymap_modes = keymap_modes or "n"
return function(bufnr)
local config = configs.get_module("textobjects." .. submodule)
Expand All @@ -68,7 +68,7 @@ function M.make_detach(normal_mode_functions, submodule, keymap_modes)
vim.keymap.del({ "o", "x" }, mapping, { buffer = bufnr })
end
end
for _, function_call in pairs(normal_mode_functions) do
for _, function_call in pairs(functions) do
for mapping, query in pairs(config[function_call] or {}) do
if type(query) == "table" then
query = query[lang]
Expand Down
71 changes: 52 additions & 19 deletions lua/nvim-treesitter/textobjects/move.lua
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
local ts_utils = require "nvim-treesitter.ts_utils"
local attach = require "nvim-treesitter.textobjects.attach"
local shared = require "nvim-treesitter.textobjects.shared"
local repeatable_move = require "nvim-treesitter.textobjects.repeatable_move"
local queries = require "nvim-treesitter.query"
local configs = require "nvim-treesitter.configs"
local parsers = require "nvim-treesitter.parsers"

local M = {}

local function move(query_strings_regex, query_group, forward, start, winid)
local function move(opts)
-- opts includes query_strings_regex, query_group, forward, start, winid
-- start: bool or nil. If true, choose the start of the node, and false is for the end.
-- If nil, it considers both and chooses the closer side.
query_strings_regex = shared.make_query_strings_table(query_strings_regex)
query_group = query_group or "textobjects"
local query_strings_regex = shared.make_query_strings_table(opts.query_strings_regex)
local query_group = opts.query_group or "textobjects"
local forward = opts.forward
local starts
if start == nil then
if opts.start == nil then
starts = { true, false }
else
if start then
if opts.start then
starts = { true }
else
starts = { false }
end
end
winid = winid or vim.api.nvim_get_current_win()
local winid = opts.winid or vim.api.nvim_get_current_win()

local bufnr = vim.api.nvim_win_get_buf(winid)
local query_strings =
shared.get_query_strings_from_regex(query_strings_regex, query_group, parsers.get_buf_lang(bufnr))
Expand Down Expand Up @@ -96,25 +100,54 @@ local function move(query_strings_regex, query_group, forward, start, winid)
end
end

M.goto_next_start = function(query_strings, query_group)
query_group = query_group or "textobjects"
move(query_strings, query_group, "forward", "start")
local move_repeatable = repeatable_move.make_repeatable_move(move)

M.goto_next_start = function(query_strings_regex, query_group)
move_repeatable {
forward = true,
start = true,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end
M.goto_next_end = function(query_strings, query_group)
move(query_strings, query_group, "forward", not "start")
M.goto_next_end = function(query_strings_regex, query_group)
move_repeatable {
forward = true,
start = false,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end
M.goto_previous_start = function(query_strings, query_group)
move(query_strings, query_group, not "forward", "start")
M.goto_previous_start = function(query_strings_regex, query_group)
move_repeatable {
forward = false,
start = true,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end
M.goto_previous_end = function(query_strings, query_group)
move(query_strings, query_group, not "forward", not "start")
M.goto_previous_end = function(query_strings_regex, query_group)
move_repeatable {
forward = false,
start = false,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end

M.goto_next = function(query_strings, query_group)
move(query_strings, query_group, "forward")
M.goto_next = function(query_strings_regex, query_group)
move_repeatable {
forward = true,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end
M.goto_previous = function(query_strings, query_group)
move(query_strings, query_group, not "forward")
M.goto_previous = function(query_strings_regex, query_group)
move_repeatable {
forward = false,
query_strings_regex = query_strings_regex,
query_group = query_group,
}
end

local nxo_mode_functions = {
Expand Down
Loading