Skip to content

Commit

Permalink
Add nvim-lsp support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shougo committed Jun 15, 2020
1 parent a52edf7 commit f19da70
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
32 changes: 32 additions & 0 deletions autoload/neosnippet/helpers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,35 @@ function! neosnippet#helpers#get_snippet_files(filetype) abort
endfor
return s:get_list().uniq(snippet_files)
endfunction

function! neosnippet#helpers#get_user_data(completed_item) abort
let user_data = {}
if type(a:completed_item.user_data) ==# v:t_dict
let user_data = a:completed_item.user_data
else
silent! let user_data = json_decode(a:completed_item.user_data)
endif
if type(user_data) !=# v:t_dict || empty(user_data)
return {}
endif

return user_data
endfunction
function! neosnippet#helpers#get_lspitem(user_data) abort
if has_key(a:user_data, 'lspitem') && type(a:user_data.lspitem) == v:t_dict
" For vim-lsp
let lspitem = a:user_data.lspitem
elseif has_key(a:user_data, 'nvim')
\ && type(a:user_data.nvim) == v:t_dict
\ && has_key(a:user_data.nvim, 'lsp')
\ && type(a:user_data.nvim.lsp) == v:t_dict
\ && has_key(a:user_data.nvim.lsp, 'completion_item')
\ && type(a:user_data.nvim.lsp.completion_item) == v:t_dict
" For nvim-lsp
let lspitem = a:user_data.nvim.lsp.completion_item
else
let lspitem = {}
endif

return lspitem
endfunction
20 changes: 13 additions & 7 deletions autoload/neosnippet/mappings.vim
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ function! s:get_completed_snippets(cur_text, col) abort
return []
endif

let user_data = get(v:completed_item, 'user_data', '')
if user_data !=# ''
if has_key(v:completed_item, 'user_data')
let ret = s:get_user_data(a:cur_text)
if !empty(ret)
return [ret[0], ret[1], ret[2]]
Expand All @@ -174,23 +173,30 @@ function! s:get_completed_snippets(cur_text, col) abort
return []
endfunction
function! s:get_user_data(cur_text) abort
let user_data = {}
silent! let user_data = json_decode(v:completed_item.user_data)
let user_data = neosnippet#helpers#get_user_data(v:completed_item)
if type(v:completed_item.user_data) ==# v:t_dict
let user_data = v:completed_item.user_data
else
silent! let user_data = json_decode(v:completed_item.user_data)
endif
if type(user_data) !=# v:t_dict || empty(user_data)
return []
endif

let cur_text = a:cur_text
let has_lspitem = has_key(user_data, 'lspitem')
let snippet = ''
let snippet_trigger = v:completed_item.word

if has_lspitem && type(user_data.lspitem) == v:t_dict
let lspitem = user_data.lspitem
let lspitem = neosnippet#helpers#get_lspitem(user_data)
let has_lspitem = v:false

if !empty(lspitem)
if has_key(lspitem, 'textEdit') && type(lspitem.textEdit) == v:t_dict
let snippet = lspitem.textEdit.newText
let has_lspitem = v:true
elseif get(lspitem, 'insertTextFormat', -1) == 2
let snippet = lspitem.insertText
let has_lspitem = v:true
endif
elseif get(user_data, 'snippet', '') !=# ''
let snippet = user_data.snippet
Expand Down
14 changes: 5 additions & 9 deletions autoload/neosnippet/parser.vim
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,11 @@ function! neosnippet#parser#_get_completed_snippet(completed_item, cur_text, nex
call add(abbrs, item.word)
endif

if type(get(item, 'user_data', 0)) ==# v:t_string && item.user_data !=# ''
let user_data = {}
silent! let user_data = json_decode(item.user_data)
if type(user_data) ==# v:t_dict && has_key(user_data, 'lspitem')
" Use lspitem userdata
let lspitem = user_data.lspitem
if has_key(lspitem, 'label')
call add(abbrs, lspitem.label)
endif
let user_data = neosnippet#helpers#get_user_data(v:completed_item)
if !empty(user_data)
let lspitem = neosnippet#helpers#get_lspitem(user_data)
if has_key(lspitem, 'label')
call add(abbrs, lspitem.label)
endif
endif

Expand Down

0 comments on commit f19da70

Please sign in to comment.