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

View File

@ -259,15 +259,24 @@ function! neosnippet#parser#_initialize_snippet(dict, path, line, pattern, name)
endif endif
let snippet = { let snippet = {
\ 'word' : a:dict.name, 'snip' : a:dict.word, \ 'word': a:dict.name, 'snip': a:dict.word,
\ 'description' : a:dict.word, \ 'description': a:dict.word,
\ 'menu_template' : abbr, \ 'menu_template': abbr,
\ 'menu_abbr' : abbr, \ 'menu_abbr': abbr,
\ 'options' : a:dict.options, \ 'options': a:dict.options,
\ 'action__path' : a:path, 'action__line' : a:line, \ 'real_name': a:name,
\ 'action__pattern' : a:pattern, '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') if has_key(a:dict, 'regexp')
let snippet.regexp = a:dict.regexp let snippet.regexp = a:dict.regexp
endif endif
@ -292,10 +301,6 @@ function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, nex
return '' return ''
endif endif
if has_key(item, 'snippet')
return item.snippet
endif
" Set abbr " Set abbr
let abbrs = [] let abbrs = []
if get(item, 'info', '') =~# '^.\+(' 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({ call s:assert.equals(neosnippet#parser#_get_completed_snippet({
\ 'word' : 'something', 'abbr' : 'something(else)', \ 'word' : 'something', 'abbr' : 'something(else)',
\ 'menu' : '', 'info' : '', 'snippet' : '(${1:custom})${2}' \ 'menu' : '', 'info' : 'func()',
\ }, 'something', ''), '(${1:custom})${2}') \ }, 'something', ''), '(${1:#:else})${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}')
endfunction endfunction