neocomplete.vim 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. let s:plugin_name = expand('<sfile>:t:r')
  2. let s:vital_base_dir = expand('<sfile>:h')
  3. let s:project_root = expand('<sfile>:h:h:h')
  4. let s:is_vital_vim = s:plugin_name is# 'vital'
  5. let s:loaded = {}
  6. let s:cache_sid = {}
  7. " function() wrapper
  8. if v:version > 703 || v:version == 703 && has('patch1170')
  9. function! s:_function(fstr) abort
  10. return function(a:fstr)
  11. endfunction
  12. else
  13. function! s:_SID() abort
  14. return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze__SID$')
  15. endfunction
  16. let s:_s = '<SNR>' . s:_SID() . '_'
  17. function! s:_function(fstr) abort
  18. return function(substitute(a:fstr, 's:', s:_s, 'g'))
  19. endfunction
  20. endif
  21. function! vital#{s:plugin_name}#new() abort
  22. return s:new(s:plugin_name)
  23. endfunction
  24. function! vital#{s:plugin_name}#import(...) abort
  25. if !exists('s:V')
  26. let s:V = s:new(s:plugin_name)
  27. endif
  28. return call(s:V.import, a:000, s:V)
  29. endfunction
  30. let s:Vital = {}
  31. function! s:new(plugin_name) abort
  32. let base = deepcopy(s:Vital)
  33. let base._plugin_name = a:plugin_name
  34. return base
  35. endfunction
  36. function! s:vital_files() abort
  37. if !exists('s:vital_files')
  38. let s:vital_files = map(
  39. \ s:is_vital_vim ? s:_global_vital_files() : s:_self_vital_files(),
  40. \ 'fnamemodify(v:val, ":p:gs?[\\\\/]?/?")')
  41. endif
  42. return copy(s:vital_files)
  43. endfunction
  44. let s:Vital.vital_files = s:_function('s:vital_files')
  45. function! s:import(name, ...) abort dict
  46. let target = {}
  47. let functions = []
  48. for a in a:000
  49. if type(a) == type({})
  50. let target = a
  51. elseif type(a) == type([])
  52. let functions = a
  53. endif
  54. unlet a
  55. endfor
  56. let module = self._import(a:name)
  57. if empty(functions)
  58. call extend(target, module, 'keep')
  59. else
  60. for f in functions
  61. if has_key(module, f) && !has_key(target, f)
  62. let target[f] = module[f]
  63. endif
  64. endfor
  65. endif
  66. return target
  67. endfunction
  68. let s:Vital.import = s:_function('s:import')
  69. function! s:load(...) abort dict
  70. for arg in a:000
  71. let [name; as] = type(arg) == type([]) ? arg[: 1] : [arg, arg]
  72. let target = split(join(as, ''), '\W\+')
  73. let dict = self
  74. let dict_type = type({})
  75. while !empty(target)
  76. let ns = remove(target, 0)
  77. if !has_key(dict, ns)
  78. let dict[ns] = {}
  79. endif
  80. if type(dict[ns]) == dict_type
  81. let dict = dict[ns]
  82. else
  83. unlet dict
  84. break
  85. endif
  86. endwhile
  87. if exists('dict')
  88. call extend(dict, self._import(name))
  89. endif
  90. unlet arg
  91. endfor
  92. return self
  93. endfunction
  94. let s:Vital.load = s:_function('s:load')
  95. function! s:unload() abort dict
  96. let s:loaded = {}
  97. let s:cache_sid = {}
  98. unlet! s:vital_files
  99. endfunction
  100. let s:Vital.unload = s:_function('s:unload')
  101. function! s:exists(name) abort dict
  102. if a:name !~# '\v^\u\w*%(\.\u\w*)*$'
  103. throw 'vital: Invalid module name: ' . a:name
  104. endif
  105. return s:_module_path(a:name) isnot# ''
  106. endfunction
  107. let s:Vital.exists = s:_function('s:exists')
  108. function! s:search(pattern) abort dict
  109. let paths = s:_extract_files(a:pattern, self.vital_files())
  110. let modules = sort(map(paths, 's:_file2module(v:val)'))
  111. return s:_uniq(modules)
  112. endfunction
  113. let s:Vital.search = s:_function('s:search')
  114. function! s:plugin_name() abort dict
  115. return self._plugin_name
  116. endfunction
  117. let s:Vital.plugin_name = s:_function('s:plugin_name')
  118. function! s:_self_vital_files() abort
  119. let builtin = printf('%s/__%s__/', s:vital_base_dir, s:plugin_name)
  120. let installed = printf('%s/_%s/', s:vital_base_dir, s:plugin_name)
  121. let base = builtin . ',' . installed
  122. return split(globpath(base, '**/*.vim', 1), "\n")
  123. endfunction
  124. function! s:_global_vital_files() abort
  125. let pattern = 'autoload/vital/__*__/**/*.vim'
  126. return split(globpath(&runtimepath, pattern, 1), "\n")
  127. endfunction
  128. function! s:_extract_files(pattern, files) abort
  129. let tr = {'.': '/', '*': '[^/]*', '**': '.*'}
  130. let target = substitute(a:pattern, '\.\|\*\*\?', '\=tr[submatch(0)]', 'g')
  131. let regexp = printf('autoload/vital/[^/]\+/%s.vim$', target)
  132. return filter(a:files, 'v:val =~# regexp')
  133. endfunction
  134. function! s:_file2module(file) abort
  135. let filename = fnamemodify(a:file, ':p:gs?[\\/]?/?')
  136. let tail = matchstr(filename, 'autoload/vital/_\w\+/\zs.*\ze\.vim$')
  137. return join(split(tail, '[\\/]\+'), '.')
  138. endfunction
  139. " @param {string} name e.g. Data.List
  140. function! s:_import(name) abort dict
  141. if has_key(s:loaded, a:name)
  142. return copy(s:loaded[a:name])
  143. endif
  144. let module = self._get_module(a:name)
  145. if has_key(module, '_vital_created')
  146. call module._vital_created(module)
  147. endif
  148. let export_module = filter(copy(module), 'v:key =~# "^\\a"')
  149. " Cache module before calling module.vital_loaded() to avoid cyclic
  150. " dependences but remove the cache if module._vital_loaded() fails.
  151. " let s:loaded[a:name] = export_module
  152. let s:loaded[a:name] = export_module
  153. if has_key(module, '_vital_loaded')
  154. try
  155. call module._vital_loaded(vital#{s:plugin_name}#new())
  156. catch
  157. unlet s:loaded[a:name]
  158. throw 'vital: fail to call ._vital_loaded(): ' . v:exception
  159. endtry
  160. endif
  161. return copy(s:loaded[a:name])
  162. endfunction
  163. let s:Vital._import = s:_function('s:_import')
  164. " s:_get_module() returns module object wihch has all script local functions.
  165. function! s:_get_module(name) abort dict
  166. let funcname = s:_import_func_name(self.plugin_name(), a:name)
  167. if s:_exists_autoload_func_with_source(funcname)
  168. return call(funcname, [])
  169. else
  170. return s:_get_builtin_module(a:name)
  171. endif
  172. endfunction
  173. function! s:_get_builtin_module(name) abort
  174. return s:sid2sfuncs(s:_module_sid(a:name))
  175. endfunction
  176. if s:is_vital_vim
  177. " For vital.vim, we can use s:_get_builtin_module directly
  178. let s:Vital._get_module = s:_function('s:_get_builtin_module')
  179. else
  180. let s:Vital._get_module = s:_function('s:_get_module')
  181. endif
  182. function! s:_import_func_name(plugin_name, module_name) abort
  183. return printf('vital#_%s#%s#import', a:plugin_name, s:_dot_to_sharp(a:module_name))
  184. endfunction
  185. function! s:_module_sid(name) abort
  186. let path = s:_module_path(a:name)
  187. if !filereadable(path)
  188. throw 'vital: module not found: ' . a:name
  189. endif
  190. let vital_dir = s:is_vital_vim ? '__\w\+__' : printf('_\{1,2}%s\%%(__\)\?', s:plugin_name)
  191. let base = join([vital_dir, ''], '[/\\]\+')
  192. let p = base . substitute('' . a:name, '\.', '[/\\\\]\\+', 'g')
  193. let sid = s:_sid(path, p)
  194. if !sid
  195. call s:_source(path)
  196. let sid = s:_sid(path, p)
  197. if !sid
  198. throw printf('vital: cannot get <SID> from path: %s', path)
  199. endif
  200. endif
  201. return sid
  202. endfunction
  203. function! s:_module_path(name) abort
  204. return get(s:_extract_files(a:name, s:vital_files()), 0, '')
  205. endfunction
  206. function! s:_module_sid_base_dir() abort
  207. return s:is_vital_vim ? &rtp : s:project_root
  208. endfunction
  209. function! s:_dot_to_sharp(name) abort
  210. return substitute(a:name, '\.', '#', 'g')
  211. endfunction
  212. " It will sources autoload file if a given func is not already defined.
  213. function! s:_exists_autoload_func_with_source(funcname) abort
  214. if exists('*' . a:funcname)
  215. " Return true if a given func is already defined
  216. return 1
  217. endif
  218. " source a file which may include a given func definition and try again.
  219. let path = 'autoload/' . substitute(substitute(a:funcname, '#[^#]*$', '.vim', ''), '#', '/', 'g')
  220. call s:_runtime(path)
  221. return exists('*' . a:funcname)
  222. endfunction
  223. function! s:_runtime(path) abort
  224. execute 'runtime' fnameescape(a:path)
  225. endfunction
  226. function! s:_source(path) abort
  227. execute 'source' fnameescape(a:path)
  228. endfunction
  229. " @vimlint(EVL102, 1, l:_)
  230. " @vimlint(EVL102, 1, l:__)
  231. function! s:_sid(path, filter_pattern) abort
  232. let unified_path = s:_unify_path(a:path)
  233. if has_key(s:cache_sid, unified_path)
  234. return s:cache_sid[unified_path]
  235. endif
  236. for line in filter(split(s:_redir(':scriptnames'), "\n"), 'v:val =~# a:filter_pattern')
  237. let [_, sid, path; __] = matchlist(line, '^\s*\(\d\+\):\s\+\(.\+\)\s*$')
  238. if s:_unify_path(path) is# unified_path
  239. let s:cache_sid[unified_path] = sid
  240. return s:cache_sid[unified_path]
  241. endif
  242. endfor
  243. return 0
  244. endfunction
  245. function! s:_redir(cmd) abort
  246. let [save_verbose, save_verbosefile] = [&verbose, &verbosefile]
  247. set verbose=0 verbosefile=
  248. redir => res
  249. silent! execute a:cmd
  250. redir END
  251. let [&verbose, &verbosefile] = [save_verbose, save_verbosefile]
  252. return res
  253. endfunction
  254. if filereadable(expand('<sfile>:r') . '.VIM') " is case-insensitive or not
  255. let s:_unify_path_cache = {}
  256. " resolve() is slow, so we cache results.
  257. " Note: On windows, vim can't expand path names from 8.3 formats.
  258. " So if getting full path via <sfile> and $HOME was set as 8.3 format,
  259. " vital load duplicated scripts. Below's :~ avoid this issue.
  260. function! s:_unify_path(path) abort
  261. if has_key(s:_unify_path_cache, a:path)
  262. return s:_unify_path_cache[a:path]
  263. endif
  264. let value = tolower(fnamemodify(resolve(fnamemodify(
  265. \ a:path, ':p')), ':~:gs?[\\/]?/?'))
  266. let s:_unify_path_cache[a:path] = value
  267. return value
  268. endfunction
  269. else
  270. function! s:_unify_path(path) abort
  271. return resolve(fnamemodify(a:path, ':p:gs?[\\/]?/?'))
  272. endfunction
  273. endif
  274. " copied and modified from Vim.ScriptLocal
  275. let s:SNR = join(map(range(len("\<SNR>")), '"[\\x" . printf("%0x", char2nr("\<SNR>"[v:val])) . "]"'), '')
  276. function! s:sid2sfuncs(sid) abort
  277. let fs = split(s:_redir(printf(':function /^%s%s_', s:SNR, a:sid)), "\n")
  278. let r = {}
  279. let pattern = printf('\m^function\s<SNR>%d_\zs\w\{-}\ze(', a:sid)
  280. for fname in map(fs, 'matchstr(v:val, pattern)')
  281. let r[fname] = function(s:_sfuncname(a:sid, fname))
  282. endfor
  283. return r
  284. endfunction
  285. "" Return funcname of script local functions with SID
  286. function! s:_sfuncname(sid, funcname) abort
  287. return printf('<SNR>%s_%s', a:sid, a:funcname)
  288. endfunction
  289. if exists('*uniq')
  290. function! s:_uniq(list) abort
  291. return uniq(a:list)
  292. endfunction
  293. else
  294. function! s:_uniq(list) abort
  295. let i = len(a:list) - 1
  296. while 0 < i
  297. if a:list[i] ==# a:list[i - 1]
  298. call remove(a:list, i)
  299. endif
  300. let i -= 1
  301. endwhile
  302. return a:list
  303. endfunction
  304. endif