2012-04-15 19:57:10 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2012-04-15 23:28:46 -04:00
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
2012-04-15 19:57:10 -04:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2012-08-01 01:01:41 -04:00
|
|
|
import os
|
2013-09-06 02:43:14 -04:00
|
|
|
import time
|
2013-02-05 01:07:06 -05:00
|
|
|
import vim
|
2013-05-19 23:06:14 -04:00
|
|
|
import ycm_core
|
2013-09-20 20:24:34 -04:00
|
|
|
import subprocess
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm import vimsupport
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm import utils
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm.completers.all.omni_completer import OmniCompleter
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm.client.base_request import BaseRequest
|
|
|
|
from ycm.client.command_request import CommandRequest
|
|
|
|
from ycm.client.completion_request import CompletionRequest
|
|
|
|
from ycm.client.event_notification import SendEventNotificationAsync
|
2013-09-07 20:39:52 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
SERVER_PORT_RANGE_START = 10000
|
2013-09-02 22:46:30 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
class YouCompleteMe( object ):
|
2013-09-02 17:45:53 -04:00
|
|
|
def __init__( self, user_options ):
|
|
|
|
self._user_options = user_options
|
|
|
|
self._omnicomp = OmniCompleter( user_options )
|
2013-09-02 22:46:30 -04:00
|
|
|
self._current_completion_request = None
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
server_port = SERVER_PORT_RANGE_START + os.getpid()
|
|
|
|
command = ''.join( [ 'python ',
|
|
|
|
_PathToServerScript(),
|
|
|
|
' --port=',
|
|
|
|
str( server_port ) ] )
|
|
|
|
|
|
|
|
BaseRequest.server_location = 'http://localhost:' + str( server_port )
|
|
|
|
|
|
|
|
filename_format = os.path.join( utils.PathToTempDir(),
|
|
|
|
'server_{port}_{std}.log' )
|
|
|
|
|
|
|
|
self._server_stdout = filename_format.format( port=server_port,
|
|
|
|
std='stdout' )
|
|
|
|
self._server_stderr = filename_format.format( port=server_port,
|
|
|
|
std='stderr' )
|
|
|
|
|
|
|
|
with open( self._server_stderr, 'w' ) as fstderr:
|
|
|
|
with open( self._server_stdout, 'w' ) as fstdout:
|
|
|
|
subprocess.Popen( command, stdout=fstdout, stderr=fstderr, shell=True )
|
|
|
|
|
2013-09-02 22:46:30 -04:00
|
|
|
|
|
|
|
def CreateCompletionRequest( self ):
|
|
|
|
# We have to store a reference to the newly created CompletionRequest
|
|
|
|
# because VimScript can't store a reference to a Python object across
|
|
|
|
# function calls... Thus we need to keep this request somewhere.
|
2013-09-20 20:24:34 -04:00
|
|
|
self._current_completion_request = CompletionRequest()
|
2013-09-02 22:46:30 -04:00
|
|
|
return self._current_completion_request
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def SendCommandRequest( self, arguments, completer ):
|
|
|
|
# TODO: This should be inside a method in a command_request module
|
|
|
|
request = CommandRequest( self, arguments, completer )
|
|
|
|
request.Start()
|
|
|
|
while not request.Done():
|
|
|
|
time.sleep( 0.1 )
|
|
|
|
|
|
|
|
return request.Response()
|
|
|
|
|
|
|
|
|
2013-09-02 22:46:30 -04:00
|
|
|
def GetCurrentCompletionRequest( self ):
|
|
|
|
return self._current_completion_request
|
2012-08-01 01:01:41 -04:00
|
|
|
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2013-02-25 09:14:01 -05:00
|
|
|
def GetOmniCompleter( self ):
|
2013-09-01 23:19:46 -04:00
|
|
|
return self._omnicomp
|
2013-02-25 09:14:01 -05:00
|
|
|
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
def NativeFiletypeCompletionAvailable( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: Talk to server about this.
|
|
|
|
return False
|
2013-02-10 22:55:05 -05:00
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: This may not be needed at all when the server is ready. Evaluate this
|
|
|
|
# later.
|
|
|
|
# def FiletypeCompletionAvailable( self ):
|
|
|
|
# return bool( self.GetFiletypeCompleter() )
|
2012-07-20 00:17:39 -04:00
|
|
|
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
def NativeFiletypeCompletionUsable( self ):
|
2013-09-02 17:45:53 -04:00
|
|
|
return ( self.CurrentFiletypeCompletionEnabled() and
|
2013-02-10 22:55:05 -05:00
|
|
|
self.NativeFiletypeCompletionAvailable() )
|
|
|
|
|
2013-01-31 19:19:56 -05:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: This may not be needed at all when the server is ready. Evaluate this
|
|
|
|
# later.
|
|
|
|
# def FiletypeCompletionUsable( self ):
|
|
|
|
# return ( self.CurrentFiletypeCompletionEnabled() and
|
|
|
|
# self.FiletypeCompletionAvailable() )
|
2012-07-28 18:27:30 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
2013-09-07 20:39:52 -04:00
|
|
|
extra_data = {}
|
|
|
|
if self._user_options[ 'collect_identifiers_from_tags_files' ]:
|
|
|
|
extra_data[ 'tag_files' ] = _GetTagFiles()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2013-09-07 20:39:52 -04:00
|
|
|
# TODO: make this work again
|
|
|
|
# if self._user_options[ 'seed_identifiers_with_syntax' ]:
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
SendEventNotificationAsync( 'FileReadyToParse', extra_data )
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2013-03-14 23:39:44 -04:00
|
|
|
|
2013-09-07 20:39:52 -04:00
|
|
|
def OnBufferUnload( self, deleted_buffer_file ):
|
|
|
|
SendEventNotificationAsync( 'BufferUnload',
|
|
|
|
{ 'unloaded_buffer': deleted_buffer_file } )
|
2013-03-14 23:39:44 -04:00
|
|
|
|
|
|
|
|
2013-04-24 16:31:28 -04:00
|
|
|
def OnBufferVisit( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
SendEventNotificationAsync( 'BufferVisit' )
|
2013-04-24 16:31:28 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnInsertLeave( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
SendEventNotificationAsync( 'InsertLeave' )
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-07-31 18:30:50 -04:00
|
|
|
|
2013-07-07 13:59:48 -04:00
|
|
|
def OnVimLeave( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
SendEventNotificationAsync( 'VimLeave' )
|
2013-07-07 13:59:48 -04:00
|
|
|
|
2013-09-07 20:39:52 -04:00
|
|
|
|
|
|
|
def OnCurrentIdentifierFinished( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
SendEventNotificationAsync( 'CurrentIdentifierFinished' )
|
2013-07-07 13:59:48 -04:00
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: Make this work again.
|
2012-08-04 20:46:54 -04:00
|
|
|
def DiagnosticsForCurrentFileReady( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
# if self.FiletypeCompletionUsable():
|
|
|
|
# return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady()
|
2012-08-04 20:46:54 -04:00
|
|
|
return False
|
2012-07-31 18:30:50 -04:00
|
|
|
|
2012-07-23 21:35:48 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: Make this work again.
|
2012-08-06 23:14:21 -04:00
|
|
|
def GetDiagnosticsForCurrentFile( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
# if self.FiletypeCompletionUsable():
|
|
|
|
# return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile()
|
2012-08-06 23:14:21 -04:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: Make this work again.
|
2013-09-06 02:43:14 -04:00
|
|
|
def GetDetailedDiagnostic( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
# if self.FiletypeCompletionUsable():
|
|
|
|
# return self.GetFiletypeCompleter().GetDetailedDiagnostic()
|
|
|
|
pass
|
2012-08-15 22:39:03 -04:00
|
|
|
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
# TODO: Make this work again.
|
2013-02-06 00:22:50 -05:00
|
|
|
def GettingCompletions( self ):
|
2013-09-20 20:24:34 -04:00
|
|
|
# if self.FiletypeCompletionUsable():
|
|
|
|
# return self.GetFiletypeCompleter().GettingCompletions()
|
2013-02-06 00:22:50 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
def DebugInfo( self ):
|
2013-09-01 23:19:46 -04:00
|
|
|
completers = set( self._filetype_completers.values() )
|
|
|
|
completers.add( self._gencomp )
|
2013-01-26 14:44:42 -05:00
|
|
|
output = []
|
|
|
|
for completer in completers:
|
|
|
|
if not completer:
|
|
|
|
continue
|
|
|
|
debug = completer.DebugInfo()
|
|
|
|
if debug:
|
|
|
|
output.append( debug )
|
|
|
|
|
|
|
|
has_clang_support = ycm_core.HasClangSupport()
|
|
|
|
output.append( 'Has Clang support compiled in: {0}'.format(
|
|
|
|
has_clang_support ) )
|
|
|
|
|
|
|
|
if has_clang_support:
|
|
|
|
output.append( ycm_core.ClangVersion() )
|
|
|
|
|
|
|
|
return '\n'.join( output )
|
|
|
|
|
|
|
|
|
2013-09-02 17:45:53 -04:00
|
|
|
def CurrentFiletypeCompletionEnabled( self ):
|
|
|
|
filetypes = vimsupport.CurrentFiletypes()
|
|
|
|
filetype_to_disable = self._user_options[
|
|
|
|
'filetype_specific_completion_to_disable' ]
|
|
|
|
return not all([ x in filetype_to_disable for x in filetypes ])
|
2013-02-10 22:55:05 -05:00
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
|
2013-09-07 20:39:52 -04:00
|
|
|
|
|
|
|
def _GetTagFiles():
|
|
|
|
tag_files = vim.eval( 'tagfiles()' )
|
|
|
|
current_working_directory = os.getcwd()
|
|
|
|
return [ os.path.join( current_working_directory, x ) for x in tag_files ]
|
2013-09-20 20:24:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _PathToServerScript():
|
|
|
|
dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
|
|
|
|
return os.path.join( dir_of_current_script, 'server/server.py' )
|