From 3d305f9c74522160e4002f6241f5e8ad2ed5274e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= Date: Mon, 25 Feb 2013 15:14:01 +0100 Subject: [PATCH] Implement completer-specific commands This provides a framework for completer-writers to create completer-specific commands. I have in mind to use this for the clang completer to force reloading of a flags module via `:YcmCompleter reload`. --- README.md | 9 +++++++++ autoload/youcompleteme.vim | 34 ++++++++++++++++++++++++++++++++++ python/completers/completer.py | 15 ++++++++++++++- python/ycm.py | 4 ++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 66fee654..7f6a43b0 100644 --- a/README.md +++ b/README.md @@ -446,6 +446,15 @@ This will print out various debug information for the current file. Useful to see what compile commands will be used for the file if you're using the semantic completion engine. +### The `YcmCompleter` command + +This command can be used to invoke completer-specific commands. If the first +argument is of the form `ft=...` the completer for that file type will be used +(for example `ft=cpp`), else the native completer of the current buffer will be +used. +Call `YcmCompleter` without further arguments for information about the +commands you can call for the selected completer. + Options ------- diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index 7dcc3973..a3acf3a7 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -484,6 +484,40 @@ 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:omni" + " to select the omni completer or "ft=ycm:ident" to select the identifier + " completer. The remaining arguments will passed to the completer. + let arguments = copy(a:000) + + if a:0 > 0 && strpart(a:1, 0, 3) == 'ft=' + if a:1 == 'ft=ycm:omni' + py completer = ycm_state.GetOmniCompleter() + elseif a:1 == 'ft=ycm:ident' + py completer = ycm_state.GetIdentifierCompleter() + else + py completer = ycm_state.GetFiletypeCompleterForFiletype( + \ vim.eval('a:1').lstrip('ft=') ) + endif + let arguments = arguments[1:] + elseif pyeval( 'ycm_state.NativeFiletypeCompletionAvailable()' ) + py completer = ycm_state.GetFiletypeCompleter() + else + echohl WarningMsg | + \ echomsg "No native completer found for current buffer." | + \ echomsg "Use ft=... as the first argument to specify a completer." | + \ echohl None + return + endif + + py completer.OnUserCommand( vim.eval( 'l:arguments' ) ) +endfunction + +command! -nargs=* YcmCompleter call s:CompleterCommand() function! s:ForceCompile() if !pyeval( 'ycm_state.NativeFiletypeCompletionUsable()' ) diff --git a/python/completers/completer.py b/python/completers/completer.py index 35ad7268..fbdcf67e 100644 --- a/python/completers/completer.py +++ b/python/completers/completer.py @@ -23,6 +23,7 @@ import vimsupport import ycm_core from collections import defaultdict +NO_USER_COMMANDS = 'This completer does not define any commands.' class Completer( object ): """A base class for all Completers in YCM. @@ -96,7 +97,15 @@ class Completer( object ): their specific events occur. For instance, the identifier completer collects all the identifiers in the file in OnFileReadyToParse() which gets called when the user stops typing for 2 seconds (Vim's CursorHold and CursorHoldI events). - """ + + One special function is OnUserCommand. It is called when the user uses the + command :YcmCompleter and is passed all extra arguments used on command + invocation (e.g. OnUserCommand(['first argument', 'second'])). This can be + used for completer-specific commands such as reloading external + configuration. + When the command is called with no arguments you should print a short summary + of the supported commands or point the user to the help section where this + information can be found.""" __metaclass__ = abc.ABCMeta @@ -234,6 +243,10 @@ class Completer( object ): pass + def OnUserCommand( self, arguments ): + vimsupport.PostVimMessage( NO_USER_COMMANDS ) + + def OnCurrentIdentifierFinished( self ): pass diff --git a/python/ycm.py b/python/ycm.py index c47cf754..f39ee81a 100644 --- a/python/ycm.py +++ b/python/ycm.py @@ -53,6 +53,10 @@ class YouCompleteMe( object ): return self.identcomp + def GetOmniCompleter( self ): + return self.omnicomp + + def GetFiletypeCompleter( self ): filetypes = vimsupport.CurrentFiletypes()