ultisnips icon indicating copy to clipboard operation
ultisnips copied to clipboard

Ultisnips help func ExpandPossibleShorterSnippet adds space at end of snippet.

Open NCSantos opened this issue 6 years ago • 10 comments

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.

NCSantos avatar Mar 31 '18 23:03 NCSantos

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?

SirVer avatar Apr 01 '18 20:04 SirVer

In what filetype that snippet should work?

NCSantos avatar Apr 01 '18 21:04 NCSantos

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.

NCSantos avatar Apr 02 '18 08:04 NCSantos

@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.

SirVer avatar Apr 02 '18 19:04 SirVer

@SirVer For me the lorem snippet expands ok.

NCSantos avatar Apr 02 '18 20:04 NCSantos

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

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

SirVer avatar Apr 03 '18 19:04 SirVer

@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>

NCSantos avatar Apr 03 '18 21:04 NCSantos

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

SirVer avatar Apr 06 '18 07:04 SirVer

@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.

NCSantos avatar Apr 06 '18 15:04 NCSantos

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.

SirVer avatar Dec 22 '19 19:12 SirVer