From 90e097efa40c16b7a7376cb008341013d5f0f7f0 Mon Sep 17 00:00:00 2001 From: Strahinja Val Markovic Date: Fri, 21 Mar 2014 12:37:01 -0700 Subject: [PATCH] Implementing the new GoToImprecise command Same as GoTo, but trades correctness for speed. See the docs for details. --- README.md | 13 +++++++ cpp/ycm/ClangCompleter/ClangCompleter.cpp | 10 +++-- cpp/ycm/ClangCompleter/ClangCompleter.h | 6 ++- cpp/ycm/ClangCompleter/TranslationUnit.cpp | 12 ++++-- cpp/ycm/ClangCompleter/TranslationUnit.h | 6 ++- cpp/ycm/versioning.cpp | 2 +- python/ycm/base.py | 2 +- python/ycm/completers/cpp/clang_completer.py | 39 +++++++++++++------- 8 files changed, 63 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index af80cf30..7e8f1e20 100644 --- a/README.md +++ b/README.md @@ -627,6 +627,19 @@ the current translation unit, jumps to the symbol's declaration. Supported in filetypes: `c, cpp, objc, objcpp, python, cs` +### The `GoToImprecise` subcommand + +WARNING: This command trades correctness for speed! + +Same as the `GoTo` command except that it doesn't recompile the file with +libclang before looking up nodes in the AST. This can be very useful when you're +editing files that take long to compile but you know that you haven't made any +changes since the last parse that would lead to incorrect jumps. When you're +just browsing around your codebase, this command can spare you quite a bit of +latency. + +Supported in filetypes: `c, cpp, objc, objcpp` + ### The `ClearCompilationFlagCache` subcommand YCM caches the flags it gets from the `FlagsForFile` function in your diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.cpp b/cpp/ycm/ClangCompleter/ClangCompleter.cpp index b23004ed..effadeac 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.cpp +++ b/cpp/ycm/ClangCompleter/ClangCompleter.cpp @@ -133,7 +133,8 @@ Location ClangCompleter::GetDeclarationLocation( int line, int column, const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + const std::vector< std::string > &flags, + bool reparse ) { ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -142,7 +143,7 @@ Location ClangCompleter::GetDeclarationLocation( return Location(); } - return unit->GetDeclarationLocation( line, column, unsaved_files ); + return unit->GetDeclarationLocation( line, column, unsaved_files, reparse ); } @@ -151,7 +152,8 @@ Location ClangCompleter::GetDefinitionLocation( int line, int column, const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ) { + const std::vector< std::string > &flags, + bool reparse ) { ReleaseGil unlock; shared_ptr< TranslationUnit > unit = translation_unit_store_.GetOrCreate( filename, unsaved_files, flags ); @@ -160,7 +162,7 @@ Location ClangCompleter::GetDefinitionLocation( return Location(); } - return unit->GetDefinitionLocation( line, column, unsaved_files ); + return unit->GetDefinitionLocation( line, column, unsaved_files, reparse ); } diff --git a/cpp/ycm/ClangCompleter/ClangCompleter.h b/cpp/ycm/ClangCompleter/ClangCompleter.h index 8a21df8d..ee40b24a 100644 --- a/cpp/ycm/ClangCompleter/ClangCompleter.h +++ b/cpp/ycm/ClangCompleter/ClangCompleter.h @@ -62,14 +62,16 @@ public: int line, int column, const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + const std::vector< std::string > &flags, + bool reparse = true ); Location GetDefinitionLocation( const std::string &filename, int line, int column, const std::vector< UnsavedFile > &unsaved_files, - const std::vector< std::string > &flags ); + const std::vector< std::string > &flags, + bool reparse = true ); void DeleteCachesForFile( const std::string &filename ); diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.cpp b/cpp/ycm/ClangCompleter/TranslationUnit.cpp index b17925ee..d169ec3a 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.cpp +++ b/cpp/ycm/ClangCompleter/TranslationUnit.cpp @@ -177,8 +177,10 @@ std::vector< CompletionData > TranslationUnit::CandidatesForLocation( Location TranslationUnit::GetDeclarationLocation( int line, int column, - const std::vector< UnsavedFile > &unsaved_files ) { - ReparseForIndexing( unsaved_files ); + const std::vector< UnsavedFile > &unsaved_files, + bool reparse ) { + if (reparse) + ReparseForIndexing( unsaved_files ); unique_lock< mutex > lock( clang_access_mutex_ ); if ( !clang_translation_unit_ ) @@ -200,8 +202,10 @@ Location TranslationUnit::GetDeclarationLocation( Location TranslationUnit::GetDefinitionLocation( int line, int column, - const std::vector< UnsavedFile > &unsaved_files ) { - ReparseForIndexing( unsaved_files ); + const std::vector< UnsavedFile > &unsaved_files, + bool reparse ) { + if (reparse) + ReparseForIndexing( unsaved_files ); unique_lock< mutex > lock( clang_access_mutex_ ); if ( !clang_translation_unit_ ) diff --git a/cpp/ycm/ClangCompleter/TranslationUnit.h b/cpp/ycm/ClangCompleter/TranslationUnit.h index 4f852508..03becc8a 100644 --- a/cpp/ycm/ClangCompleter/TranslationUnit.h +++ b/cpp/ycm/ClangCompleter/TranslationUnit.h @@ -69,12 +69,14 @@ public: Location GetDeclarationLocation( int line, int column, - const std::vector< UnsavedFile > &unsaved_files ); + const std::vector< UnsavedFile > &unsaved_files, + bool reparse = true ); Location GetDefinitionLocation( int line, int column, - const std::vector< UnsavedFile > &unsaved_files ); + const std::vector< UnsavedFile > &unsaved_files, + bool reparse = true ); private: void Reparse( std::vector< CXUnsavedFile > &unsaved_files ); diff --git a/cpp/ycm/versioning.cpp b/cpp/ycm/versioning.cpp index d7339dc3..f4d736df 100644 --- a/cpp/ycm/versioning.cpp +++ b/cpp/ycm/versioning.cpp @@ -20,7 +20,7 @@ namespace YouCompleteMe { int YcmCoreVersion() { // We increment this every time when we want to force users to recompile // ycm_core. - return 8; + return 9; } } // namespace YouCompleteMe diff --git a/python/ycm/base.py b/python/ycm/base.py index e1e4dde0..da0a4bc2 100644 --- a/python/ycm/base.py +++ b/python/ycm/base.py @@ -188,7 +188,7 @@ def OverlapLength( left_string, right_string ): length += 1 -COMPATIBLE_WITH_CORE_VERSION = 8 +COMPATIBLE_WITH_CORE_VERSION = 9 def CompatibleWithYcmCore(): try: diff --git a/python/ycm/completers/cpp/clang_completer.py b/python/ycm/completers/cpp/clang_completer.py index eafcc23d..7e699912 100644 --- a/python/ycm/completers/cpp/clang_completer.py +++ b/python/ycm/completers/cpp/clang_completer.py @@ -104,6 +104,7 @@ class ClangCompleter( Completer ): return [ 'GoToDefinition', 'GoToDeclaration', 'GoTo', + 'GoToImprecise', 'ClearCompilationFlagCache'] @@ -118,12 +119,14 @@ class ClangCompleter( Completer ): return self._GoToDeclaration( request_data ) elif command == 'GoTo': return self._GoTo( request_data ) + elif command == 'GoToImprecise': + return self._GoToImprecise( request_data ) elif command == 'ClearCompilationFlagCache': return self._ClearCompilationFlagCache() raise ValueError( self.UserCommandsHelpMessage() ) - def _LocationForGoTo( self, goto_function, request_data ): + def _LocationForGoTo( self, goto_function, request_data, reparse = True ): filename = request_data[ 'filepath' ] if not filename: raise ValueError( INVALID_FILE_MESSAGE ) @@ -140,27 +143,22 @@ class ClangCompleter( Completer ): line, column, files, - flags ) + flags, + reparse ) def _GoToDefinition( self, request_data ): location = self._LocationForGoTo( 'GetDefinitionLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition.' ) - - return responses.BuildGoToResponse( location.filename_, - location.line_number_ - 1, - location.column_number_ - 1) + return _ResponseForLocation( location ) def _GoToDeclaration( self, request_data ): location = self._LocationForGoTo( 'GetDeclarationLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to declaration.' ) - - return responses.BuildGoToResponse( location.filename_, - location.line_number_ - 1, - location.column_number_ - 1) + return _ResponseForLocation( location ) def _GoTo( self, request_data ): @@ -169,11 +167,20 @@ class ClangCompleter( Completer ): location = self._LocationForGoTo( 'GetDeclarationLocation', request_data ) if not location or not location.IsValid(): raise RuntimeError( 'Can\'t jump to definition or declaration.' ) + return _ResponseForLocation( location ) - return responses.BuildGoToResponse( location.filename_, - location.line_number_ - 1, - location.column_number_ - 1) + def _GoToImprecise( self, request_data ): + location = self._LocationForGoTo( 'GetDefinitionLocation', + request_data, + reparse = False ) + if not location or not location.IsValid(): + location = self._LocationForGoTo( 'GetDeclarationLocation', + request_data, + reparse = False ) + if not location or not location.IsValid(): + raise RuntimeError( 'Can\'t jump to definition or declaration.' ) + return _ResponseForLocation( location ) def _ClearCompilationFlagCache( self ): @@ -295,3 +302,9 @@ def _FilterDiagnostics( diagnostics ): x.text_ != TOO_MANY_ERRORS_DIAG_TEXT_TO_IGNORE ] +def _ResponseForLocation( location ): + return responses.BuildGoToResponse( location.filename_, + location.line_number_ - 1, + location.column_number_ - 1) + +