2012-08-04 20:46:54 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# 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-15 22:39:03 -04:00
|
|
|
from collections import defaultdict
|
2012-08-04 21:59:22 -04:00
|
|
|
import ycm_core
|
2013-09-20 20:24:34 -04:00
|
|
|
from ycm.server import responses
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm import extra_conf_store
|
2013-09-07 14:58:42 -04:00
|
|
|
from ycm.utils import ToUtf8IfNeeded
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm.completers.completer import Completer
|
2013-09-27 17:59:00 -04:00
|
|
|
from ycm.completers.cpp.flags import Flags, PrepareFlagsForClang
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] )
|
2013-09-06 02:43:14 -04:00
|
|
|
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.'
|
|
|
|
INVALID_FILE_MESSAGE = 'File is invalid.'
|
2013-09-30 16:40:25 -04:00
|
|
|
NO_COMPLETIONS_MESSAGE = 'No completions found; errors in the file?'
|
2013-09-06 02:43:14 -04:00
|
|
|
FILE_TOO_SHORT_MESSAGE = (
|
2013-10-04 18:44:16 -04:00
|
|
|
'File is less than {0} lines long; not compiling.'.format(
|
2013-09-06 02:43:14 -04:00
|
|
|
MIN_LINES_IN_FILE_TO_PARSE ) )
|
|
|
|
NO_DIAGNOSTIC_MESSAGE = 'No diagnostic for current line!'
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ClangCompleter( Completer ):
|
2013-09-02 17:45:53 -04:00
|
|
|
def __init__( self, user_options ):
|
|
|
|
super( ClangCompleter, self ).__init__( user_options )
|
2013-09-30 16:40:25 -04:00
|
|
|
self._max_diagnostics_to_display = user_options[
|
2013-09-02 17:45:53 -04:00
|
|
|
'max_diagnostics_to_display' ]
|
2013-09-30 16:40:25 -04:00
|
|
|
self._completer = ycm_core.ClangCompleter()
|
|
|
|
self._flags = Flags()
|
|
|
|
self._diagnostic_store = None
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
return CLANG_FILETYPES
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def GetUnsavedFilesVector( self, request_data ):
|
2012-08-04 21:59:22 -04:00
|
|
|
files = ycm_core.UnsavedFileVec()
|
2013-09-06 02:43:14 -04:00
|
|
|
for filename, file_data in request_data[ 'file_data' ].iteritems():
|
|
|
|
if not ClangAvailableForFiletypes( file_data[ 'filetypes' ] ):
|
2012-08-04 20:46:54 -04:00
|
|
|
continue
|
2013-09-06 02:43:14 -04:00
|
|
|
contents = file_data[ 'contents' ]
|
|
|
|
if not contents or not filename:
|
2012-08-04 20:46:54 -04:00
|
|
|
continue
|
|
|
|
|
2012-08-04 21:59:22 -04:00
|
|
|
unsaved_file = ycm_core.UnsavedFile()
|
2013-09-20 20:24:34 -04:00
|
|
|
utf8_contents = ToUtf8IfNeeded( contents )
|
|
|
|
unsaved_file.contents_ = utf8_contents
|
|
|
|
unsaved_file.length_ = len( utf8_contents )
|
|
|
|
unsaved_file.filename_ = ToUtf8IfNeeded( filename )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
files.append( unsaved_file )
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
def ComputeCandidatesInner( self, request_data ):
|
2013-09-06 02:43:14 -04:00
|
|
|
filename = request_data[ 'filepath' ]
|
2013-02-06 21:46:57 -05:00
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
if self._completer.UpdatingTranslationUnit( ToUtf8IfNeeded( filename ) ):
|
|
|
|
raise RuntimeError( PARSING_FILE_MESSAGE )
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-09-27 17:59:00 -04:00
|
|
|
flags = self._FlagsForRequest( request_data )
|
2013-01-23 20:23:51 -05:00
|
|
|
if not flags:
|
2013-09-30 16:40:25 -04:00
|
|
|
raise RuntimeError( NO_COMPILE_FLAGS_MESSAGE )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
files = self.GetUnsavedFilesVector( request_data )
|
2013-09-06 02:43:14 -04:00
|
|
|
line = request_data[ 'line_num' ] + 1
|
|
|
|
column = request_data[ 'start_column' ] + 1
|
2013-09-30 16:40:25 -04:00
|
|
|
results = self._completer.CandidatesForLocationInFile(
|
2013-09-07 14:58:42 -04:00
|
|
|
ToUtf8IfNeeded( filename ),
|
2013-01-23 20:23:51 -05:00
|
|
|
line,
|
|
|
|
column,
|
|
|
|
files,
|
2013-09-30 16:40:25 -04:00
|
|
|
flags )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
if not results:
|
2013-09-06 02:43:14 -04:00
|
|
|
raise RuntimeError( NO_COMPLETIONS_MESSAGE )
|
2013-09-30 16:40:25 -04:00
|
|
|
|
|
|
|
return [ ConvertCompletionData( x ) for x in results ]
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-05-08 16:38:56 -04:00
|
|
|
def DefinedSubcommands( self ):
|
2013-05-24 17:48:46 -04:00
|
|
|
return [ 'GoToDefinition',
|
|
|
|
'GoToDeclaration',
|
|
|
|
'GoToDefinitionElseDeclaration',
|
|
|
|
'ClearCompilationFlagCache']
|
2013-05-08 16:38:56 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def OnUserCommand( self, arguments, request_data ):
|
2013-03-31 23:38:29 -04:00
|
|
|
if not arguments:
|
2013-09-06 02:43:14 -04:00
|
|
|
raise ValueError( self.UserCommandsHelpMessage() )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
command = arguments[ 0 ]
|
|
|
|
if command == 'GoToDefinition':
|
2013-09-20 20:24:34 -04:00
|
|
|
return self._GoToDefinition( request_data )
|
2013-03-31 23:38:29 -04:00
|
|
|
elif command == 'GoToDeclaration':
|
2013-09-20 20:24:34 -04:00
|
|
|
return self._GoToDeclaration( request_data )
|
2013-03-31 23:38:29 -04:00
|
|
|
elif command == 'GoToDefinitionElseDeclaration':
|
2013-09-20 20:24:34 -04:00
|
|
|
return self._GoToDefinitionElseDeclaration( request_data )
|
2013-05-24 17:48:46 -04:00
|
|
|
elif command == 'ClearCompilationFlagCache':
|
2013-09-27 19:16:55 -04:00
|
|
|
return self._ClearCompilationFlagCache()
|
2013-09-27 16:52:04 -04:00
|
|
|
raise ValueError( self.UserCommandsHelpMessage() )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def _LocationForGoTo( self, goto_function, request_data ):
|
|
|
|
filename = request_data[ 'filepath' ]
|
2013-03-31 23:38:29 -04:00
|
|
|
if not filename:
|
2013-09-27 17:59:00 -04:00
|
|
|
raise ValueError( INVALID_FILE_MESSAGE )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
2013-09-27 17:59:00 -04:00
|
|
|
flags = self._FlagsForRequest( request_data )
|
2013-03-31 23:38:29 -04:00
|
|
|
if not flags:
|
2013-09-27 17:59:00 -04:00
|
|
|
raise ValueError( NO_COMPILE_FLAGS_MESSAGE )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
files = self.GetUnsavedFilesVector( request_data )
|
2013-09-06 02:43:14 -04:00
|
|
|
line = request_data[ 'line_num' ] + 1
|
2013-09-27 16:52:04 -04:00
|
|
|
column = request_data[ 'column_num' ] + 1
|
2013-09-30 16:40:25 -04:00
|
|
|
return getattr( self._completer, goto_function )(
|
2013-09-20 20:24:34 -04:00
|
|
|
ToUtf8IfNeeded( filename ),
|
2013-03-31 23:38:29 -04:00
|
|
|
line,
|
|
|
|
column,
|
|
|
|
files,
|
|
|
|
flags )
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def _GoToDefinition( self, request_data ):
|
2013-09-27 16:52:04 -04:00
|
|
|
location = self._LocationForGoTo( 'GetDefinitionLocation', request_data )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-09-06 02:43:14 -04:00
|
|
|
raise RuntimeError( 'Can\'t jump to definition.' )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
return responses.BuildGoToResponse( location.filename_,
|
2013-09-27 17:59:00 -04:00
|
|
|
location.line_number_ - 1,
|
|
|
|
location.column_number_ - 1)
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def _GoToDeclaration( self, request_data ):
|
2013-09-27 16:52:04 -04:00
|
|
|
location = self._LocationForGoTo( 'GetDeclarationLocation', request_data )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-09-06 02:43:14 -04:00
|
|
|
raise RuntimeError( 'Can\'t jump to declaration.' )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
return responses.BuildGoToResponse( location.filename_,
|
2013-09-27 17:59:00 -04:00
|
|
|
location.line_number_ - 1,
|
|
|
|
location.column_number_ - 1)
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def _GoToDefinitionElseDeclaration( self, request_data ):
|
2013-09-27 16:52:04 -04:00
|
|
|
location = self._LocationForGoTo( 'GetDefinitionLocation', request_data )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-09-27 16:52:04 -04:00
|
|
|
location = self._LocationForGoTo( 'GetDeclarationLocation', request_data )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-09-06 02:43:14 -04:00
|
|
|
raise RuntimeError( 'Can\'t jump to definition or declaration.' )
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
return responses.BuildGoToResponse( location.filename_,
|
2013-09-27 17:59:00 -04:00
|
|
|
location.line_number_ - 1,
|
|
|
|
location.column_number_ - 1)
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-05-24 17:48:46 -04:00
|
|
|
def _ClearCompilationFlagCache( self ):
|
2013-09-30 16:40:25 -04:00
|
|
|
self._flags.Clear()
|
2013-05-24 17:48:46 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
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:
|
|
|
|
raise ValueError( FILE_TOO_SHORT_MESSAGE )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2013-02-06 21:46:57 -05:00
|
|
|
if not filename:
|
2013-09-30 16:40:25 -04:00
|
|
|
raise ValueError( INVALID_FILE_MESSAGE )
|
2013-01-30 17:46:58 -05:00
|
|
|
|
2013-09-27 17:59:00 -04:00
|
|
|
flags = self._FlagsForRequest( request_data )
|
2013-01-23 20:23:51 -05:00
|
|
|
if not flags:
|
2013-09-30 16:40:25 -04:00
|
|
|
raise ValueError( NO_COMPILE_FLAGS_MESSAGE )
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-10-03 16:15:43 -04:00
|
|
|
diagnostics = self._completer.UpdateTranslationUnit(
|
2013-09-20 20:24:34 -04:00
|
|
|
ToUtf8IfNeeded( filename ),
|
2013-09-06 02:43:14 -04:00
|
|
|
self.GetUnsavedFilesVector( request_data ),
|
2013-01-23 20:23:51 -05:00
|
|
|
flags )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2013-10-03 16:15:43 -04:00
|
|
|
self._diagnostic_store = DiagnosticsToDiagStructure( diagnostics )
|
|
|
|
return [ ConvertToDiagnosticResponse( x ) for x in
|
|
|
|
diagnostics[ : self._max_diagnostics_to_display ] ]
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2013-09-07 20:39:52 -04:00
|
|
|
def OnBufferUnload( self, request_data ):
|
2013-09-30 16:40:25 -04:00
|
|
|
self._completer.DeleteCachesForFile(
|
2013-09-20 20:24:34 -04:00
|
|
|
ToUtf8IfNeeded( request_data[ 'unloaded_buffer' ] ) )
|
2013-03-14 23:39:44 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def GetDetailedDiagnostic( self, request_data ):
|
|
|
|
current_line = request_data[ 'line_num' ] + 1
|
|
|
|
current_column = request_data[ 'column_num' ] + 1
|
|
|
|
current_file = request_data[ 'filepath' ]
|
2012-08-15 22:39:03 -04:00
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
if not self._diagnostic_store:
|
2013-10-03 17:49:51 -04:00
|
|
|
raise ValueError( NO_DIAGNOSTIC_MESSAGE )
|
2013-02-05 23:12:43 -05:00
|
|
|
|
2013-09-30 16:40:25 -04:00
|
|
|
diagnostics = self._diagnostic_store[ current_file ][ current_line ]
|
2012-08-15 22:39:03 -04:00
|
|
|
if not diagnostics:
|
2013-10-03 17:49:51 -04:00
|
|
|
raise ValueError( NO_DIAGNOSTIC_MESSAGE )
|
2012-08-15 22:39:03 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-09-20 20:24:34 -04:00
|
|
|
return responses.BuildDisplayMessageResponse(
|
2013-09-06 02:43:14 -04:00
|
|
|
closest_diagnostic.long_formatted_text_ )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def DebugInfo( self, request_data ):
|
|
|
|
filename = request_data[ 'filepath' ]
|
2013-04-28 18:51:59 -04:00
|
|
|
if not filename:
|
|
|
|
return ''
|
2013-09-27 17:59:00 -04:00
|
|
|
flags = self._FlagsForRequest( request_data ) or []
|
2013-04-12 15:06:52 -04:00
|
|
|
source = extra_conf_store.ModuleFileForSourceFile( filename )
|
2013-09-27 20:13:43 -04:00
|
|
|
return 'Flags for {0} loaded from {1}:\n{2}'.format( filename,
|
|
|
|
source,
|
|
|
|
list( flags ) )
|
2013-01-28 13:00:15 -05:00
|
|
|
|
2013-10-03 13:14:31 -04:00
|
|
|
|
2013-09-27 17:59:00 -04:00
|
|
|
def _FlagsForRequest( self, request_data ):
|
2013-11-05 13:28:40 -05:00
|
|
|
filename = ToUtf8IfNeeded( request_data[ 'filepath' ] )
|
2013-09-27 17:59:00 -04:00
|
|
|
if 'compilation_flags' in request_data:
|
|
|
|
return PrepareFlagsForClang( request_data[ 'compilation_flags' ],
|
|
|
|
filename )
|
2013-10-26 19:22:43 -04:00
|
|
|
client_data = request_data.get( 'extra_conf_data', None )
|
|
|
|
return self._flags.FlagsForFile( filename, client_data = client_data )
|
2013-09-27 17:59:00 -04:00
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
|
|
|
|
def ConvertCompletionData( completion_data ):
|
2013-09-20 20:24:34 -04:00
|
|
|
return responses.BuildCompletionData(
|
2013-09-06 02:43:14 -04:00
|
|
|
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() )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
def DiagnosticsToDiagStructure( diagnostics ):
|
|
|
|
structure = defaultdict(lambda : defaultdict(list))
|
|
|
|
for diagnostic in diagnostics:
|
|
|
|
structure[ diagnostic.filename_ ][ diagnostic.line_number_ ].append(
|
|
|
|
diagnostic )
|
|
|
|
return structure
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def ClangAvailableForFiletypes( filetypes ):
|
2013-03-03 13:48:34 -05:00
|
|
|
return any( [ filetype in CLANG_FILETYPES for filetype in filetypes ] )
|
|
|
|
|
2013-04-24 22:59:14 -04:00
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def InCFamilyFile( filetypes ):
|
|
|
|
return ClangAvailableForFiletypes( filetypes )
|
2013-09-07 14:58:42 -04:00
|
|
|
|
|
|
|
|
2013-10-03 13:14:31 -04:00
|
|
|
def ConvertToDiagnosticResponse( diagnostic ):
|
|
|
|
return responses.BuildDiagnosticData( diagnostic.filename_,
|
|
|
|
diagnostic.line_number_ - 1,
|
|
|
|
diagnostic.column_number_ - 1,
|
|
|
|
diagnostic.text_,
|
|
|
|
diagnostic.kind_ )
|
|
|
|
|
|
|
|
|