SirVer / ultisnips

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

Ultisnips help func ExpandPossibleShorterSnippet adds space at end of snippet. #951

Open NCSantos opened 6 years ago

NCSantos commented 6 years ago

Using the function described on Ultisnips help:

function! ExpandPossibleShorterSnippet()
  if len(UltiSnips#SnippetsInCurrentScope()) == 1 "only one candidate...
    let curr_key = keys(UltiSnips#SnippetsInCurrentScope())[0]
    normal diw
    exe "normal a" . curr_key
    exe "normal a "
    return 1
  endif
  return 0
endfunction
inoremap <silent> <C-L> <C-R>=(ExpandPossibleShorterSnippet() == 0? '': UltiSnips#ExpandSnippet())<CR>

It adds a space to the end of the snippet, like:

func name(params) {

}<space>

<space> beeing only " ".

Is there a way to prevent this?

Thanks.

SirVer commented 6 years ago

That function does not work at all for me. When I type lor<C-l>, I get lolorem as result. Can you try with a lorem ipsum snippet too?

NCSantos commented 6 years ago

In what filetype that snippet should work?

NCSantos commented 6 years ago

My real goal here is to create a function that expands if there is only 1 match (its what that function does) or lists snippets if there is more than 1.

SirVer commented 6 years ago

@NCSantos Try this one: https://github.com/honza/vim-snippets/blob/master/UltiSnips/all.snippets#L59, just copy & paste it into all.snippets.

I understand the motivation for this, however as said the snippet in the documentation does not work for me at all. I need to repro in a cleaner env eventually.

NCSantos commented 6 years ago

@SirVer For me the lorem snippet expands ok.

SirVer commented 6 years ago

@NCSantos For me it only works in the first line of a file. Weird... See screencast below:

https://www.youtube.com/watch?v=xAD9IyuLAxA

NCSantos commented 6 years ago

@SirVer For me it works every where.

The problem is that Ultisnips is moving 1 char back.

For example you only have this snippets:

snippet f1 "f1" b
f1
endsnippet

snippet f2 "f2" b
f2
endsnippet

snippet b1 "b1" b
b1
endsnippet

On vim in insert mode you do: f<trigger> This will show a list with all snippets, f1, f2 and b1. If you,e.g., choose f1 snippet from the list it will insert in vim: f1f

If on vim in insert mode you do: f1<trigger> It will show a list with snippets f1 and f2. Again if you coose f1 snippet from the list it will insert in vim: f1f

It is moving 1 char back,

Sorry forgot the function:


function! s:ListSnip()
  call UltiSnips#ListSnippets()
  return ""
endfunction
inoremap <silent> <leader>s <ESC>:call <SID>ListSnip()<CR>
SirVer commented 6 years ago

@NCSantos I understand your problem, but I am unable to repro because it does not work for me in the first place :/.

NCSantos commented 6 years ago

@SirVer Tried it in a new vim install and it works as I mentioned.

~$ tree -L 4 .vim/
.vim/
└── pack
    └── plugins
        └── start
            ├── ultisnips
            └── vim-snippets

5 directories, 0 files

.vimrc

set nocompatible

filetype plugin  on

let mapleader = ","

function! s:ListSnip()
  call UltiSnips#ListSnippets()
  return ""
endfunction
inoremap <silent> <leader>s <ESC>:call <SID>ListSnip()<CR>

On vim, c file, I insert: func<trigger> <trigger> being the function ListSnip() keymap. From menu I choose 1 and get:

void function_name()
{

}c

As you can see it goes a char back and puts 'c' in the end.

SirVer commented 4 years ago

I was able to reproduce both the original post & the problem described in the last post at c4bb89495a2af3ed41a510db9a2f589748643801 inside of make repro. The problem seems more involved to fix, it seems bad interplay between Vim & python execution timing.