youcompleteme.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. import imp
  20. import os
  21. import vim
  22. from ycm import vimsupport
  23. from ycm import utils
  24. try:
  25. import ycm_core
  26. except ImportError as e:
  27. vimsupport.PostVimMessage(
  28. 'Error importing ycm_core. Are you sure you have placed a version 3.2+ '
  29. 'libclang.[so|dll|dylib] in folder "{0}"? See the Installation Guide in '
  30. 'the docs. Full error: {1}'.format(
  31. os.path.dirname( os.path.abspath( __file__ ) ), str( e ) ) )
  32. from ycm.completers.all.omni_completer import OmniCompleter
  33. from ycm.completers.general.general_completer_store import GeneralCompleterStore
  34. FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE = vim.eval(
  35. 'g:ycm_filetype_specific_completion_to_disable' )
  36. class YouCompleteMe( object ):
  37. def __init__( self ):
  38. self.gencomp = GeneralCompleterStore()
  39. self.omnicomp = OmniCompleter()
  40. self.filetype_completers = {}
  41. def GetGeneralCompleter( self ):
  42. return self.gencomp
  43. def GetOmniCompleter( self ):
  44. return self.omnicomp
  45. def GetFiletypeCompleter( self ):
  46. filetypes = vimsupport.CurrentFiletypes()
  47. completers = [ self.GetFiletypeCompleterForFiletype( filetype )
  48. for filetype in filetypes ]
  49. if not completers:
  50. return None
  51. # Try to find a native completer first
  52. for completer in completers:
  53. if completer and completer is not self.omnicomp:
  54. return completer
  55. # Return the omni completer for the first filetype
  56. return completers[0]
  57. def GetFiletypeCompleterForFiletype( self, filetype ):
  58. try:
  59. return self.filetype_completers[ filetype ]
  60. except KeyError:
  61. pass
  62. module_path = _PathToFiletypeCompleterPluginLoader( filetype )
  63. completer = None
  64. supported_filetypes = [ filetype ]
  65. if os.path.exists( module_path ):
  66. module = imp.load_source( filetype, module_path )
  67. completer = module.GetCompleter()
  68. if completer:
  69. supported_filetypes.extend( completer.SupportedFiletypes() )
  70. else:
  71. completer = self.omnicomp
  72. for supported_filetype in supported_filetypes:
  73. self.filetype_completers[ supported_filetype ] = completer
  74. return completer
  75. def ShouldUseGeneralCompleter( self, start_column ):
  76. return self.gencomp.ShouldUseNow( start_column )
  77. def ShouldUseFiletypeCompleter( self, start_column ):
  78. if self.FiletypeCompletionUsable():
  79. return self.GetFiletypeCompleter().ShouldUseNow(
  80. start_column )
  81. return False
  82. def NativeFiletypeCompletionAvailable( self ):
  83. completer = self.GetFiletypeCompleter()
  84. return bool( completer ) and completer is not self.omnicomp
  85. def FiletypeCompletionAvailable( self ):
  86. return bool( self.GetFiletypeCompleter() )
  87. def NativeFiletypeCompletionUsable( self ):
  88. return ( _CurrentFiletypeCompletionEnabled() and
  89. self.NativeFiletypeCompletionAvailable() )
  90. def FiletypeCompletionUsable( self ):
  91. return ( _CurrentFiletypeCompletionEnabled() and
  92. self.FiletypeCompletionAvailable() )
  93. def OnFileReadyToParse( self ):
  94. self.gencomp.OnFileReadyToParse()
  95. if self.FiletypeCompletionUsable():
  96. self.GetFiletypeCompleter().OnFileReadyToParse()
  97. def OnBufferUnload( self, deleted_buffer_file ):
  98. self.gencomp.OnBufferUnload( deleted_buffer_file )
  99. if self.FiletypeCompletionUsable():
  100. self.GetFiletypeCompleter().OnBufferUnload( deleted_buffer_file )
  101. def OnBufferVisit( self ):
  102. self.gencomp.OnBufferVisit()
  103. if self.FiletypeCompletionUsable():
  104. self.GetFiletypeCompleter().OnBufferVisit()
  105. def OnInsertLeave( self ):
  106. self.gencomp.OnInsertLeave()
  107. if self.FiletypeCompletionUsable():
  108. self.GetFiletypeCompleter().OnInsertLeave()
  109. def DiagnosticsForCurrentFileReady( self ):
  110. if self.FiletypeCompletionUsable():
  111. return self.GetFiletypeCompleter().DiagnosticsForCurrentFileReady()
  112. return False
  113. def GetDiagnosticsForCurrentFile( self ):
  114. if self.FiletypeCompletionUsable():
  115. return self.GetFiletypeCompleter().GetDiagnosticsForCurrentFile()
  116. return []
  117. def ShowDetailedDiagnostic( self ):
  118. if self.FiletypeCompletionUsable():
  119. return self.GetFiletypeCompleter().ShowDetailedDiagnostic()
  120. def GettingCompletions( self ):
  121. if self.FiletypeCompletionUsable():
  122. return self.GetFiletypeCompleter().GettingCompletions()
  123. return False
  124. def OnCurrentIdentifierFinished( self ):
  125. self.gencomp.OnCurrentIdentifierFinished()
  126. if self.FiletypeCompletionUsable():
  127. self.GetFiletypeCompleter().OnCurrentIdentifierFinished()
  128. def DebugInfo( self ):
  129. completers = set( self.filetype_completers.values() )
  130. completers.add( self.gencomp )
  131. output = []
  132. for completer in completers:
  133. if not completer:
  134. continue
  135. debug = completer.DebugInfo()
  136. if debug:
  137. output.append( debug )
  138. has_clang_support = ycm_core.HasClangSupport()
  139. output.append( 'Has Clang support compiled in: {0}'.format(
  140. has_clang_support ) )
  141. if has_clang_support:
  142. output.append( ycm_core.ClangVersion() )
  143. return '\n'.join( output )
  144. def _CurrentFiletypeCompletionEnabled():
  145. filetypes = vimsupport.CurrentFiletypes()
  146. return not all([ x in FILETYPE_SPECIFIC_COMPLETION_TO_DISABLE
  147. for x in filetypes ])
  148. def _PathToCompletersFolder():
  149. dir_of_current_script = os.path.dirname( os.path.abspath( __file__ ) )
  150. return os.path.join( dir_of_current_script, 'completers' )
  151. def _PathToFiletypeCompleterPluginLoader( filetype ):
  152. return os.path.join( _PathToCompletersFolder(), filetype, 'hook.py' )
  153. def CompletionStartColumn():
  154. """Returns the 0-based index where the completion string should start. So if
  155. the user enters:
  156. foo.bar^
  157. with the cursor being at the location of the caret, then the starting column
  158. would be the index of the letter 'b'.
  159. """
  160. line = vim.current.line
  161. start_column = vimsupport.CurrentColumn()
  162. while start_column > 0 and utils.IsIdentifierChar( line[ start_column - 1 ] ):
  163. start_column -= 1
  164. return start_column
  165. def CurrentIdentifierFinished():
  166. current_column = vimsupport.CurrentColumn()
  167. previous_char_index = current_column - 1
  168. if previous_char_index < 0:
  169. return True
  170. line = vim.current.line
  171. try:
  172. previous_char = line[ previous_char_index ]
  173. except IndexError:
  174. return False
  175. if utils.IsIdentifierChar( previous_char ):
  176. return False
  177. if ( not utils.IsIdentifierChar( previous_char ) and
  178. previous_char_index > 0 and
  179. utils.IsIdentifierChar( line[ previous_char_index - 1 ] ) ):
  180. return True
  181. else:
  182. return line[ : current_column ].isspace()
  183. COMPATIBLE_WITH_CORE_VERSION = 3
  184. def CompatibleWithYcmCore():
  185. try:
  186. current_core_version = ycm_core.YcmCoreVersion()
  187. except AttributeError:
  188. return False
  189. return current_core_version == COMPATIBLE_WITH_CORE_VERSION