youcompleteme.vim 21 KB

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