123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- from collections import defaultdict
- import ycm_core
- import logging
- from ycm import server_responses
- from ycm import extra_conf_store
- from ycm.completers.completer import Completer
- from ycm.completers.cpp.flags import Flags
- CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] )
- MIN_LINES_IN_FILE_TO_PARSE = 5
- PARSING_FILE_MESSAGE = 'Still parsing file, no completions yet.'
- NO_COMPILE_FLAGS_MESSAGE = 'Still no compile flags, no completions yet.'
- NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?'
- INVALID_FILE_MESSAGE = 'File is invalid.'
- FILE_TOO_SHORT_MESSAGE = (
- 'File is less than {} lines long; not compiling.'.format(
- MIN_LINES_IN_FILE_TO_PARSE ) )
- NO_DIAGNOSTIC_MESSAGE = 'No diagnostic for current line!'
- class ClangCompleter( Completer ):
- def __init__( self, user_options ):
- super( ClangCompleter, self ).__init__( user_options )
- self.max_diagnostics_to_display = user_options[
- 'max_diagnostics_to_display' ]
- self.completer = ycm_core.ClangCompleter()
- self.completer.EnableThreading()
- self.last_prepared_diagnostics = []
- self.parse_future = None
- self.flags = Flags()
- self.diagnostic_store = None
- self._logger = logging.getLogger( __name__ )
-
-
-
-
- self.extra_parse_desired = False
- def SupportedFiletypes( self ):
- return CLANG_FILETYPES
- def GetUnsavedFilesVector( self, request_data ):
- files = ycm_core.UnsavedFileVec()
- for filename, file_data in request_data[ 'file_data' ].iteritems():
- if not ClangAvailableForFiletypes( file_data[ 'filetypes' ] ):
- continue
- contents = file_data[ 'contents' ]
- if not contents or not filename:
- continue
- unsaved_file = ycm_core.UnsavedFile()
- unsaved_file.contents_ = contents
- unsaved_file.length_ = len( contents )
- unsaved_file.filename_ = filename
- files.append( unsaved_file )
- return files
- def CandidatesForQueryAsync( self, request_data ):
- filename = request_data[ 'filepath' ]
- if not filename:
- return
- if self.completer.UpdatingTranslationUnit( filename ):
- self.completions_future = None
- self._logger.info( PARSING_FILE_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- PARSING_FILE_MESSAGE )
- flags = self.flags.FlagsForFile( filename )
- if not flags:
- self.completions_future = None
- self._logger.info( NO_COMPILE_FLAGS_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- NO_COMPILE_FLAGS_MESSAGE )
-
- files = ycm_core.UnsavedFileVec()
- query = request_data[ 'query' ]
- if not query:
- files = self.GetUnsavedFilesVector( request_data )
- line = request_data[ 'line_num' ] + 1
- column = request_data[ 'start_column' ] + 1
- self.completions_future = (
- self.completer.CandidatesForQueryAndLocationInFileAsync(
- query,
- filename,
- line,
- column,
- files,
- flags ) )
- def CandidatesFromStoredRequest( self ):
- if not self.completions_future:
- return []
- results = [ ConvertCompletionData( x ) for x in
- self.completions_future.GetResults() ]
- if not results:
- self._logger.warning( NO_COMPLETIONS_MESSAGE )
- raise RuntimeError( NO_COMPLETIONS_MESSAGE )
- return results
- def DefinedSubcommands( self ):
- return [ 'GoToDefinition',
- 'GoToDeclaration',
- 'GoToDefinitionElseDeclaration',
- 'ClearCompilationFlagCache']
- def OnUserCommand( self, arguments, request_data ):
- if not arguments:
- raise ValueError( self.UserCommandsHelpMessage() )
- command = arguments[ 0 ]
- if command == 'GoToDefinition':
- self._GoToDefinition( request_data )
- elif command == 'GoToDeclaration':
- self._GoToDeclaration( request_data )
- elif command == 'GoToDefinitionElseDeclaration':
- self._GoToDefinitionElseDeclaration( request_data )
- elif command == 'ClearCompilationFlagCache':
- self._ClearCompilationFlagCache( request_data )
- def _LocationForGoTo( self, goto_function, request_data ):
- filename = request_data[ 'filepath' ]
- if not filename:
- self._logger.warning( INVALID_FILE_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- INVALID_FILE_MESSAGE )
- flags = self.flags.FlagsForFile( filename )
- if not flags:
- self._logger.info( NO_COMPILE_FLAGS_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- NO_COMPILE_FLAGS_MESSAGE )
- files = self.GetUnsavedFilesVector()
- line = request_data[ 'line_num' ] + 1
- column = request_data[ 'start_column' ] + 1
- return getattr( self.completer, goto_function )(
- filename,
- line,
- column,
- files,
- flags )
- def _GoToDefinition( self, request_data ):
- location = self._LocationForGoTo( 'GetDefinitionLocation' )
- if not location or not location.IsValid():
- raise RuntimeError( 'Can\'t jump to definition.' )
- return server_responses.BuildGoToResponse( location.filename_,
- location.line_number_,
- location.column_number_ )
- def _GoToDeclaration( self, request_data ):
- location = self._LocationForGoTo( 'GetDeclarationLocation' )
- if not location or not location.IsValid():
- raise RuntimeError( 'Can\'t jump to declaration.' )
- return server_responses.BuildGoToResponse( location.filename_,
- location.line_number_,
- location.column_number_ )
- def _GoToDefinitionElseDeclaration( self, request_data ):
- location = self._LocationForGoTo( 'GetDefinitionLocation' )
- if not location or not location.IsValid():
- location = self._LocationForGoTo( 'GetDeclarationLocation' )
- if not location or not location.IsValid():
- raise RuntimeError( 'Can\'t jump to definition or declaration.' )
- return server_responses.BuildGoToResponse( location.filename_,
- location.line_number_,
- location.column_number_ )
- def _ClearCompilationFlagCache( self ):
- self.flags.Clear()
- def OnFileReadyToParse( self, request_data ):
- filename = request_data[ 'filepath' ]
- contents = request_data[ 'file_data' ][ filename ][ 'contents' ]
- if contents.count( '\n' ) < MIN_LINES_IN_FILE_TO_PARSE:
- self.parse_future = None
- self._logger.warning( FILE_TOO_SHORT_MESSAGE )
- raise ValueError( FILE_TOO_SHORT_MESSAGE )
- if not filename:
- self._logger.warning( INVALID_FILE_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- INVALID_FILE_MESSAGE )
- if self.completer.UpdatingTranslationUnit( filename ):
- self.extra_parse_desired = True
- return
- flags = self.flags.FlagsForFile( filename )
- if not flags:
- self.parse_future = None
- self._logger.info( NO_COMPILE_FLAGS_MESSAGE )
- return server_responses.BuildDisplayMessageResponse(
- NO_COMPILE_FLAGS_MESSAGE )
- self.parse_future = self.completer.UpdateTranslationUnitAsync(
- filename,
- self.GetUnsavedFilesVector( request_data ),
- flags )
- self.extra_parse_desired = False
- def OnBufferUnload( self, deleted_buffer_file ):
- self.completer.DeleteCachesForFileAsync( deleted_buffer_file )
- def DiagnosticsForCurrentFileReady( self ):
- if not self.parse_future:
- return False
- return self.parse_future.ResultsReady()
- def GettingCompletions( self, request_data ):
- return self.completer.UpdatingTranslationUnit( request_data[ 'filepath' ] )
- def GetDiagnosticsForCurrentFile( self, request_data ):
- filename = request_data[ 'filepath' ]
- if self.DiagnosticsForCurrentFileReady():
- diagnostics = self.completer.DiagnosticsForFile( filename )
- self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics )
- self.last_prepared_diagnostics = [
- server_responses.BuildDiagnosticData( x ) for x in
- diagnostics[ : self.max_diagnostics_to_display ] ]
- self.parse_future = None
- if self.extra_parse_desired:
- self.OnFileReadyToParse()
- return self.last_prepared_diagnostics
- def GetDetailedDiagnostic( self, request_data ):
- current_line = request_data[ 'line_num' ] + 1
- current_column = request_data[ 'column_num' ] + 1
- current_file = request_data[ 'filepath' ]
- if not self.diagnostic_store:
- return server_responses.BuildDisplayMessageResponse(
- NO_DIAGNOSTIC_MESSAGE )
- diagnostics = self.diagnostic_store[ current_file ][ current_line ]
- if not diagnostics:
- return server_responses.BuildDisplayMessageResponse(
- NO_DIAGNOSTIC_MESSAGE )
- closest_diagnostic = None
- distance_to_closest_diagnostic = 999
- for diagnostic in diagnostics:
- distance = abs( current_column - diagnostic.column_number_ )
- if distance < distance_to_closest_diagnostic:
- distance_to_closest_diagnostic = distance
- closest_diagnostic = diagnostic
- return server_responses.BuildDisplayMessageResponse(
- closest_diagnostic.long_formatted_text_ )
- def ShouldUseNow( self, request_data ):
-
- return self.ShouldUseNowInner( request_data )
- def DebugInfo( self, request_data ):
- filename = request_data[ 'filepath' ]
- if not filename:
- return ''
- flags = self.flags.FlagsForFile( filename ) or []
- source = extra_conf_store.ModuleFileForSourceFile( filename )
- return server_responses.BuildDisplayMessageResponse(
- 'Flags for {0} loaded from {1}:\n{2}'.format( filename,
- source,
- list( flags ) ) )
- def ConvertCompletionData( completion_data ):
- return server_responses.BuildCompletionData(
- insertion_text = completion_data.TextToInsertInBuffer(),
- menu_text = completion_data.MainCompletionText(),
- extra_menu_info = completion_data.ExtraMenuInfo(),
- kind = completion_data.kind_,
- detailed_info = completion_data.DetailedInfoForPreviewWindow() )
- def DiagnosticsToDiagStructure( diagnostics ):
- structure = defaultdict(lambda : defaultdict(list))
- for diagnostic in diagnostics:
- structure[ diagnostic.filename_ ][ diagnostic.line_number_ ].append(
- diagnostic )
- return structure
- def ClangAvailableForFiletypes( filetypes ):
- return any( [ filetype in CLANG_FILETYPES for filetype in filetypes ] )
- def InCFamilyFile( filetypes ):
- return ClangAvailableForFiletypes( filetypes )
|