Fix #398 user_data support

This commit is contained in:
Shougo Matsushita 2018-02-18 19:33:56 +09:00
parent e70864c53c
commit 7121b95c9e
3 changed files with 42 additions and 48 deletions

View File

@ -20,19 +20,12 @@ function! neosnippet#mappings#jumpable() abort
\ .neosnippet#get_sync_placeholder_marker_pattern(), 'nw') > 0
endfunction
function! neosnippet#mappings#completed_expandable() abort
if !s:enabled_completed_snippet()
if empty(get(v:, 'completed_item', {}))
return 0
endif
let snippet = neosnippet#parser#_get_completed_snippet(
\ v:completed_item, neosnippet#util#get_cur_text(),
\ neosnippet#util#get_next_text())
return snippet != ''
endfunction
function! s:enabled_completed_snippet() abort
return exists('v:completed_item')
\ && !empty(v:completed_item)
\ && g:neosnippet#enable_completed_snippet
return !empty(s:get_completed_snippets(
\ neosnippet#util#get_cur_text(), col('.')))
endfunction
function! neosnippet#mappings#_clear_select_mode_mappings() abort
@ -163,35 +156,36 @@ function! s:snippets_expand(cur_text, col) abort
return 1
endfunction
function! s:expand_completed_snippets(cur_text, col) abort
if !s:enabled_completed_snippet()
return 0
function! s:get_completed_snippets(cur_text, col) abort
if empty(get(v:, 'completed_item', {}))
return []
endif
let cur_text = a:cur_text
if !empty(get(g:, 'deoplete#_context', []))
\ && has_key(v:completed_item, 'word')
let completed_candidates = filter(copy(g:deoplete#_context),
\ "has_key(v:val, 'snippet') && has_key(v:val, 'snippet_trigger')
\ && v:val.word ==# v:completed_item.word")
if !empty(completed_candidates)
let v:completed_item.snippet = completed_candidates[0].snippet
let v:completed_item.snippet_trigger =
\ completed_candidates[0].snippet_trigger
if !empty(get(v:completed_item, 'user_data', '{}'))
let user_data = json_decode(v:completed_item.user_data)
if has_key(user_data, 'snippet')
let snippet = user_data.snippet
if has_key(user_data, 'snippet_trigger')
let cur_text = cur_text[: -1-len(user_data.snippet_trigger)]
endif
return [cur_text, snippet]
endif
endif
if g:neosnippet#enable_completed_snippet
let snippet = neosnippet#parser#_get_completed_snippet(
\ v:completed_item, cur_text, neosnippet#util#get_next_text())
if snippet == ''
return 0
endif
if has_key(v:completed_item, 'snippet_trigger')
let cur_text = cur_text[: -1-len(v:completed_item.snippet_trigger)]
if snippet != ''
return [cur_text, snippet]
endif
endif
return []
endfunction
function! s:expand_completed_snippets(cur_text, col) abort
let [cur_text, snippet] = s:get_completed_snippets(a:cur_text, a:col)
call neosnippet#view#_insert(snippet, {}, cur_text, a:col)
return 1
endfunction

View File

@ -264,10 +264,19 @@ function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name)
\ 'menu_template': abbr,
\ 'menu_abbr': abbr,
\ 'options': a:dict.options,
\ 'action__path' : a:path, 'action__line' : a:line,
\ 'action__pattern' : a:pattern, 'real_name' : a:name,
\ 'real_name': a:name,
\ 'action__path': a:path,
\ 'action__line': a:line,
\ 'action__pattern': a:pattern,
\}
if exists('*json_encode')
let snippet.user_data = json_encode({
\ 'snippet': a:dict.word,
\ 'snippet_trigger': a:dict.name
\ })
endif
if has_key(a:dict, 'regexp')
let snippet.regexp = a:dict.regexp
endif
@ -292,10 +301,6 @@ function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, nex
return ''
endif
if has_key(item, 'snippet')
return item.snippet
endif
" Set abbr
let abbrs = []
if get(item, 'info', '') =~# '^.\+('

View File

@ -125,12 +125,7 @@ function! s:suite.get_completed_snippet() abort
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'something', 'abbr' : 'something(else)',
\ 'menu' : '', 'info' : '', 'snippet' : '(${1:custom})${2}'
\ }, 'something', ''), '(${1:custom})${2}')
call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'something', 'abbr' : 'something(else)',
\ 'menu' : '', 'info' : 'func()', 'snippet' : '(${1:custom})${2}'
\ }, 'something', ''), '(${1:custom})${2}')
\ 'menu' : '', 'info' : 'func()',
\ }, 'something', ''), '(${1:#:else})${2}')
endfunction