bug: Cross-file global mark jump is overwritten by VS Code's previous cursor position
Check the following:
- I have tried setting
vscode-neovim.neovimCleanin VSCode settings and restarting VSCode. - I have read the vscode-neovim docs.
- I have searched existing (closed!) issues.
Neovim version (nvim -v)
v0.12.2
VSCode version and flavor (Cursor, vscodium, …)
Vscode 1.139.1
Operating system/version
Ubuntu 24.04.4 LTS
Problem
When using a Neovim global mark to jump to another file, Neovim initially reaches the correct buffer and cursor position, but that position is later overwritten by the cursor position previously saved by VS Code.
For example:
- The global mark points to
test.vat[23, 14]. - The current editor is
formatter.v. - VS Code previously saved
[28, 15]as the selection intest.v. - Executing
`Amakes Neovim opentest.vand initially report[23, 14]. - After the VS Code editor/layout synchronization finishes,
[28, 15]is written back to Neovim. - The final cursor position is therefore
[28, 15]instead of the global mark position[23, 14].
There also appears to be an intermediate incorrect update.
While Neovim has already switched its window to the test.v buffer, the extension's grid-to-editor mapping still points to the previous formatter.v editor. As a result, the cursor position [23, 14] is temporarily applied to formatter.v.
A shortened log sequence looks like this:
Buffer request for .../test.v, bufId: 3
ViewportManager:
Previous viewport cursor, gridId: 8, cursor: [23, 14]
CursorManager:
Applying editor selection,
editor: .../formatter.v,
gridId: 8,
previous active: [1, 0],
next active: [23, 14]
CursorManager:
Applied editor selection,
editor: .../formatter.v,
active: [23, 14]
ViewportManager:
gridId: 9, cursor: [23, 14]
CursorManager:
No editor for gridId: 9
BufferManager:
Updating cursor pos in neovim,
winId: 1006,
pos: [28, 15]
ViewportManager:
gridId: 9,
cursor: [28, 15]My current understanding of the race is:
- Neovim changes the buffer in its existing window and jumps to the global mark.
- The viewport cursor becomes
[23, 14]. - The extension's grid/window/editor mapping has not yet caught up with the Neovim buffer change.
- The target cursor is either applied to the old
formatter.veditor or dropped because the new grid does not have an editor mapping yet. - VS Code opens
test.vand restores its previous selection[28, 15]. - During layout synchronization, that VS Code selection is sent back to Neovim and overwrites the global mark position.
This does not appear to be caused by DocumentChangeManager waiting for pending document changes. Instrumentation shows that the wait completes immediately in this case.
Actual behavior
Neovim initially jumps to [23, 14], but the final cursor position becomes the previous VS Code selection [28, 15].
The logs may also show [23, 14] being applied to the previous formatter.v editor while the layout mapping is being updated.
Possible solution
One possible direction would be to treat a Neovim-initiated cross-buffer navigation as a cursor ownership handoff:
- Associate the pending cursor position with the Neovim window and buffer involved in the external-buffer request.
- Before applying a viewport cursor to a VS Code editor, verify that the Neovim buffer mapped to the grid matches the buffer/document mapped to that editor.
- If the editor mapping has not been updated yet, preserve the Neovim cursor instead of applying it to the currently mapped old editor or dropping it.
- After
showTextDocument()returns the target editor, apply the preserved Neovim cursor before layout synchronization sends the VS Code selection back to Neovim. - Avoid sending the restored VS Code selection to Neovim when a pending Neovim-initiated navigation already provides the authoritative cursor position.
- Add a regression test for a global mark jump between two files where the target editor has a stale saved VS Code selection.
The existing native win_viewport handling and custom viewport-changed notification seem to cover different historical edge cases, including long lines and incsearch. Therefore, removing one of these listeners probably is not sufficient and may introduce regressions.
I am not very familiar with this part of the codebase, especially the interaction between buffer synchronization, grid/window mappings, viewport notifications, and cursor ownership. I am therefore not sure whether the proposed approach would cause other synchronization problems or regress existing viewport behavior.
Guidance from the maintainers on the appropriate synchronization boundary would be appreciated.
Steps To Reproduce
Open
test.v.Move the cursor to a known position, for example
[23, 14].Set a global mark:
mAMove the cursor in
test.vto another position, for example[28, 15], so VS Code saves a different editor selection.Switch to
formatter.v.Execute:
`AObserve the cursor position after
test.vis opened.
Expected Behavior
The cross-file global mark jump should finish at the cursor position selected by Neovim, [23, 14].
The target cursor should not be applied to formatter.v, and VS Code's previously saved selection should not overwrite the position of a Neovim-initiated jump.
Source: vscode-neovim/vscode-neovim