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 imp
|
|
|
|
import os
|
2012-08-05 17:14:31 -04:00
|
|
|
import sys
|
2013-02-05 01:07:06 -05:00
|
|
|
import vimsupport
|
|
|
|
import vim
|
|
|
|
import ycm_utils as utils
|
2013-01-26 20:29:18 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
import ycm_core
|
2013-03-24 18:21:54 -04:00
|
|
|
except ImportError as e:
|
2013-01-26 20:29:18 -05:00
|
|
|
vimsupport.PostVimMessage(
|
|
|
|
'Error importing ycm_core. Are you sure you have placed a version 3.2+ '
|
|
|
|
'libclang.[so|dll|dylib] in folder "{0}"? See the Installation Guide in '
|
|
|
|
'the docs. Full error: {1}'.format(
|
|
|
|
os.path.dirname( os.path.abspath( __file__ ) ), str( e ) ) )
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
|
|
|
|
from completers.all.omni_completer import OmniCompleter
|
2013-04-23 01:19:26 -04:00
|
|
|
from completers.general.general_completer_store import GeneralCompleterStore
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-16 00:29:43 -04:00
|
|
|
FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval(
|
|
|
|
'g:ycm_filetype_specific_completion_to_disable' )
|
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
class YouCompleteMe( object ):
|
2012-07-10 18:26:07 -04:00
|
|
|
def __init__( self ):
|
2013-04-01 07:44:41 -04:00
|
|
|
self.gencomp = GeneralCompleterStore()
|
2013-02-10 22:55:05 -05:00
|
|
|
self.omnicomp = OmniCompleter()
|
2012-08-04 20:46:54 -04:00
|
|
|
self.filetype_completers = {}
|
2012-08-01 01:01:41 -04:00
|
|
|
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2013-04-01 07:44:41 -04:00
|
|
|
def GetGeneralCompleter( self ):
|
|
|
|
return self.gencomp
|
2012-07-28 18:27:30 -04:00
|
|
|
|
|
|
|
|
2013-02-25 09:14:01 -05:00
|
|
|
def GetOmniCompleter( self ):
|
|
|
|
return self.omnicomp
|
|
|
|
|
|
|
|
|
2013-02-09 19:22:23 -05:00
|
|
|
def GetFiletypeCompleter( self ):
|
2013-01-31 19:19:56 -05:00
|
|
|
filetypes = vimsupport.CurrentFiletypes()
|
|
|
|
|
2013-03-02 21:14:22 -05:00
|
|
|
completers = [self.GetFiletypeCompleterForFiletype( filetype )
|
|
|
|
for filetype in filetypes ]
|
|
|
|
|
|
|
|
if not completers:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Try to find a native completer first
|
|
|
|
for completer in completers:
|
|
|
|
if completer and completer is not self.omnicomp:
|
2013-01-31 19:19:56 -05:00
|
|
|
return completer
|
2013-03-02 21:14:22 -05:00
|
|
|
|
|
|
|
# Return the omni completer for the first filetype
|
|
|
|
return completers[0]
|
2013-01-31 19:19:56 -05:00
|
|
|
|
|
|
|
|
|
|
|
def GetFiletypeCompleterForFiletype( self, filetype ):
|
2012-08-01 01:01:41 -04:00
|
|
|
try:
|
2012-08-04 20:46:54 -04:00
|
|
|
return self.filetype_completers[ filetype ]
|
2012-08-01 01:01:41 -04:00
|
|
|
except KeyError:
|
2012-08-04 20:46:54 -04:00
|
|
|
pass
|
2012-08-01 01:01:41 -04:00
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
module_path = _PathToFiletypeCompleterPluginLoader( filetype )
|
2012-08-01 01:01:41 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
completer = None
|
2013-01-13 23:56:10 -05:00
|
|
|
supported_filetypes = [ filetype ]
|
2012-08-04 20:46:54 -04:00
|
|
|
if os.path.exists( module_path ):
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-01-31 20:32:39 -05:00
|
|
|
sys.path.insert( 0, os.path.dirname( module_path ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
module = imp.load_source( filetype, module_path )
|
2013-01-31 20:32:39 -05:00
|
|
|
del sys.path[ 0 ]
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
completer = module.GetCompleter()
|
2013-01-13 23:56:10 -05:00
|
|
|
if completer:
|
|
|
|
supported_filetypes.extend( completer.SupportedFiletypes() )
|
2013-02-10 22:55:05 -05:00
|
|
|
else:
|
|
|
|
completer = self.omnicomp
|
2013-01-13 23:56:10 -05:00
|
|
|
|
|
|
|
for supported_filetype in supported_filetypes:
|
|
|
|
self.filetype_completers[ supported_filetype ] = completer
|
2012-08-04 20:46:54 -04:00
|
|
|
return completer
|
2012-07-28 18:27:30 -04:00
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
|
2013-04-01 07:44:41 -04:00
|
|
|
def ShouldUseGeneralCompleter( self, start_column ):
|
|
|
|
return self.gencomp.ShouldUseNow( start_column )
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-07-10 23:27:46 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def ShouldUseFiletypeCompleter( self, start_column ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
return self.GetFiletypeCompleter().ShouldUseNow(
|
2012-08-04 20:46:54 -04:00
|
|
|
start_column )
|
|
|
|
return False
|
2012-07-10 23:27:46 -04:00
|
|
|
|
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
def NativeFiletypeCompletionAvailable( self ):
|
|
|
|
completer = self.GetFiletypeCompleter()
|
|
|
|
return bool( completer ) and completer is not self.omnicomp
|
|
|
|
|
|
|
|
|
2013-02-09 19:22:23 -05:00
|
|
|
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 ):
|
|
|
|
return ( _CurrentFiletypeCompletionEnabled() and
|
|
|
|
self.NativeFiletypeCompletionAvailable() )
|
|
|
|
|
2013-01-31 19:19:56 -05:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
def FiletypeCompletionUsable( self ):
|
|
|
|
return ( _CurrentFiletypeCompletionEnabled() and
|
2013-02-09 19:22:23 -05:00
|
|
|
self.FiletypeCompletionAvailable() )
|
2012-07-28 18:27:30 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
2013-04-01 07:44:41 -04:00
|
|
|
self.gencomp.OnFileReadyToParse()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
self.GetFiletypeCompleter().OnFileReadyToParse()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2013-03-14 23:39:44 -04:00
|
|
|
def OnBufferDelete( self, deleted_buffer_file ):
|
2013-04-01 07:44:41 -04:00
|
|
|
self.gencomp.OnBufferDelete( deleted_buffer_file )
|
2013-03-14 23:39:44 -04:00
|
|
|
|
|
|
|
if self.FiletypeCompletionUsable():
|
|
|
|
self.GetFiletypeCompleter().OnBufferDelete( deleted_buffer_file )
|
|
|
|
|
|
|
|
|
2013-04-24 16:31:28 -04:00
|
|
|
def OnBufferVisit( self ):
|
|
|
|
self.gencomp.OnBufferVisit()
|
|
|
|
|
|
|
|
if self.FiletypeCompletionUsable():
|
|
|
|
self.GetFiletypeCompleter().OnBufferVisit()
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnInsertLeave( self ):
|
2013-04-01 07:44:41 -04:00
|
|
|
self.gencomp.OnInsertLeave()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
self.GetFiletypeCompleter().OnInsertLeave()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-07-31 18:30:50 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def DiagnosticsForCurrentFileReady( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
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
|
|
|
|
2012-08-06 23:14:21 -04:00
|
|
|
def GetDiagnosticsForCurrentFile( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile()
|
2012-08-06 23:14:21 -04:00
|
|
|
return []
|
|
|
|
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
def ShowDetailedDiagnostic( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
return self.GetFiletypeCompleter().ShowDetailedDiagnostic()
|
2012-08-15 22:39:03 -04:00
|
|
|
|
|
|
|
|
2013-02-06 00:22:50 -05:00
|
|
|
def GettingCompletions( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
return self.GetFiletypeCompleter().GettingCompletions()
|
2013-02-06 00:22:50 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnCurrentIdentifierFinished( self ):
|
2013-04-01 07:44:41 -04:00
|
|
|
self.gencomp.OnCurrentIdentifierFinished()
|
2012-07-23 21:35:48 -04:00
|
|
|
|
2013-02-10 22:55:05 -05:00
|
|
|
if self.FiletypeCompletionUsable():
|
2013-02-09 19:22:23 -05:00
|
|
|
self.GetFiletypeCompleter().OnCurrentIdentifierFinished()
|
2012-07-10 18:26:07 -04:00
|
|
|
|
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
def DebugInfo( self ):
|
|
|
|
completers = set( self.filetype_completers.values() )
|
2013-04-01 07:44:41 -04:00
|
|
|
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-02-10 22:55:05 -05:00
|
|
|
def _CurrentFiletypeCompletionEnabled():
|
|
|
|
filetypes = vimsupport.CurrentFiletypes()
|
|
|
|
return not all([ x in FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE
|
|
|
|
for x in filetypes ])
|
|
|
|
|
2013-01-26 14:44:42 -05:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def _PathToCompletersFolder():
|
|
|
|
dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
|
|
|
|
return os.path.join( dir_of_current_script, 'completers' )
|
2012-07-10 18:26:07 -04:00
|
|
|
|
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
def _PathToFiletypeCompleterPluginLoader( filetype ):
|
|
|
|
return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' )
|
2012-07-10 18:26:07 -04:00
|
|
|
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
def CompletionStartColumn():
|
2012-07-10 18:26:07 -04:00
|
|
|
"""Returns the 0-based index where the completion string should start. So if
|
|
|
|
the user enters:
|
|
|
|
foo.bar^
|
|
|
|
with the cursor being at the location of the caret, then the starting column
|
|
|
|
would be the index of the letter 'b'.
|
|
|
|
"""
|
|
|
|
|
2012-04-15 19:57:10 -04:00
|
|
|
line = vim.current.line
|
2012-08-04 20:46:54 -04:00
|
|
|
start_column = vimsupport.CurrentColumn()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
while start_column > 0 and utils.IsIdentifierChar( line[ start_column - 1 ] ):
|
2012-04-15 19:57:10 -04:00
|
|
|
start_column -= 1
|
|
|
|
return start_column
|
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-07-28 01:16:23 -04:00
|
|
|
def CurrentIdentifierFinished():
|
2012-08-04 20:46:54 -04:00
|
|
|
current_column = vimsupport.CurrentColumn()
|
2012-05-12 18:20:03 -04:00
|
|
|
previous_char_index = current_column - 1
|
|
|
|
if previous_char_index < 0:
|
2012-05-12 23:42:45 -04:00
|
|
|
return True
|
2012-05-12 18:20:03 -04:00
|
|
|
line = vim.current.line
|
|
|
|
try:
|
|
|
|
previous_char = line[ previous_char_index ]
|
|
|
|
except IndexError:
|
2012-05-12 23:42:45 -04:00
|
|
|
return False
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
if utils.IsIdentifierChar( previous_char ):
|
2012-05-12 23:42:45 -04:00
|
|
|
return False
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
if ( not utils.IsIdentifierChar( previous_char ) and
|
2012-05-12 18:20:03 -04:00
|
|
|
previous_char_index > 0 and
|
2012-08-04 20:46:54 -04:00
|
|
|
utils.IsIdentifierChar( line[ previous_char_index - 1 ] ) ):
|
2012-05-12 23:42:45 -04:00
|
|
|
return True
|
2012-05-12 18:20:03 -04:00
|
|
|
else:
|
2012-05-12 23:42:45 -04:00
|
|
|
return line[ : current_column ].isspace()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
COMPATIBLE_WITH_CORE_VERSION = 3
|
2013-02-12 23:54:27 -05:00
|
|
|
|
|
|
|
def CompatibleWithYcmCore():
|
|
|
|
try:
|
|
|
|
current_core_version = ycm_core.YcmCoreVersion()
|
|
|
|
except AttributeError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return current_core_version == COMPATIBLE_WITH_CORE_VERSION
|
|
|
|
|