neovim icon indicating copy to clipboard operation
neovim copied to clipboard

API: `VisualChanged` autocmd

Open theol0403 opened this issue 3 years ago • 10 comments

Feature description

For there to be a VisualChanged autocommand that fires whenever the visual selection changes. For v:event to be populated with visual mode and visual boundaries (row and col).

Alternatively, for there to be a visual_change ui event with the same information.

The information (mode and boundaries) is not strictly required, as it can be obtained by mode(), getpos('v'), and getpos('.'). However, getting it directly would be better. The most important thing is to be notified when the selection changes.

Background

I am the current maintainer of https://github.com/vscode-neovim/vscode-neovim.

Currently, visual selection is not properly managed. Instead of telling vscode exactly where the visual selection is (so that vscode can render it, use it for commands, show it in minimap, etc), we render it ourselves using the Visual highlight. This is suboptimal.

To properly sync visual selection to vscode, we need to know when the visual selection changes in neovim. We can't use the Visual highlight, because it only gives updates for ranges inside the viewport. We can't use CursorMoved, because the visual selection can change without the cursor moving (viw).

This is a duplicate of https://github.com/neovim/neovim/issues/11590.

theol0403 avatar Aug 11 '22 04:08 theol0403

I'm also interested in this new event, as I'm having the exact same issue in ShadowVim (Neovim embedded in Xcode).

At the moment I'm using ModeChanged and CursorMoved, which works in most cases. It fails when hitting viw if the cursor is at the end of the word, as none of these events are emitted.

mickael-menu avatar Apr 04 '23 20:04 mickael-menu

@justinmk I don't really know how neovim development works/who is generally in charge of these types of changes, but in order to prioritise the simplification of vscode-neovim, what can be done to increase the priority of this issue (and others like it)? They seem quite straightforward to implement (for someone familiar with the stack), but significantly block progress. Thanks!

theol0403 avatar Jun 25 '23 07:06 theol0403

what can be done to increase the priority of this issue (and others like it)?

Mentioning that something unblocks an important use-case definitely increases the priority. But ultimately things get priority when we see a PR.

justinmk avatar Jun 25 '23 14:06 justinmk

Mentioning that something unblocks an important use-case definitely increases the priority.

A second use-case would be synchronizing visual selections between two buffers.

I've been developing a small "assembly viewer". Two buffers, displayed in a vertical split, allow users to look at a source code line in Neovim and see the generated assembly line on the left. It's a WIP plugin but a gif should hopefully illustrate the functionality.

268551190-421f9c08-c2b9-43ce-ab15-17e9d2e6369a

Right now the plug-in only highlights the assembly lines for the current line in Normal mode but it'd be great a visual selection could be accepted. And when changed, the assembly lines would show for the whole selected range. Changing the visual selection would change the highlighted assembly.

It might be doable without a VisualChanged event but would definitely be simpler with it.

To summarize: VisualChanged would be helpful to sync selections between 2+ buffers and this plug-in is another use-case.

ColinKennedy avatar Oct 05 '23 18:10 ColinKennedy

Status

  • Implementation was attempted in https://github.com/neovim/neovim/pull/26052 , but stalled and needs an owner.
  • The CursorMoved event seems to trigger during visual-mode. So it may be possible for clients/plugins to implement VisualChanged by:
    • listening to CursorMoved and WinScrolled
    • using the getregionpos() function
    • ⚠️ caveat: doesn't capture when the visual selection can change without the cursor moving (viw at end of word).

justinmk avatar Aug 15 '24 14:08 justinmk

So it may be possible for clients/plugins to implement VisualChanged by: - listening to CursorMoved and WinScrolled - using the getregionpos() function

No, the visual selection can change without the cursor moving (viw at end of word).

theol0403 avatar Aug 15 '24 17:08 theol0403

but stalled and needs an owner.

Has there not been active development by the original author in the last week?

theol0403 avatar Aug 15 '24 17:08 theol0403

Has there not been active development by the original author in the last week?

I revisited the PR, but I'm not confident I can finish it

BaraTudor2025 avatar Aug 15 '24 19:08 BaraTudor2025

I asked this earlier in the PR but didn't see a response so asking here too - Is there no way to contribute VisualChanged to Vim and patch to Neovim? I think both communities would appreciate the change. Bringing it to Vim's attention also means having more people available to pick the work up, if needed. I'm personally interested and would be happy to try implementing it too if @BaraTudor2025 doesn't think they can finish it.

Edit: FWIW I think even just VisualChanged itself (and the current PR) is useful enough on its own. Additional data like v:event can always be added in follow-up work.

ColinKennedy avatar Aug 15 '24 21:08 ColinKennedy

Edit: FWIW I think even just VisualChanged itself (and the current PR) is useful enough on its own. Additional data like v:event

v:event may not be needed if getregionpos() provides the same info.

justinmk avatar Aug 16 '24 15:08 justinmk