vscode-neovim icon indicating copy to clipboard operation
vscode-neovim copied to clipboard

Centering editor when searching

Open uraza opened this issue 9 months ago • 3 comments

Check the following:

  • [X] I have checked the behavior after setting "vscode-neovim.neovimClean" in VSCode settings.
  • [X] I have read the vscode-neovim docs.
  • [X] I have searched existing issues.

Neovim version (nvim -v)

0.9.5

Operating system/version

Windows 11

Describe the bug

I have this mapping in my init.lua, which is supposed to center the current search result on screen:

vim.keymap.set("n", "n", "nzzzv")

While this work in neovim, it does not work in vscode-neovim.

Running the same command by hand in vscode-neovim (i.e. "n" to move to the next search result and then "zz" to center it on screen) works as expected.

There was a similar issue opened some time back (#767) but it got closed while the issue is still there.

Steps To Reproduce

  1. Search for a pattern that appears at least twice in a file using the "/" command.
  2. Press n to go to the next occurrence: it is not centered on screen.

Expected Behavior

Search results should be centered on screen.

uraza avatar Apr 25 '24 13:04 uraza

Currently valid alternative: Use vscode.call to call the vscode command to center the viewport

xiyaowong avatar Apr 26 '24 01:04 xiyaowong

Thanks @xiyaowong , it seems like a good idea.

Here is what I came up with: I used "revealLine" as I could not find a command that centers the viewport based on the current cursor position.

local vscode = require("vscode-neovim")

-- Sends the specified keys to Neovim.
function nvim_feedkeys(keys)
  local feedable_keys = vim.api.nvim_replace_termcodes(keys, true, false, true)
  vim.api.nvim_feedkeys(feedable_keys, "n", false)
end

-- Centers the viewport. This needs to be delayed for the cursor position to be
-- correct after the nvim_feedkeys operations.
function center_viewport()
  vim.defer_fn(function()
    local current_line = vim.api.nvim_win_get_cursor(0)[1]
    vscode.call("revealLine", {args = {lineNumber = current_line, at = "center"}})
  end, 20)
end

vim.keymap.set("n", "n", function()
  nvim_feedkeys("n")
  center_viewport()
end)

uraza avatar Apr 26 '24 09:04 uraza

@uraza try sth like this:

vim.keymap.set("n", "*", function()
  vim.cmd(":norm! *")
  local curline = vim.fn.line(".")
  vscode.call("revealLine", { args = {lineNumber = curline, at = "center"} })
end, { noremap = true, silent = true })

zhuoqun-chen avatar Sep 20 '24 04:09 zhuoqun-chen