ggandor / leap.nvim

Neovim's answer to the mouse 🦘
MIT License
4.31k stars 46 forks source link

Discussion: (suggested) mappings for treesitter and remote #242

Open ggandor opened 2 months ago

ggandor commented 2 months ago

Remote

gs, gS (linewise), g<c-s> (blockwise)? Do we need linewise (and blockwise) by default?

Treesitter

Dedicated linewise keys would be great, since they are much more efficient (filtering).

gy/gY, ga/gA, gb/gB, gl/gL... ?

Remote + treesitter

What about shortcuts, basically require'leap.remote'.action { input = '<treesitter-trigger>' }?

pkazmier commented 1 month ago

[Disclaimer: I'm new to leap and have only started using it since yesterday 😄]

With regard to remote, I'm not sure you need line-wise or block-wise by default. The documentation encourages users to use normal remote mode versus operator remote mode to allow greater flexibility with making selections anyways. And for those that use the operator remote mode solely (like me), I still don't have a problem making line-wise selections using standard vim methods. And in the worse case, I can always just press V before selecting what I want. So, I don't think it's worth taking up more keys for something that's probably not really needed. And, for that reason, I've chosen to stick with r in operating remote mode only, so I don't waste another normal mode mapping. My brain is wired for operator then motion, so I'll never get used to the normal remote mode. Plus, it doesn't work well with the tip for auto pasting after a yank.

With regards to treesitter, I lean towards to gy/gY for the slight mnemonic (and ga/gA are used by mini.align in my config). But, again in the spirit of minimizing how many keys need to be defined, I've opted only for the non-line-wise binding to <C-Space>. I picked that binding up from my LazyVim days and it has stuck with me ever since. While I think line-wise treesitter offers great utility, when I want to use it, I just press V before <C-Space>.

Finally, with regards to remote and treesitter, I don't need any shortcuts as the above compose well. If I want to do a remote yank of a line-wise treesitter selection, then it's yr{leap}V<C-Space>{leap}. Obviously, I'm a fan of minimizing the amount of keys consumed by mappings. My g namespace is chock full with a bunch of mini things (gs sort, gS split join, ga/gA align, etc ...).

My leap configuration for reference and the basis for my comments above (not shown, I use LeapBackdrop to make the labels easier to read instead of reverse text in a single cell):

require("leap").opts.equivalence_classes = { " \t\r\n", "([{", ")]}", "'\"`" }
require("leap.user").set_repeat_keys("<enter>", "<backspace>")

-- I prefer bi-directional searches as the downsides are mostly compensated
-- for by the above set_repeat_keys. I lose dot-repeats, but I'm not using
-- them anyways because I don't want repeat.vim. The other downside is
-- that we don't get as many autojumps, but we'll see how that pans out.
--
-- I had to move mini.surround to gz to make room for this binding.
vim.keymap.set({ "x", "n", "o" }, "s", function()
  require("leap").leap({
    target_windows = { vim.api.nvim_get_current_win() },
  })
end)

-- With bi-directional searching enabled, I use this binding to search other
-- windows. I originally combined them all into one binding to search all
-- windows, but one of leap's benefites is that first autojump, so this helps
-- improve the odds by limiting matches.
vim.keymap.set({ "x", "n", "o" }, "S", function()
  require("leap").leap({
    target_windows = require("leap.util").get_enterable_windows(),
  })
end)

-- I only use remote operations via operating pending mode. This allows me to
-- save wasting another key in my normal mode mappings.
vim.keymap.set({ "o" }, "r", function()
  require("leap.remote").action()
end)

-- This mapping is what LazyVim uses for the built-in treesitter plugin
-- incremental selection, so using it doesn't waste another key in my
-- configuration as I already had this one bound to the same function..
vim.keymap.set({ "n", "x", "o" }, "<C-Space>", function()
  require("leap.treesitter").select()
end)

-- Automatically paste when doing a remote yank operation.
vim.api.nvim_create_augroup("LeapRemote", {})
vim.api.nvim_create_autocmd("User", {
  pattern = "RemoteOperationDone",
  group = "LeapRemote",
  callback = function(event)
    vim.notify(event.data.register)
    -- Do not paste if some special register was in use.
    if vim.v.operator == "y" and event.data.register == "+" then
      vim.cmd("normal! p")
    end
  end,
})