Clang: Prioritize current and visible translation units

We reparse first the current and then the visible translation units before
we reparse all other units.

The signals connections are queued to wait for the visible editor update.

Change-Id: I5e2b8bc80568450268ca24e26720b3f5af640995
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
Marco Bubke
2015-12-01 11:57:08 +01:00
parent db5bd69b26
commit d579608e8a
8 changed files with 121 additions and 4 deletions

View File

@@ -68,6 +68,7 @@
#include <clangbackendipc/translationunitdoesnotexistmessage.h>
#include <clangbackendipc/unregisterunsavedfilesforeditormessage.h>
#include <clangbackendipc/updatetranslationunitsforeditormessage.h>
#include <clangbackendipc/updatevisibletranslationunitsmessage.h>
#include <cplusplus/Icons.h>
@@ -223,6 +224,7 @@ public:
void completeCode(const ClangBackEnd::CompleteCodeMessage &message) override;
void requestDiagnostics(const ClangBackEnd::RequestDiagnosticsMessage &message) override;
void requestHighlighting(const ClangBackEnd::RequestHighlightingMessage &message) override;
void updateVisibleTranslationUnits(const UpdateVisibleTranslationUnitsMessage &message) override;
private:
ClangBackEnd::ConnectionClient &m_connection;
@@ -294,6 +296,12 @@ void IpcSender::requestHighlighting(const RequestHighlightingMessage &message)
m_connection.serverProxy().requestHighlighting(message);
}
void IpcSender::updateVisibleTranslationUnits(const UpdateVisibleTranslationUnitsMessage &message)
{
QTC_CHECK(m_connection.isConnected());
m_connection.serverProxy().updateVisibleTranslationUnits(message);
}
IpcCommunicator::IpcCommunicator()
: m_connection(&m_ipcReceiver)
, m_ipcSender(new IpcSender(m_connection))
@@ -370,6 +378,85 @@ void IpcCommunicator::registerFallbackProjectPart()
registerProjectPartsForEditor({projectPartContainer});
}
namespace {
Utf8String currentCppEditorDocumentFilePath()
{
Utf8String currentCppEditorDocumentFilePath;
const auto currentEditor = Core::EditorManager::currentEditor();
if (currentEditor && CppTools::CppModelManager::isCppEditor(currentEditor)) {
const auto currentDocument = currentEditor->document();
if (currentDocument)
currentCppEditorDocumentFilePath = currentDocument->filePath().toString();
}
return currentCppEditorDocumentFilePath;
}
void removeDuplicates(Utf8StringVector &visibleEditorDocumentsFilePaths)
{
std::sort(visibleEditorDocumentsFilePaths.begin(),
visibleEditorDocumentsFilePaths.end());
const auto end = std::unique(visibleEditorDocumentsFilePaths.begin(),
visibleEditorDocumentsFilePaths.end());
visibleEditorDocumentsFilePaths.erase(end,
visibleEditorDocumentsFilePaths.end());
}
void removeNonCppEditors(QList<Core::IEditor*> &visibleEditors)
{
const auto isNotCppEditor = [] (Core::IEditor *editor) {
return !CppTools::CppModelManager::isCppEditor(editor);
};
const auto end = std::remove_if(visibleEditors.begin(),
visibleEditors.end(),
isNotCppEditor);
visibleEditors.erase(end, visibleEditors.end());
}
Utf8StringVector visibleCppEditorDocumentsFilePaths()
{
auto visibleEditors = Core::EditorManager::visibleEditors();
removeNonCppEditors(visibleEditors);
Utf8StringVector visibleCppEditorDocumentsFilePaths;
visibleCppEditorDocumentsFilePaths.reserve(visibleEditors.size());
const auto editorFilePaths = [] (Core::IEditor *editor) {
return Utf8String(editor->document()->filePath().toString());
};
std::transform(visibleEditors.begin(),
visibleEditors.end(),
std::back_inserter(visibleCppEditorDocumentsFilePaths),
editorFilePaths);
removeDuplicates(visibleCppEditorDocumentsFilePaths);
return visibleCppEditorDocumentsFilePaths;
}
}
void IpcCommunicator::updateTranslationUnitVisiblity()
{
updateTranslationUnitVisiblity(currentCppEditorDocumentFilePath(), visibleCppEditorDocumentsFilePaths());
}
void IpcCommunicator::updateTranslationUnitVisiblity(const Utf8String &currentEditorFilePath,
const Utf8StringVector &visibleEditorsFilePaths)
{
if (m_sendMode == IgnoreSendRequests)
return;
const UpdateVisibleTranslationUnitsMessage message(currentEditorFilePath, visibleEditorsFilePaths);
qCDebug(log) << ">>>" << message;
m_ipcSender->updateVisibleTranslationUnits(message);
}
void IpcCommunicator::registerCurrentProjectParts()
{
using namespace CppTools;
@@ -561,6 +648,7 @@ void IpcCommunicator::initializeBackendWithCurrentData()
registerCurrentProjectParts();
registerCurrentCppEditorDocuments();
registerCurrentCodeModelUiHeaders();
updateTranslationUnitVisiblity();
emit backendReinitialized();
}

View File

