You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

73 lines
2.1 KiB

  1. "=============================================================================
  2. " FILE: snippets_complete.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
  4. " License: MIT license
  5. "=============================================================================
  6. let s:source = {
  7. \ 'name' : 'snippets_complete',
  8. \ 'kind' : 'complfunc',
  9. \ 'min_pattern_length' :
  10. \ g:neocomplcache_auto_completion_start_length,
  11. \}
  12. function! s:source.initialize() abort
  13. " Initialize.
  14. call neocomplcache#set_dictionary_helper(
  15. \ g:neocomplcache_source_rank, 'snippets_complete', 8)
  16. call neocomplcache#set_completion_length('snippets_complete',
  17. \ g:neocomplcache_auto_completion_start_length)
  18. endfunction
  19. function! s:source.get_keyword_pos(cur_text) abort
  20. let cur_word = matchstr(a:cur_text, '\w\+$')
  21. let word_candidates = neocomplcache#keyword_filter(
  22. \ filter(values(neosnippet#helpers#get_snippets()),
  23. \ 'v:val.options.word'), cur_word)
  24. if !empty(word_candidates)
  25. return match(a:cur_text, '\w\+$')
  26. endif
  27. return match(a:cur_text, '\S\+$')
  28. endfunction
  29. function! s:source.get_complete_words(cur_keyword_pos, cur_keyword_str) abort
  30. let list = s:keyword_filter(neosnippet#helpers#get_snippets(), a:cur_keyword_str)
  31. for snippet in list
  32. let snippet.dup = 1
  33. let snippet.menu = neosnippet#util#strwidthpart(
  34. \ snippet.menu_template, winwidth(0)/3)
  35. endfor
  36. return list
  37. endfunction
  38. function! s:keyword_filter(snippets, cur_keyword_str) abort
  39. " Uniq by real_name.
  40. let dict = {}
  41. " Use default filter.
  42. let list = neocomplcache#keyword_filter(
  43. \ values(a:snippets), a:cur_keyword_str)
  44. " Add cur_keyword_str snippet.
  45. if has_key(a:snippets, a:cur_keyword_str)
  46. call add(list, a:snippets[a:cur_keyword_str])
  47. endif
  48. for snippet in neocomplcache#dup_filter(list)
  49. if !has_key(dict, snippet.real_name) ||
  50. \ len(dict[snippet.real_name].word) > len(snippet.word)
  51. let dict[snippet.real_name] = snippet
  52. endif
  53. endfor
  54. return values(dict)
  55. endfunction
  56. function! neocomplcache#sources#snippets_complete#define() abort
  57. return s:source
  58. endfunction