OSDN Git Service

C++ editor: Make semantic info aware of file rename
authorLeandro Melo <leandro.melo@nokia.com>
Wed, 13 Jul 2011 10:23:57 +0000 (12:23 +0200)
committerLeandro T. C. Melo <leandro.melo@nokia.com>
Wed, 13 Jul 2011 11:11:45 +0000 (13:11 +0200)
This also changes the mutex so that the check for reusing
the last semantic info is consistent.

Task-number: QTCREATORBUG-5276

Change-Id: Ia5a2dae25ba3aa40949ac751bcda209d0bcf1f3c
Reviewed-on: http://codereview.qt.nokia.com/1581
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
src/plugins/cppeditor/cppeditor.cpp

index b1da326..bc07e4f 100644 (file)
@@ -655,7 +655,11 @@ void CPPEditorWidget::onDocumentUpdated(Document::Ptr doc)
     if (doc->editorRevision() != editorRevision())
         return;
 
-    if (! m_initialized) {
+    if (! m_initialized ||
+            (Core::EditorManager::instance()->currentEditor() == editor()
+             && (!m_lastSemanticInfo.doc
+                 || !m_lastSemanticInfo.doc->translationUnit()->ast()
+                 || m_lastSemanticInfo.doc->fileName() != file()->fileName()))) {
         m_initialized = true;
         rehighlight(/* force = */ true);
     }
@@ -2149,58 +2153,55 @@ void SemanticHighlighter::run()
 
 SemanticInfo SemanticHighlighter::semanticInfo(const Source &source)
 {
-    m_mutex.lock();
-    const int revision = m_lastSemanticInfo.revision;
-    m_mutex.unlock();
-
-    Snapshot snapshot;
-    Document::Ptr doc;
-    QList<Document::DiagnosticMessage> diagnosticMessages;
-    QList<SemanticInfo::Use> objcKeywords;
+    SemanticInfo semanticInfo;
+    semanticInfo.revision = source.revision;
+    semanticInfo.forced = source.force;
 
-    if (! source.force && revision == source.revision) {
-        m_mutex.lock();
-        snapshot = m_lastSemanticInfo.snapshot; // ### TODO: use the new snapshot.
-        doc = m_lastSemanticInfo.doc;
-        diagnosticMessages = m_lastSemanticInfo.diagnosticMessages;
-        objcKeywords = m_lastSemanticInfo.objcKeywords;
-        m_mutex.unlock();
+    m_mutex.lock();
+    if (! source.force
+            && m_lastSemanticInfo.revision == source.revision
+            && m_lastSemanticInfo.doc
+            && m_lastSemanticInfo.doc->translationUnit()->ast()
+            && m_lastSemanticInfo.doc->fileName() == source.fileName) {
+        semanticInfo.snapshot = m_lastSemanticInfo.snapshot; // ### TODO: use the new snapshot.
+        semanticInfo.doc = m_lastSemanticInfo.doc;
+        semanticInfo.diagnosticMessages = m_lastSemanticInfo.diagnosticMessages;
+        semanticInfo.objcKeywords = m_lastSemanticInfo.objcKeywords;
     }
+    m_mutex.unlock();
 
-    if (! doc) {
-        snapshot = source.snapshot;
-        const QByteArray preprocessedCode = snapshot.preprocessedCode(source.code, source.fileName);
-
-        doc = snapshot.documentFromSource(preprocessedCode, source.fileName);
-        doc->control()->setTopLevelDeclarationProcessor(this);
-        doc->check();
+    if (! semanticInfo.doc) {
+        semanticInfo.snapshot = source.snapshot;
+        if (source.snapshot.contains(source.fileName)) {
+            const QByteArray &preprocessedCode =
+                    source.snapshot.preprocessedCode(source.code, source.fileName);
+            Document::Ptr doc =
+                    source.snapshot.documentFromSource(preprocessedCode, source.fileName);
+            doc->control()->setTopLevelDeclarationProcessor(this);
+            doc->check();
+            semanticInfo.doc = doc;
 
 #if 0
-        if (TranslationUnit *unit = doc->translationUnit()) {
-            FindObjCKeywords findObjCKeywords(unit); // ### remove me
-            objcKeywords = findObjCKeywords();
-        }
+            if (TranslationUnit *unit = doc->translationUnit()) {
+                FindObjCKeywords findObjCKeywords(unit); // ### remove me
+                objcKeywords = findObjCKeywords();
+            }
 #endif
+        }
     }
 
-    TranslationUnit *translationUnit = doc->translationUnit();
-    AST *ast = translationUnit->ast();
-
-    FunctionDefinitionUnderCursor functionDefinitionUnderCursor(translationUnit);
-    DeclarationAST *currentFunctionDefinition = functionDefinitionUnderCursor(ast, source.line, source.column);
+    if (semanticInfo.doc) {
+        TranslationUnit *translationUnit = semanticInfo.doc->translationUnit();
+        AST * ast = translationUnit->ast();
 
-    const LocalSymbols useTable(doc, currentFunctionDefinition);
+        FunctionDefinitionUnderCursor functionDefinitionUnderCursor(semanticInfo.doc->translationUnit());
+        DeclarationAST *currentFunctionDefinition = functionDefinitionUnderCursor(ast, source.line, source.column);
 
-    SemanticInfo semanticInfo;
-    semanticInfo.revision = source.revision;
-    semanticInfo.snapshot = snapshot;
-    semanticInfo.doc = doc;
-    semanticInfo.localUses = useTable.uses;
-    semanticInfo.hasQ = useTable.hasQ;
-    semanticInfo.hasD = useTable.hasD;
-    semanticInfo.forced = source.force;
-    semanticInfo.diagnosticMessages = diagnosticMessages;
-    semanticInfo.objcKeywords = objcKeywords;
+        const LocalSymbols useTable(semanticInfo.doc, currentFunctionDefinition);
+        semanticInfo.localUses = useTable.uses;
+        semanticInfo.hasQ = useTable.hasQ;
+        semanticInfo.hasD = useTable.hasD;
+    }
 
     return semanticInfo;
 }