@@ -107,6 +107,7 @@ public:
virtual void completeCode(const ClangBackEnd::CompleteCodeMessage &message) = 0;
virtual void requestDiagnostics(const ClangBackEnd::RequestDiagnosticsMessage &message) = 0;
virtual void requestHighlighting(const ClangBackEnd::RequestHighlightingMessage &message) = 0;
virtual void updateVisibleTranslationUnits(const ClangBackEnd::UpdateVisibleTranslationUnitsMessage &message) = 0;
};
class IpcCommunicator : public QObject
@@ -149,6 +150,7 @@ public:
void updateChangeContentStartPosition(const QString &filePath, int position);
void registerFallbackProjectPart();
void updateTranslationUnitVisiblity();
public: // for tests
IpcSenderInterface *setIpcSender(IpcSenderInterface *ipcSender);
@@ -171,6 +173,9 @@ private:
void onEditorAboutToClose(Core::IEditor *editor);
void onCoreAboutToClose();
void updateTranslationUnitVisiblity(const Utf8String &currentEditorFilePath,
const Utf8StringVector &visibleEditorsFilePaths);
private:
IpcReceiver m_ipcReceiver;
ClangBackEnd::ConnectionClient m_connection;

View File

@@ -268,10 +268,12 @@ void ClangEditorDocumentProcessor::registerTranslationUnitForEditor(CppTools::Pr
if (projectPart->id() != m_projectPart->id()) {
ipcCommunicator.unregisterTranslationUnitsForEditor({fileContainerWithArguments()});
ipcCommunicator.registerTranslationUnitsForEditor({fileContainerWithArguments(projectPart)});
ipcCommunicator.updateTranslationUnitVisiblity();
requestDocumentAnnotations(projectPart->id());
}
} else {
ipcCommunicator.registerTranslationUnitsForEditor({{fileContainerWithArguments(projectPart)}});
ipcCommunicator.updateTranslationUnitVisiblity();
requestDocumentAnnotations(projectPart->id());
}
}

View File

@@ -66,9 +66,14 @@ ModelManagerSupportClang::ModelManagerSupportClang()
Core::EditorManager *editorManager = Core::EditorManager::instance();
connect(editorManager, &Core::EditorManager::currentEditorChanged,
this, &ModelManagerSupportClang::onCurrentEditorChanged);
this, &ModelManagerSupportClang::onCurrentEditorChanged,
Qt::QueuedConnection);
connect(editorManager, &Core::EditorManager::editorOpened,
this, &ModelManagerSupportClang::onEditorOpened);
this, &ModelManagerSupportClang::onEditorOpened,
Qt::QueuedConnection);
connect(editorManager, &Core::EditorManager::editorsClosed,
this, &ModelManagerSupportClang::onEditorClosed,
Qt::QueuedConnection);
CppTools::CppModelManager *modelManager = cppModelManager();
connect(modelManager, &CppTools::CppModelManager::abstractEditorSupportContentsUpdated,
@@ -111,6 +116,8 @@ void ModelManagerSupportClang::onCurrentEditorChanged(Core::IEditor *newCurrent)
m_previousCppEditor = newCurrent;
else
m_previousCppEditor.clear();
m_ipcCommunicator.updateTranslationUnitVisiblity();
}
void ModelManagerSupportClang::connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument)
@@ -181,6 +188,11 @@ void ModelManagerSupportClang::onEditorOpened(Core::IEditor *editor)
}
}
void ModelManagerSupportClang::onEditorClosed(const QList<Core::IEditor *> &)
{
m_ipcCommunicator.updateTranslationUnitVisiblity();
}
void ModelManagerSupportClang::onCppDocumentAboutToReloadOnTranslationUnit()
{
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(sender());

View File

@@ -71,6 +71,7 @@ public:
private:
void onEditorOpened(Core::IEditor *editor);
void onEditorClosed(const QList<Core::IEditor *> &editors);
void onCurrentEditorChanged(Core::IEditor *newCurrent);
void onCppDocumentAboutToReloadOnTranslationUnit();
void onCppDocumentReloadFinishedOnTranslationUnit(bool success);

View File

@@ -385,6 +385,11 @@ QString toString(const RequestHighlightingMessage &)
return QStringLiteral("RequestHighlightingMessage\n");
}
QString toString(const UpdateVisibleTranslationUnitsMessage &)
{
return QStringLiteral("UpdateVisibleTranslationUnitsMessage\n");
}
class IpcSenderSpy : public IpcSenderInterface
{
public:
@@ -421,6 +426,9 @@ public:
void requestHighlighting(const RequestHighlightingMessage &message) override
{ senderLog.append(toString(message)); }
void updateVisibleTranslationUnits(const UpdateVisibleTranslationUnitsMessage &message) override
{ senderLog.append(toString(message)); }
public:
QString senderLog;
};
@@ -1074,6 +1082,7 @@ void ClangCodeCompletionTest::testCompleteAfterModifyingIncludedHeaderInOtherEdi
// Switch back to source file and check if modified header is reflected in completions.
Core::EditorManager::activateEditor(openSource.editor());
QCoreApplication::processEvents(); // connections are queued
proposal = completionResults(openSource.editor());
QVERIFY(hasItem(proposal, "globalFromHeader"));
QVERIFY(hasItem(proposal, "globalFromHeaderUnsaved"));

View File

@@ -936,7 +936,7 @@ ProjectPart::Ptr CppModelManager::fallbackProjectPart()
return part;
}
bool CppModelManager::isCppEditor(Core::IEditor *editor) const
bool CppModelManager::isCppEditor(Core::IEditor *editor)
{
return editor->context().contains(ProjectExplorer::Constants::LANG_CXX);
}

View File

@@ -120,7 +120,7 @@ public:
const QByteArray &contents);
void emitAbstractEditorSupportRemoved(const QString &filePath);
bool isCppEditor(Core::IEditor *editor) const;
static bool isCppEditor(Core::IEditor *editor);
bool isClangCodeModelAvailable() const;
bool isClangCodeModelActive() const;