diff --git a/ycmd/completers/typescript/typescript_completer.py b/ycmd/completers/typescript/typescript_completer.py index e9dcd1c809..eb41323233 100755 --- a/ycmd/completers/typescript/typescript_completer.py +++ b/ycmd/completers/typescript/typescript_completer.py @@ -301,6 +301,8 @@ def GetSubcommandsMap( self ): self._GoToDefinition( request_data ) ), 'GoToReferences' : ( lambda self, request_data, args: self._GoToReferences( request_data ) ), + 'GoToType' : ( lambda self, request_data, args: + self._GoToType( request_data ) ), 'GetType' : ( lambda self, request_data, args: self._GetType( request_data ) ), 'GetDoc' : ( lambda self, request_data, args: @@ -358,6 +360,25 @@ def _GoToReferences( self, request_data ): ) for ref in response[ 'refs' ] ] + def _GoToType( self, request_data ): + self._Reload( request_data ) + try: + filespans = self._SendRequest( 'typeDefinition', { + 'file': request_data[ 'filepath' ], + 'line': request_data[ 'line_num' ], + 'offset': request_data[ 'column_num' ] + } ) + + span = filespans[ 0 ] + return responses.BuildGoToResponse( + filepath = span[ 'file' ], + line_num = span[ 'start' ][ 'line' ], + column_num = span[ 'start' ][ 'offset' ] + ) + except RuntimeError: + raise RuntimeError( 'Could not find type definition' ) + + def _GetType( self, request_data ): self._Reload( request_data ) info = self._SendRequest( 'quickinfo', { diff --git a/ycmd/tests/typescript/subcommands_test.py b/ycmd/tests/typescript/subcommands_test.py index a5e501927b..da1fd1f33a 100644 --- a/ycmd/tests/typescript/subcommands_test.py +++ b/ycmd/tests/typescript/subcommands_test.py @@ -233,6 +233,62 @@ def Subcommands_GoTo_Fail_test( app ): ErrorMatcher( RuntimeError, 'Could not find definition' ) ) +@SharedYcmd +def Subcommands_GoToType_test( app ): + filepath = PathToTestFile( 'test.ts' ) + contents = ReadFile( filepath ) + + event_data = BuildRequest( filepath = filepath, + filetype = 'typescript', + contents = contents, + event_name = 'BufferVisit' ) + + app.post_json( '/event_notification', event_data ) + + goto_data = BuildRequest( completer_target = 'filetype_default', + command_arguments = [ 'GoToType' ], + line_num = 14, + column_num = 6, + contents = contents, + filetype = 'typescript', + filepath = filepath ) + + response = app.post_json( '/run_completer_command', goto_data ).json + assert_that( response, + has_entries( { + 'filepath': filepath, + 'line_num': 2, + 'column_num': 1, + } ) ) + + +@SharedYcmd +def Subcommands_GoToType_fail_test( app ): + filepath = PathToTestFile( 'test.ts' ) + contents = ReadFile( filepath ) + + event_data = BuildRequest( filepath = filepath, + filetype = 'typescript', + contents = contents, + event_name = 'BufferVisit' ) + + app.post_json( '/event_notification', event_data ) + + goto_data = BuildRequest( completer_target = 'filetype_default', + command_arguments = [ 'GoToType' ], + line_num = 39, + column_num = 8, + contents = contents, + filetype = 'typescript', + filepath = filepath ) + + response = app.post_json( '/run_completer_command', + goto_data, + expect_errors = True ).json + assert_that( response, + ErrorMatcher( RuntimeError, 'Could not find type definition' ) ) + + @SharedYcmd def Subcommands_RefactorRename_Missing_test( app ): filepath = PathToTestFile( 'test.ts' )