1
0
Эх сурвалжийг харах

Implementing the new GoToImprecise command

Same as GoTo, but trades correctness for speed. See the docs for details.
Strahinja Val Markovic 11 жил өмнө
parent
commit
90e097efa4

+ 13 - 0
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

+ 6 - 4
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 );
 }
 
 

+ 4 - 2
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 );
 

+ 8 - 4
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_ )

+ 4 - 2
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 );

+ 1 - 1
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

+ 1 - 1
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:

+ 26 - 13
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)
+
+