Vscode-neovim: Folding

Created on 25 Nov 2019  路  18Comments  路  Source: asvetliakov/vscode-neovim

Need to sync vscode folds to neovim. from the initial look onDidChangeTextEditorVisibleRanges could be used with :{range}fold command
Rebind neovim folding keys/commands to call vscode folds commands

enhancement

Most helpful comment

@6a68 just set nmap j gj and nmap k gk in ~/.config/nvim/init.vim (potentially with restriction of exists('g:vscode')), and also set Vscode-neovim: Neovim Init Path to ~/.config/nvim/init.vim or equivalent, and reload VS Code. This recursively defines j and k as https://github.com/asvetliakov/vscode-neovim/blob/68f056b4c9cb6b2559baa917f8c02166abd86f11/vim/vscode-code-actions.vim#L93-L95

All 18 comments

Perhaps add a note about folding not being supported just now to known issues in README?

Any progress on this? Opening folds makes this extension almost unusable for me.

@pianocomposer321 This one is very tricky, because the folding API has a sad story in vscode. The range event is very generic and there are many limitations/issues with it.

I found this solution on another issue. It's actually for vscodevim, but I like this extension much better, and it still works for it, with a few changes. Instead of changing the keybindings in vscode's keybindins.json do this:

if exists('g:vscode')
nnoremap j :call VSCodeCall('cursorDown')
nnoremap k :call VSCodeCall('cursorUp')
endif

I was ssssoooo happy that this works, and I don't know why nobody ever thought of it before! it doesn't even slow vscode down!

You need <cr> on the ends, but anyway good workaround, if you don't use something like 10j. Although this could be modified by looking into v:count and calling it in the loop (but cursor movement may be slow in this case). Alternative way is the cursorMove command which supports jump count, but it opens folds if i remember correctly

I meant to use \

Actually, things like 10j do work, but a little slower than before.

So I actually have noticed a bit of a lag, but a much bigger problem is that remapping the keys to cursorDown and cursorUp makes the neovim instance think that the cursor is on a different line then it is in some cases. This causes problems like dd deleting the wrong line, yy yanking the wrong line, etc.

There is another solution that I think may work, but I don't know how to implement it. Pressing gj and gk skip over folds as they should, but in my init.vim I actually had already mapped j and k to gj and gk before I started using vscode neovim, but vscode neovim does not seem to use those mappings. Any ideas?

I just solved my own problem. I just changed nnoremap j gj to nmap j gj. Not sure why this doesn't cause an endless loop, or why it solves the problem, but it works!

gj/gk are mapped to cursorMove command this why it solves your problem. Looks like vscode devs have finally fixed cursorMove to skip over folds. nnoremap j gj skips these custom mappings so they're trying to use native neovim's gj which is same as j because set nowrap

Actually, they haven't fixed it. I just did some experiments and it only skips folds when you specify "by": "wrappedLine". Anyway, I'm really glad this works, because cursorMove seems to be a lot more efficient than cursorDown and cursorUp.

So, I determined that the lag I was experiencing with both of my solutions didn't have anything to do with the vscode commands, there is actually lag with VSCodeCall(). So I changed by keybindings.json to make j and k call cursorMove instead of doing it in the init.vim. This works and solves the lag, but it does break things like 10j and 10k.

So I have two questions. First, is VSCodeNotify any more efficient than VSCodeCall? And secondly, if not, do you know of any way to fix 10j etc?

First, is VSCodeNotify any more efficient than VSCodeCall

In this case yes, should be. The main difference the notify one is not blocking. For VSCodeCall neovim sends an event to the extension, the extension handles it, awaiting a result of vscode.commands.executeCommand(...), then sends it back to neovim. While it's doing all of this, neovim is blocked and doesn't process any inputs, means if you press and hold j the second j won't be handled until a response comes from the first one.
VSCodeNotify is "send and forget". However using it may produce a cursor jitter since an events are handled independently and they might be out of order. But it may be not case for simple j/k movement, so i'd try it.

And secondly, if not, do you know of any way to fix 10j etc?

No way. VSCode keybindings don't accept dynamic pre-chords. Using a simple extension specially for this potentially may work in theory, but in reality it needs to listen to type vscode event and vscode allows to utilize it only by single extension.

Thank you. VSCodeNotify takes care of the lag. It doesn't look quite as nice - the cursor looks like it skips a few lines here and there when I press and hold j, but that's much better than overshooting a line of code.

The more I tried it, the more I noticed the cursor jitter becoming a problem. So I tried disabling all extensions except neovim, and this solved the problem. I tracked down the extension that was causing the lag, and I found out it was Bracket Pair Colorizer 2, which makes sense because it has to redraw colors for the brackets every time the cursor enters a different bracket pair. I should have thought of this in the first place. I am not having any problems now!

btw, I did change VSCodeNotify back to VSCodeCall in my init.vim and have not noticed any problems so far.

Thank you for all your help!

I'm not quite following all the discussion about VSCodeNotify and related internals. What exactly do I need to do to get indent folding to work? I'd really appreciate it if someone could summarize the workaround.

@6a68 just set nmap j gj and nmap k gk in ~/.config/nvim/init.vim (potentially with restriction of exists('g:vscode')), and also set Vscode-neovim: Neovim Init Path to ~/.config/nvim/init.vim or equivalent, and reload VS Code. This recursively defines j and k as https://github.com/asvetliakov/vscode-neovim/blob/68f056b4c9cb6b2559baa917f8c02166abd86f11/vim/vscode-code-actions.vim#L93-L95

nmap j gj and nmap k gk works, thx! I have another problem

I use ctrl + shift [ and ] to fold code in vscode, but when I use za, it shows: No fold found, I have add set foldmethod=indent to init.vim, any solution?

I have add set foldmethod=indent to init.vim, any solution?

This option is hardcoded.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kkorus picture kkorus  路  4Comments

trkoch picture trkoch  路  3Comments

pieterdd picture pieterdd  路  4Comments

DrakeXiang picture DrakeXiang  路  5Comments

anhyeast picture anhyeast  路  5Comments