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-04 20:46:54 -04:00
|
|
|
import vimsupport
|
2012-08-01 01:01:41 -04:00
|
|
|
import imp
|
2012-08-04 20:46:54 -04:00
|
|
|
import vim
|
|
|
|
import utils
|
2012-08-01 01:01:41 -04:00
|
|
|
import os
|
2012-08-05 17:14:31 -04:00
|
|
|
import sys
|
|
|
|
from completers.all.identifier_completer import IdentifierCompleter
|
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 ):
|
2012-08-04 20:46:54 -04:00
|
|
|
self.identcomp = IdentifierCompleter()
|
|
|
|
self.filetype_completers = {}
|
2012-08-01 01:01:41 -04:00
|
|
|
|
2012-07-22 18:19:28 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def GetIdentifierCompleter( self ):
|
|
|
|
return self.identcomp
|
2012-07-28 18:27:30 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def GetFiletypeCompleterForCurrentFile( self ):
|
|
|
|
filetype = vimsupport.CurrentFiletype()
|
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
|
|
|
|
if os.path.exists( module_path ):
|
2012-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
sys.path.append( os.path.dirname( module_path ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
module = imp.load_source( filetype, module_path )
|
2012-08-05 17:14:31 -04:00
|
|
|
del sys.path[ -1 ]
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
completer = module.GetCompleter()
|
|
|
|
for supported_filetype in completer.SupportedFiletypes():
|
|
|
|
self.filetype_completers[ supported_filetype ] = completer
|
|
|
|
else:
|
|
|
|
self.filetype_completers[ filetype ] = None
|
|
|
|
return completer
|
2012-07-28 18:27:30 -04:00
|
|
|
|
2012-07-26 23:50:56 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def ShouldUseIdentifierCompleter( self, start_column ):
|
|
|
|
return self.identcomp.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 ):
|
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
return self.GetFiletypeCompleterForCurrentFile().ShouldUseNow(
|
|
|
|
start_column )
|
|
|
|
return False
|
2012-07-10 23:27:46 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def FiletypeCompletionAvailableForFile( self ):
|
|
|
|
return bool( self.GetFiletypeCompleterForCurrentFile() )
|
2012-07-20 00:17:39 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def FiletypeCompletionEnabledForCurrentFile( self ):
|
2012-08-16 00:29:43 -04:00
|
|
|
return ( vimsupport.CurrentFiletype() not in
|
|
|
|
FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE and
|
2012-08-04 20:46:54 -04:00
|
|
|
self.FiletypeCompletionAvailableForFile() )
|
2012-07-28 18:27:30 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
|
|
|
self.identcomp.OnFileReadyToParse()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
self.GetFiletypeCompleterForCurrentFile().OnFileReadyToParse()
|
2012-04-15 19:57:10 -04:00
|
|
|
|
2012-05-08 00:23:38 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnInsertLeave( self ):
|
|
|
|
self.identcomp.OnInsertLeave()
|
2012-05-12 18:20:03 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
self.GetFiletypeCompleterForCurrentFile().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 ):
|
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
return self.GetFiletypeCompleterForCurrentFile().DiagnosticsForCurrentFileReady()
|
|
|
|
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 ):
|
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
return self.GetFiletypeCompleterForCurrentFile().GetDiagnosticsForCurrentFile()
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
def ShowDetailedDiagnostic( self ):
|
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
return self.GetFiletypeCompleterForCurrentFile().ShowDetailedDiagnostic()
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnCurrentIdentifierFinished( self ):
|
|
|
|
self.identcomp.OnCurrentIdentifierFinished()
|
2012-07-23 21:35:48 -04:00
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
if self.FiletypeCompletionEnabledForCurrentFile():
|
|
|
|
self.GetFiletypeCompleterForCurrentFile().OnCurrentIdentifierFinished()
|
2012-07-10 18:26:07 -04: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
|
|
|
|
|
|
|
|