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

Centering editor when searching #1909

Open
3 tasks done
uraza opened this issue Apr 25, 2024 · 2 comments
Open
3 tasks done

Centering editor when searching #1909

uraza opened this issue Apr 25, 2024 · 2 comments
Labels
bug Something isn't working category: sync synchronization between vscode and nvim (text, windows, etc) manager: viewport

Comments

@uraza
Copy link

uraza commented Apr 25, 2024

Check the following:

  • I have checked the behavior after setting "vscode-neovim.neovimClean" in VSCode settings.
  • I have read the vscode-neovim docs.
  • 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 uraza added the bug Something isn't working label Apr 25, 2024
@justinmk justinmk added category: sync synchronization between vscode and nvim (text, windows, etc) manager: viewport labels Apr 25, 2024
@xiyaowong
Copy link
Collaborator

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

@uraza
Copy link
Author

uraza commented Apr 26, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working category: sync synchronization between vscode and nvim (text, windows, etc) manager: viewport
Projects
None yet
Development

No branches or pull requests

3 participants