SirVer / ultisnips

UltiSnips - The ultimate snippet solution for Vim. Send pull requests to SirVer/ultisnips!
GNU General Public License v3.0
7.54k stars 694 forks source link

Unexpected tab to select mod, coc.nvim unexpected behavior. #1327

Open jfab20 opened 3 years ago

jfab20 commented 3 years ago

Some snippets (especially nested ones) don't expand when triggered but instead jump to previous placeholder and start select mode. I have noticed that this behavior started when I installed coc.nvim, and in fact, if I do :CocDisable, I get the normal expected behavior.

Expected behavior: Consider this snippet:

snippet test "test" iA
test{$1}{$2}$0
endsnippet

When I type test, It expands to text{}{} and if I press tab 2 times, it takes me out of the parenthesis, and I can even add text in between, all fine.

Now suppose I write text -> text{}{} and then in the first placeholder I write test again. I want test{test{}{}}{}. And then I expect that if I typetab two times, it takes me out of the first text{}{} (which it does) and then if I type another two times tab, it takes me out of the whole thing.This is expected with arbitrary nested snippets, and it works this way if i disable coc.nvim

Actual behavior: When I type test, It expands to text{}{} and if I press tab 2 times, it takes me out of the parenthesis, and I can even add text in between, all fine.

Now suppose I write text -> text{}{} and then in the first placeholder I write test again. I want test{test{}{}}{}. And then I expect that if I typetab two times, it takes me out of the first text{}{} (which it does ) BUT when I type tab another time, i see that vim starts select mode and selects the place holder test{}{}, like this:

image

Steps to reproduce

set nocompatible
set runtimepath^=/path/to/coc.nvim
filetype plugin indent on
syntax on
set hidden
call plug#begin('~/.vim/plugged')

Plug 'neoclide/coc.nvim', {'branch': 'release'}

Plug 'SirVer/UltiSnips'

let g:UltiSnipsExpandTrigger='<tab>'
let g:UltiSnipsJumpForwardTrigger='<tab>'
let g:UltiSnipsJumpBackwardTrigger='<s-tab>'

call plug#end()

Also, add this snippet to tex snippets:

snippet test "test" iA
test{$1}{$2}$0
endsnippet

Then do :cocdisable and try it again, now it works as it should.


SirVer commented 3 years ago

I am unable to repro this. Here is my attempt: https://github.com/SirVer/ultisnips/tree/issues/1327%2Frepro. You can fetch that branch, do make image_repro repro and you will be in a shell that has Vim 8.2, Coc and the latest UltiSnips installed. Maybe you can create a repro case in that one.

jfab20 commented 3 years ago

I get the exact same problem in the shell with vim 8.2, Coc and the latest Ultisnips that the one in my regular vim files. I had to install nodejs to make coc work. Maybe try to nest 3 snippets or more? . Again when I do :CocDisable everything works fine. I posted this on coc.nvim issues and they told me ultisnips maps my tab key, but i don't think coc rewrites that map in anyway.

Do you want me to upload screenshots or video of me reproducing the bug?

jfab20 commented 3 years ago

I am unable to repro this. Here is my attempt: https://github.com/SirVer/ultisnips/tree/issues/1327%2Frepro. You can fetch that branch, do make image_repro repro and you will be in a shell that has Vim 8.2, Coc and the latest UltiSnips installed. Maybe you can create a repro case in that one.

Sorry, maybe I did not understand, do i need to do something else? I've already followed your steps, fetched the branch and I got the shell in my system which had Ultisnips, coc and vim. I still get the same error on the repro case. Do you need me to take screenshots or something else?

SirVer commented 3 years ago

@jfab-123 I need to try again with node installed in the container, maybe I can repro then. There are two possibilities:

  1. either I cannot repro at which point we have a confusing situation we need to consolidate. A video would then help.
  2. or I can repro. In that case there is a chance of investigating the issue, but the likelyhood that I can fix it is small, given the high number of moving pieces in this bug report (i.e. node, coc, UltiSnips). Let's see when I find time to try to repro again.
jfab20 commented 3 years ago

@jfab-123 I need to try again with node installed in the container, maybe I can repro then. There are two possibilities:

  1. either I cannot repro at which point we have a confusing situation we need to consolidate. A video would then help.
  2. or I can repro. In that case there is a chance of investigating the issue, but the likelyhood that I can fix it is small, given the high number of moving pieces in this bug report (i.e. node, coc, UltiSnips). Let's see when I find time to try to repro again.

Ok, I'll wait! Thanks!

SirVer commented 3 years ago

@jfab-123 After installing nodejs I was able to trigger the problem as described, i.e. I can repro this one now. My repro branch is this here https://github.com/SirVer/ultisnips/tree/issues/1327/repro.

The first interesting thing I noticed is that the problem disappears when we are not using A as option, i.e. if you have to hit <tab> to expand the snippet. Can you verify that it is the same for you?

SirVer commented 3 years ago

Actually, the bug is flaky for me: sometimes I can reproduce it, but most of the time the motions work correctly. I have attached a video where it worked every single time without fail. I hence retract that the A option is at fault.

https://user-images.githubusercontent.com/140115/113519077-4c1e1f00-958a-11eb-9b55-cdb92dfa6041.mov

@jfab-123 Sorry mate, I do not think I can fix this issue without the ability of reproducing it 100% of the time.

jfab20 commented 3 years ago

@jfab-123 After installing nodejs I was able to trigger the problem as described, i.e. I can repro this one now. My repro branch is this here https://github.com/SirVer/ultisnips/tree/issues/1327/repro.

The first interesting thing I noticed is that the problem disappears when we are not using A as option, i.e. if you have to hit <tab> to expand the snippet. Can you verify that it is the same for you?

Actuallly for me this seems to be the problem, because when I remove the A option it works perfectly, I didn't noticed that. I think it could be a temporary fix at least for me!. Maybe there's something wrong with the automatic jump and coc.

Also, in the video you posted I noticed that you nested just two test which sometimes works for me as well and also I have noticed that if I write inside the brackets, sometimes it works as expected,but sometimes it doesn't. Maybe you could try to nest three or four times the snippet? I mean, I noticed the first time this with my \frac{}{} snippet for latex, and I sometimes have to nest several snippets inside of it like exponents or so, and it started happening just when I installed coc. If you can't reproduce it though then it's okay, I guess I have to stop using the autotrigger function (or Coc). Any recommendations?

jfab20 commented 3 years ago

Actually, the bug is flaky for me: sometimes I can reproduce it, but most of the time the motions work correctly. I have attached a video where it worked every single time without fail. I hence retract that the A option is at fault.

Screen.Recording.2021-04-04.at.21.07.23.mov @jfab-123 Sorry mate, I do not think I can fix this issue without the ability of reproducing it 100% of the time.

I have noticed that the bug is irregular as well, but when I want it to happen 100% of the time, I nest like 5 or more test snippets and then try to get out of all of them. It always reproduces the bug for me in that way. Removing the A option removes the bug consistently.

jfab20 commented 3 years ago

When I put suggest.autoTrigger=none in the coc config file, (which is the option for the menu that appears for completion), the issue disappears. So maybe there's an issue with coc auto trigger completion and utlisnips autotrigger.

"suggest.autoTrigger":~

    How should completion be triggered, default: `"always"`

    Valid options: ["always", "trigger", "none"]

    - `always`: trigger suggest on word characters and trigger
      characters.
    - `trigger`: trigger suggest on trigger characters only.
    - `none`: no auto trigger at all.
jfab20 commented 3 years ago

This is not a coc problem, nor a nodejs problem. Today I tried with the completion engine nvim-compe and the bug is still present. So this is an Ultisnips problem. I think it has to do with the popup menu, because when I have no completion engine, everything works okay.
Using latest neovim, latest ultisnips and latest nvim-compe.

PhyX-Meow commented 2 years ago

Same issue here, I figure out that The bug is always triggered if the completion menu is poped up. My snippet is something like alpha -> \alpha inside pp -> ^{$1}$0. With word completion like coc-dictionary enabled, I can always reproduce since typping alpha always trigger word completion. Another thing I notice that the problem only occur when the snippet alpha -> \alpha is triggered with completion menu open, not relavent to when I press <Tab>. Hope these can give some help to debugging. BAD Sorry I don't know how to display keys, but I have same config as @jfab-123

PhyX-Meow commented 2 years ago

I now found another thing, the bug may be not fault of coc.nvim, even if I disable it, with completion menu by original vim, the bug also occurs, for eg. these to enable word completion of vim. Press <c-n> to trigger.

set dictionary=spell,/usr/share/dict/words,~/.vim/spell/en.utf-8.add
set complete+=k

BAD1

PhyX-Meow commented 2 years ago

Oh, forget to say, I'm using vim 8.2.3582-1, Archlinux. All plugins are current master.

One easiest (but may be not good) way to resolve this can be, do a ctrl-E to kill the popup menu before triggering snippet....

jfab20 commented 2 years ago

I now found another thing, the bug may be not fault of coc.nvim, even if I disable it, with completion menu by original vim, the bug also occurs, for eg. these to enable word completion of vim. Press <c-n> to trigger.

set dictionary=spell,/usr/share/dict/words,~/.vim/spell/en.utf-8.add
set complete+=k

BAD1

it is not the fault of coc.nvim. I have submitted 2 more issues here: https://github.com/SirVer/ultisnips/issues/1380 https://github.com/SirVer/ultisnips/issues/1400 about ultisnips problem with the popup menu. However, no one seems to answer so I think this not going to be solved soon...

yangxing844 commented 2 years ago

one work around you can try is to avoid this by making the snippet unique such there wont be any completion menu popping up, for example, for the greek letter snippet, i would suggest a leader ;or some thing else, so this alpha snippet would be ;a,beta would be ;b, so completion menu would not pop up, hence no visual selecting. check out the imaps feature of vimtex, it is awesome!

PhyX-Meow commented 2 years ago

one work around you can try is to avoid this by making the snippet unique such there wont be any completion menu popping up, for example, for the greek letter snippet, i would suggest a leader ;or some thing else, so this alpha snippet would be ;a,beta would be ;b, so completion menu would not pop up, hence no visual selecting. check out the imaps feature of vimtex, it is awesome!

Thank you!! This is very useful, how can I miss this feature!?

jfab20 commented 2 years ago

This is still not a solution in my case. The issue stills occurs still even if the completion menu doesn't pop up. As expained in my frac snippet.

PhyX-Meow commented 2 years ago

Any progress of this?

ucasyfp commented 2 years ago

Any progress of this?

I just found that coc-snippets, a plugin of coc.nvim can avoid such behavior. It can load ultisnips' snippets though it can not provide all the features of ultisnips such like reformatted snippets. Maybe using both of them while restricted ultisnips to a limited snippets file, making it only deal with the snippets can't be supported by coc-snippets can be a solution.