youcompleteme.vim 21 KB

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