youcompleteme.vim 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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 )
  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. function! s:OnVimLeave()
  170. py ycm_state.OnVimLeave()
  171. py extra_conf_store.CallExtraConfVimCloseIfExists()
  172. endfunction
  173. function! s:OnBufferVisit()
  174. if !s:AllowedToCompleteInCurrentFile()
  175. return
  176. endif
  177. call s:SetUpCompleteopt()
  178. call s:SetCompleteFunc()
  179. py ycm_state.OnBufferVisit()
  180. call s:OnFileReadyToParse()
  181. endfunction
  182. function! s:OnBufferUnload( deleted_buffer_file )
  183. if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
  184. return
  185. endif
  186. py ycm_state.OnBufferUnload( vim.eval( 'a:deleted_buffer_file' ) )
  187. endfunction
  188. function! s:OnCursorHold()
  189. if !s:AllowedToCompleteInCurrentFile()
  190. return
  191. endif
  192. call s:SetUpCompleteopt()
  193. " Order is important here; we need to extract any done diagnostics before
  194. " reparsing the file again
  195. call s:UpdateDiagnosticNotifications()
  196. call s:OnFileReadyToParse()
  197. endfunction
  198. function! s:OnFileReadyToParse()
  199. py ycm_state.OnFileReadyToParse()
  200. endfunction
  201. function! s:SetCompleteFunc()
  202. let &completefunc = 'youcompleteme#Complete'
  203. let &l:completefunc = 'youcompleteme#Complete'
  204. if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
  205. let &omnifunc = 'youcompleteme#OmniComplete'
  206. let &l:omnifunc = 'youcompleteme#OmniComplete'
  207. " If we don't have native filetype support but the omnifunc is set to YCM's
  208. " omnifunc because the previous file the user was editing DID have native
  209. " support, we remove our omnifunc.
  210. elseif &omnifunc == 'youcompleteme#OmniComplete'
  211. let &omnifunc = ''
  212. let &l:omnifunc = ''
  213. endif
  214. endfunction
  215. function! s:OnCursorMovedInsertMode()
  216. if !s:AllowedToCompleteInCurrentFile()
  217. return
  218. endif
  219. call s:UpdateCursorMoved()
  220. " Basically, we need to only trigger the completion menu when the user has
  221. " inserted or deleted a character, NOT just when the user moves in insert mode
  222. " (with, say, the arrow keys). If we trigger the menu even on pure moves, then
  223. " it's impossible to move in insert mode since the up/down arrows start moving
  224. " the selected completion in the completion menu. Yeah, people shouldn't be
  225. " moving in insert mode at all (that's what normal mode is for) but explain
  226. " that to the users who complain...
  227. if !s:BufferTextChangedSinceLastMoveInInsertMode()
  228. return
  229. endif
  230. call s:IdentifierFinishedOperations()
  231. if g:ycm_autoclose_preview_window_after_completion
  232. call s:ClosePreviewWindowIfNeeded()
  233. endif
  234. call s:InvokeCompletion()
  235. endfunction
  236. function! s:OnCursorMovedNormalMode()
  237. if !s:AllowedToCompleteInCurrentFile()
  238. return
  239. endif
  240. call s:UpdateDiagnosticNotifications()
  241. endfunction
  242. function! s:OnInsertLeave()
  243. if !s:AllowedToCompleteInCurrentFile()
  244. return
  245. endif
  246. let s:omnifunc_mode = 0
  247. call s:UpdateDiagnosticNotifications()
  248. py ycm_state.OnInsertLeave()
  249. if g:ycm_autoclose_preview_window_after_completion ||
  250. \ g:ycm_autoclose_preview_window_after_insertion
  251. call s:ClosePreviewWindowIfNeeded()
  252. endif
  253. endfunction
  254. function! s:OnInsertEnter()
  255. if !s:AllowedToCompleteInCurrentFile()
  256. return
  257. endif
  258. let s:old_cursor_position = []
  259. endfunction
  260. function! s:UpdateCursorMoved()
  261. let current_position = getpos('.')
  262. let s:cursor_moved = current_position != s:old_cursor_position
  263. let s:moved_vertically_in_insert_mode = s:old_cursor_position != [] &&
  264. \ current_position[ 1 ] != s:old_cursor_position[ 1 ]
  265. let s:old_cursor_position = current_position
  266. endfunction
  267. function! s:BufferTextChangedSinceLastMoveInInsertMode()
  268. if s:moved_vertically_in_insert_mode
  269. let s:previous_num_chars_on_current_line = -1
  270. return 0
  271. endif
  272. let num_chars_in_current_cursor_line = strlen( getline('.') )
  273. if s:previous_num_chars_on_current_line == -1
  274. let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
  275. return 0
  276. endif
  277. let changed_text_on_current_line = num_chars_in_current_cursor_line !=
  278. \ s:previous_num_chars_on_current_line
  279. let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
  280. return changed_text_on_current_line
  281. endfunction
  282. function! s:ClosePreviewWindowIfNeeded()
  283. let current_buffer_name = bufname('')
  284. " We don't want to try to close the preview window in special buffers like
  285. " "[Command Line]"; if we do, Vim goes bonkers. Special buffers always start
  286. " with '['.
  287. if current_buffer_name[ 0 ] == '['
  288. return
  289. endif
  290. if s:searched_and_results_found
  291. " This command does the actual closing of the preview window. If no preview
  292. " window is shown, nothing happens.
  293. pclose
  294. endif
  295. endfunction
  296. function! s:UpdateDiagnosticNotifications()
  297. if get( g:, 'loaded_syntastic_plugin', 0 ) &&
  298. \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) &&
  299. \ pyeval( 'ycm_state.DiagnosticsForCurrentFileReady()' ) &&
  300. \ g:ycm_register_as_syntastic_checker
  301. SyntasticCheck
  302. endif
  303. endfunction
  304. function! s:IdentifierFinishedOperations()
  305. if !pyeval( 'base.CurrentIdentifierFinished()' )
  306. return
  307. endif
  308. py ycm_state.OnCurrentIdentifierFinished()
  309. let s:omnifunc_mode = 0
  310. endfunction
  311. " Returns 1 when inside comment and 2 when inside string
  312. function! s:InsideCommentOrString()
  313. " Has to be col('.') -1 because col('.') doesn't exist at this point. We are
  314. " in insert mode when this func is called.
  315. let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')
  316. if stridx(syntax_group, 'Comment') > -1
  317. return 1
  318. endif
  319. if stridx(syntax_group, 'String') > -1
  320. return 2
  321. endif
  322. return 0
  323. endfunction
  324. function! s:InsideCommentOrStringAndShouldStop()
  325. let retval = s:InsideCommentOrString()
  326. let inside_comment = retval == 1
  327. let inside_string = retval == 2
  328. if inside_comment && g:ycm_complete_in_comments ||
  329. \ inside_string && g:ycm_complete_in_strings
  330. return 0
  331. endif
  332. return retval
  333. endfunction
  334. function! s:OnBlankLine()
  335. return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
  336. endfunction
  337. function! s:InvokeCompletion()
  338. if &completefunc != "youcompleteme#Complete"
  339. return
  340. endif
  341. if s:InsideCommentOrStringAndShouldStop() || s:OnBlankLine()
  342. return
  343. endif
  344. " This is tricky. First, having 'refresh' set to 'always' in the dictionary
  345. " that our completion function returns makes sure that our completion function
  346. " is called on every keystroke. Second, when the sequence of characters the
  347. " user typed produces no results in our search an infinite loop can occur. The
  348. " problem is that our feedkeys call triggers the OnCursorMovedI event which we
  349. " are tied to. We prevent this infinite loop from starting by making sure that
  350. " the user has moved the cursor since the last time we provided completion
  351. " results.
  352. if !s:cursor_moved
  353. return
  354. endif
  355. " <c-x><c-u> invokes the user's completion function (which we have set to
  356. " youcompleteme#Complete), and <c-p> tells Vim to select the previous
  357. " completion candidate. This is necessary because by default, Vim selects the
  358. " first candidate when completion is invoked, and selecting a candidate
  359. " automatically replaces the current text with it. Calling <c-p> forces Vim to
  360. " deselect the first candidate and in turn preserve the user's current text
  361. " until he explicitly chooses to replace it with a completion.
  362. call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
  363. endfunction
  364. function! s:CompletionsForQuery( query, use_filetype_completer,
  365. \ completion_start_column )
  366. if a:use_filetype_completer
  367. py completer = ycm_state.GetFiletypeCompleter()
  368. else
  369. py completer = ycm_state.GetGeneralCompleter()
  370. endif
  371. py completer.CandidatesForQueryAsync( vim.eval( 'a:query' ),
  372. \ int( vim.eval( 'a:completion_start_column' ) ) )
  373. let l:results_ready = 0
  374. while !l:results_ready
  375. let l:results_ready = pyeval( 'completer.AsyncCandidateRequestReady()' )
  376. if complete_check()
  377. let s:searched_and_results_found = 0
  378. return { 'words' : [], 'refresh' : 'always'}
  379. endif
  380. endwhile
  381. let l:results = pyeval( 'base.AdjustCandidateInsertionText( completer.CandidatesFromStoredRequest() )' )
  382. let s:searched_and_results_found = len( l:results ) != 0
  383. return { 'words' : l:results, 'refresh' : 'always' }
  384. endfunction
  385. " This is our main entry point. This is what vim calls to get completions.
  386. function! youcompleteme#Complete( findstart, base )
  387. " After the user types one character after the call to the omnifunc, the
  388. " completefunc will be called because of our mapping that calls the
  389. " completefunc on every keystroke. Therefore we need to delegate the call we
  390. " 'stole' back to the omnifunc
  391. if s:omnifunc_mode
  392. return youcompleteme#OmniComplete( a:findstart, a:base )
  393. endif
  394. if a:findstart
  395. " InvokeCompletion has this check but we also need it here because of random
  396. " Vim bugs and unfortunate interactions with the autocommands of other
  397. " plugins
  398. if !s:cursor_moved
  399. " for vim, -2 means not found but don't trigger an error message
  400. " see :h complete-functions
  401. return -2
  402. endif
  403. " TODO: make this a function-local variable instead of a script-local one
  404. let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' )
  405. let s:should_use_filetype_completion =
  406. \ pyeval( 'ycm_state.ShouldUseFiletypeCompleter(' .
  407. \ s:completion_start_column . ')' )
  408. if !s:should_use_filetype_completion &&
  409. \ !pyeval( 'ycm_state.ShouldUseGeneralCompleter(' .
  410. \ s:completion_start_column . ')' )
  411. " for vim, -2 means not found but don't trigger an error message
  412. " see :h complete-functions
  413. return -2
  414. endif
  415. return s:completion_start_column
  416. else
  417. return s:CompletionsForQuery( a:base, s:should_use_filetype_completion,
  418. \ s:completion_start_column )
  419. endif
  420. endfunction
  421. function! youcompleteme#OmniComplete( findstart, base )
  422. if a:findstart
  423. let s:omnifunc_mode = 1
  424. let s:completion_start_column = pyeval( 'base.CompletionStartColumn()' )
  425. return s:completion_start_column
  426. else
  427. return s:CompletionsForQuery( a:base, 1, s:completion_start_column )
  428. endif
  429. endfunction
  430. function! s:ShowDetailedDiagnostic()
  431. py ycm_state.ShowDetailedDiagnostic()
  432. endfunction
  433. command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()
  434. " This is what Syntastic calls indirectly when it decides an auto-check is
  435. " required (currently that's on buffer save) OR when the SyntasticCheck command
  436. " is invoked
  437. function! youcompleteme#CurrentFileDiagnostics()
  438. return pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
  439. endfunction
  440. function! s:DebugInfo()
  441. echom "Printing YouCompleteMe debug information..."
  442. let debug_info = pyeval( 'ycm_state.DebugInfo()' )
  443. for line in split( debug_info, "\n" )
  444. echom '-- ' . line
  445. endfor
  446. endfunction
  447. command! YcmDebugInfo call s:DebugInfo()
  448. function! s:CompleterCommand(...)
  449. " CompleterCommand will call the OnUserCommand function of a completer.
  450. " If the first arguments is of the form "ft=..." it can be used to specify the
  451. " completer to use (for example "ft=cpp"). Else the native filetype completer
  452. " of the current buffer is used. If no native filetype completer is found and
  453. " no completer was specified this throws an error. You can use "ft=ycm:omni"
  454. " to select the omni completer or "ft=ycm:ident" to select the identifier
  455. " completer. The remaining arguments will passed to the completer.
  456. let arguments = copy(a:000)
  457. if a:0 > 0 && strpart(a:1, 0, 3) == 'ft='
  458. if a:1 == 'ft=ycm:omni'
  459. py completer = ycm_state.GetOmniCompleter()
  460. elseif a:1 == 'ft=ycm:ident'
  461. py completer = ycm_state.GetGeneralCompleter()
  462. else
  463. py completer = ycm_state.GetFiletypeCompleterForFiletype(
  464. \ vim.eval('a:1').lstrip('ft=') )
  465. endif
  466. let arguments = arguments[1:]
  467. elseif pyeval( 'ycm_state.NativeFiletypeCompletionAvailable()' )
  468. py completer = ycm_state.GetFiletypeCompleter()
  469. else
  470. echohl WarningMsg |
  471. \ echomsg "No native completer found for current buffer." |
  472. \ echomsg "Use ft=... as the first argument to specify a completer." |
  473. \ echohl None
  474. return
  475. endif
  476. py completer.OnUserCommand( vim.eval( 'l:arguments' ) )
  477. endfunction
  478. function! youcompleteme#OpenGoToList()
  479. set lazyredraw
  480. cclose
  481. execute 'belowright copen 3'
  482. set nolazyredraw
  483. au WinLeave <buffer> q " automatically leave, if an option is chosen
  484. redraw!
  485. endfunction
  486. command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete
  487. \ YcmCompleter call s:CompleterCommand(<f-args>)
  488. function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos )
  489. return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ),
  490. \ "\n")
  491. endfunction
  492. function! s:ForceCompile()
  493. if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
  494. echom "Native filetype completion not supported for current file, "
  495. \ . "cannot force recompilation."
  496. return 0
  497. endif
  498. echom "Forcing compilation, this will block Vim until done."
  499. py ycm_state.OnFileReadyToParse()
  500. while 1
  501. let diagnostics_ready = pyeval(
  502. \ 'ycm_state.DiagnosticsForCurrentFileReady()' )
  503. if diagnostics_ready
  504. break
  505. endif
  506. let getting_completions = pyeval(
  507. \ 'ycm_state.GettingCompletions()' )
  508. if !getting_completions
  509. echom "Unable to retrieve diagnostics, see output of `:mes` for possible details."
  510. return 0
  511. endif
  512. sleep 100m
  513. endwhile
  514. return 1
  515. endfunction
  516. function! s:ForceCompileAndDiagnostics()
  517. let compilation_succeeded = s:ForceCompile()
  518. if !compilation_succeeded
  519. return
  520. endif
  521. call s:UpdateDiagnosticNotifications()
  522. echom "Diagnostics refreshed."
  523. endfunction
  524. command! YcmForceCompileAndDiagnostics call s:ForceCompileAndDiagnostics()
  525. function! s:ShowDiagnostics()
  526. let compilation_succeeded = s:ForceCompile()
  527. if !compilation_succeeded
  528. return
  529. endif
  530. let diags = pyeval( 'ycm_state.GetDiagnosticsForCurrentFile()' )
  531. if !empty( diags )
  532. call setloclist( 0, diags )
  533. lopen
  534. else
  535. echom "No warnings or errors detected"
  536. endif
  537. endfunction
  538. command! YcmDiags call s:ShowDiagnostics()
  539. " This is basic vim plugin boilerplate
  540. let &cpo = s:save_cpo
  541. unlet s:save_cpo