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

Enable dot-repeatability after operators #4

Merged
merged 3 commits into from
Apr 1, 2023

Conversation

vypxl
Copy link
Contributor

@vypxl vypxl commented Mar 31, 2023

I looked into how to dot-repeat motions and textobjects. It cost me many hours of my life.
But, thanks to @ii14 I found a solution.

It seems that dot repeating an operator-motion combination requires the motion to be a built in motion, or an ex command, which somehow moves the cursor.
This means, it is possible to dot-repeat a spider-y dw for example, by mapping w not to function() require... but to <cmd>lua require.... That way, that command is used as the motion to be repeated when bound in operator-pending mode.

I put together a little patch that applies this. I hope this helps, and maybe it helps for the problems in nvim-various-textobjs (chrisgrieser/nvim-various-textobjs#7) as well :)

@chrisgrieser chrisgrieser self-requested a review March 31, 2023 17:58
Copy link
Owner

@chrisgrieser chrisgrieser left a comment

Choose a reason for hiding this comment

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

There are a few issues why the current version of the PR won't run.

I think you could simply skip the whole <Plug> and all the associated issues by telling the user that that the keymaps need to be added as:

Keymap({ "n", "o", "x" }, "b", '<cmd>lua require("spider").motion("b")<CR>', { desc = "Spider-b" })

which I tested with my version of the plugin and works nicely!

So could you revert the changes in the lua file, and instead make the respective changes to the README?

lua/spider.lua Outdated
"",
"<Plug>(spider-motion-" .. key .. ")",
"<cmd>lua require('spider').motion('" .. key .. "')<CR>"
)
Copy link
Owner

Choose a reason for hiding this comment

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

this code is not valid, it's missing an end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, was kinda in a hurry earlier :)
Didn't test it

lua/spider.lua Outdated
@@ -147,6 +147,14 @@ function M.motion(key)
if isOperatorPending and key == "e" then col = col + 1 end

vim.api.nvim_win_set_cursor(0, { row, col })

-- set plug keymaps
Copy link
Owner

Choose a reason for hiding this comment

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

even with the end added, the plugin still won't run, since you set the <Plug> keymaps inside the function they are supposed to call. You would need set them up elsewhere, e.g. in the setup function (which is then becoming mandatory for dot repeats)

README.md Outdated
Copy link
Owner

Choose a reason for hiding this comment

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

on line 70 (github won't let me comment it, since you haven't changed it), I think the lazy-loading needs to be dropped since, since iirc <Plug> keymaps do not play nice with lazy loading.

@chrisgrieser
Copy link
Owner

That being said, awesome, thank you so much! I did tons of research and asked around a lot, but did not come up with a solution that works.

May I ask where you got the info that it's the <cmd>lua ... is the solution to this?

@vypxl
Copy link
Contributor Author

vypxl commented Apr 1, 2023

Well, I did research because I really wanted it to work, and I saw you everywhere this topic came up, so I did some testing. Then I kinda gave up and filed that issue with neovim itself, when @ii14 pointed out that ex commands could be used as repeatable motions for operators.

I tried that, first using vim style :map commands, then came to the solution of using a <cmd> mapping to make it work without showing the command line :)

@vypxl
Copy link
Contributor Author

vypxl commented Apr 1, 2023

To the issue with the plug mapping, yeah I recall having problems with that in my config.
Maybe just letting the users set their mapping to the <cmd>.. thing is better.

@chrisgrieser
Copy link
Owner

great, this works now!

btw, I also updated the various textobjs plugin, in case you are using it.

@chrisgrieser chrisgrieser merged commit 616aef8 into chrisgrieser:main Apr 1, 2023
@utkarshgupta137
Copy link

dew is locking up the editor for me until I press enter, after which I get the following error:

E5108: Error executing lua: attempt to call a nil value
stack traceback:

lazy.vim:

return {
  "chrisgrieser/nvim-spider",
  keys = {
    { "ew", function() require("spider").motion("w") end, desc = "Spider-w", mode = { "n", "o", "x" } },
    { "ee", function() require("spider").motion("e") end, desc = "Spider-e", mode = { "n", "o", "x" } },
    { "eb", function() require("spider").motion("b") end, desc = "Spider-b", mode = { "n", "o", "x" } },
    { "ge", function() require("spider").motion("ge") end, desc = "Spider-ge", mode = { "n", "o", "x" } },
  },
  opts = {
    skipInsignificantPunctuation = false,
  },
}

Am I doing something wrong or should I open an issue?

@chrisgrieser
Copy link
Owner

@utkarshgupta137 yes in general, you should open an issue instead of commenting on an unrelated PR.

In your particular case the issue arises because you are not using the keys spec from lazy.nvim correctly. the keys spec triggers the loading of a plugin, but the way you have set it up, the plugin needs to be already loaded before the triggers works.

You can use this snippet:

return {
  "chrisgrieser/nvim-spider",
  init = function()
    vim.keymap.set({"n", "o", "x"}, "ew", "<cmd>lua require('spider').motion('w')<CR>", { desc = "Spider-w" })
    vim.keymap.set({"n", "o", "x"}, "ee", "<cmd>lua require('spider').motion('e')<CR>", { desc = "Spider-e" })
    vim.keymap.set({"n", "o", "x"}, "eb", "<cmd>lua require('spider').motion('b')<CR>", { desc = "Spider-b" })
    vim.keymap.set({"n", "o", "x"}, "ge", "<cmd>lua require('spider').motion('ge')<CR>", { desc = "Spider-ge" })
  end,
  opts = {
    skipInsignificantPunctuation = false,
  },
}

Repository owner locked as off-topic and limited conversation to collaborators May 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants