File: youcompleteme.vim

package info (click to toggle)
vim-youcompleteme 0%2B20140207%2Bgit18be5c2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,396 kB
  • ctags: 1,488
  • sloc: python: 4,436; cpp: 3,349; sh: 239; makefile: 44; cs: 22
file content (803 lines) | stat: -rw-r--r-- 24,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
" Copyright (C) 2011, 2012  Google Inc.
"
" This file is part of YouCompleteMe.
"
" YouCompleteMe is free software: you can redistribute it and/or modify
" it under the terms of the GNU General Public License as published by
" the Free Software Foundation, either version 3 of the License, or
" (at your option) any later version.
"
" YouCompleteMe is distributed in the hope that it will be useful,
" but WITHOUT ANY WARRANTY; without even the implied warranty of
" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
" GNU General Public License for more details.
"
" You should have received a copy of the GNU General Public License
" along with YouCompleteMe.  If not, see <http://www.gnu.org/licenses/>.

" This is basic vim plugin boilerplate
let s:save_cpo = &cpo
set cpo&vim

" This needs to be called outside of a function
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
let s:searched_and_results_found = 0
let s:omnifunc_mode = 0

let s:old_cursor_position = []
let s:cursor_moved = 0
let s:moved_vertically_in_insert_mode = 0
let s:previous_num_chars_on_current_line = -1

let s:diagnostic_ui_filetypes = {
      \ 'cpp': 1,
      \ 'c': 1,
      \ 'objc': 1,
      \ 'objcpp': 1,
      \ }


function! youcompleteme#Enable()
  " When vim is in diff mode, don't run
  if &diff
    return
  endif

  call s:SetUpBackwardsCompatibility()

  py import sys
  py import vim
  exe 'python sys.path.insert( 0, "/usr/lib/vim-youcompleteme/" )'
  py from ycm import utils
  py from ycm import base
  py base.LoadJsonDefaultsIntoVim()
  py from ycm import vimsupport
  py from ycm import user_options_store
  py user_options_store.SetAll( base.BuildServerConf() )

  if !pyeval( 'base.CompatibleWithYcmCore()')
    echohl WarningMsg |
      \ echomsg "YouCompleteMe unavailable: YCM support libs too old, PLEASE RECOMPILE" |
      \ echohl None
    return
  endif

  py from ycm.youcompleteme import YouCompleteMe
  py ycm_state = YouCompleteMe( user_options_store.GetAll() )

  call s:SetUpCpoptions()
  call s:SetUpCompleteopt()
  call s:SetUpKeyMappings()

  if g:ycm_show_diagnostics_ui
    call s:TurnOffSyntasticForCFamily()
  endif

  call s:SetUpSigns()
  call s:SetUpSyntaxHighlighting()

  if g:ycm_allow_changing_updatetime
    set ut=2000
  endif

  augroup youcompleteme
    autocmd!
    autocmd CursorMovedI * call s:OnCursorMovedInsertMode()
    autocmd CursorMoved * call s:OnCursorMovedNormalMode()
    " Note that these events will NOT trigger for the file vim is started with;
    " so if you do "vim foo.cc", these events will not trigger when that buffer
    " is read. This is because youcompleteme#Enable() is called on VimEnter and
    " that happens *after" BufRead/BufEnter has already triggered for the
    " initial file.
    " We also need to trigger buf init code on the FileType event because when
    " the user does :enew and then :set ft=something, we need to run buf init
    " code again.
    autocmd BufRead,BufEnter,FileType * call s:OnBufferVisit()
    autocmd BufUnload * call s:OnBufferUnload( expand( '<afile>:p' ) )
    autocmd CursorHold,CursorHoldI * call s:OnCursorHold()
    autocmd InsertLeave * call s:OnInsertLeave()
    autocmd InsertEnter * call s:OnInsertEnter()
    autocmd VimLeave * call s:OnVimLeave()
  augroup END

  " Calling this once solves the problem of BufRead/BufEnter not triggering for
  " the first loaded file. This should be the last command executed in this
  " function!
  call s:OnBufferVisit()
endfunction


