-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
youcompleteme.vim
1777 lines (1434 loc) · 52.6 KB
/
youcompleteme.vim
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Copyright (C) 2011-2018 YouCompleteMe contributors
"
" 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
" NOTE: Neovim reports v:version as 800, which is garbage. For some features
" that are supported by our minimum Vim version, we have to guard them against
" neovim, which doesn't implement them.
let s:is_neovim = has( 'nvim' )
" Only useful in neovim, for handling text properties... I mean extmarks.
let g:ycm_neovim_ns_id = s:is_neovim ? nvim_create_namespace( 'ycm_id' ) : -1
" This needs to be called outside of a function
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
let s:force_semantic = 0
let s:force_manual = 0
let s:completion_stopped = 0
" These two variables are initialized in youcompleteme#Enable.
let s:default_completion = {}
let s:completion = s:default_completion
let s:default_signature_help = {}
let s:signature_help = s:default_completion
let s:previous_allowed_buffer_number = 0
let s:pollers = {
\ 'completion': {
\ 'id': -1,
\ 'wait_milliseconds': 10,
\ },
\ 'signature_help': {
\ 'id': -1,
\ 'wait_milliseconds': 10,
\ },
\ 'file_parse_response': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ },
\ 'server_ready': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ },
\ 'receive_messages': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ },
\ 'command': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ 'requests': {},
\ },
\ 'semantic_highlighting': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ },
\ 'inlay_hints': {
\ 'id': -1,
\ 'wait_milliseconds': 100,
\ },
\ }
let s:buftype_blacklist = {
\ 'help': 1,
\ 'terminal': 1,
\ 'quickfix': 1,
\ 'popup': 1,
\ 'nofile': 1,
\ }
let s:last_char_inserted_by_user = v:true
let s:enable_hover = 0
let s:cursorhold_popup = -1
let s:enable_inlay_hints = 0
let s:force_preview_popup = 0
let s:RESOLVE_NONE = 0
let s:RESOLVE_UP_FRONT = 1
let s:RESOLVE_ON_DEMAND = 2
let s:resolve_completions = s:RESOLVE_NONE
function! s:StartMessagePoll()
if s:pollers.receive_messages.id < 0
let s:pollers.receive_messages.id = timer_start(
\ s:pollers.receive_messages.wait_milliseconds,
\ function( 's:ReceiveMessages' ) )
endif
endfunction
function! s:ReceiveMessages( timer_id )
let poll_again = v:false
if s:AllowedToCompleteInCurrentBuffer()
let poll_again = py3eval( 'ycm_state.OnPeriodicTick()' )
endif
if poll_again
let s:pollers.receive_messages.id = timer_start(
\ s:pollers.receive_messages.wait_milliseconds,
\ function( 's:ReceiveMessages' ) )
else
" Don't poll again until we open another buffer
let s:pollers.receive_messages.id = -1
endif
endfunction
function! s:SetUpOptions()
call s:SetUpCommands()
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()
endfunction
function! youcompleteme#Enable()
call s:SetUpBackwardsCompatibility()
let completeopt = split( &completeopt, ',' )
" Will we add 'popup' to the 'completeopt' (later)
let s:force_preview_popup =
\ type( g:ycm_add_preview_to_completeopt ) == v:t_string &&
\ g:ycm_add_preview_to_completeopt ==# 'popup' &&
\ !s:is_neovim
" Will we add 'preview' to the 'completeopt' (later)
let force_preview =
\ type( g:ycm_add_preview_to_completeopt ) != v:t_string &&
\ g:ycm_add_preview_to_completeopt
" Will we be using the preview popup ? That is either the user set it in their
" completeopt or we're going to add it later.
let use_preview_popup =
\ s:force_preview_popup ||
\ index( completeopt, 'popup' ) >= 0
" We should only ask the server to resolve completion items upfront if we're
" going to display them - that is either:
" - popup is (or will be) in completeopt
" - preview is (or will be) in completeopt, or
let require_resolve =
\ use_preview_popup ||
\ force_preview ||
\ index( completeopt, 'preview' ) >= 0
if use_preview_popup && exists( '*popup_findinfo' )
" If the preview popup is going to be used, and on-demand resolve can be
" supported, enable it.
let s:resolve_completions = s:RESOLVE_ON_DEMAND
elseif require_resolve
" The preview window or info popup is enabled - request the server
" pre-resolves completion items
let s:resolve_completions = s:RESOLVE_UP_FRONT
else
" Otherwise, there's no point in resolving completions - they'll never be
" displayed.
endif
if !s:SetUpPython()
return
endif
call s:SetUpOptions()
py3 ycm_semantic_highlighting.Initialise()
let s:enable_inlay_hints = py3eval( 'ycm_inlay_hints.Initialise()' ) ? 1 : 0
call youcompleteme#EnableCursorMovedAutocommands()
augroup youcompleteme
autocmd!
" 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* FileType has already triggered for the initial file.
" We don't parse the buffer on the BufRead event since it would only be
" useful if the buffer filetype is set (we ignore the buffer if there is no
" filetype) and if so, the FileType event has triggered before and thus the
" buffer is already parsed.
autocmd BufWritePost,FileWritePost * call s:OnFileSave()
autocmd FileType * call s:OnFileTypeSet()
autocmd BufEnter,CmdwinEnter,WinEnter * call s:OnBufferEnter()
autocmd BufUnload * call s:OnBufferUnload()
autocmd InsertLeave * call s:OnInsertLeave()
autocmd VimLeave * call s:OnVimLeave()
autocmd CompleteDone * call s:OnCompleteDone()
autocmd CompleteChanged * call s:OnCompleteChanged()
augroup END
" The FileType event is not triggered for the first loaded file. We wait until
" the server is ready to manually run the s:OnFileTypeSet function.
let s:pollers.server_ready.id = timer_start(
\ s:pollers.server_ready.wait_milliseconds,
\ function( 's:PollServerReady' ) )
let s:default_completion = py3eval( 'vimsupport.NO_COMPLETIONS' )
let s:completion = s:default_completion
if s:PropertyTypeNotDefined( 'YCM-signature-help-current-argument' )
hi default YCMInverse term=reverse cterm=reverse gui=reverse
call prop_type_add( 'YCM-signature-help-current-argument', {
\ 'highlight': 'YCMInverse',
\ 'combine': 1,
\ 'priority': 50,
\ } )
endif
nnoremap <silent> <plug>(YCMFindSymbolInWorkspace)
\ :call youcompleteme#finder#FindSymbol( 'workspace' )<CR>
nnoremap <silent> <plug>(YCMFindSymbolInDocument)
\ :call youcompleteme#finder#FindSymbol( 'document' )<CR>
endfunction
function! youcompleteme#EnableCursorMovedAutocommands()
augroup ycmcompletemecursormove
autocmd!
autocmd CursorMoved * call s:OnCursorMovedNormalMode()
autocmd CursorMovedI * let s:current_cursor_position = getpos( '.' )
autocmd InsertEnter * call s:OnInsertEnter()
autocmd TextChanged * call s:OnTextChangedNormalMode()
autocmd TextChangedI * call s:OnTextChangedInsertMode( v:false )
autocmd TextChangedP * call s:OnTextChangedInsertMode( v:true )
autocmd InsertCharPre * call s:OnInsertChar()
if exists( '##WinScrolled' )
autocmd WinScrolled * call s:OnWinScrolled()
endif
augroup END
endfunction
function! youcompleteme#DisableCursorMovedAutocommands()
autocmd! ycmcompletemecursormove
endfunction
function! youcompleteme#GetErrorCount()
return py3eval( 'ycm_state.GetErrorCount()' )
endfunction
function! youcompleteme#GetWarningCount()
return py3eval( 'ycm_state.GetWarningCount()' )
endfunction
function! s:SetUpPython() abort
py3 << EOF
import os.path as p
import sys
import traceback
import vim
root_folder = p.normpath( p.join( vim.eval( 's:script_folder_path' ), '..' ) )
third_party_folder = p.join( root_folder, 'third_party' )
# Add dependencies to Python path.
sys.path[ 0:0 ] = [ p.join( root_folder, 'python' ),
p.join( third_party_folder, 'ycmd' ) ]
# We enclose this code in a try/except block to avoid backtraces in Vim.
try:
# Import the modules used in this file.
from ycm import base, vimsupport, youcompleteme
from ycm import semantic_highlighting as ycm_semantic_highlighting
from ycm import inlay_hints as ycm_inlay_hints
if 'ycm_state' in globals():
# If re-initializing, pretend that we shut down
ycm_state.OnVimLeave()
del ycm_state
# If we're able to resolve completion details asynchronously, set the option
# which enables this in the server.
if int( vim.eval( 's:resolve_completions == s:RESOLVE_ON_DEMAND' ) ):
# resolve a small number upfront, the rest on demand
default_options = {
'max_num_candidates_to_detail': 10
}
elif int( vim.eval( 's:resolve_completions == s:RESOLVE_NONE' ) ):
# don't resolve any
default_options = {
'max_num_candidates_to_detail': 0
}
else:
# i.e. s:resolve_completions == s:RESOLVE_UP_FRONT
# The server will decide - i.e. resolve everything upfront
default_options = {}
ycm_state = youcompleteme.YouCompleteMe( default_options )
except Exception as error:
# We don't use PostVimMessage or EchoText from the vimsupport module because
# importing this module may fail.
vim.command( 'redraw | echohl WarningMsg' )
for line in traceback.format_exc().splitlines():
vim.command( "echom '{0}'".format( line.replace( "'", "''" ) ) )
vim.command( "echo 'YouCompleteMe unavailable: {0}'"
.format( str( error ).replace( "'", "''" ) ) )
vim.command( 'echohl None' )
vim.command( 'return 0' )
else:
vim.command( 'return 1' )
EOF
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
for key in g:ycm_key_list_stop_completion
" When selecting a candidate and closing the completion menu with the <C-y>
" key, the menu will automatically be reopened because of the TextChangedI
" event. We define a command to prevent that.
exe 'inoremap <expr>' . key . ' <SID>StopCompletion( "\' . 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>'
imap <Nul> <C-Space>
endif
silent! exe 'inoremap <unique> <silent> ' . invoke_key .
\ ' <C-R>=<SID>RequestSemanticCompletion()<CR>'
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 default link YcmErrorSign SyntasticErrorSign
else
highlight default link YcmErrorSign error
endif
endif
if !hlexists( 'YcmWarningSign' )
if hlexists( 'SyntasticWarningSign')
highlight default link YcmWarningSign SyntasticWarningSign
else
highlight default link YcmWarningSign todo
endif
endif
if !hlexists( 'YcmErrorLine' )
highlight default link YcmErrorLine SyntasticErrorLine
endif
if !hlexists( 'YcmWarningLine' )
highlight default link YcmWarningLine SyntasticWarningLine
endif
call sign_define( [
\ { 'name': 'YcmError',
\ 'text': g:ycm_error_symbol,
\ 'texthl': 'YcmErrorSign',
\ 'linehl': 'YcmErrorLine',
\ 'group': 'ycm_signs' },
\ { 'name': 'YcmWarning',
\ 'text': g:ycm_warning_symbol,
\ 'texthl': 'YcmWarningSign',
\ 'linehl': 'YcmWarningLine',
\ 'group': 'ycm_signs' }
\ ] )
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 default link YcmErrorSection SyntasticError
else
highlight default link YcmErrorSection SpellBad
endif
endif
if s:PropertyTypeNotDefined( 'YcmErrorProperty' )
call prop_type_add( 'YcmErrorProperty', {
\ 'highlight': 'YcmErrorSection',
\ 'priority': 30,
\ 'combine': 0,
\ 'override': 1 } )
endif
" Used for virtual text
if !hlexists( 'YcmInvisible' )
highlight default link YcmInvisible Normal
endif
if !hlexists( 'YcmInlayHint' )
highlight default link YcmInlayHint NonText
endif
if !hlexists( 'YcmErrorText' )
if exists( '*hlget' )
let YcmErrorText = hlget( 'SpellBad', v:true )[ 0 ]
let YcmErrorText.name = 'YcmErrorText'
let YcmErrorText.cterm = {}
let YcmErrorText.gui = {}
let YcmErrorText.term = {}
call hlset( [ YcmErrorText ] )
else
" approximation
hi default link YcmErrorText WarningMsg
endif
endif
if !hlexists( 'YcmWarningText' )
if exists( '*hlget' )
let YcmWarningText = hlget( 'SpellCap', v:true )[ 0 ]
let YcmWarningText.name = 'YcmWarningText'
let YcmWarningText.cterm = {}
let YcmWarningText.gui = {}
let YcmWarningText.term = {}
call hlset( [ YcmWarningText] )
else
" Lame approximation
hi default link YcmWarningText Conceal
endif
endif
if s:PropertyTypeNotDefined( 'YcmVirtDiagError' )
call prop_type_add( 'YcmVirtDiagError', {
\ 'highlight': 'YcmErrorText',
\ 'priority': 20,
\ 'combine': 0 } )
endif
if s:PropertyTypeNotDefined( 'YcmVirtDiagWarning' )
call prop_type_add( 'YcmVirtDiagWarning', {
\ 'highlight': 'YcmWarningText',
\ 'priority': 19,
\ 'combine': 0 } )
endif
if s:PropertyTypeNotDefined( 'YcmVirtDiagPadding' )
call prop_type_add( 'YcmVirtDiagPadding', {
\ 'highlight': 'YcmInvisible',
\ 'priority': 100,
\ 'combine': 1 } )
endif
if !hlexists( 'YcmWarningSection' )
if hlexists( 'SyntasticWarning' )
highlight default link YcmWarningSection SyntasticWarning
else
highlight default link YcmWarningSection SpellCap
endif
endif
if s:PropertyTypeNotDefined( 'YcmWarningProperty' )
call prop_type_add( 'YcmWarningProperty', {
\ 'highlight': 'YcmWarningSection',
\ 'priority': 29,
\ 'combine': 0,
\ 'override': 1 } )
endif
if !hlexists( 'YcmErrorPopup' )
highlight default link YcmErrorPopup ErrorMsg
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 = []
let g:syntastic_cuda_checkers = []
endfunction
function! s:DisableOnLargeFile( buffer )
if exists( 'b:ycm_largefile' )
return b:ycm_largefile
endif
let threshold = g:ycm_disable_for_files_larger_than_kb * 1024
let b:ycm_largefile =
\ threshold > 0 && getfsize( expand( a:buffer ) ) > threshold
if b:ycm_largefile
py3 vimsupport.PostVimMessage( 'YouCompleteMe is disabled in this buffer;' +
\ ' the file exceeded the max size (see YCM options).' )
endif
return b:ycm_largefile
endfunction
function! s:PropertyTypeNotDefined( type )
return exists( '*prop_type_add' ) &&
\ index( prop_type_list(), a:type ) == -1
endfunction
function! s:AllowedToCompleteInBuffer( buffer )
let buftype = getbufvar( a:buffer, '&buftype' )
if has_key( s:buftype_blacklist, buftype )
return 0
endif
let filetype = getbufvar( a:buffer, '&filetype' )
if empty( filetype )
let filetype = 'ycm_nofiletype'
endif
let allowed = youcompleteme#filetypes#AllowedForFiletype( filetype )
if !allowed || s:DisableOnLargeFile( a:buffer )
return 0
endif
let s:previous_allowed_buffer_number = bufnr( a:buffer )
return allowed
endfunction
function! s:AllowedToCompleteInCurrentBuffer()
return s:AllowedToCompleteInBuffer( '%' )
endfunction
function! s:VisitedBufferRequiresReparse()
if bufnr( '%' ) ==# s:previous_allowed_buffer_number
return 0
endif
return s:AllowedToCompleteInCurrentBuffer()
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.
set shortmess+=c
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 s:resolve_completions == s:RESOLVE_ON_DEMAND
set completeopt+=popuphidden
endif
if s:force_preview_popup
set completeopt+=popup
elseif g:ycm_add_preview_to_completeopt
set completeopt+=preview
endif
endfunction
function! s:EnableCompletingInCurrentBuffer()
if !g:ycm_auto_trigger
call s:SetCompleteFunc()
endif
let b:ycm_completing = 1
endfunction
function s:StopPoller( poller ) abort
call timer_stop( a:poller.id )
let a:poller.id = -1
endfunction
function! s:OnVimLeave()
" Workaround a NeoVim issue - not shutting down timers correctly
" https://github.com/neovim/neovim/issues/6840
for poller in values( s:pollers )
call s:StopPoller( poller )
endfor
py3 ycm_state.OnVimLeave()
endfunction
function! s:OnCompleteDone()
if !s:AllowedToCompleteInCurrentBuffer()
return
endif
let s:last_char_inserted_by_user = v:false
py3 ycm_state.OnCompleteDone()
call s:UpdateSignatureHelp()
endfunction
function! s:OnCompleteChanged()
if !s:AllowedToCompleteInCurrentBuffer()
return
endif
if ! empty( v:event.completed_item )
let s:last_char_inserted_by_user = v:false
call s:ResolveCompletionItem( v:event.completed_item )
endif
call s:UpdateSignatureHelp()
endfunction
function! s:ResolveCompletionItem( item )
if s:resolve_completions != s:RESOLVE_ON_DEMAND
return
endif
let complete_mode = complete_info( [ 'mode' ] ).mode
if complete_mode !=# 'eval' && complete_mode !=# 'function'
return
endif
if py3eval( 'ycm_state.ResolveCompletionItem( vim.eval( "a:item" ) )' )
call s:StopPoller( s:pollers.completion )
call timer_start( 0, function( 's:PollResolve', [ a:item ] ) )
else
call s:ShowInfoPopup( a:item )
endif
endfunction
function! s:EnableAutoHover()
if g:ycm_auto_hover ==# 'CursorHold' && s:enable_hover
augroup YcmBufHover
autocmd! * <buffer>
autocmd CursorHold <buffer> call s:Hover()
if exists( '##WinResized' )
autocmd WinResized <buffer> call popup_close( s:cursorhold_popup )
endif
if exists( '##WinScrolled' )
autocmd WinScrolled <buffer> call popup_close( s:cursorhold_popup )
endif
augroup END
endif
endfunction
function! s:DisableAutoHover()
augroup YcmBufHover
autocmd! * <buffer>
augroup END
endfunction
function! s:OnFileTypeSet()
" The contents of the command-line window are empty when the filetype is set
" for the first time. Users should never change its filetype so we only rely
" on the CmdwinEnter event for that window.
if !empty( getcmdwintype() )
return
endif
if !s:AllowedToCompleteInCurrentBuffer()
return
endif
call s:SetUpCompleteopt()
call s:EnableCompletingInCurrentBuffer()
call s:StartMessagePoll()
call s:EnableAutoHover()
py3 ycm_state.OnFileTypeSet()
call s:OnFileReadyToParse( 1 )
endfunction
function! s:OnFileSave()
let buffer_number = str2nr( expand( '<abuf>' ) )
if !s:AllowedToCompleteInBuffer( buffer_number )
return
endif
py3 ycm_state.OnFileSave( vimsupport.GetIntValue( 'buffer_number' ) )
endfunction
function! s:AbortAutohoverRequest() abort
if g:ycm_auto_hover ==# 'CursorHold' && s:enable_hover
let requests = copy( s:pollers.command.requests )
for request_id in keys( requests )
let request = requests[ request_id ]
if request.origin == 'autohover'
call remove( s:pollers.command.requests, request_id )
call request.callback( '' )
endif
endfor
endif
endfunction
function! s:OnBufferEnter()
call s:StartMessagePoll()
if !s:VisitedBufferRequiresReparse()
return
endif
call s:AbortAutohoverRequest()
call s:SetUpCompleteopt()
call s:EnableCompletingInCurrentBuffer()
py3 ycm_state.UpdateMatches()
py3 ycm_state.OnBufferVisit()
" Last parse may be outdated because of changes from other buffers. Force a
" new parse.
call s:OnFileReadyToParse( 1 )
endfunction
function! s:OnBufferUnload()
" Expanding <abuf> returns the unloaded buffer number as a string but we want
" it as a true number for the getbufvar function.
let buffer_number = str2nr( expand( '<abuf>' ) )
if !s:AllowedToCompleteInBuffer( buffer_number )
return
endif
py3 ycm_state.OnBufferUnload( vimsupport.GetIntValue( 'buffer_number' ) )
endfunction
function! s:PollServerReady( timer_id )
if !py3eval( 'ycm_state.IsServerAlive()' )
py3 ycm_state.NotifyUserIfServerCrashed()
" Server crashed. Don't poll it again.
return
endif
if !py3eval( 'ycm_state.CheckIfServerIsReady()' )
let s:pollers.server_ready.id = timer_start(
\ s:pollers.server_ready.wait_milliseconds,
\ function( 's:PollServerReady' ) )
return
endif
call s:OnFileTypeSet()
endfunction
function! s:OnFileReadyToParse( ... )
" Accepts an optional parameter that is either 0 or 1. If 1, send a
" FileReadyToParse event notification, whether the buffer has changed or not;
" effectively forcing a parse of the buffer. Default is 0.
let force_parsing = a:0 > 0 && a:1
" We only want to send a new FileReadyToParse event notification if the buffer
" has changed since the last time we sent one, or if forced.
if force_parsing || py3eval( "ycm_state.NeedsReparse()" )
" We switched buffers or something, so clear.
" FIXME: sig help should be buffer local?
call s:ClearSignatureHelp()
py3 ycm_state.OnFileReadyToParse()
call s:StopPoller( s:pollers.file_parse_response )
let s:pollers.file_parse_response.id = timer_start(
\ s:pollers.file_parse_response.wait_milliseconds,
\ function( 's:PollFileParseResponse' ) )
call s:UpdateSemanticHighlighting( bufnr(), 1, 0 )
call s:UpdateInlayHints( bufnr(), 1, 0 )
endif
endfunction
function! s:UpdateSemanticHighlighting( bufnr, force, redraw_anyway ) abort
call s:StopPoller( s:pollers.semantic_highlighting )
if !s:is_neovim &&
\ get( b:, 'ycm_enable_semantic_highlighting',
\ get( g:, 'ycm_enable_semantic_highlighting', 0 ) )
if py3eval(
\ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) ).'
\ . 'semantic_highlighting.Request( '
\ . ' force=int( vim.eval( "a:force" ) ) )' )
let s:pollers.semantic_highlighting.id = timer_start(
\ s:pollers.semantic_highlighting.wait_milliseconds,
\ function( 's:PollSemanticHighlighting', [ a:bufnr ] ) )
elseif a:redraw_anyway
py3 ycm_state.Buffer(
\ int( vim.eval( "a:bufnr" ) ) ).semantic_highlighting.Refresh()
endif
endif
endfunction
function s:ShouldUseInlayHintsNow( bufnr )
return s:enable_inlay_hints &&
\ getbufvar( a:bufnr, 'ycm_enable_inlay_hints',
\ get( g:, 'ycm_enable_inlay_hints', 0 ) )
endfunction
function! s:UpdateInlayHints( bufnr, force, redraw_anyway ) abort
call s:StopPoller( s:pollers.inlay_hints )
if s:ShouldUseInlayHintsNow( a:bufnr )
if py3eval(
\ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) ).'
\ . 'inlay_hints.Request( force=int( vim.eval( "a:force" ) ) )' )
let s:pollers.inlay_hints.id = timer_start(
\ s:pollers.inlay_hints.wait_milliseconds,
\ function( 's:PollInlayHints', [ a:bufnr ] ) )
elseif a:redraw_anyway
py3 ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) ).inlay_hints.Refresh()
endif
endif
endfunction
function! s:PollFileParseResponse( ... )
if !py3eval( "ycm_state.FileParseRequestReady()" )
let s:pollers.file_parse_response.id = timer_start(
\ s:pollers.file_parse_response.wait_milliseconds,
\ function( 's:PollFileParseResponse' ) )
return
endif
py3 ycm_state.HandleFileParseRequest()
if py3eval( "ycm_state.ShouldResendFileParseRequest()" )
call s:OnFileReadyToParse( 1 )
endif
endfunction
function! s:PollSemanticHighlighting( bufnr, ... ) abort
return s:PollScrollable( a:bufnr, 'semantic_highlighting' )
endfunction
function! s:PollInlayHints( bufnr, ... ) abort
return s:PollScrollable( a:bufnr, 'inlay_hints' )
endfunction
function! s:PollScrollable( bufnr, scrollable, ... ) abort
if !py3eval(
\ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) )'
\ . '.' . a:scrollable . '.Ready()' )
let s:pollers[a:scrollable].id = timer_start(
\ s:pollers[a:scrollable].wait_milliseconds,
\ function( 's:PollScrollable', [ a:bufnr, a:scrollable ] ) )
elseif ! py3eval(
\ 'ycm_state.Buffer( int( vim.eval( "a:bufnr" ) ) )'
\ . '.' . a:scrollable . '.Update()' )
let s:pollers[ a:scrollable ].id = timer_start(
\ s:pollers[ a:scrollable ].wait_milliseconds,
\ function( 's:PollScrollable', [ a:bufnr, a:scrollable ] ) )
endif
endfunction
function! s:SendKeys( keys )
" By default keys are added to the end of the typeahead buffer. If there are
" already keys in the buffer, they will be processed first and may change
" the state that our keys combination was sent for (e.g. <C-X><C-U><C-P> in
" normal mode instead of insert mode or <C-e> outside of completion mode).
" We avoid that by inserting the keys at the start of the typeahead buffer
" with the 'i' option. Also, we don't want the keys to be remapped to
" something else so we add the 'n' option.
call feedkeys( a:keys, 'in' )
endfunction
function! s:CloseCompletionMenu()
if pumvisible()
call s:SendKeys( "\<C-e>" )
endif
endfunction
function! s:OnInsertChar()
if !s:AllowedToCompleteInCurrentBuffer()
return
endif
let s:last_char_inserted_by_user = v:true
endfunction
function! s:StopCompletion( key )
call s:StopPoller( s:pollers.completion )
call s:ClearSignatureHelp()
if pumvisible()
let s:completion_stopped = 1
return "\<C-y>"
endif
return a:key
endfunction
function! s:OnCursorMovedNormalMode()
if !s:AllowedToCompleteInCurrentBuffer()
return
endif
call s:AbortAutohoverRequest()
py3 ycm_state.OnCursorMoved()
endfunction
function! s:OnWinScrolled()
if !s:AllowedToCompleteInCurrentBuffer()
return
endif