Skip to content

Commit

Permalink
Merge pull request ycm-core#509 from Chiel92/master
Browse files Browse the repository at this point in the history
Goto definition support for C# completer
  • Loading branch information
Valloric committed Aug 16, 2013
2 parents 745d48d + 44786c5 commit 9d14562
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ The various `GoTo*` subcommands add entries to Vim's `jumplist` so you can use

Looks up the symbol under the cursor and jumps to its declaration.

Supported in filetypes: `c, cpp, objc, objcpp, python`
Supported in filetypes: `c, cpp, objc, objcpp, python, cs`

### The `GoToDefinition` subcommand

Expand All @@ -574,15 +574,15 @@ the definition of the symbol is in the current translation unit. A translation
unit consists of the file you are editing and all the files you are including
with `#include` directives (directly or indirectly) in that file.

Supported in filetypes: `c, cpp, objc, objcpp, python`
Supported in filetypes: `c, cpp, objc, objcpp, python, cs`

### The `GoToDefinitionElseDeclaration` subcommand

Looks up the symbol under the cursor and jumps to its definition if possible; if
the definition is not accessible from the current translation unit, jumps to the
symbol's declaration.

Supported in filetypes: `c, cpp, objc, objcpp, python`
Supported in filetypes: `c, cpp, objc, objcpp, python, cs`

### The `ClearCompilationFlagCache` subcommand

Expand Down
74 changes: 48 additions & 26 deletions python/ycm/completers/cs/cs_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class CsharpCompleter( ThreadedCompleter ):

def __init__( self ):
super( CsharpCompleter, self ).__init__()
self._omnisharp_port = int( vimsupport.GetVariableValue(
'g:ycm_csharp_server_port' ) )
self._omnisharp_port = None

if vimsupport.GetBoolValue( 'g:ycm_auto_start_csharp_server' ):
self._StartServer()
Expand All @@ -71,7 +70,10 @@ def ComputeCandidates( self, unused_query, unused_start_column ):
def DefinedSubcommands( self ):
return [ 'StartServer',
'StopServer',
'RestartServer' ]
'RestartServer',
'GoToDefinition',
'GoToDeclaration',
'GoToDefinitionElseDeclaration' ]


def OnUserCommand( self, arguments ):
Expand All @@ -88,6 +90,10 @@ def OnUserCommand( self, arguments ):
if self._ServerIsRunning():
self._StopServer()
self._StartServer()
elif command in [ 'GoToDefinition',
'GoToDeclaration',
'GoToDefinitionElseDeclaration' ]:
self._GoToDefinition()


def DebugInfo( self ):
Expand Down Expand Up @@ -154,22 +160,50 @@ def _StartServer( self ):
def _StopServer( self ):
""" Stop the OmniSharp server """
self._GetResponse( '/stopserver' )
self._omnisharp_port = int( vimsupport.GetVariableValue(
'g:ycm_csharp_server_port' ) )
self._omnisharp_port = None
vimsupport.PostVimMessage( 'Stopping OmniSharp server' )


def _ServerIsRunning( self, port=None ):
""" Check if the OmniSharp server is running """
return self._GetResponse( '/checkalivestatus',
silent=True,
port=port ) != None
def _GetCompletions( self ):
""" Ask server for completions """
completions = self._GetResponse( '/autocomplete', self._DefaultParameters() )
return completions if completions != None else []


def _GoToDefinition( self ):
""" Jump to definition of identifier under cursor """
definition = self._GetResponse( '/gotodefinition', self._DefaultParameters() )
if definition[ 'FileName' ] != None:
vimsupport.JumpToLocation( definition[ 'FileName' ],
definition[ 'Line' ],
definition[ 'Column' ] )
else:
vimsupport.PostVimMessage( 'Can\'t jump to definition' )


def _DefaultParameters( self ):
""" Some very common request parameters """
line, column = vimsupport.CurrentLineAndColumn()
parameters = {}
parameters[ 'line' ], parameters[ 'column' ] = line + 1, column + 1
parameters[ 'buffer' ] = '\n'.join( vim.current.buffer )
parameters[ 'filename' ] = vim.current.buffer.name
return parameters


def _ServerIsRunning( self ):
""" Check if our OmniSharp server is running """
return ( self._omnisharp_port != None and
self._GetResponse( '/checkalivestatus', silent=True ) != None )


def _FindFreePort( self ):
""" Find port without an omnisharp instance running on it """
port = self._omnisharp_port
while self._ServerIsRunning( port ):
""" Find port without an OmniSharp server running on it """
port = int( vimsupport.GetVariableValue(
'g:ycm_csharp_server_port' ) )
while self._GetResponse( '/checkalivestatus',
silent=True,
port=port ) != None:
port += 1
return port

Expand All @@ -180,19 +214,6 @@ def _PortToHost( self, port=None ):
return 'http://localhost:' + str( port )


def _GetCompletions( self ):
""" Ask server for completions """
line, column = vimsupport.CurrentLineAndColumn()

parameters = {}
parameters[ 'line' ], parameters[ 'column' ] = line + 1, column + 1
parameters[ 'buffer' ] = '\n'.join( vim.current.buffer )
parameters[ 'filename' ] = vim.current.buffer.name

completions = self._GetResponse( '/autocomplete', parameters )
return completions if completions != None else []


def _GetResponse( self, endPoint, parameters={}, silent=False, port=None ):
""" Handle communication with server """
target = urlparse.urljoin( self._PortToHost( port ), endPoint )
Expand All @@ -207,6 +228,7 @@ def _GetResponse( self, endPoint, parameters={}, silent=False, port=None ):


def _FindSolutionFiles():
""" Find solution files by searching upwards in the file tree """
folder = os.path.dirname( vim.current.buffer.name )
solutionfiles = glob.glob1( folder, '*.sln' )
while not solutionfiles:
Expand Down

0 comments on commit 9d14562

Please sign in to comment.