Skip to content

Commit c6e80b0

Browse files
committed
Provide a way to manually trigger YCM's completions
When automatic triggering is disabled, we now set the completefunc, so the user can use C-x C-u. We also provide a plug mapping that does the completion as users may not be comfortable with c-x c-u. As this is a very unusual use-case for YCM we sweep under the rug that most users probably want to use <Tab> for both completion and "next". Or perhaps want to use C-p in the same way. For now a unique mapping works.
1 parent 060c564 commit c6e80b0

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

README.md

+19-3
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,21 @@ If you want to just turn off the identifier completer but keep the semantic
26722672
triggers, you should set `g:ycm_min_num_of_chars_for_completion` to a high
26732673
number like `99`.
26742674

2675+
When `g:ycm_auto_trigger` is `0`, YCM sets the `completefunc`, so that you can
2676+
manually trigger normal completion using `C-x C-u`.
2677+
2678+
If you want to map something else to trigger completion, such as `C-d``,
2679+
then you can map it to `<plug>(YCMComplete)`. For example:
2680+
2681+
```viml
2682+
let g:ycm_auto_trigger = 0
2683+
imap <c-d> <plug>(YCMComplete)
2684+
```
2685+
2686+
NOTE: It's not possible to map one of the keys in
2687+
`g:ycm_key_list_select_completion` (or similar) to `<plug>(YCMComplete)`. In
2688+
practice that means that you can't use `<Tab>` for this.
2689+
26752690
Default: `1`
26762691

26772692
```viml
@@ -3386,9 +3401,10 @@ let g:ycm_key_list_stop_completion = ['<C-y>']
33863401
33873402
This option controls the key mapping used to invoke the completion menu for
33883403
semantic completion. By default, semantic completion is triggered automatically
3389-
after typing `.`, `->` and `::` in insert mode (if semantic completion support
3390-
has been compiled in). This key mapping can be used to trigger semantic
3391-
completion anywhere. Useful for searching for top-level functions and classes.
3404+
after typing characters appropriate for the language, such as `.`, `->`, `::`,
3405+
etc. in insert mode (if semantic completion support has been compiled in). This
3406+
key mapping can be used to trigger semantic completion anywhere. Useful for
3407+
searching for top-level functions and classes.
33923408
33933409
Console Vim (not Gvim or MacVim) passes `<Nul>` to Vim when the user types
33943410
`<C-Space>` so YCM will make sure that `<Nul>` is used in the map command when

autoload/youcompleteme.vim

+41-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ let g:ycm_neovim_ns_id = s:is_neovim ? nvim_create_namespace( 'ycm_id' ) : -1
3030
" This needs to be called outside of a function
3131
let s:script_folder_path = escape( expand( '<sfile>:p:h' ), '\' )
3232
let s:force_semantic = 0
33+
let s:force_manual = 0
3334
let s:completion_stopped = 0
3435
" These two variables are initialized in youcompleteme#Enable.
3536
let s:default_completion = {}
@@ -224,9 +225,9 @@ function! youcompleteme#Enable()
224225
\ } )
225226
endif
226227

227-
nnoremap <silent> <Plug>(YCMFindSymbolInWorkspace)
228+
nnoremap <silent> <plug>(YCMFindSymbolInWorkspace)
228229
\ :call youcompleteme#finder#FindSymbol( 'workspace' )<CR>
229-
nnoremap <silent> <Plug>(YCMFindSymbolInDocument)
230+
nnoremap <silent> <plug>(YCMFindSymbolInDocument)
230231
\ :call youcompleteme#finder#FindSymbol( 'document' )<CR>
231232
endfunction
232233

@@ -639,6 +640,9 @@ endfunction
639640

640641

641642
function! s:EnableCompletingInCurrentBuffer()
643+
if !g:ycm_auto_trigger
644+
call s:SetCompleteFunc()
645+
endif
642646
let b:ycm_completing = 1
643647
endfunction
644648

