YouCompleteMe/python/ycm/completers/cs/cs_completer.py

154 lines
5.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# Copyright (C) 2011, 2012 Chiel ten Brinke <ctenbrinke@gmail.com>
# Strahinja Val Markovic <val@markovic.io>
#
# This file is part of YouCompleteMe.
#
# YouCompleteMe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# YouCompleteMe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
import vim
2013-06-21 16:26:11 -04:00
import os
import glob
from ycm.completers.threaded_completer import ThreadedCompleter
from ycm import vimsupport
import urllib2
import urllib
import urlparse
import json
2013-06-28 15:15:05 -04:00
import subprocess
class CsharpCompleter( ThreadedCompleter ):
"""
A Completer that uses the Omnisharp server as completion engine.
"""
def __init__( self ):
super( CsharpCompleter, self ).__init__()
2013-07-16 06:33:07 -04:00
self.OmniSharpPort = int( vimsupport.GetVariableValue(
"g:ycm_csharp_server_port" ) )
2013-06-28 15:15:05 -04:00
self.OmniSharpHost = 'http://localhost:' + str( self.OmniSharpPort )
2013-07-07 16:36:05 -04:00
if vimsupport.GetBoolValue( "g:ycm_auto_start_csharp_server" ):
self._StartServer()
2013-07-08 05:39:17 -04:00
def OnVimLeave( self ):
2013-07-08 08:39:43 -04:00
if self._ServerIsRunning():
self._StopServer()
def SupportedFiletypes( self ):
""" Just csharp """
2013-06-28 15:15:05 -04:00
return [ 'cs' ]
def ComputeCandidates( self, unused_query, unused_start_column ):
return [ { 'word': str( completion['CompletionText'] ),
2013-06-07 05:45:36 -04:00
'menu': str( completion['DisplayText'] ),
'info': str( completion['Description'] ) }
2013-06-13 17:34:34 -04:00
for completion in self._GetCompletions() ]
2013-06-13 17:34:34 -04:00
def DefinedSubcommands( self ):
2013-06-28 15:15:05 -04:00
return [ 'StartServer',
2013-07-07 16:36:05 -04:00
'StopServer',
'RestartServer' ]
2013-06-13 17:34:34 -04:00
def OnUserCommand( self, arguments ):
if not arguments:
self.EchoUserCommandsHelpMessage()
return
command = arguments[ 0 ]
if command == 'StartServer':
self._StartServer()
elif command == 'StopServer':
self._StopServer()
2013-07-07 16:36:05 -04:00
elif command == 'RestartServer':
2013-07-08 05:44:06 -04:00
if self._ServerIsRunning():
self._StopServer()
2013-07-07 16:36:05 -04:00
self._StartServer()
2013-06-13 17:34:34 -04:00
def _StartServer( self ):
""" Start the OmniSharp server """
2013-07-08 05:44:06 -04:00
if not self._ServerIsRunning():
2013-07-16 10:12:40 -04:00
solutionfiles, folder = self._FindSolutionFiles()
2013-06-21 16:26:11 -04:00
2013-06-29 04:41:45 -04:00
if len( solutionfiles ) == 0:
2013-07-16 06:33:07 -04:00
vimsupport.PostVimMessage(
'Error starting OmniSharp server: no solutionfile found' )
2013-07-07 07:06:07 -04:00
return
2013-06-29 04:41:45 -04:00
elif len( solutionfiles ) == 1:
2013-07-07 07:06:07 -04:00
solutionfile = solutionfiles[0]
2013-06-21 16:26:11 -04:00
else:
2013-07-16 06:33:07 -04:00
choice = vimsupport.PresentDialog(
"Which solutionfile should be loaded?",
[ str(i) + " " + s for i, s in enumerate( solutionfiles ) ] )
if choice == -1:
vimsupport.PostVimMessage( 'OmniSharp not started' )
return
else:
solutionfile = solutionfiles[ choice ]
2013-07-07 07:06:07 -04:00
omnisharp = os.path.join( os.path.abspath( os.path.dirname( __file__ ) ),
'OmniSharpServer/OmniSharp/bin/Debug/OmniSharp.exe' )
solutionfile = os.path.join ( folder, solutionfile )
# command has to be provided as one string for some reason
2013-07-16 06:33:07 -04:00
command = [ omnisharp + ' -p ' + str( self.OmniSharpPort )
+ ' -s ' + solutionfile ]
2013-07-07 07:06:07 -04:00
with open( os.devnull, "w" ) as fnull:
subprocess.Popen( command, stdout = fnull, stderr = fnull, shell=True )
2013-06-13 17:34:34 -04:00
def _StopServer( self ):
""" Stop the OmniSharp server """
2013-07-08 05:39:17 -04:00
self._GetResponse( '/stopserver' )
2013-06-13 17:34:34 -04:00
def _ServerIsRunning( self ):
""" Check if the OmniSharp server is running """
2013-07-08 08:39:43 -04:00
return self._GetResponse( '/checkalivestatus', silent=True ) != None
2013-06-13 17:34:34 -04:00
2013-07-16 06:33:07 -04:00
def _FindSolutionFiles( self ):
folder = os.path.dirname( vim.current.buffer.name )
solutionfiles = glob.glob1( folder, '*.sln' )
while not solutionfiles:
lastfolder = folder
folder = os.path.dirname( folder )
if folder == lastfolder:
break
solutionfiles = glob.glob1( folder, '*.sln' )
2013-07-16 10:12:40 -04:00
return solutionfiles, folder
2013-07-16 06:33:07 -04:00
2013-06-13 17:34:34 -04:00
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
2013-07-16 06:33:07 -04:00
completions = self._GetResponse( '/autocomplete', parameters )
2013-06-28 15:15:05 -04:00
return completions if completions != None else []
def _GetResponse( self, endPoint, parameters={}, silent = False ):
2013-06-13 17:34:34 -04:00
""" Handle communication with server """
2013-06-28 15:15:05 -04:00
target = urlparse.urljoin( self.OmniSharpHost, endPoint )
parameters = urllib.urlencode( parameters )
try:
response = urllib2.urlopen( target, parameters )
2013-06-13 17:34:34 -04:00
return json.loads( response.read() )
2013-06-28 15:15:05 -04:00
except Exception as e:
if not silent:
2013-07-16 06:33:07 -04:00
vimsupport.PostVimMessage('OmniSharp : Could not connect to '
+ target + ': ' + str(e))
2013-06-13 17:34:34 -04:00
return None