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 20:46:54 -04:00
|
|
|
import vim
|
2012-08-04 21:59:22 -04:00
|
|
|
import ycm_core
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm import vimsupport
|
|
|
|
from ycm import extra_conf_store
|
|
|
|
from ycm.completers.completer import Completer
|
|
|
|
from ycm.completers.cpp.flags import Flags
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
CLANG_FILETYPES = set( [ 'c', 'cpp', 'objc', 'objcpp' ] )
|
2012-08-15 23:58:21 -04:00
|
|
|
MAX_DIAGNOSTICS_TO_DISPLAY = int( vimsupport.GetVariableValue(
|
|
|
|
"g:ycm_max_diagnostics_to_display" ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ClangCompleter( Completer ):
|
|
|
|
def __init__( self ):
|
2013-02-10 22:55:05 -05:00
|
|
|
super( ClangCompleter, self ).__init__()
|
2012-08-04 21:59:22 -04:00
|
|
|
self.completer = ycm_core.ClangCompleter()
|
2012-08-04 20:46:54 -04:00
|
|
|
self.completer.EnableThreading()
|
|
|
|
self.contents_holder = []
|
|
|
|
self.filename_holder = []
|
2012-08-15 22:39:03 -04:00
|
|
|
self.last_prepared_diagnostics = []
|
2012-08-12 17:36:36 -04:00
|
|
|
self.parse_future = None
|
2012-08-04 20:46:54 -04:00
|
|
|
self.flags = Flags()
|
2013-02-05 23:12:43 -05:00
|
|
|
self.diagnostic_store = None
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def SupportedFiletypes( self ):
|
|
|
|
return CLANG_FILETYPES
|
|
|
|
|
|
|
|
|
|
|
|
def GetUnsavedFilesVector( self ):
|
2012-09-06 00:49:51 -04:00
|
|
|
# CAREFUL HERE! For UnsavedFile filename and contents we are referring
|
|
|
|
# directly to Python-allocated and -managed memory since we are accepting
|
|
|
|
# pointers to data members of python objects. We need to ensure that those
|
|
|
|
# objects outlive our UnsavedFile objects. This is why we need the
|
|
|
|
# contents_holder and filename_holder lists, to make sure the string objects
|
|
|
|
# are still around when we call CandidatesForQueryAndLocationInFile. We do
|
|
|
|
# this to avoid an extra copy of the entire file contents.
|
|
|
|
|
2012-08-04 21:59:22 -04:00
|
|
|
files = ycm_core.UnsavedFileVec()
|
2012-08-04 20:46:54 -04:00
|
|
|
self.contents_holder = []
|
|
|
|
self.filename_holder = []
|
|
|
|
for buffer in vimsupport.GetUnsavedBuffers():
|
|
|
|
if not ClangAvailableForBuffer( buffer ):
|
|
|
|
continue
|
|
|
|
contents = '\n'.join( buffer )
|
|
|
|
name = buffer.name
|
|
|
|
if not contents or not name:
|
|
|
|
continue
|
|
|
|
self.contents_holder.append( contents )
|
|
|
|
self.filename_holder.append( name )
|
|
|
|
|
2012-08-04 21:59:22 -04:00
|
|
|
unsaved_file = ycm_core.UnsavedFile()
|
2012-08-04 20:46:54 -04:00
|
|
|
unsaved_file.contents_ = self.contents_holder[ -1 ]
|
|
|
|
unsaved_file.length_ = len( self.contents_holder[ -1 ] )
|
|
|
|
unsaved_file.filename_ = self.filename_holder[ -1 ]
|
|
|
|
|
|
|
|
files.append( unsaved_file )
|
|
|
|
|
|
|
|
return files
|
|
|
|
|
|
|
|
|
2013-04-09 22:25:53 -04:00
|
|
|
def CandidatesForQueryAsync( self, query, start_column ):
|
2013-01-24 20:14:10 -05:00
|
|
|
filename = vim.current.buffer.name
|
|
|
|
|
2013-02-06 21:46:57 -05:00
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
|
2013-01-24 20:14:10 -05:00
|
|
|
if self.completer.UpdatingTranslationUnit( filename ):
|
2012-08-04 20:46:54 -04:00
|
|
|
vimsupport.PostVimMessage( 'Still parsing file, no completions yet.' )
|
2013-01-23 20:23:51 -05:00
|
|
|
self.completions_future = None
|
|
|
|
return
|
|
|
|
|
|
|
|
flags = self.flags.FlagsForFile( filename )
|
|
|
|
if not flags:
|
|
|
|
vimsupport.PostVimMessage( 'Still no compile flags, no completions yet.' )
|
|
|
|
self.completions_future = None
|
2012-08-04 20:46:54 -04:00
|
|
|
return
|
|
|
|
|
2013-01-22 20:29:05 -05:00
|
|
|
# TODO: sanitize query, probably in C++ code
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2012-08-04 21:59:22 -04:00
|
|
|
files = ycm_core.UnsavedFileVec()
|
2012-08-04 20:46:54 -04:00
|
|
|
if not query:
|
|
|
|
files = self.GetUnsavedFilesVector()
|
|
|
|
|
|
|
|
line, _ = vim.current.window.cursor
|
2013-04-09 22:25:53 -04:00
|
|
|
column = start_column + 1
|
2013-01-23 20:23:51 -05:00
|
|
|
self.completions_future = (
|
|
|
|
self.completer.CandidatesForQueryAndLocationInFileAsync(
|
|
|
|
query,
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
files,
|
|
|
|
flags ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def CandidatesFromStoredRequest( self ):
|
2013-01-23 20:23:51 -05:00
|
|
|
if not self.completions_future:
|
2012-08-04 20:46:54 -04:00
|
|
|
return []
|
2013-01-23 20:23:51 -05:00
|
|
|
results = [ CompletionDataToDict( x ) for x in
|
|
|
|
self.completions_future.GetResults() ]
|
2012-08-04 20:46:54 -04:00
|
|
|
if not results:
|
|
|
|
vimsupport.PostVimMessage( 'No completions found; errors in the file?' )
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2013-05-08 16:38:56 -04:00
|
|
|
def DefinedSubcommands( self ):
|
|
|
|
return [ "GoToDefinition",
|
|
|
|
"GoToDeclaration",
|
|
|
|
"GoToDefinitionElseDeclaration" ]
|
|
|
|
|
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
def OnUserCommand( self, arguments ):
|
|
|
|
if not arguments:
|
2013-05-09 23:28:04 -04:00
|
|
|
self.EchoUserCommandsHelpMessage()
|
|
|
|
return
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
command = arguments[ 0 ]
|
|
|
|
if command == 'GoToDefinition':
|
|
|
|
self._GoToDefinition()
|
|
|
|
elif command == 'GoToDeclaration':
|
|
|
|
self._GoToDeclaration()
|
|
|
|
elif command == 'GoToDefinitionElseDeclaration':
|
|
|
|
self._GoToDefinitionElseDeclaration()
|
|
|
|
|
|
|
|
|
|
|
|
def _LocationForGoTo( self, goto_function ):
|
|
|
|
filename = vim.current.buffer.name
|
|
|
|
if not filename:
|
|
|
|
return None
|
|
|
|
|
|
|
|
flags = self.flags.FlagsForFile( filename )
|
|
|
|
if not flags:
|
|
|
|
vimsupport.PostVimMessage( 'Still no compile flags, can\'t compile.' )
|
|
|
|
return None
|
|
|
|
|
|
|
|
files = self.GetUnsavedFilesVector()
|
|
|
|
line, column = vimsupport.CurrentLineAndColumn()
|
|
|
|
# Making the line & column 1-based instead of 0-based
|
|
|
|
line += 1
|
|
|
|
column += 1
|
|
|
|
return getattr( self.completer, goto_function )(
|
|
|
|
filename,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
files,
|
|
|
|
flags )
|
|
|
|
|
|
|
|
|
|
|
|
def _GoToDefinition( self ):
|
|
|
|
location = self._LocationForGoTo( 'GetDefinitionLocation' )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-03-31 23:38:29 -04:00
|
|
|
vimsupport.PostVimMessage( 'Can\'t jump to definition.' )
|
|
|
|
return
|
|
|
|
|
|
|
|
vimsupport.JumpToLocation( location.filename_,
|
|
|
|
location.line_number_,
|
|
|
|
location.column_number_ )
|
|
|
|
|
|
|
|
|
|
|
|
def _GoToDeclaration( self ):
|
|
|
|
location = self._LocationForGoTo( 'GetDeclarationLocation' )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-03-31 23:38:29 -04:00
|
|
|
vimsupport.PostVimMessage( 'Can\'t jump to declaration.' )
|
|
|
|
return
|
|
|
|
|
|
|
|
vimsupport.JumpToLocation( location.filename_,
|
|
|
|
location.line_number_,
|
|
|
|
location.column_number_ )
|
|
|
|
|
|
|
|
|
|
|
|
def _GoToDefinitionElseDeclaration( self ):
|
|
|
|
location = self._LocationForGoTo( 'GetDefinitionLocation' )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-03-31 23:38:29 -04:00
|
|
|
location = self._LocationForGoTo( 'GetDeclarationLocation' )
|
2013-04-08 14:14:06 -04:00
|
|
|
if not location or not location.IsValid():
|
2013-03-31 23:38:29 -04:00
|
|
|
vimsupport.PostVimMessage( 'Can\'t jump to definition or declaration.' )
|
|
|
|
return
|
|
|
|
|
|
|
|
vimsupport.JumpToLocation( location.filename_,
|
|
|
|
location.line_number_,
|
|
|
|
location.column_number_ )
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def OnFileReadyToParse( self ):
|
|
|
|
if vimsupport.NumLinesInBuffer( vim.current.buffer ) < 5:
|
2013-01-23 20:23:51 -05:00
|
|
|
self.parse_future = None
|
2012-08-04 20:46:54 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
filename = vim.current.buffer.name
|
2013-02-06 21:46:57 -05:00
|
|
|
if not filename:
|
|
|
|
return
|
|
|
|
|
2013-01-30 17:46:58 -05:00
|
|
|
if self.completer.UpdatingTranslationUnit( filename ):
|
|
|
|
return
|
|
|
|
|
2013-01-23 20:23:51 -05:00
|
|
|
flags = self.flags.FlagsForFile( filename )
|
|
|
|
if not flags:
|
|
|
|
self.parse_future = None
|
|
|
|
return
|
|
|
|
|
2012-08-12 17:36:36 -04:00
|
|
|
self.parse_future = self.completer.UpdateTranslationUnitAsync(
|
2012-08-04 20:46:54 -04:00
|
|
|
filename,
|
|
|
|
self.GetUnsavedFilesVector(),
|
2013-01-23 20:23:51 -05:00
|
|
|
flags )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-05-11 16:49:48 -04:00
|
|
|
def OnBufferUnload( self, deleted_buffer_file ):
|
2013-03-16 15:10:24 -04:00
|
|
|
self.completer.DeleteCachesForFileAsync( deleted_buffer_file )
|
2013-03-14 23:39:44 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def DiagnosticsForCurrentFileReady( self ):
|
2012-08-12 17:36:36 -04:00
|
|
|
if not self.parse_future:
|
|
|
|
return False
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2012-08-12 17:36:36 -04:00
|
|
|
return self.parse_future.ResultsReady()
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2012-08-12 22:01:39 -04:00
|
|
|
|
2013-02-06 00:22:50 -05:00
|
|
|
def GettingCompletions( self ):
|
|
|
|
return self.completer.UpdatingTranslationUnit( vim.current.buffer.name )
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def GetDiagnosticsForCurrentFile( self ):
|
|
|
|
if self.DiagnosticsForCurrentFileReady():
|
2012-08-15 22:39:03 -04:00
|
|
|
diagnostics = self.completer.DiagnosticsForFile( vim.current.buffer.name )
|
|
|
|
self.diagnostic_store = DiagnosticsToDiagStructure( diagnostics )
|
|
|
|
self.last_prepared_diagnostics = [ DiagnosticToDict( x ) for x in
|
2012-08-15 23:58:21 -04:00
|
|
|
diagnostics[ : MAX_DIAGNOSTICS_TO_DISPLAY ] ]
|
2012-08-12 17:36:36 -04:00
|
|
|
self.parse_future = None
|
2012-08-15 22:39:03 -04:00
|
|
|
return self.last_prepared_diagnostics
|
|
|
|
|
|
|
|
|
|
|
|
def ShowDetailedDiagnostic( self ):
|
|
|
|
current_line, current_column = vimsupport.CurrentLineAndColumn()
|
|
|
|
|
|
|
|
# CurrentLineAndColumn() numbers are 0-based, clang numbers are 1-based
|
|
|
|
current_line += 1
|
|
|
|
current_column += 1
|
|
|
|
|
|
|
|
current_file = vim.current.buffer.name
|
|
|
|
|
2013-02-05 23:12:43 -05:00
|
|
|
if not self.diagnostic_store:
|
|
|
|
vimsupport.PostVimMessage( "No diagnostic for current line!" )
|
|
|
|
return
|
|
|
|
|
|
|
|
diagnostics = self.diagnostic_store[ current_file ][ current_line ]
|
2012-08-15 22:39:03 -04:00
|
|
|
if not diagnostics:
|
|
|
|
vimsupport.PostVimMessage( "No diagnostic for current line!" )
|
|
|
|
return
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
vimsupport.EchoText( closest_diagnostic.long_formatted_text_ )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def ShouldUseNow( self, start_column ):
|
2013-02-12 00:46:06 -05:00
|
|
|
# We don't want to use the Completer API cache, we use one in the C++ code.
|
|
|
|
return self.ShouldUseNowInner( start_column )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-01-28 13:00:15 -05:00
|
|
|
def DebugInfo( self ):
|
|
|
|
filename = vim.current.buffer.name
|
2013-04-28 18:51:59 -04:00
|
|
|
if not filename:
|
|
|
|
return ''
|
2013-01-28 15:56:45 -05:00
|
|
|
flags = self.flags.FlagsForFile( filename ) or []
|
2013-04-12 15:06:52 -04:00
|
|
|
source = extra_conf_store.ModuleFileForSourceFile( filename )
|
2013-04-25 01:51:53 -04:00
|
|
|
return 'Flags for {0} loaded from {1}:\n{2}'.format( filename,
|
|
|
|
source,
|
|
|
|
list( flags ) )
|
2013-01-28 13:00:15 -05:00
|
|
|
|
|
|
|
|
2012-08-12 17:07:38 -04:00
|
|
|
# TODO: make these functions module-local
|
2012-08-04 20:46:54 -04:00
|
|
|
def CompletionDataToDict( completion_data ):
|
|
|
|
# see :h complete-items for a description of the dictionary fields
|
|
|
|
return {
|
|
|
|
'word' : completion_data.TextToInsertInBuffer(),
|
|
|
|
'abbr' : completion_data.MainCompletionText(),
|
|
|
|
'menu' : completion_data.ExtraMenuInfo(),
|
|
|
|
'kind' : completion_data.kind_,
|
2012-08-05 22:49:54 -04:00
|
|
|
'info' : completion_data.DetailedInfoForPreviewWindow(),
|
2012-08-04 20:46:54 -04:00
|
|
|
'dup' : 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def DiagnosticToDict( diagnostic ):
|
|
|
|
# see :h getqflist for a description of the dictionary fields
|
|
|
|
return {
|
2012-08-05 22:49:54 -04:00
|
|
|
# TODO: wrap the bufnr generation into a function
|
2012-08-04 20:46:54 -04:00
|
|
|
'bufnr' : int( vim.eval( "bufnr('{0}', 1)".format(
|
|
|
|
diagnostic.filename_ ) ) ),
|
|
|
|
'lnum' : diagnostic.line_number_,
|
|
|
|
'col' : diagnostic.column_number_,
|
|
|
|
'text' : diagnostic.text_,
|
|
|
|
'type' : diagnostic.kind_,
|
|
|
|
'valid' : 1
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def ClangAvailableForBuffer( buffer_object ):
|
2013-03-03 13:48:34 -05:00
|
|
|
filetypes = vimsupport.FiletypesForBuffer( buffer_object )
|
|
|
|
return any( [ filetype in CLANG_FILETYPES for filetype in filetypes ] )
|
|
|
|
|
2013-04-24 22:59:14 -04:00
|
|
|
|
|
|
|
def InCFamilyFile():
|
|
|
|
return any( [ filetype in CLANG_FILETYPES for filetype in
|
|
|
|
vimsupport.CurrentFiletypes() ] )
|
|
|
|
|