2013-06-04 06:14:12 -04:00
|
|
|
#!/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
|
2013-07-18 06:35:46 -04:00
|
|
|
from sys import platform
|
2013-06-21 16:26:11 -04:00
|
|
|
import glob
|
2013-06-04 06:14:12 -04:00
|
|
|
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
|
2013-08-14 09:40:43 -04:00
|
|
|
import tempfile
|
2013-06-04 06:14:12 -04:00
|
|
|
|
|
|
|
|
2013-07-17 22:29:43 -04:00
|
|
|
SERVER_NOT_FOUND_MSG = ( 'OmniSharp server binary not found at {0}. ' +
|
|
|
|
'Did you compile it? You can do so by running ' +
|
|
|
|
'"./install.sh --omnisharp_completer".' )
|
|
|
|
|
2013-06-04 06:14:12 -04:00
|
|
|
class CsharpCompleter( ThreadedCompleter ):
|
|
|
|
"""
|
|
|
|
A Completer that uses the Omnisharp server as completion engine.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__( self ):
|
|
|
|
super( CsharpCompleter, self ).__init__()
|
2013-07-17 21:29:34 -04:00
|
|
|
self._omnisharp_port = int( vimsupport.GetVariableValue(
|
2013-07-17 22:29:43 -04:00
|
|
|
'g:ycm_csharp_server_port' ) )
|
2013-07-19 05:55:25 -04:00
|
|
|
|
2013-07-17 22:29:43 -04:00
|
|
|
if vimsupport.GetBoolValue( 'g:ycm_auto_start_csharp_server' ):
|
2013-07-07 16:36:05 -04:00
|
|
|
self._StartServer()
|
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
2013-07-08 05:39:17 -04:00
|
|
|
def OnVimLeave( self ):
|
2013-08-14 21:57:41 -04:00
|
|
|
if ( vimsupport.GetBoolValue( 'g:ycm_auto_stop_csharp_server' ) and
|
|
|
|
self._ServerIsRunning() ):
|
2013-07-08 08:39:43 -04:00
|
|
|
self._StopServer()
|
2013-06-04 06:14:12 -04:00
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
2013-06-04 06:14:12 -04:00
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
""" Just csharp """
|
2013-06-28 15:15:05 -04:00
|
|
|
return [ 'cs' ]
|
2013-06-04 06:14:12 -04:00
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
2013-06-04 06:14:12 -04:00
|
|
|
def ComputeCandidates( self, unused_query, unused_start_column ):
|
2013-07-17 21:29:34 -04:00
|
|
|
return [ { 'word': str( completion[ 'CompletionText' ] ),
|
|
|
|
'menu': str( completion[ 'DisplayText' ] ),
|
|
|
|
'info': str( completion[ 'Description' ] ) }
|
2013-06-13 17:34:34 -04:00
|
|
|
for completion in self._GetCompletions() ]
|
2013-06-04 06:14:12 -04:00
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
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
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
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
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
2013-08-14 10:34:44 -04:00
|
|
|
def DebugInfo( self ):
|
|
|
|
if self._ServerIsRunning():
|
2013-08-14 22:21:20 -04:00
|
|
|
return 'Server running at: {}\nLogfiles:\n{}\n{}'.format(
|
|
|
|
self._PortToHost(), self._filename_stdout, self._filename_stderr )
|
2013-08-14 10:34:44 -04:00
|
|
|
else:
|
2013-08-14 11:02:10 -04:00
|
|
|
return 'Server is not running'
|
2013-08-14 10:34:44 -04:00
|
|
|
|
|
|
|
|
2013-06-13 17:34:34 -04:00
|
|
|
def _StartServer( self ):
|
|
|
|
""" Start the OmniSharp server """
|
2013-08-14 09:40:43 -04:00
|
|
|
self._omnisharp_port = self._FindFreePort()
|
2013-07-17 22:29:43 -04:00
|
|
|
solutionfiles, folder = _FindSolutionFiles()
|
|
|
|
|
|
|
|
if len( solutionfiles ) == 0:
|
|
|
|
vimsupport.PostVimMessage(
|
|
|
|
'Error starting OmniSharp server: no solutionfile found' )
|
|
|
|
return
|
|
|
|
elif len( solutionfiles ) == 1:
|
|
|
|
solutionfile = solutionfiles[ 0 ]
|
|
|
|
else:
|
|
|
|
choice = vimsupport.PresentDialog(
|
2013-07-19 06:16:16 -04:00
|
|
|
'Which solutionfile should be loaded?',
|
2013-07-17 22:29:43 -04:00
|
|
|
[ str( i ) + " " + solution for i, solution in
|
|
|
|
enumerate( solutionfiles ) ] )
|
|
|
|
if choice == -1:
|
|
|
|
vimsupport.PostVimMessage( 'OmniSharp not started' )
|
2013-07-07 07:06:07 -04:00
|
|
|
return
|
2013-06-21 16:26:11 -04:00
|
|
|
else:
|
2013-07-17 22:29:43 -04:00
|
|
|
solutionfile = solutionfiles[ choice ]
|
2013-07-12 16:46:33 -04:00
|
|
|
|
2013-07-17 22:29:43 -04:00
|
|
|
omnisharp = os.path.join(
|
|
|
|
os.path.abspath( os.path.dirname( __file__ ) ),
|
|
|
|
'OmniSharpServer/OmniSharp/bin/Debug/OmniSharp.exe' )
|
2013-07-12 16:46:33 -04:00
|
|
|
|
2013-07-17 22:29:43 -04:00
|
|
|
if not os.path.isfile( omnisharp ):
|
|
|
|
vimsupport.PostVimMessage( SERVER_NOT_FOUND_MSG.format( omnisharp ) )
|
|
|
|
return
|
|
|
|
|
2013-07-18 11:18:17 -04:00
|
|
|
if not platform.startswith( 'win' ):
|
2013-07-19 06:16:16 -04:00
|
|
|
omnisharp = 'mono ' + omnisharp
|
2013-07-18 06:35:46 -04:00
|
|
|
|
2013-08-14 09:40:43 -04:00
|
|
|
path_to_solutionfile = os.path.join( folder, solutionfile )
|
2013-07-17 22:29:43 -04:00
|
|
|
# command has to be provided as one string for some reason
|
|
|
|
command = [ omnisharp + ' -p ' + str( self._omnisharp_port ) + ' -s ' +
|
2013-08-14 09:40:43 -04:00
|
|
|
path_to_solutionfile ]
|
2013-07-17 22:29:43 -04:00
|
|
|
|
2013-08-14 21:57:41 -04:00
|
|
|
filename_format = ( tempfile.gettempdir() +
|
|
|
|
'/omnisharp_{port}_{sln}_{std}.log' )
|
|
|
|
|
2013-08-14 10:34:44 -04:00
|
|
|
self._filename_stdout = filename_format.format(
|
2013-08-14 11:02:10 -04:00
|
|
|
port=self._omnisharp_port, sln=solutionfile, std='stdout' )
|
2013-08-14 10:34:44 -04:00
|
|
|
self._filename_stderr = filename_format.format(
|
2013-08-14 11:02:10 -04:00
|
|
|
port=self._omnisharp_port, sln=solutionfile, std='stderr' )
|
2013-06-13 17:34:34 -04:00
|
|
|
|
2013-08-14 10:34:44 -04:00
|
|
|
with open( self._filename_stderr, 'w' ) as fstderr:
|
|
|
|
with open( self._filename_stdout, 'w' ) as fstdout:
|
2013-07-19 05:31:52 -04:00
|
|
|
subprocess.Popen( command, stdout=fstdout, stderr=fstderr, shell=True )
|
|
|
|
|
2013-07-19 06:16:16 -04:00
|
|
|
vimsupport.PostVimMessage( 'Starting OmniSharp server' )
|
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-08-14 09:40:43 -04:00
|
|
|
self._omnisharp_port = int( vimsupport.GetVariableValue(
|
|
|
|
'g:ycm_csharp_server_port' ) )
|
2013-07-19 06:16:16 -04:00
|
|
|
vimsupport.PostVimMessage( 'Stopping OmniSharp server' )
|
2013-06-13 17:34:34 -04:00
|
|
|
|
|
|
|
|
2013-08-14 09:40:43 -04:00
|
|
|
def _ServerIsRunning( self, port=None ):
|
2013-06-13 17:34:34 -04:00
|
|
|
""" Check if the OmniSharp server is running """
|
2013-08-14 11:02:10 -04:00
|
|
|
return self._GetResponse( '/checkalivestatus',
|
2013-08-14 21:57:41 -04:00
|
|
|
silent=True,
|
|
|
|
port=port ) != None
|
2013-06-13 17:34:34 -04:00
|
|
|
|
2013-07-19 06:16:16 -04:00
|
|
|
|
2013-07-15 11:12:32 -04:00
|
|
|
def _FindFreePort( self ):
|
2013-08-14 09:40:43 -04:00
|
|
|
""" Find port without an omnisharp instance running on it """
|
|
|
|
port = self._omnisharp_port
|
2013-08-14 11:02:10 -04:00
|
|
|
while self._ServerIsRunning( port ):
|
2013-08-14 09:40:43 -04:00
|
|
|
port += 1
|
|
|
|
return port
|
2013-07-15 11:12:32 -04:00
|
|
|
|
2013-07-19 05:55:25 -04:00
|
|
|
|
2013-08-14 09:40:43 -04:00
|
|
|
def _PortToHost( self, port=None ):
|
|
|
|
if port == None:
|
|
|
|
port = self._omnisharp_port
|
|
|
|
return 'http://localhost:' + str( port )
|
2013-07-19 05:55:25 -04:00
|
|
|
|
2013-07-16 06:33:07 -04:00
|
|
|
|
2013-06-13 17:34:34 -04:00
|
|
|
def _GetCompletions( self ):
|
|
|
|
""" Ask server for completions """
|
2013-06-04 06:14:12 -04:00
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
|
|
|
|
|
|
|
parameters = {}
|
2013-08-14 11:02:10 -04:00
|
|
|
parameters[ 'line' ], parameters[ 'column' ] = line + 1, column + 1
|
|
|
|
parameters[ 'buffer' ] = '\n'.join( vim.current.buffer )
|
|
|
|
parameters[ 'filename' ] = vim.current.buffer.name
|
2013-06-04 06:14:12 -04:00
|
|
|
|
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 []
|
2013-06-04 06:14:12 -04:00
|
|
|
|
2013-07-17 21:29:34 -04:00
|
|
|
|
2013-08-14 11:02:10 -04:00
|
|
|
def _GetResponse( self, endPoint, parameters={}, silent=False, port=None ):
|
2013-06-13 17:34:34 -04:00
|
|
|
""" Handle communication with server """
|
2013-08-14 11:02:10 -04:00
|
|
|
target = urlparse.urljoin( self._PortToHost( port ), endPoint )
|
2013-06-04 06:14:12 -04:00
|
|
|
parameters = urllib.urlencode( parameters )
|
|
|
|
try:
|
|
|
|
response = urllib2.urlopen( target, parameters )
|
2013-06-13 17:34:34 -04:00
|
|
|
return json.loads( response.read() )
|
2013-07-30 14:18:37 -04:00
|
|
|
except Exception:
|
|
|
|
# TODO: Add logging for this case. We can't post a Vim message because Vim
|
|
|
|
# crashes when that's done from a no-GUI thread.
|
2013-06-13 17:34:34 -04:00
|
|
|
return None
|
2013-07-17 21:29:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _FindSolutionFiles():
|
|
|
|
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' )
|
|
|
|
return solutionfiles, folder
|