YouCompleteMe/python/ycm/youcompleteme.py

218 lines
6.1 KiB
Python
Raw Normal View History

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/>.
import imp
import os
2013-02-05 01:07:06 -05:00
import vim
2013-05-19 23:06:14 -04:00
import ycm_core
2013-05-19 22:44:42 -04:00
from ycm import vimsupport
from ycm.completers.all.omni_completer import OmniCompleter
from ycm.completers.general.general_completer_store import GeneralCompleterStore
2013-05-19 23:06:14 -04:00
class YouCompleteMe( object ):
def __init__( self, user_options ):
self._user_options = user_options
self._gencomp = GeneralCompleterStore( user_options )
self._omnicomp = OmniCompleter( user_options )
2013-09-01 23:19:46 -04:00
self._filetype_completers = {}
2013-04-01 07:44:41 -04:00
def GetGeneralCompleter( self ):
2013-09-01 23:19:46 -04:00
return self._gencomp
def GetOmniCompleter( self ):
2013-09-01 23:19:46 -04:00
return self._omnicomp
def GetFiletypeCompleter( self ):
filetypes = vimsupport.CurrentFiletypes()
2013-04-24 22:59:14 -04: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:
2013-09-01 23:19:46 -04:00
if completer and completer is not self._omnicomp:
return completer
# Return the omni completer for the first filetype
return completers[ 0 ]
def GetFiletypeCompleterForFiletype( self, filetype ):
try:
2013-09-01 23:19:46 -04:00
return self._filetype_completers[ filetype ]
except KeyError:
pass
module_path = _PathToFiletypeCompleterPluginLoader( filetype )
completer = None
supported_filetypes = [ filetype ]
if os.path.exists( module_path ):
module = imp.load_source( filetype, module_path )
completer = module.GetCompleter( self._user_options )
if completer:
supported_filetypes.extend( completer.SupportedFiletypes() )
else:
2013-09-01 23:19:46 -04:00
completer = self._omnicomp
for supported_filetype in supported_filetypes:
2013-09-01 23:19:46 -04:00
self._filetype_completers[ supported_filetype ] = completer
return completer
2013-04-01 07:44:41 -04:00
def ShouldUseGeneralCompleter( self, start_column ):
2013-09-01 23:19:46 -04:00
return self._gencomp.ShouldUseNow( start_column, vim.current.line )
def ShouldUseFiletypeCompleter( self, start_column ):
if self.FiletypeCompletionUsable():
return self.GetFiletypeCompleter().ShouldUseNow(
start_column, vim.current.line )
return False
def NativeFiletypeCompletionAvailable( self ):
completer = self.GetFiletypeCompleter()
2013-09-01 23:19:46 -04:00
return bool( completer ) and completer is not self._omnicomp
def FiletypeCompletionAvailable( self ):
return bool( self.GetFiletypeCompleter() )
def NativeFiletypeCompletionUsable( self ):
return ( self.CurrentFiletypeCompletionEnabled() and
self.NativeFiletypeCompletionAvailable() )
def FiletypeCompletionUsable( self ):
return ( self.CurrentFiletypeCompletionEnabled() and
self.FiletypeCompletionAvailable() )
def OnFileReadyToParse( self ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnFileReadyToParse()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnFileReadyToParse()
2012-04-15 19:57:10 -04:00
2012-05-08 00:23:38 -04:00
def OnBufferUnload( self, deleted_buffer_file ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnBufferUnload( deleted_buffer_file )
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnBufferUnload( deleted_buffer_file )
def OnBufferVisit( self ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnBufferVisit()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnBufferVisit()
def OnInsertLeave( self ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnInsertLeave()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnInsertLeave()
def OnVimLeave( self ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnVimLeave()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnVimLeave()
def DiagnosticsForCurrentFileReady( self ):
if self.FiletypeCompletionUsable():
return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady()
return False
2012-08-06 23:14:21 -04:00
def GetDiagnosticsForCurrentFile( self ):
if self.FiletypeCompletionUsable():
return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile()
2012-08-06 23:14:21 -04:00
return []
def ShowDetailedDiagnostic( self ):
if self.FiletypeCompletionUsable():
return self.GetFiletypeCompleter().ShowDetailedDiagnostic()
def GettingCompletions( self ):
if self.FiletypeCompletionUsable():
return self.GetFiletypeCompleter().GettingCompletions()
return False
def OnCurrentIdentifierFinished( self ):
2013-09-01 23:19:46 -04:00
self._gencomp.OnCurrentIdentifierFinished()
if self.FiletypeCompletionUsable():
self.GetFiletypeCompleter().OnCurrentIdentifierFinished()
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 )
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-01-26 14:44:42 -05:00
def _PathToCompletersFolder():
dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
return os.path.join( dir_of_current_script, 'completers' )
def _PathToFiletypeCompleterPluginLoader( filetype ):
return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' )