@@ -1010,10 +1014,11 @@ function! s:OnTextChangedInsertMode( popup_is_visible )
10101014
" CurrentIdentifierFinished check.
10111015
if s:force_semantic && !py3eval( 'base.LastEnteredCharIsIdentifierChar()' )
10121016
let s:force_semantic = 0
1017+
let s:force_manual = 0
10131018
endif
10141019

10151020
if get( b:, 'ycm_completing' ) &&
1016-
\ ( g:ycm_auto_trigger || s:force_semantic ) &&
1021+
\ ( g:ycm_auto_trigger || s:force_semantic || s:force_manual ) &&
10171022
\ !s:InsideCommentOrStringAndShouldStop() &&
10181023
\ !s:OnBlankLine()
10191024
call s:RequestCompletion()
@@ -1045,6 +1050,7 @@ function! s:OnInsertLeave()
10451050

10461051
call s:StopPoller( s:pollers.completion )
10471052
let s:force_semantic = 0
1053+
let s:force_manual = 0
10481054
let s:completion = s:default_completion
10491055

10501056
call s:OnFileReadyToParse()
@@ -1085,6 +1091,7 @@ function! s:IdentifierFinishedOperations()
10851091
endif
10861092
py3 ycm_state.OnCurrentIdentifierFinished()
10871093
let s:force_semantic = 0
1094+
let s:force_manual = 0
10881095
let s:completion = s:default_completion
10891096
endfunction
10901097

@@ -1149,8 +1156,37 @@ function! s:RequestCompletion()
11491156
endif
11501157
endfunction
11511158

1159+
function! s:ManuallyRequestCompletion() abort
1160+
" Since this function is called in a mapping through the expression register
1161+
" <C-R>=, its return value is inserted (see :h c_CTRL-R_=). We don't want to
1162+
" insert anything so we return an empty string.
1163+
1164+
if !s:AllowedToCompleteInCurrentBuffer()
1165+
return ''
1166+
endif
1167+
1168+
if get( b:, 'ycm_completing' )
1169+
let s:force_manual = 0
1170+
call s:RequestCompletion()
1171+
call s:RequestSignatureHelp()
1172+
endif
1173+
1174+
return ''
1175+
endfunction
1176+
1177+
function! s:SetCompleteFunc()
1178+
let &completefunc = 'youcompleteme#CompleteFunc'
1179+
endfunction
1180+
1181+
function! youcompleteme#CompleteFunc( findstart, base ) abort
1182+
call s:ManuallyRequestCompletion()
1183+
" Cancel, but silently stay in completion mode.
1184+
return -2
1185+
endfunction
1186+
1187+
inoremap <silent> <plug>(YCMComplete) <C-r>=<SID>ManuallyRequestCompletion()<CR>
11521188
1153-
function! s:RequestSemanticCompletion()
1189+
function! s:RequestSemanticCompletion() abort
11541190
if !s:AllowedToCompleteInCurrentBuffer()
11551191
return ''
11561192
endif
@@ -1682,7 +1718,7 @@ function! s:ToggleInlayHints()
16821718
endif
16831719
endfunction
16841720

1685-
silent! nnoremap <silent> <Plug>(YCMToggleInlayHints)
1721+
silent! nnoremap <silent> <plug>(YCMToggleInlayHints)
16861722
\ <cmd>call <SID>ToggleInlayHints()<CR>
16871723
16881724
" This is basic vim plugin boilerplate

vimrc_ycm_minimal

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ set encoding=utf-8
1313
let g:ycm_keep_logfiles = 1
1414
let g:ycm_log_level = 'debug'
1515

16-
" If you're on an arm mac, uncomment the following line
17-
" let g:ycm_clangd_binary_path=trim(system('brew --prefix llvm')).'/bin/clangd'
18-
1916
" If the base settings don't repro, paste your existing config for YCM only,
2017
" here:
2118
" let g:ycm_....
2219

2320
" Load YCM (only)
2421
let &rtp .= ',' . expand( '<sfile>:p:h' )
2522
filetype plugin indent on
26-
23+
syn on

0 commit comments

Comments
 (0)