function! s:SetUpKeyMappings()
  " The g:ycm_key_select_completion and g:ycm_key_previous_completion used to
  " exist and are now here purely for the sake of backwards compatibility; we
  " don't want to break users if we can avoid it.

  if exists('g:ycm_key_select_completion') &&
        \ index(g:ycm_key_list_select_completion,
        \       g:ycm_key_select_completion) == -1
    call add(g:ycm_key_list_select_completion, g:ycm_key_select_completion)
  endif

  if exists('g:ycm_key_previous_completion') &&
        \ index(g:ycm_key_list_previous_completion,
        \       g:ycm_key_previous_completion) == -1
    call add(g:ycm_key_list_previous_completion, g:ycm_key_previous_completion)
  endif

  for key in g:ycm_key_list_select_completion
    " With this command, when the completion window is visible, the tab key
    " (default) will select the next candidate in the window. In vim, this also
    " changes the typed-in text to that of the candidate completion.
    exe 'inoremap <expr>' . key .
          \ ' pumvisible() ? "\<C-n>" : "\' . key .'"'
  endfor


  for key in g:ycm_key_list_previous_completion
    " This selects the previous candidate for shift-tab (default)
    exe 'inoremap <expr>' . key .
          \ ' pumvisible() ? "\<C-p>" : "\' . key .'"'
  endfor

  if !empty( g:ycm_key_invoke_completion )
    let invoke_key = g:ycm_key_invoke_completion

    " Inside the console, <C-Space> is passed as <Nul> to Vim
    if invoke_key ==# '<C-Space>' && !has('gui_running')
      let invoke_key = '<Nul>'
    endif

    " <c-x><c-o> trigger omni completion, <c-p> deselects the first completion
    " candidate that vim selects by default
    silent! exe 'inoremap <unique> ' . invoke_key .  ' <C-X><C-O><C-P>'
  endif

  if !empty( g:ycm_key_detailed_diagnostics )
    silent! exe 'nnoremap <unique> ' . g:ycm_key_detailed_diagnostics .
          \ ' :YcmShowDetailedDiagnostic<cr>'
  endif
endfunction


function! s:SetUpSigns()
  " We try to ensure backwards compatibility with Syntastic if the user has
  " already defined styling for Syntastic highlight groups.

  if !hlexists( 'YcmErrorSign' )
    if hlexists( 'SyntasticErrorSign')
      highlight link YcmErrorSign SyntasticErrorSign
    else
      highlight link YcmErrorSign error
    endif
  endif

  if !hlexists( 'YcmWarningSign' )
    if hlexists( 'SyntasticWarningSign')
      highlight link YcmWarningSign SyntasticWarningSign
    else
      highlight link YcmWarningSign todo
    endif
  endif

  if !hlexists( 'YcmErrorLine' )
    highlight link YcmErrorLine SyntasticErrorLine
  endif

  if !hlexists( 'YcmWarningLine' )
    highlight link YcmWarningLine SyntasticWarningLine
  endif

  exe 'sign define YcmError text=' . g:ycm_error_symbol .
        \ ' texthl=YcmErrorSign linehl=YcmErrorLine'
  exe 'sign define YcmWarning text=' . g:ycm_warning_symbol .
        \ ' texthl=YcmWarningSign linehl=YcmWarningLine'
endfunction


function! s:SetUpSyntaxHighlighting()
  " We try to ensure backwards compatibility with Syntastic if the user has
  " already defined styling for Syntastic highlight groups.

  if !hlexists( 'YcmErrorSection' )
    if hlexists( 'SyntasticError' )
      highlight link YcmErrorSection SyntasticError
    else
      highlight link YcmErrorSection SpellBad
    endif
  endif

  if !hlexists( 'YcmWarningSection' )
    if hlexists( 'SyntasticWarning' )
      highlight link YcmWarningSection SyntasticWarning
    else
      highlight link YcmWarningSection SpellCap
    endif
  endif
endfunction


function! s:SetUpBackwardsCompatibility()
  let complete_in_comments_and_strings =
        \ get( g:, 'ycm_complete_in_comments_and_strings', 0 )

  if complete_in_comments_and_strings
    let g:ycm_complete_in_strings = 1
    let g:ycm_complete_in_comments = 1
  endif

  " ycm_filetypes_to_completely_ignore is the old name for fileype_blacklist
  if has_key( g:, 'ycm_filetypes_to_completely_ignore' )
    let g:filetype_blacklist =  g:ycm_filetypes_to_completely_ignore
  endif
endfunction


" Needed so that YCM is used instead of Syntastic
function! s:TurnOffSyntasticForCFamily()
  let g:syntastic_cpp_checkers = []
  let g:syntastic_c_checkers = []
  let g:syntastic_objc_checkers = []
  let g:syntastic_objcpp_checkers = []
endfunction


function! s:DiagnosticUiSupportedForCurrentFiletype()
  return get( s:diagnostic_ui_filetypes, &filetype, 0 )
endfunction


function! s:AllowedToCompleteInCurrentFile()
  if empty( &filetype ) ||
        \ getbufvar( winbufnr( winnr() ), "&buftype" ) ==# 'nofile' ||
        \ &filetype ==# 'qf'
    return 0
  endif

  let whitelist_allows = has_key( g:ycm_filetype_whitelist, '*' ) ||
        \ has_key( g:ycm_filetype_whitelist, &filetype )
  let blacklist_allows = !has_key( g:ycm_filetype_blacklist, &filetype )

  return whitelist_allows && blacklist_allows
endfunction


function! s:SetUpCpoptions()
  " Without this flag in cpoptions, critical YCM mappings do not work. There's
  " no way to not have this and have YCM working, so force the flag.
  set cpoptions+=B

  " This prevents the display of "Pattern not found" & similar messages during
  " completion.
  " Patch: https://groups.google.com/forum/#!topic/vim_dev/WeBBjkXE8H8
  " At the time of writing, the patch hasn't been merged into Vim upstream, but
  " will at some point.
  " TODO: Check the Vim version (when patch lands) instead of doing try-catch
  " here.
  try
    set shortmess+=c
  catch
  endtry
endfunction


function! s:SetUpCompleteopt()
  " Some plugins (I'm looking at you, vim-notes) change completeopt by for
  " instance adding 'longest'. This breaks YCM. So we force our settings.
  " There's no two ways about this: if you want to use YCM then you have to
  " have these completeopt settings, otherwise YCM won't work at all.

  " We need menuone in completeopt, otherwise when there's only one candidate
  " for completion, the menu doesn't show up.
  set completeopt-=menu
  set completeopt+=menuone

  " This is unnecessary with our features. People use this option to insert
  " the common prefix of all the matches and then add more differentiating chars
  " so that they can select a more specific match. With our features, they
  " don't need to insert the prefix; they just type the differentiating chars.
  " Also, having this option set breaks the plugin.
  set completeopt-=longest

  if g:ycm_add_preview_to_completeopt
    set completeopt+=preview
  endif
endfunction


" For various functions/use-cases, we want to keep track of whether the buffer
" has changed since the last time they were invoked. We keep the state of
" b:changedtick of the last time the specific function was called in
" b:ycm_changedtick.
function! s:SetUpYcmChangedTick()
  let b:ycm_changedtick  =
        \ get( b:, 'ycm_changedtick', {
        \   'file_ready_to_parse' : -1,
        \ } )
endfunction


function! s:OnVimLeave()
  py ycm_state.OnVimLeave()
endfunction


function! s:OnBufferVisit()
  " We need to do this even when we are not allowed to complete in the current
  " file because we might be allowed to complete in the future! The canonical
  " example is creating a new buffer with :enew and then setting a filetype.
  call s:SetUpYcmChangedTick()

  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  call s:SetUpCompleteopt()
  call s:SetCompleteFunc()
  py ycm_state.OnBufferVisit()
  call s:OnFileReadyToParse()
endfunction


function! s:OnBufferUnload( deleted_buffer_file )
  if !s:AllowedToCompleteInCurrentFile() || empty( a:deleted_buffer_file )
    return
  endif

  py ycm_state.OnBufferUnload( vim.eval( 'a:deleted_buffer_file' ) )
endfunction


function! s:OnCursorHold()
  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  call s:SetUpCompleteopt()
  call s:OnFileReadyToParse()
endfunction


function! s:OnFileReadyToParse()
  " We need to call this just in case there is no b:ycm_changetick; this can
  " happen for special buffers.
  call s:SetUpYcmChangedTick()

  " Order is important here; we need to extract any done diagnostics before
  " reparsing the file again. If we sent the new parse request first, then
  " the response would always be pending when we called
  " UpdateDiagnosticNotifications.
  call s:UpdateDiagnosticNotifications()

  let buffer_changed = b:changedtick != b:ycm_changedtick.file_ready_to_parse
  if buffer_changed
    py ycm_state.OnFileReadyToParse()
  endif
  let b:ycm_changedtick.file_ready_to_parse = b:changedtick
endfunction


function! s:SetCompleteFunc()
  let &completefunc = 'youcompleteme#Complete'
  let &l:completefunc = 'youcompleteme#Complete'

  if pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
    let &omnifunc = 'youcompleteme#OmniComplete'
    let &l:omnifunc = 'youcompleteme#OmniComplete'

  " If we don't have native filetype support but the omnifunc is set to YCM's
  " omnifunc because the previous file the user was editing DID have native
  " support, we remove our omnifunc.
  elseif &omnifunc == 'youcompleteme#OmniComplete'
    let &omnifunc = ''
    let &l:omnifunc = ''
  endif
endfunction


function! s:OnCursorMovedInsertMode()
  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  py ycm_state.OnCursorMoved()
  call s:UpdateCursorMoved()

  " Basically, we need to only trigger the completion menu when the user has
  " inserted or deleted a character, NOT just when the user moves in insert mode
  " (with, say, the arrow keys). If we trigger the menu even on pure moves, then
  " it's impossible to move in insert mode since the up/down arrows start moving
  " the selected completion in the completion menu. Yeah, people shouldn't be
  " moving in insert mode at all (that's what normal mode is for) but explain
  " that to the users who complain...
  if !s:BufferTextChangedSinceLastMoveInInsertMode()
    return
  endif

  call s:IdentifierFinishedOperations()
  if g:ycm_autoclose_preview_window_after_completion
    call s:ClosePreviewWindowIfNeeded()
  endif

  if g:ycm_auto_trigger || s:omnifunc_mode
    call s:InvokeCompletion()
  endif

  " We have to make sure we correctly leave omnifunc mode even when the user
  " inserts something like a "operator[]" candidate string which fails
  " CurrentIdentifierFinished check.
  if s:omnifunc_mode && !pyeval( 'base.LastEnteredCharIsIdentifierChar()')
    let s:omnifunc_mode = 0
  endif
endfunction


function! s:OnCursorMovedNormalMode()
  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  call s:OnFileReadyToParse()
  py ycm_state.OnCursorMoved()
endfunction


function! s:OnInsertLeave()
  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  let s:omnifunc_mode = 0
  call s:OnFileReadyToParse()
  py ycm_state.OnInsertLeave()
  if g:ycm_autoclose_preview_window_after_completion ||
        \ g:ycm_autoclose_preview_window_after_insertion
    call s:ClosePreviewWindowIfNeeded()
  endif
endfunction


function! s:OnInsertEnter()
  if !s:AllowedToCompleteInCurrentFile()
    return
  endif

  let s:old_cursor_position = []
endfunction


function! s:UpdateCursorMoved()
  let current_position = getpos('.')
  let s:cursor_moved = current_position != s:old_cursor_position

  let s:moved_vertically_in_insert_mode = s:old_cursor_position != [] &&
        \ current_position[ 1 ] != s:old_cursor_position[ 1 ]

  let s:old_cursor_position = current_position
endfunction


function! s:BufferTextChangedSinceLastMoveInInsertMode()
  if s:moved_vertically_in_insert_mode
    let s:previous_num_chars_on_current_line = -1
    return 0
  endif

  let num_chars_in_current_cursor_line = strlen( getline('.') )

  if s:previous_num_chars_on_current_line == -1
    let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line
    return 0
  endif

  let changed_text_on_current_line = num_chars_in_current_cursor_line !=
        \ s:previous_num_chars_on_current_line
  let s:previous_num_chars_on_current_line = num_chars_in_current_cursor_line

  return changed_text_on_current_line
endfunction


function! s:ClosePreviewWindowIfNeeded()
  let current_buffer_name = bufname('')

  " We don't want to try to close the preview window in special buffers like
  " "[Command Line]"; if we do, Vim goes bonkers. Special buffers always start
  " with '['.
  if current_buffer_name[ 0 ] == '['
    return
  endif

  if s:searched_and_results_found
    " This command does the actual closing of the preview window. If no preview
    " window is shown, nothing happens.
    pclose
  endif
endfunction


function! s:UpdateDiagnosticNotifications()
  let should_display_diagnostics = g:ycm_show_diagnostics_ui &&
        \ s:DiagnosticUiSupportedForCurrentFiletype() &&
        \ pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )

  if !should_display_diagnostics
    return
  endif

  py ycm_state.UpdateDiagnosticInterface()
endfunction


function! s:IdentifierFinishedOperations()
  if !pyeval( 'base.CurrentIdentifierFinished()' )
    return
  endif
  py ycm_state.OnCurrentIdentifierFinished()
  let s:omnifunc_mode = 0
endfunction


" Returns 1 when inside comment and 2 when inside string
function! s:InsideCommentOrString()
  " Has to be col('.') -1 because col('.') doesn't exist at this point. We are
  " in insert mode when this func is called.
  let syntax_group = synIDattr( synIDtrans( synID( line( '.' ), col( '.' ) - 1, 1 ) ), 'name')

  if stridx(syntax_group, 'Comment') > -1
    return 1
  endif

  if stridx(syntax_group, 'String') > -1
    return 2
  endif

  return 0
endfunction


function! s:InsideCommentOrStringAndShouldStop()
  let retval = s:InsideCommentOrString()
  let inside_comment = retval == 1
  let inside_string = retval == 2

  if inside_comment && g:ycm_complete_in_comments ||
        \ inside_string && g:ycm_complete_in_strings
    return 0
  endif

  return retval
endfunction


function! s:OnBlankLine()
  return pyeval( 'not vim.current.line or vim.current.line.isspace()' )
endfunction


function! s:InvokeCompletion()
  if &completefunc != "youcompleteme#Complete"
    return
  endif

  if s:InsideCommentOrStringAndShouldStop() || s:OnBlankLine()
    return
  endif

  " This is tricky. First, having 'refresh' set to 'always' in the dictionary
  " that our completion function returns makes sure that our completion function
  " is called on every keystroke. Second, when the sequence of characters the
  " user typed produces no results in our search an infinite loop can occur. The
  " problem is that our feedkeys call triggers the OnCursorMovedI event which we
  " are tied to. We prevent this infinite loop from starting by making sure that
  " the user has moved the cursor since the last time we provided completion
  " results.
  if !s:cursor_moved
    return
  endif

  " <c-x><c-u> invokes the user's completion function (which we have set to
  " youcompleteme#Complete), and <c-p> tells Vim to select the previous
  " completion candidate. This is necessary because by default, Vim selects the
  " first candidate when completion is invoked, and selecting a candidate
  " automatically replaces the current text with it. Calling <c-p> forces Vim to
  " deselect the first candidate and in turn preserve the user's current text
  " until he explicitly chooses to replace it with a completion.
  call feedkeys( "\<C-X>\<C-U>\<C-P>", 'n' )
endfunction


python << EOF
def GetCompletions( query ):
  request = ycm_state.GetCurrentCompletionRequest()
  request.Start( query )
  while not request.Done():
    if bool( int( vim.eval( 'complete_check()' ) ) ):
      return { 'words' : [], 'refresh' : 'always'}

  results = base.AdjustCandidateInsertionText( request.Response() )
  return { 'words' : results, 'refresh' : 'always' }
EOF


function! s:CompletionsForQuery( query )
  py results = GetCompletions( vim.eval( 'a:query' ) )
  let results = pyeval( 'results' )
  let s:searched_and_results_found = len( results.words ) != 0
  return results
endfunction


" This is our main entry point. This is what vim calls to get completions.
function! youcompleteme#Complete( findstart, base )
  " After the user types one character after the call to the omnifunc, the
  " completefunc will be called because of our mapping that calls the
  " completefunc on every keystroke. Therefore we need to delegate the call we
  " 'stole' back to the omnifunc
  if s:omnifunc_mode
    return youcompleteme#OmniComplete( a:findstart, a:base )
  endif

  if a:findstart
    " InvokeCompletion has this check but we also need it here because of random
    " Vim bugs and unfortunate interactions with the autocommands of other
    " plugins
    if !s:cursor_moved
      " for vim, -2 means not found but don't trigger an error message
      " see :h complete-functions
      return -2
    endif

    py request = ycm_state.CreateCompletionRequest()
    if !pyeval( 'bool(request)' )
      return -2
    endif
    return pyeval( 'request.CompletionStartColumn()' )
  else
    return s:CompletionsForQuery( a:base )
  endif
endfunction


function! youcompleteme#OmniComplete( findstart, base )
  if a:findstart
    let s:omnifunc_mode = 1
    py request = ycm_state.CreateCompletionRequest( force_semantic = True )
    return pyeval( 'request.CompletionStartColumn()' )
  else
    return s:CompletionsForQuery( a:base )
  endif
endfunction


function! youcompleteme#ServerPid()
  return pyeval( 'ycm_state.ServerPid()' )
endfunction


function! s:RestartServer()
  py ycm_state.RestartServer()
endfunction

command! YcmRestartServer call s:RestartServer()


function! s:ShowDetailedDiagnostic()
  py ycm_state.ShowDetailedDiagnostic()
endfunction

command! YcmShowDetailedDiagnostic call s:ShowDetailedDiagnostic()


function! s:DebugInfo()
  echom "Printing YouCompleteMe debug information..."
  let debug_info = pyeval( 'ycm_state.DebugInfo()' )
  for line in split( debug_info, "\n" )
    echom '-- ' . line
  endfor
endfunction

command! YcmDebugInfo call s:DebugInfo()


function! s:CompleterCommand(...)
  " CompleterCommand will call the OnUserCommand function of a completer.
  " If the first arguments is of the form "ft=..." it can be used to specify the
  " completer to use (for example "ft=cpp").  Else the native filetype completer
  " of the current buffer is used.  If no native filetype completer is found and
  " no completer was specified this throws an error.  You can use
  " "ft=ycm:ident" to select the identifier completer.
  " The remaining arguments will be passed to the completer.
  let arguments = copy(a:000)
  let completer = ''

  if a:0 > 0 && strpart(a:1, 0, 3) == 'ft='
    if a:1 == 'ft=ycm:ident'
      let completer = 'identifier'
    endif
    let arguments = arguments[1:]
  endif

  py ycm_state.SendCommandRequest( vim.eval( 'l:arguments' ),
        \                          vim.eval( 'l:completer' ) )
endfunction


function! youcompleteme#OpenGoToList()
  set lazyredraw
  cclose
  execute 'belowright copen 3'
  set nolazyredraw
  au WinLeave <buffer> q  " automatically leave, if an option is chosen
  redraw!
endfunction


command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete
  \ YcmCompleter call s:CompleterCommand(<f-args>)

function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos )
  return join( pyeval( 'ycm_state.GetDefinedSubcommands()' ),
    \ "\n")
endfunction


function! s:ForceCompile()
  if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' )
    echom "Native filetype completion not supported for current file, "
          \ . "cannot force recompilation."
    return 0
  endif

  echom "Forcing compilation, this will block Vim until done."
  py ycm_state.OnFileReadyToParse()
  while 1
    let diagnostics_ready = pyeval(
          \ 'ycm_state.DiagnosticsForCurrentFileReady()' )
    if diagnostics_ready
      break
    endif

    sleep 100m
  endwhile
  return 1
endfunction


function! s:ForceCompileAndDiagnostics()
  let compilation_succeeded = s:ForceCompile()
  if !compilation_succeeded
    return
  endif

  call s:UpdateDiagnosticNotifications()
  echom "Diagnostics refreshed."
endfunction

command! YcmForceCompileAndDiagnostics call s:ForceCompileAndDiagnostics()


function! s:ShowDiagnostics()
  let compilation_succeeded = s:ForceCompile()
  if !compilation_succeeded
    return
  endif

  let diags = pyeval(
        \ 'ycm_state.GetDiagnosticsFromStoredRequest( qflist_format = True )' )
  if !empty( diags )
    call setloclist( 0, diags )

    if g:ycm_open_loclist_on_ycm_diags
      lopen
    endif
  else
    echom "No warnings or errors detected"
  endif
endfunction

command! YcmDiags call s:ShowDiagnostics()


" This is basic vim plugin boilerplate
let &cpo = s:save_cpo
unlet s:save_cpo