neocomplete.vim 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "=============================================================================
  2. " FILE: neocomplete.vim
  3. " AUTHOR: Shougo Matsushita <Shougo.Matsu@gmail.com>
  4. " License: MIT license {{{
  5. " Permission is hereby granted, free of charge, to any person obtaining
  6. " a copy of this software and associated documentation files (the
  7. " "Software"), to deal in the Software without restriction, including
  8. " without limitation the rights to use, copy, modify, merge, publish,
  9. " distribute, sublicense, and/or sell copies of the Software, and to
  10. " permit persons to whom the Software is furnished to do so, subject to
  11. " the following conditions:
  12. "
  13. " The above copyright notice and this permission notice shall be included
  14. " in all copies or substantial portions of the Software.
  15. "
  16. " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. " OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. " MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. " IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. " CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. " TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. " SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. " }}}
  24. "=============================================================================
  25. let s:save_cpo = &cpo
  26. set cpo&vim
  27. function! unite#sources#neocomplete#define() abort "{{{
  28. return s:neocomplete_source
  29. endfunction "}}}
  30. " neocomplete unite source.
  31. let s:neocomplete_source = {
  32. \ 'name': 'neocomplete',
  33. \ 'hooks' : {},
  34. \ }
  35. function! s:neocomplete_source.hooks.on_init(args, context) abort "{{{
  36. if !neocomplete#is_enabled()
  37. let a:context.source__complete_pos = -1
  38. let a:context.source__candidates = []
  39. return
  40. endif
  41. " Save options.
  42. let max_list_save = g:neocomplete#max_list
  43. let max_keyword_width_save = g:neocomplete#max_keyword_width
  44. let manual_start_length = g:neocomplete#manual_completion_start_length
  45. let neocomplete = neocomplete#get_current_neocomplete()
  46. let sources_save = get(neocomplete, 'sources', {})
  47. try
  48. let g:neocomplete#max_list = -1
  49. let g:neocomplete#max_keyword_width = -1
  50. let g:neocomplete#manual_completion_start_length = 0
  51. let cur_text = neocomplete#get_cur_text(1)
  52. let sources = get(a:context, 'source__sources', [])
  53. let args = [cur_text]
  54. if !empty(sources)
  55. call add(args, neocomplete#helper#get_sources_list(sources))
  56. endif
  57. let complete_sources = call('neocomplete#complete#_get_results', args)
  58. let a:context.source__complete_pos =
  59. \ neocomplete#complete#_get_complete_pos(complete_sources)
  60. let a:context.source__candidates = neocomplete#complete#_get_words(
  61. \ complete_sources, a:context.source__complete_pos,
  62. \ cur_text[a:context.source__complete_pos :])
  63. finally
  64. " Restore options.
  65. let g:neocomplete#max_list = max_list_save
  66. let g:neocomplete#max_keyword_width = max_keyword_width_save
  67. let g:neocomplete#manual_completion_start_length = manual_start_length
  68. let neocomplete.sources = empty(sources_save) ?
  69. \ neocomplete#helper#get_sources_list() : sources_save
  70. endtry
  71. endfunction"}}}
  72. function! s:neocomplete_source.gather_candidates(args, context) abort "{{{
  73. let keyword_pos = a:context.source__complete_pos
  74. let candidates = []
  75. for keyword in a:context.source__candidates
  76. let dict = {
  77. \ 'word' : keyword.word,
  78. \ 'abbr' : printf('%-50s', get(keyword, 'abbr', keyword.word)),
  79. \ 'kind': 'completion',
  80. \ 'action__complete_word' : keyword.word,
  81. \ 'action__complete_pos' : keyword_pos,
  82. \ }
  83. if has_key(keyword, 'kind')
  84. let dict.abbr .= ' ' . keyword.kind
  85. endif
  86. if has_key(keyword, 'menu')
  87. let dict.abbr .= ' ' . keyword.menu
  88. endif
  89. if has_key(keyword, 'description')
  90. if type(keyword.description) ==# type(function('tr'))
  91. let dict.action__complete_info_lazy = keyword.description
  92. else
  93. let dict.action__complete_info = keyword.description
  94. endif
  95. endif
  96. call add(candidates, dict)
  97. endfor
  98. return candidates
  99. endfunction "}}}
  100. function! unite#sources#neocomplete#start_complete() abort "{{{
  101. return s:start_complete(0)
  102. endfunction "}}}
  103. function! unite#sources#neocomplete#start_quick_match() abort "{{{
  104. return s:start_complete(1)
  105. endfunction "}}}
  106. function! s:start_complete(is_quick_match) abort "{{{
  107. if !neocomplete#is_enabled()
  108. return ''
  109. endif
  110. if !exists(':Unite')
  111. echoerr 'unite.vim is not installed.'
  112. return ''
  113. endif
  114. let cur_text = neocomplete#get_cur_text(1)
  115. let complete_sources = neocomplete#complete#_set_results_pos(cur_text)
  116. if empty(complete_sources)
  117. return ''
  118. endif
  119. return unite#start_complete(['neocomplete'], {
  120. \ 'auto_preview' : 1, 'quick_match' : a:is_quick_match,
  121. \ 'input' : cur_text[neocomplete#complete#_get_complete_pos(
  122. \ complete_sources) :],
  123. \ })
  124. endfunction"}}}
  125. let &cpo = s:save_cpo
  126. unlet s:save_cpo
  127. " vim: foldmethod=marker