Skip to content

refactor(jump): only do extra search for target if there was no jump#2303

Open
abeldekat wants to merge 1 commit intonvim-mini:mainfrom
abeldekat:jump_refactor
Open

refactor(jump): only do extra search for target if there was no jump#2303
abeldekat wants to merge 1 commit intonvim-mini:mainfrom
abeldekat:jump_refactor

Conversation

@abeldekat
Copy link
Member

Details:

Instead of always checking if target exists in text, only do so if there was no jump.
Additional benefit: The code using the cursor to check if a jump occurred can be replaced by a more direct check

Details:

Instead of always checking if target exists in text,
only do so if there was no jump.
Additional benefit: The code using the cursor to check if a jump
occurred can be replaced by a more direct check
@abeldekat abeldekat requested a review from echasnovski March 6, 2026 13:58
@abeldekat
Copy link
Member Author

abeldekat commented Mar 6, 2026

@echasnovski,

The solution applied to solve issue #688 in February 2024 had a side-effect regarding highlighting.

Relevant part of diff:

@@ -364,6 +365,10 @@ H.make_expr_jump = function(backward, till)
     if target == nil then return '<Esc>' end
     H.update_state(target, backward, till, vim.v.count1)

+    vim.schedule(function()
+      if H.cache.has_changed_cursor then return end
+      vim.cmd('undo' .. (vim.fn.has('nvim-0.8') == 1 and '!' or ''))
+    end)
     return 'v<Cmd>lua MiniJump.jump()<CR>'

Whilst the scheduled undo command restored the character, it also prevented the highlighting.

Scenario with file containing a single word:

mark

Using its previous commit '1d49300d50a2'

  • Open file, position cursor on the 'a'
  • Type dfm
  • Notice that the 'a' disappears, cursor is on 'r'
  • Notice that 'm' is highlighted

Using the fix, commit '4f69339'

  • Open file, position cursor on the 'a'
  • Type dfm
  • Notice that text is not modified, as expected
  • Notice that m is not highlighted
  • Type .
  • Notice that 'm' is highlighted, as dot-repeat has no scheduled undo

To restore the original highlighting:

diff --git a/lua/mini/jump.lua b/lua/mini/jump.lua
index 234510b9..7bf50dd6 100644
--- a/lua/mini/jump.lua
+++ b/lua/mini/jump.lua
@@ -239,7 +239,8 @@ MiniJump.jump = function(target, backward, till, n_times)
   -- Ensure to undo "consuming a character" effect if there is no target found
   -- Do it here to also act on dot-repeat
   if has_jumped or not is_expr then return end
-  vim.schedule(function() vim.cmd('undo!') end)
+  -- vim.schedule(function() vim.cmd('undo!') end)
+  vim.api.nvim_create_autocmd('ModeChanged', { once = true, callback = function() vim.cmd('un
do!') end })
 end

 --- Make smart jump

I think it also is more precise to use the ModeChanged event.

@echasnovski
Copy link
Member

Thanks for the PR!

I'll take a closer look a bit later (probably next week), as I can not quickly see if the new approach is optimal. Should be, as tests pass and it is negative diff 💪

To restore the original highlighting:
...
I think it also is more precise to use the ModeChanged event.

I don't really understand why ModeChanged event is better or even why it works. Initial reaction here is that executing the undo! command without vim.schedule might be dangerous due to how Neovim works (:h textlock, :h :map-expression, and similar things).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants