Skip to content

Commit

Permalink
Provide a way to manually trigger YCM's completions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
puremourning committed Dec 14, 2022
1 parent 060c564 commit c6e80b0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2672,6 +2672,21 @@ If you want to just turn off the identifier completer but keep the semantic
triggers, you should set `g:ycm_min_num_of_chars_for_completion` to a high
number like `99`.

When `g:ycm_auto_trigger` is `0`, YCM sets the `completefunc`, so that you can
manually trigger normal completion using `C-x C-u`.

If you want to map something else to trigger completion, such as `C-d``,
then you can map it to `<plug>(YCMComplete)`. For example:

```viml
let g:ycm_auto_trigger = 0
imap <c-d> <plug>(YCMComplete)
```

NOTE: It's not possible to map one of the keys in
`g:ycm_key_list_select_completion` (or similar) to `<plug>(YCMComplete)`. In
practice that means that you can't use `<Tab>` for this.

Default: `1`

```viml
Expand Down Expand Up @@ -3386,9 +3401,10 @@ let g:ycm_key_list_stop_completion = ['<C-y>']
This option controls the key mapping used to invoke the completion menu for
semantic completion. By default, semantic completion is triggered automatically
after typing `.`, `->` and `::` in insert mode (if semantic completion support
has been compiled in). This key mapping can be used to trigger semantic
completion anywhere. Useful for searching for top-level functions and classes.
after typing characters appropriate for the language, such as `.`, `->`, `::`,
etc. in insert mode (if semantic completion support has been compiled in). This
key mapping can be used to trigger semantic completion anywhere. Useful for
searching for top-level functions and classes.
Console Vim (not Gvim or MacVim) passes `<Nul>` to Vim when the user types
`<C-Space>` so YCM will make sure that `<Nul>` is used in the map command when
Expand Down
46 changes: 41 additions & 5 deletions autoload/youcompleteme.vim
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ 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 = {}
Expand Down Expand Up @@ -224,9 +225,9 @@ function! youcompleteme#Enable()
\ } )
endif

nnoremap <silent> <Plug>(YCMFindSymbolInWorkspace)
nnoremap <silent> <plug>(YCMFindSymbolInWorkspace)
\ :call youcompleteme#finder#FindSymbol( 'workspace' )<CR>
nnoremap <silent> <Plug>(YCMFindSymbolInDocument)
nnoremap <silent> <plug>(YCMFindSymbolInDocument)
\ :call youcompleteme#finder#FindSymbol( 'document' )<CR>
endfunction

Expand Down Expand Up @@ -639,6 +640,9 @@ endfunction


function! s:EnableCompletingInCurrentBuffer()
if !g:ycm_auto_trigger
call s:SetCompleteFunc()
endif
let b:ycm_completing = 1
endfunction

Expand Down Expand Up @@ -1010,10 +1014,11 @@ function! s:OnTextChangedInsertMode( popup_is_visible )
" CurrentIdentifierFinished check.
if s:force_semantic && !py3eval( 'base.LastEnteredCharIsIdentifierChar()' )
let s:force_semantic = 0
let s:force_manual = 0
endif

if get( b:, 'ycm_completing' ) &&
\ ( g:ycm_auto_trigger || s:force_semantic ) &&
\ ( g:ycm_auto_trigger || s:force_semantic || s:force_manual ) &&
\ !s:InsideCommentOrStringAndShouldStop() &&
\ !s:OnBlankLine()
call s:RequestCompletion()
Expand Down Expand Up @@ -1045,6 +1050,7 @@ function! s:OnInsertLeave()

call s:StopPoller( s:pollers.completion )
let s:force_semantic = 0
let s:force_manual = 0
let s:completion = s:default_completion

call s:OnFileReadyToParse()
Expand Down Expand Up @@ -1085,6 +1091,7 @@ function! s:IdentifierFinishedOperations()
endif
py3 ycm_state.OnCurrentIdentifierFinished()
let s:force_semantic = 0
let s:force_manual = 0
let s:completion = s:default_completion
endfunction

Expand Down Expand Up @@ -1149,8 +1156,37 @@ function! s:RequestCompletion()
endif
endfunction

function! s:ManuallyRequestCompletion() abort
" Since this function is called in a mapping through the expression register
" <C-R>=, its return value is inserted (see :h c_CTRL-R_=). We don't want to
" insert anything so we return an empty string.

if !s:AllowedToCompleteInCurrentBuffer()
return ''
endif

if get( b:, 'ycm_completing' )
let s:force_manual = 0
call s:RequestCompletion()
call s:RequestSignatureHelp()
endif

return ''
endfunction

function! s:SetCompleteFunc()
let &completefunc = 'youcompleteme#CompleteFunc'
endfunction

function! youcompleteme#CompleteFunc( findstart, base ) abort
call s:ManuallyRequestCompletion()
" Cancel, but silently stay in completion mode.
return -2
endfunction

inoremap <silent> <plug>(YCMComplete) <C-r>=<SID>ManuallyRequestCompletion()<CR>
function! s:RequestSemanticCompletion()
function! s:RequestSemanticCompletion() abort
if !s:AllowedToCompleteInCurrentBuffer()
return ''
endif
Expand Down Expand Up @@ -1682,7 +1718,7 @@ function! s:ToggleInlayHints()
endif
endfunction

silent! nnoremap <silent> <Plug>(YCMToggleInlayHints)
silent! nnoremap <silent> <plug>(YCMToggleInlayHints)
\ <cmd>call <SID>ToggleInlayHints()<CR>
" This is basic vim plugin boilerplate
Expand Down
5 changes: 1 addition & 4 deletions vimrc_ycm_minimal
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ set encoding=utf-8
let g:ycm_keep_logfiles = 1
let g:ycm_log_level = 'debug'

" If you're on an arm mac, uncomment the following line
" let g:ycm_clangd_binary_path=trim(system('brew --prefix llvm')).'/bin/clangd'

" If the base settings don't repro, paste your existing config for YCM only,
" here:
" let g:ycm_....

" Load YCM (only)
let &rtp .= ',' . expand( '<sfile>:p:h' )
filetype plugin indent on

syn on

0 comments on commit c6e80b0

Please sign in to comment.