youcompleteme.vim 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. " Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
  2. "
  3. " This file is part of YouCompleteMe.
  4. "
  5. " YouCompleteMe is free software: you can redistribute it and/or modify
  6. " it under the terms of the GNU General Public License as published by
  7. " the Free Software Foundation, either version 3 of the License, or
  8. " (at your option) any later version.
  9. "
  10. " YouCompleteMe is distributed in the hope that it will be useful,
  11. " but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. " GNU General Public License for more details.
  14. "
  15. " You should have received a copy of the GNU General Public License
  16. " along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  17. " This is basic vim plugin boilerplate
  18. let s:save_cpo = &cpo
  19. set cpo&vim
  20. " This needs to be called outside of a function
  21. let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
  22. let s:searched_and_results_found = 0
  23. let s:should_use_filetype_completion = 0
  24. let s:completion_start_column = 0
  25. let s:omnifunc_mode = 0
  26. let s:old_cursor_position = []
  27. let s:cursor_moved = 0
  28. let s:moved_vertically_in_insert_mode = 0
  29. let s:previous_num_chars_on_current_line = -1
  30. function! youcompleteme#Enable()
  31. " When vim is in diff mode, don't run
  32. if &diff
  33. return
  34. endif
  35. py import sys
  36. py import vim
  37. exe 'python sys.path.insert( 0, "' . s:script_folder_path . '/../python" )'
  38. py from ycm import extra_conf_store
  39. py extra_conf_store.CallExtraConfYcmCorePreloadIfExists()
  40. py from ycm import base
  41. if !pyeval( 'base.CompatibleWithYcmCore()')
  42. echohl WarningMsg |
  43. \ echomsg "YouCompleteMe unavailable: ycm_core too old, PLEASE RECOMPILE ycm_core" |
  44. \ echohl None
  45. return
  46. endif
  47. py from ycm.youcompleteme import YouCompleteMe
  48. py ycm_state = YouCompleteMe()
  49. augroup youcompleteme
  50. autocmd!
  51. autocmd CursorMovedI * call s:OnCursorMovedInsertMode()
  52. autocmd CursorMoved * call s:OnCursorMovedNormalMode()
  53. " Note that these events will NOT trigger for the file vim is started with;
  54. " so if you do "vim foo.cc", these events will not trigger when that buffer
  55. " is read. This is because youcompleteme#Enable() is called on VimEnter and
  56. " that happens *after" BufRead/BufEnter has already triggered for the
  57. " initial file.
  58. autocmd BufRead,BufEnter * call s:OnBufferVisit()
  59. autocmd BufUnload * call s:OnBufferUnload( expand( '<afile>:p' ) )
  60. autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
  61. autocmd InsertLeave * call s:OnInsertLeave()
  62. autocmd InsertEnter * call s:OnInsertEnter()
  63. autocmd VimLeave * call s:OnVimLeave()
  64. augroup END
  65. call s:SetUpCpoptions()
  66. call s:SetUpCompleteopt()
  67. call s:SetUpKeyMappings()
  68. call s:SetUpBackwardsCompatibility()
  69. if g:ycm_register_as_syntastic_checker
  70. call s:ForceSyntasticCFamilyChecker()
  71. endif
  72. if g:ycm_allow_changing_updatetime
  73. set ut=2000
  74. endif
  75. " Calling this once solves the problem of BufRead/BufEnter not triggering for
  76. " the first loaded file. This should be the last command executed in this
  77. " function!
  78. call s:OnBufferVisit()
  79. endfunction
  80. function! s:SetUpKeyMappings()
  81. " The g:ycm_key_select_completion and g:ycm_key_previous_completion used to
  82. " exist and are now here purely for the sake of backwards compatibility; we
  83. " don't want to break users if we can avoid it.
  84. if exists('g:ycm_key_select_completion') &&
  85. \ index(g:ycm_key_list_select_completion,
  86. \ g:ycm_key_select_completion) == -1
  87. call add(g:ycm_key_list_select_completion, g:ycm_key_select_completion)
  88. endif
  89. if exists('g:ycm_key_previous_completion') &&
  90. \ index(g:ycm_key_list_previous_completion,
  91. \ g:ycm_key_previous_completion) == -1
  92. call add(g:ycm_key_list_previous_completion, g:ycm_key_previous_completion)
  93. endif
  94. for key in g:ycm_key_list_select_completion
  95. " With this command, when the completion window is visible, the tab key
  96. " (default) will select the next candidate in the window. In vim, this also
  97. " changes the typed-in text to that of the candidate completion.
  98. exe 'inoremap <expr>' . key .
  99. \ ' pumvisible() ? "\<C-n>" : "\' . key .'"'
  100. endfor
  101. for key in g:ycm_key_list_previous_completion
  102. " This selects the previous candidate for shift-tab (default)
  103. exe 'inoremap <expr>' . key .
  104. \ ' pumvisible() ? "\<C-p>" : "\' . key .'"'
  105. endfor
  106. if !empty( g:ycm_key_invoke_completion )
  107. let invoke_key = g:ycm_key_invoke_completion
  108. " Inside the console, <C-Space> is passed as <Nul> to Vim
  109. if invoke_key ==# '<C-Space>' && !has('gui_running')
  110. let invoke_key = '<Nul>'
  111. endif
  112. " <c-x><c-o> trigger omni completion, <c-p> deselects the first completion
  113. " candidate that vim selects by default
  114. silent! exe 'inoremap <unique> ' . invoke_key . ' <C-X><C-O><C-P>'
  115. endif
  116. if !empty( g:ycm_key_detailed_diagnostics )
  117. silent! exe 'nnoremap <unique> ' . g:ycm_key_detailed_diagnostics .
  118. \ ' :YcmShowDetailedDiagnostic<cr>'
  119. endif
  120. endfunction
  121. function! s:SetUpBackwardsCompatibility()
  122. let complete_in_comments_and_strings =
  123. \ get( g:, 'ycm_complete_in_comments_and_strings', 0 )
  124. if complete_in_comments_and_strings
  125. let g:ycm_complete_in_strings = 1
  126. let g:ycm_complete_in_comments = 1
  127. endif
  128. endfunction
  129. function! s:ForceSyntasticCFamilyChecker()
  130. " Needed so that YCM is used as the syntastic checker
  131. let g:syntastic_cpp_checkers = ['ycm']
  132. let g:syntastic_c_checkers = ['ycm']
  133. let g:syntastic_objc_checkers = ['ycm']
  134. let g:syntastic_objcpp_checkers = ['ycm']
  135. endfunction
  136. function! s:AllowedToCompleteInCurrentFile()
  137. if empty( &filetype ) || getbufvar(winbufnr(winnr()), "&buftype") ==# 'nofile'
  138. return 0
  139. endif
  140. let whitelist_allows = has_key( g:ycm_filetype_whitelist, '*' ) ||
  141. \ has_key( g:ycm_filetype_whitelist, &filetype )
  142. let blacklist_allows = !has_key( g:ycm_filetype_blacklist, &filetype )
  143. return whitelist_allows && blacklist_allows
  144. endfunction
  145. function! s:SetUpCpoptions()
  146. " Without this flag in cpoptions, critical YCM mappings do not work. There's
  147. " no way to not have this and have YCM working, so force the flag.
  148. set cpoptions+=B
  149. endfunction
  150. function! s:SetUpCompleteopt()
  151. " Some plugins (I'm looking at you, vim-notes) change completeopt by for
  152. " instance adding 'longest'. This breaks YCM. So we force our settings.
  153. " There's no two ways about this: if you want to use YCM then you have to
  154. " have these completeopt settings, otherwise YCM won't work at all.
  155. " We need menuone in completeopt, otherwise when there's only one candidate
  156. " for completion, the menu doesn't show up.
  157. set completeopt-=menu
  158. set completeopt+=menuone
  159. " This is unnecessary with our features. People use this option to insert
  160. " the common prefix of all the matches and then add more differentiating chars
  161. " so that they can select a more specific match. With our features, they
  162. " don't need to insert the prefix; they just type the differentiating chars.
  163. " Also, having this option set breaks the plugin.
  164. set completeopt-=longest
  165. if g:ycm_add_preview_to_completeopt
  166. set completeopt+=preview
  167. endif
  168. endfunction
  169. " For various functions/use-cases, we want to keep track of whether the buffer
  170. " has changed since the last time they were invoked. We keep the state of
  171. " b:changedtick of the last time the specific function was called in
  172. " b:ycm_changedtick.
  173. function! s:SetUpYcmChangedTick()
  174. let b:ycm_changedtick =
  175. \ get( b:, 'ycm_changedtick', {
  176. \ 'file_ready_to_parse' : -1,
  177. \ } )
  178. endfunction
  179. function! s:OnVimLeave()
  180. py ycm_state.OnVimLeave()
  181. py extra_conf_store.CallExtraConfVimCloseIfExists()
  182. endfunction
  183. function! s:OnBufferVisit()
  184. " We need to do this even when we are not allowed to complete in the current
  185. " file because we might be allowed to complete in the future! The canonical
  186. " example is creating a new buffer with :enew and then setting a filetype.
  187. call s:SetUpYcmChangedTick()
  188. if !s:AllowedToCompleteInCurrentFile()
  189. return
  190. endif
  191. call s:SetUpCompleteopt()
  192. call s:SetCompleteFunc()
  193. py ycm_state.OnBufferVisit()
  194. call s:OnFileReadyToParse()
  195. endfunction
  196. function! s:OnBufferUnload( deleted_buffer_file )
  197. if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
  198. return
  199. endif
  200. py ycm_state.OnBufferUnload( vim.eval( 'a:deleted_buffer_file' ) )
  201. endfunction
  202. function! s:OnCursorHold()
  203. if !s:AllowedToCompleteInCurrentFile()
  204. return
  205. endif
  206. call s:SetUpCompleteopt()
  207. " Order is important here; we need to extract any done diagnostics before
  208. " reparsing the file again
  209. call s:UpdateDiagnosticNotifications()
  210. call s:OnFileReadyToParse()
  211. endfunction
  212. function! s:OnFileReadyToParse()
  213. " We need to call this just in case there is no b:ycm_changetick; this can
  214. " happen for special buffers.
  215. call s:SetUpYcmChangedTick()
  216. let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse
  217. if buffer_changed
  218. py ycm_state.OnFileReadyToParse()
  219. endif
  220. let b:ycm_changedtick.file_ready_to_parse = b:changedtick
  221. endfunction
  222. function! s:SetCompleteFunc()
  223. let &completefunc = 'youcompleteme#Complete'
  224. let &l:completefunc = 'youcompleteme#Complete'
  225. if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
  226. let &omnifunc = 'youcompleteme#OmniComplete'
  227. let &l:omnifunc = 'youcompleteme#OmniComplete'
  228. " If we don't have native filetype support but the omnifunc is set to YCM's
  229. " omnifunc because the previous file the user was editing DID have native
  230. " support, we remove our omnifunc.
  231. elseif &omnifunc == 'youcompleteme#OmniComplete'
  232. let &omnifunc = ''
  233. let &l:omnifunc = ''
  234. endif
  235. endfunction
  236. function! s:OnCursorMovedInsertMode()
  237. if !s:AllowedToCompleteInCurrentFile()
  238. return
  239. endif
  240. call s:UpdateCursorMoved()
  241. " Basically, we need to only trigger the completion menu when the user has
  242. " inserted or deleted a character, NOT just when the user moves in insert mode
  243. " (with, say, the arrow keys). If we trigger the menu even on pure moves, then
  244. " it's impossible to move in insert mode since the up/down arrows start moving
  245. " the selected completion in the completion menu. Yeah, people shouldn't be
  246. " moving in insert mode at all (that's what normal mode is for) but explain
  247. " that to the users who complain...
  248. if !s:BufferTextChangedSinceLastMoveInInsertMode()
  249. return
  250. endif
  251. call s:IdentifierFinishedOperations()
  252. if g:ycm_autoclose_preview_window_after_completion
  253. call s:ClosePreviewWindowIfNeeded()
  254. endif
  255. call s:InvokeCompletion()
  256. endfunction
  257. function! s:OnCursorMovedNormalMode()
  258. if !s:AllowedToCompleteInCurrentFile()
  259. return
  260. endif
  261. call s:UpdateDiagnosticNotifications()
  262. call s:OnFileReadyToParse()
  263. endfunction
  264. function! s:OnInsertLeave()
  265. if !s:AllowedToCompleteInCurrentFile()
  266. return
  267. endif
  268. let s:omnifunc_mode = 0
  269. call s:UpdateDiagnosticNotifications()
  270. call s:OnFileReadyToParse()
  271. py ycm_state.OnInsertLeave()
  272. if g:ycm_autoclose_preview_window_after_completion ||
  273. \ g:ycm_autoclose_preview_window_after_insertion
  274. call s:ClosePreviewWindowIfNeeded()
  275. endif
  276. endfunction
  277. function! s:OnInsertEnter()
  278. if !s:AllowedToCompleteInCurrentFile()
  279. return
  280. endif
  281. let s:old_cursor_position = []
  282. endfunction
  283. function! s:UpdateCursorMoved()
  284. let current_position = getpos('.')
  285. let s:cursor_moved = current_position != s:old_cursor_position
  286. let s:moved_vertically_in_insert_mode = s:old_cursor_position != [] &&
  287. \ current_position[ 1 ] != s:old_cursor_position[ 1 ]
  288. let s:old_cursor_position = current_position
  289. endfunction
  290. function! s:BufferTextChangedSinceLastMoveInInsertMode()
  291. if s:moved_vertically_in_insert_mode
  292. let s:previous_num_chars_on_current_line = -1
  293. return 0
  294. endif
  295. let num_chars_in_current_cursor_line = strlen( getline('.') )
  296. if s:previous_num_chars_on_current_line == -1
  297. let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
  298. return 0
  299. endif
  300. let changed_text_on_current_line = num_chars_in_current_cursor_line !=
  301. \ s:previous_num_chars_on_current_line
  302. let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
  303. return changed_text_on_current_line
  304. endfunction
  305. function! s:ClosePreviewWindowIfNeeded()
  306. let current_buffer_name = bufname('')
  307. " We don't want to try to close the preview window in special buffers like
  308. " "[Command Line]"; if we do, Vim goes bonkers. Special buffers always start
  309. " with '['.
  310. if current_buffer_name[ 0 ] == '['
  311. return
  312. endif
  313. if s:searched_and_results_found
  314. " This command does the actual closing of the preview window. If no preview
  315. " window is shown, nothing happens.
  316. pclose
  317. endif
  318. endfunction
  319. function! s:UpdateDiagnosticNotifications()
  320. if get( g:, 'loaded_syntastic_plugin', 0 ) &&
  321. \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) &&
  322. \ pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' ) &&
  323. \ g:ycm_register_as_syntastic_checker
  324. SyntasticCheck
  325. endif
  326. endfunction
  327. function! s:IdentifierFinishedOperations()
  328. if !pyeval( 'base.CurrentIdentifierFinished()' )
  329. return
  330. endif
  331. py ycm_state.OnCurrentIdentifierFinished()
  332. let s:omnifunc_mode = 0
  333. endfunction
  334. " Returns 1 when inside comment and 2 when inside string
  335. function! s:InsideCommentOrString()
  336. " Has to be col('.') -1 because col('.') doesn't exist at this point. We are
  337. " in insert mode when this func is called.
  338. let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')
  339. if stridx(syntax_group, 'Comment') > -1
  340. return 1
  341. endif
  342. if stridx(syntax_group, 'String') > -1
  343. return 2
  344. endif
  345. return 0
  346. endfunction
  347. function! s:InsideCommentOrStringAndShouldStop()
  348. let retval = s:InsideCommentOrString()
  349. let inside_comment = retval == 1
  350. let inside_string = retval == 2
  351. if inside_comment && g:ycm_complete_in_comments ||
  352. \ inside_string && g:ycm_complete_in_strings
  353. return 0
  354. endif
  355. return retval
  356. endfunction
  357. function! s:OnBlankLine()
  358. return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
  359. endfunction
  360. function! s:InvokeCompletion()
  361. if &completefunc != "youcompleteme#Complete"
  362. return
  363. endif
  364. if s:InsideCommentOrStringAndShouldStop() || s:OnBlankLine()
  365. return
  366. endif
  367. " This is tricky. First, having 'refresh' set to 'always' in the dictionary
  368. " that our completion function returns makes sure that our completion function
  369. " is called on every keystroke. Second, when the sequence of characters the
  370. " user typed produces no results in our search an infinite loop can occur. The
  371. " problem is that our feedkeys call triggers the OnCursorMovedI event which we
  372. " are tied to. We prevent this infinite loop from starting by making sure that
  373. " the user has moved the cursor since the last time we provided completion
  374. " results.
  375. if !s:cursor_moved
  376. return
  377. endif
  378. " <c-x><c-u> invokes the user's completion function (which we have set to
  379. " youcompleteme#Complete), and <c-p> tells Vim to select the previous
  380. " completion candidate. This is necessary because by default, Vim selects the
  381. " first candidate when completion is invoked, and selecting a candidate
  382. " automatically replaces the current text with it. Calling <c-p> forces Vim to
  383. " deselect the first candidate and in turn preserve the user's current text
  384. " until he explicitly chooses to replace it with a completion.
  385. call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
  386. endfunction
  387. function! s:CompletionsForQuery( query, use_filetype_completer,
  388. \ completion_start_column )
  389. if a:use_filetype_completer
  390. py completer = ycm_state.GetFiletypeCompleter()
  391. else
  392. py completer = ycm_state.GetGeneralCompleter()
  393. endif
  394. py completer.CandidatesForQueryAsync( vim.eval( 'a:query' ),
  395. \ int( vim.eval( 'a:completion_start_column' ) ) )
  396. let l:results_ready = 0
  397. while !l:results_ready
  398. let l:results_ready = pyeval( 'completer.AsyncCandidateRequestReady()' )
  399. if complete_check()
  400. let s:searched_and_results_found = 0
  401. return { 'words' : [], 'refresh' : 'always'}
  402. endif
  403. endwhile
  404. let l:results = pyeval( 'base.AdjustCandidateInsertionText( completer.CandidatesFromStoredRequest() )' )
  405. let s:searched_and_results_found = len( l:results ) != 0
  406. return { 'words' : l:results, 'refresh' : 'always' }
  407. endfunction
  408. " This is our main entry point. This is what vim calls to get completions.
  409. function! youcompleteme#Complete( findstart, base )
  410. " After the user types one character after the call to the omnifunc, the
  411. " completefunc will be called because of our mapping that calls the
  412. " completefunc on every keystroke. Therefore we need to delegate the call we
  413. " 'stole' back to the omnifunc
  414. if s:omnifunc_mode
  415. return youcompleteme#OmniComplete( a:findstart, a:base )
  416. endif
  417. if a:findstart
  418. " InvokeCompletion has this check but we also need it here because of random
  419. " Vim bugs and unfortunate interactions with the autocommands of other
  420. " plugins
  421. if !s:cursor_moved
  422. " for vim, -2 means not found but don't trigger an error message
  423. " see :h complete-functions
  424. return -2
  425. endif
  426. " TODO: make this a function-local variable instead of a script-local one
  427. let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' )
  428. let s:should_use_filetype_completion =
  429. \ pyeval( 'ycm_state.ShouldUseFiletypeCompleter(' .
  430. \ s:completion_start_column . ')' )
  431. if !s:should_use_filetype_completion &&
  432. \ !pyeval( 'ycm_state.ShouldUseGeneralCompleter(' .
  433. \ s:completion_start_column . ')' )
  434. " for vim, -2 means not found but don't trigger an error message
  435. " see :h complete-functions
  436. return -2
  437. endif
  438. return s:completion_start_column
  439. else
  440. return s:CompletionsForQuery( a:base, s:should_use_filetype_completion,
  441. \ s:completion_start_column )
  442. endif
  443. endfunction
  444. function! youcompleteme#OmniComplete( findstart, base )
  445. if a:findstart
  446. let s:omnifunc_mode = 1
  447. let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' )
  448. return s:completion_start_column
  449. else
  450. return s:CompletionsForQuery( a:base, 1, s:completion_start_column )
  451. endif
  452. endfunction
  453. function! s:ShowDetailedDiagnostic()
  454. py ycm_state.ShowDetailedDiagnostic()
  455. endfunction
  456. command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()
  457. " This is what Syntastic calls indirectly when it decides an auto-check is
  458. " required (currently that's on buffer save) OR when the SyntasticCheck command
  459. " is invoked
  460. function! youcompleteme#CurrentFileDiagnostics()
  461. return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
  462. endfunction
  463. function! s:DebugInfo()
  464. echom "Printing YouCompleteMe debug information..."
  465. let debug_info = pyeval( 'ycm_state.DebugInfo()' )
  466. for line in split( debug_info, "\n" )
  467. echom '-- ' . line
  468. endfor
  469. endfunction
  470. command! YcmDebugInfo call s:DebugInfo()
  471. function! s:CompleterCommand(...)
  472. " CompleterCommand will call the OnUserCommand function of a completer.
  473. " If the first arguments is of the form "ft=..." it can be used to specify the
  474. " completer to use (for example "ft=cpp"). Else the native filetype completer
  475. " of the current buffer is used. If no native filetype completer is found and
  476. " no completer was specified this throws an error. You can use "ft=ycm:omni"
  477. " to select the omni completer or "ft=ycm:ident" to select the identifier
  478. " completer. The remaining arguments will passed to the completer.
  479. let arguments = copy(a:000)
  480. if a:0 > 0 && strpart(a:1, 0, 3) == 'ft='
  481. if a:1 == 'ft=ycm:omni'
  482. py completer = ycm_state.GetOmniCompleter()
  483. elseif a:1 == 'ft=ycm:ident'
  484. py completer = ycm_state.GetGeneralCompleter()
  485. else
  486. py completer = ycm_state.GetFiletypeCompleterForFiletype(
  487. \ vim.eval('a:1').lstrip('ft=') )
  488. endif
  489. let arguments = arguments[1:]
  490. elseif pyeval( 'ycm_state.NativeFiletypeCompletionAvailable()' )
  491. py completer = ycm_state.GetFiletypeCompleter()
  492. else
  493. echohl WarningMsg |
  494. \ echomsg "No native completer found for current buffer." |
  495. \ echomsg "Use ft=... as the first argument to specify a completer." |
  496. \ echohl None
  497. return
  498. endif
  499. py completer.OnUserCommand( vim.eval( 'l:arguments' ) )
  500. endfunction
  501. function! youcompleteme#OpenGoToList()
  502. set lazyredraw
  503. cclose
  504. execute 'belowright copen 3'
  505. set nolazyredraw
  506. au WinLeave <buffer> q " automatically leave, if an option is chosen
  507. redraw!
  508. endfunction
  509. command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete
  510. \ YcmCompleter call s:CompleterCommand(<f-args>)
  511. function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos )
  512. return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ),
  513. \ "\n")
  514. endfunction
  515. function! s:ForceCompile()
  516. if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
  517. echom "Native filetype completion not supported for current file, "
  518. \ . "cannot force recompilation."
  519. return 0
  520. endif
  521. echom "Forcing compilation, this will block Vim until done."
  522. py ycm_state.OnFileReadyToParse()
  523. while 1
  524. let diagnostics_ready = pyeval(
  525. \ 'ycm_state.DiagnosticsForCurrentFileReady()' )
  526. if diagnostics_ready
  527. break
  528. endif
  529. let getting_completions = pyeval(
  530. \ 'ycm_state.GettingCompletions()' )
  531. if !getting_completions
  532. echom "Unable to retrieve diagnostics, see output of `:mes` for possible details."
  533. return 0
  534. endif
  535. sleep 100m
  536. endwhile
  537. return 1
  538. endfunction
  539. function! s:ForceCompileAndDiagnostics()
  540. let compilation_succeeded = s:ForceCompile()
  541. if !compilation_succeeded
  542. return
  543. endif
  544. call s:UpdateDiagnosticNotifications()
  545. echom "Diagnostics refreshed."
  546. endfunction
  547. command! YcmForceCompileAndDiagnostics call s:ForceCompileAndDiagnostics()
  548. function! s:ShowDiagnostics()
  549. let compilation_succeeded = s:ForceCompile()
  550. if !compilation_succeeded
  551. return
  552. endif
  553. let diags = pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
  554. if !empty( diags )
  555. call setloclist( 0, diags )
  556. lopen
  557. else
  558. echom "No warnings or errors detected"
  559. endif
  560. endfunction
  561. command! YcmDiags call s:ShowDiagnostics()
  562. " This is basic vim plugin boilerplate
  563. let &cpo = s:save_cpo
  564. unlet s:save_cpo