tag.vim 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "=============================================================================
  2. " FILE: tag.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. " Important variables.
  28. if !exists('s:tags_list')
  29. let s:tags_list = {}
  30. let s:async_tags_list = {}
  31. endif
  32. let s:source = {
  33. \ 'name' : 'tag',
  34. \ 'kind' : 'keyword',
  35. \ 'mark' : '[T]',
  36. \ 'hooks' : {},
  37. \}
  38. function! s:source.hooks.on_init(context) abort "{{{
  39. let g:neocomplete#sources#tags#cache_limit_size =
  40. \ get(g:, 'neocomplete#sources#tags#cache_limit_size', 500000)
  41. augroup neocomplete "{{{
  42. autocmd BufWritePost * call neocomplete#sources#tag#make_cache(0)
  43. augroup END"}}}
  44. " Create cache directory.
  45. call neocomplete#cache#make_directory('tags_cache')
  46. endfunction"}}}
  47. function! s:source.hooks.on_final(context) abort "{{{
  48. silent! delcommand NeoCompleteTagMakeCache
  49. endfunction"}}}
  50. function! neocomplete#sources#tag#define() abort "{{{
  51. return s:source
  52. endfunction"}}}
  53. function! s:source.gather_candidates(context) abort "{{{
  54. if !has_key(s:async_tags_list, bufnr('%'))
  55. \ && !has_key(s:tags_list, bufnr('%'))
  56. call neocomplete#sources#tag#make_cache(0)
  57. endif
  58. if neocomplete#within_comment()
  59. return []
  60. endif
  61. call neocomplete#cache#check_cache(
  62. \ 'tags_cache', bufnr('%'), s:async_tags_list, s:tags_list, 0)
  63. return copy(get(s:tags_list, bufnr('%'), []))
  64. endfunction"}}}
  65. function! s:initialize_tags(filename) abort "{{{
  66. " Initialize tags list.
  67. let ft = &filetype
  68. if ft == ''
  69. let ft = 'nothing'
  70. endif
  71. return {
  72. \ 'filename' : a:filename,
  73. \ 'cachename' : neocomplete#cache#async_load_from_tags(
  74. \ 'tags_cache', a:filename,
  75. \ neocomplete#get_keyword_pattern(ft, s:source.name),
  76. \ ft, s:source.mark)
  77. \ }
  78. endfunction"}}}
  79. function! neocomplete#sources#tag#make_cache(force) abort "{{{
  80. if !neocomplete#is_enabled()
  81. call neocomplete#initialize()
  82. endif
  83. let bufnumber = bufnr('%')
  84. let s:async_tags_list[bufnumber] = []
  85. let tagfiles = tagfiles()
  86. if get(g:, 'loaded_neoinclude', 0)
  87. let tagfiles += neoinclude#include#get_tag_files()
  88. endif
  89. for tags in map(filter(tagfiles, 'getfsize(v:val) > 0'),
  90. \ "neocomplete#util#substitute_path_separator(
  91. \ fnamemodify(v:val, ':p'))")
  92. if tags !~? '/doc/tags\%(-\w\+\)\?$' &&
  93. \ (a:force || getfsize(tags)
  94. \ < g:neocomplete#sources#tags#cache_limit_size)
  95. call add(s:async_tags_list[bufnumber],
  96. \ s:initialize_tags(tags))
  97. endif
  98. endfor
  99. endfunction"}}}
  100. let &cpo = s:save_cpo
  101. unlet s:save_cpo
  102. " vim: foldmethod=marker