From 446d02f66edcce63852b601e92db6865d18193d4 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 27 Sep 2013 16:20:35 -0700 Subject: [PATCH] Subcommand name completion works again --- autoload/youcompleteme.vim | 16 ++++------ python/ycm/server/tests/basic_test.py | 46 ++++++++++++++++++++++++--- python/ycm/server/ycmd.py | 26 +++++++++++---- python/ycm/youcompleteme.py | 5 +++ 4 files changed, 72 insertions(+), 21 deletions(-) diff --git a/autoload/youcompleteme.vim b/autoload/youcompleteme.vim index ed1577a3..c60c207a 100644 --- a/autoload/youcompleteme.vim +++ b/autoload/youcompleteme.vim @@ -619,17 +619,13 @@ function! youcompleteme#OpenGoToList() endfunction -command! -nargs=* YcmCompleter call s:CompleterCommand() +command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete + \ YcmCompleter call s:CompleterCommand() -" TODO: Make this work again -" command! -nargs=* -complete=custom,youcompleteme#SubCommandsComplete -" \ YcmCompleter call s:CompleterCommand() -" -" -" function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) -" return join( pyeval( 'ycm_state.GetFiletypeCompleter().DefinedSubcommands()' ), -" \ "\n") -" endfunction +function! youcompleteme#SubCommandsComplete( arglead, cmdline, cursorpos ) + return join( pyeval( 'ycm_state.GetDefinedSubcommands()' ), + \ "\n") +endfunction function! s:ForceCompile() diff --git a/python/ycm/server/tests/basic_test.py b/python/ycm/server/tests/basic_test.py index 632e9d40..ad8b52a8 100644 --- a/python/ycm/server/tests/basic_test.py +++ b/python/ycm/server/tests/basic_test.py @@ -26,14 +26,15 @@ import bottle bottle.debug( True ) # 'contents' should be just one line of text -def RequestDataForFileWithContents( filename, contents ): +def RequestDataForFileWithContents( filename, contents = None ): + real_contents = contents if contents else '' return { 'filetypes': ['foo'], 'filepath': filename, - 'line_value': contents, + 'line_value': real_contents, 'file_data': { filename: { - 'contents': contents, + 'contents': real_contents, 'filetypes': ['foo'] } } @@ -71,7 +72,7 @@ def GetCompletions_IdentifierCompleter_Works_test(): @with_setup( Setup ) def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data.update( { 'event_name': 'FileReadyToParse', 'syntax_keywords': ['foo', 'bar', 'zoo'] @@ -96,7 +97,7 @@ def GetCompletions_IdentifierCompleter_SyntaxKeywordsAdded_test(): @with_setup( Setup ) def GetCompletions_UltiSnipsCompleter_Works_test(): app = TestApp( ycmd.app ) - event_data = RequestDataForFileWithContents( '/foo/bar', '' ) + event_data = RequestDataForFileWithContents( '/foo/bar' ) event_data.update( { 'event_name': 'BufferVisit', 'ultisnips_snippets': [ @@ -197,6 +198,41 @@ int main() app.post_json( '/run_completer_command', goto_data ).json ) +@with_setup( Setup ) +def DefinedSubcommands_Works_test(): + app = TestApp( ycmd.app ) + subcommands_data = RequestDataForFileWithContents( '/foo/bar' ) + subcommands_data.update( { + 'completer_target': 'python', + } ) + + eq_( [ 'GoToDefinition', + 'GoToDeclaration', + 'GoToDefinitionElseDeclaration' ], + app.post_json( '/defined_subcommands', subcommands_data ).json ) + + +@with_setup( Setup ) +def DefinedSubcommands_WorksWhenNoExplicitCompleterTargetSpecified_test(): + app = TestApp( ycmd.app ) + filename = 'foo.py' + subcommands_data = { + 'filetypes': ['python'], + 'filepath': filename, + 'file_data': { + filename: { + 'contents': '', + 'filetypes': ['python'] + } + } + } + + eq_( [ 'GoToDefinition', + 'GoToDeclaration', + 'GoToDefinitionElseDeclaration' ], + app.post_json( '/defined_subcommands', subcommands_data ).json ) + + @with_setup( Setup ) def FiletypeCompletionAvailable_Works_test(): app = TestApp( ycmd.app ) diff --git a/python/ycm/server/ycmd.py b/python/ycm/server/ycmd.py index c0284c15..9a628761 100755 --- a/python/ycm/server/ycmd.py +++ b/python/ycm/server/ycmd.py @@ -69,12 +69,7 @@ def EventNotification(): def RunCompleterCommand(): LOGGER.info( 'Received command request') request_data = request.json - completer_target = request_data[ 'completer_target' ] - - if completer_target == 'identifier': - completer = SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() - else: - completer = SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) + completer = _GetCompleterForRequestData( request_data ) return _JsonResponse( completer.OnUserCommand( request_data[ 'command_arguments' ], @@ -124,6 +119,14 @@ def FiletypeCompletionAvailable(): request.json[ 'filetypes' ] ) ) +@app.post( '/defined_subcommands') +def DefinedSubcommands(): + LOGGER.info( 'Received defined subcommands request') + completer = _GetCompleterForRequestData( request.json ) + + return _JsonResponse( completer.DefinedSubcommands() ) + + # The type of the param is Bottle.HTTPError @app.error( 500 ) def ErrorHandler( httperror ): @@ -136,6 +139,17 @@ def _JsonResponse( data ): return json.dumps( data ) +def _GetCompleterForRequestData( request_data ): + completer_target = request_data.get( 'completer_target', None ) + + if completer_target == 'identifier': + return SERVER_STATE.GetGeneralCompleter().GetIdentifierCompleter() + elif completer_target == 'filetype_default' or not completer_target: + return SERVER_STATE.GetFiletypeCompleter( request_data[ 'filetypes' ] ) + else: + return SERVER_STATE.GetFiletypeCompleter( [ completer_target ] ) + + @atexit.register def _ServerShutdown(): if SERVER_STATE: diff --git a/python/ycm/youcompleteme.py b/python/ycm/youcompleteme.py index a6bdc490..0e0009d6 100644 --- a/python/ycm/youcompleteme.py +++ b/python/ycm/youcompleteme.py @@ -100,6 +100,11 @@ class YouCompleteMe( object ): return SendCommandRequest( arguments, completer ) + def GetDefinedSubcommands( self ): + return BaseRequest.PostDataToHandler( BuildRequestData(), + 'defined_subcommands' ) + + def GetCurrentCompletionRequest( self ): return self._current_completion_request