Clang: Add unsaved files messages

We have auto generated buffers from ui files which are not open but have no
file representation. So we need to provide them as unsaved files only.

Change-Id: I48a426c18e06eeda2fa707864f32f293e17ac651
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com>
This commit is contained in:
Marco Bubke
2015-08-31 16:10:36 +02:00
parent 3114780e55
commit 7eee8061f6
25 changed files with 662 additions and 69 deletions

View File

@@ -60,10 +60,12 @@
#include <clangbackendipc/cmbunregistertranslationunitsforeditormessage.h>
#include <clangbackendipc/cmbunregisterprojectsforeditormessage.h>
#include <clangbackendipc/cmbmessages.h>
#include <clangbackendipc/registerunsavedfilesforeditormessage.h>
#include <clangbackendipc/requestdiagnosticsmessage.h>
#include <clangbackendipc/filecontainer.h>
#include <clangbackendipc/projectpartsdonotexistmessage.h>
#include <clangbackendipc/translationunitdoesnotexistmessage.h>
#include <clangbackendipc/unregisterunsavedfilesforeditormessage.h>
#include <cplusplus/Icons.h>
@@ -195,6 +197,8 @@ public:
void unregisterTranslationUnitsForEditor(const ClangBackEnd::UnregisterTranslationUnitsForEditorMessage &message) override;
void registerProjectPartsForEditor(const ClangBackEnd::RegisterProjectPartsForEditorMessage &message) override;
void unregisterProjectPartsForEditor(const ClangBackEnd::UnregisterProjectPartsForEditorMessage &message) override;
void registerUnsavedFilesForEditor(const ClangBackEnd::RegisterUnsavedFilesForEditorMessage &message) override;
void unregisterUnsavedFilesForEditor(const ClangBackEnd::UnregisterUnsavedFilesForEditorMessage &message) override;
void completeCode(const ClangBackEnd::CompleteCodeMessage &message) override;
void requestDiagnostics(const ClangBackEnd::RequestDiagnosticsMessage &message) override;
@@ -232,6 +236,18 @@ void IpcSender::unregisterProjectPartsForEditor(const UnregisterProjectPartsForE
m_connection.serverProxy().unregisterProjectPartsForEditor(message);
}
void IpcSender::registerUnsavedFilesForEditor(const RegisterUnsavedFilesForEditorMessage &message)
{
QTC_CHECK(m_connection.isConnected());
m_connection.serverProxy().registerUnsavedFilesForEditor(message);
}
void IpcSender::unregisterUnsavedFilesForEditor(const UnregisterUnsavedFilesForEditorMessage &message)
{
QTC_CHECK(m_connection.isConnected());
m_connection.serverProxy().unregisterUnsavedFilesForEditor(message);
}
void IpcSender::completeCode(const CompleteCodeMessage &message)
{
QTC_CHECK(m_connection.isConnected());
@@ -306,7 +322,7 @@ void IpcCommunicator::registerCurrentUnsavedFiles()
const auto cppEditorDocuments = CppModelManager::instance()->cppEditorDocuments();
foreach (const CppEditorDocumentHandle *cppEditorDocument, cppEditorDocuments) {
if (cppEditorDocument->processor()->baseTextDocument()->isModified())
updateUnsavedFileFromCppEditorDocument(cppEditorDocument->filePath());
updateTranslationUnitFromCppEditorDocument(cppEditorDocument->filePath());
}
}
@@ -353,6 +369,13 @@ void IpcCommunicator::registerProjectsParts(const QList<CppTools::ProjectPart::P
registerProjectPartsForEditor(projectPartContainers);
}
void IpcCommunicator::updateTranslationUnitFromCppEditorDocument(const QString &filePath)
{
const auto document = CppTools::CppModelManager::instance()->cppEditorDocument(filePath);
updateTranslationUnit(filePath, document->contents(), document->revision());
}
void IpcCommunicator::updateUnsavedFileFromCppEditorDocument(const QString &filePath)
{
const auto document = CppTools::CppModelManager::instance()->cppEditorDocument(filePath);
@@ -360,7 +383,7 @@ void IpcCommunicator::updateUnsavedFileFromCppEditorDocument(const QString &file
updateUnsavedFile(filePath, document->contents(), document->revision());
}
void IpcCommunicator::updateUnsavedFile(const QString &filePath,
void IpcCommunicator::updateTranslationUnit(const QString &filePath,
const QByteArray &contents,
uint documentRevision)
{
@@ -368,25 +391,44 @@ void IpcCommunicator::updateUnsavedFile(const QString &filePath,
const bool hasUnsavedContent = true;
// TODO: Send new only if changed
registerFilesForEditor({{filePath,
projectPartId,
Utf8String::fromByteArray(contents),
hasUnsavedContent,
documentRevision}});
registerTranslationUnitsForEditor({{filePath,
projectPartId,
Utf8String::fromByteArray(contents),
hasUnsavedContent,
documentRevision}});
}
void IpcCommunicator::updateUnsavedFile(const QString &filePath, const QByteArray &contents, uint documentRevision)
{
const QString projectPartId = Utils::projectPartIdForFile(filePath);
const bool hasUnsavedContent = true;
// TODO: Send new only if changed
registerUnsavedFilesForEditor({{filePath,
projectPartId,
Utf8String::fromByteArray(contents),
hasUnsavedContent,
documentRevision}});
}
void IpcCommunicator::requestDiagnostics(const FileContainer &fileContainer)
{
registerFilesForEditor({fileContainer});
registerTranslationUnitsForEditor({fileContainer});
m_ipcSender->requestDiagnostics({fileContainer});
}
void IpcCommunicator::updateUnsavedFileIfNotCurrentDocument(Core::IDocument *document)
void IpcCommunicator::updateTranslationUnitIfNotCurrentDocument(Core::IDocument *document)
{
QTC_ASSERT(document, return);
if (Core::EditorManager::currentDocument() != document)
updateTranslationUnitFromCppEditorDocument(document->filePath().toString());
}
void IpcCommunicator::updateUnsavedFile(Core::IDocument *document)
{
QTC_ASSERT(document, return);
if (Core::EditorManager::currentDocument() != document)
updateUnsavedFileFromCppEditorDocument(document->filePath().toString());
updateUnsavedFileFromCppEditorDocument(document->filePath().toString());
}
void IpcCommunicator::onBackendRestarted()
@@ -431,7 +473,7 @@ void IpcCommunicator::killBackendProcess()
m_connection.processForTestOnly()->kill();
}
void IpcCommunicator::registerFilesForEditor(const FileContainers &fileContainers)
void IpcCommunicator::registerTranslationUnitsForEditor(const FileContainers &fileContainers)
{
if (m_sendMode == IgnoreSendRequests)
return;
@@ -441,7 +483,7 @@ void IpcCommunicator::registerFilesForEditor(const FileContainers &fileContainer
m_ipcSender->registerTranslationUnitsForEditor(message);
}
void IpcCommunicator::unregisterFilesForEditor(const FileContainers &fileContainers)
void IpcCommunicator::unregisterTranslationUnitsForEditor(const FileContainers &fileContainers)
{
if (m_sendMode == IgnoreSendRequests)
return;
@@ -472,6 +514,26 @@ void IpcCommunicator::unregisterProjectPartsForEditor(const QStringList &project
m_ipcSender->unregisterProjectPartsForEditor(message);
}
void IpcCommunicator::registerUnsavedFilesForEditor(const IpcCommunicator::FileContainers &fileContainers)
{
if (m_sendMode == IgnoreSendRequests)
return;
const RegisterUnsavedFilesForEditorMessage message(fileContainers);
qCDebug(log) << ">>>" << message;
m_ipcSender->registerUnsavedFilesForEditor(message);
}
void IpcCommunicator::unregisterUnsavedFilesForEditor(const IpcCommunicator::FileContainers &fileContainers)
{
if (m_sendMode == IgnoreSendRequests)
return;
const UnregisterUnsavedFilesForEditorMessage message(fileContainers);
qCDebug(log) << ">>>" << message;
m_ipcSender->unregisterUnsavedFilesForEditor(message);
}
void IpcCommunicator::completeCode(ClangCompletionAssistProcessor *assistProcessor,
const QString &filePath,
quint32 line,

View File

@@ -99,6 +99,8 @@ public:
virtual void unregisterTranslationUnitsForEditor(const ClangBackEnd::UnregisterTranslationUnitsForEditorMessage &message) = 0;
virtual void registerProjectPartsForEditor(const ClangBackEnd::RegisterProjectPartsForEditorMessage &message) = 0;
virtual void unregisterProjectPartsForEditor(const ClangBackEnd::UnregisterProjectPartsForEditorMessage &message) = 0;
virtual void registerUnsavedFilesForEditor(const ClangBackEnd::RegisterUnsavedFilesForEditorMessage &message) = 0;
virtual void unregisterUnsavedFilesForEditor(const ClangBackEnd::UnregisterUnsavedFilesForEditorMessage &message) = 0;
virtual void completeCode(const ClangBackEnd::CompleteCodeMessage &message) = 0;
virtual void requestDiagnostics(const ClangBackEnd::RequestDiagnosticsMessage &message) = 0;
};
@@ -115,10 +117,12 @@ public:
public:
IpcCommunicator();
void registerFilesForEditor(const FileContainers &fileContainers);
void unregisterFilesForEditor(const FileContainers &fileContainers);
void registerTranslationUnitsForEditor(const FileContainers &fileContainers);
void unregisterTranslationUnitsForEditor(const FileContainers &fileContainers);
void registerProjectPartsForEditor(const ProjectPartContainers &projectPartContainers);
void unregisterProjectPartsForEditor(const QStringList &projectPartIds);
void registerUnsavedFilesForEditor(const FileContainers &fileContainers);
void unregisterUnsavedFilesForEditor(const FileContainers &fileContainers);
void completeCode(ClangCompletionAssistProcessor *assistProcessor, const QString &filePath,
quint32 line,
quint32 column,
@@ -126,8 +130,11 @@ public:
void registerProjectsParts(const QList<CppTools::ProjectPart::Ptr> projectParts);
void updateUnsavedFileIfNotCurrentDocument(Core::IDocument *document);
void updateTranslationUnitIfNotCurrentDocument(Core::IDocument *document);
void updateUnsavedFile(Core::IDocument *document);
void updateTranslationUnitFromCppEditorDocument(const QString &filePath);
void updateUnsavedFileFromCppEditorDocument(const QString &filePath);
void updateTranslationUnit(const QString &filePath, const QByteArray &contents, uint documentRevision);
void updateUnsavedFile(const QString &filePath, const QByteArray &contents, uint documentRevision);
void requestDiagnostics(const ClangBackEnd::FileContainer &fileContainer);

View File

@@ -674,11 +674,11 @@ void ClangCompletionAssistProcessor::sendFileContent(const QString &projectPartI
const UnsavedFileContentInfo info = unsavedFileContent(customFileContent);
IpcCommunicator &ipcCommunicator = m_interface->ipcCommunicator();
ipcCommunicator.registerFilesForEditor({{m_interface->fileName(),
projectPartId,
Utf8String::fromByteArray(info.unsavedContent),
info.isDocumentModified,
uint(m_interface->textDocument()->revision())}});
ipcCommunicator.registerTranslationUnitsForEditor({{m_interface->fileName(),
projectPartId,
Utf8String::fromByteArray(info.unsavedContent),
info.isDocumentModified,
uint(m_interface->textDocument()->revision())}});
}
void ClangCompletionAssistProcessor::sendCompletionRequest(int position,

View File

@@ -109,7 +109,7 @@ ClangEditorDocumentProcessor::~ClangEditorDocumentProcessor()
if (m_projectPart) {
QTC_ASSERT(m_modelManagerSupport, return);
m_modelManagerSupport->ipcCommunicator().unregisterFilesForEditor(
m_modelManagerSupport->ipcCommunicator().unregisterTranslationUnitsForEditor(
{ClangBackEnd::FileContainer(filePath(), m_projectPart->id())});
}
}
@@ -228,18 +228,18 @@ void ClangEditorDocumentProcessor::updateTranslationUnitForEditor(CppTools::Proj
auto container1 = ClangBackEnd::FileContainer(filePath(),
m_projectPart->id(),
revision());
ipcCommunicator.unregisterFilesForEditor({container1});
ipcCommunicator.unregisterTranslationUnitsForEditor({container1});
auto container2 = ClangBackEnd::FileContainer(filePath(),
projectPart.id(),
revision());
ipcCommunicator.registerFilesForEditor({container2});
ipcCommunicator.registerTranslationUnitsForEditor({container2});
}
} else {
auto container = ClangBackEnd::FileContainer(filePath(),
projectPart.id(),
revision());
ipcCommunicator.registerFilesForEditor({container});
ipcCommunicator.registerTranslationUnitsForEditor({container});
}
}

View File

@@ -100,7 +100,7 @@ void ModelManagerSupportClang::onCurrentEditorChanged(Core::IEditor *newCurrent)
// If we switch away from a cpp editor, update the backend about
// the document's unsaved content.
if (m_previousCppEditor && m_previousCppEditor->document()->isModified()) {
m_ipcCommunicator.updateUnsavedFileFromCppEditorDocument(
m_ipcCommunicator.updateTranslationUnitFromCppEditorDocument(
m_previousCppEditor->document()->filePath().toString());
}
@@ -111,6 +111,32 @@ void ModelManagerSupportClang::onCurrentEditorChanged(Core::IEditor *newCurrent)
m_previousCppEditor.clear();
}
void ModelManagerSupportClang::connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument)
{
// Handle externally changed documents
connect(textDocument, &Core::IDocument::reloadFinished,
this, &ModelManagerSupportClang::onCppDocumentReloadFinishedOnTranslationUnit,
Qt::UniqueConnection);
// Handle changes from e.g. refactoring actions
connect(textDocument, &TextEditor::TextDocument::contentsChanged,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit,
Qt::UniqueConnection);
}
void ModelManagerSupportClang::connectTextDocumentToUnsavedFiles(TextEditor::TextDocument *textDocument)
{
// Handle externally changed documents
connect(textDocument, &Core::IDocument::reloadFinished,
this, &ModelManagerSupportClang::onCppDocumentReloadFinishedOnUnsavedFile,
Qt::UniqueConnection);
// Handle changes from e.g. refactoring actions
connect(textDocument, &TextEditor::TextDocument::contentsChanged,
this, &ModelManagerSupportClang::onCppDocumentContentsChangedOnUnsavedFile,
Qt::UniqueConnection);
}
void ModelManagerSupportClang::onEditorOpened(Core::IEditor *editor)
{
QTC_ASSERT(editor, return);
@@ -119,33 +145,41 @@ void ModelManagerSupportClang::onEditorOpened(Core::IEditor *editor)
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(document);
if (textDocument && cppModelManager()->isCppEditor(editor)) {
// Handle externally changed documents
connect(textDocument, &Core::IDocument::reloadFinished,
this, &ModelManagerSupportClang::onCppDocumentReloadFinished,
Qt::UniqueConnection);
// Handle changes from e.g. refactoring actions
connect(textDocument, &TextEditor::TextDocument::contentsChanged,
this, &ModelManagerSupportClang::onCppDocumentContentsChanged,
Qt::UniqueConnection);
if (cppModelManager()->isManagedByModelManagerSupport(textDocument, QLatin1String(Constants::CLANG_MODELMANAGERSUPPORT_ID)))
connectTextDocumentToTranslationUnit(textDocument);
else
connectTextDocumentToUnsavedFiles(textDocument);
// TODO: Ensure that not fully loaded documents are updated?
}
}
void ModelManagerSupportClang::onCppDocumentReloadFinished(bool success)
void ModelManagerSupportClang::onCppDocumentReloadFinishedOnTranslationUnit(bool success)
{
if (!success)
return;
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateUnsavedFileIfNotCurrentDocument(document);
if (success) {
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateTranslationUnitIfNotCurrentDocument(document);
}
}
void ModelManagerSupportClang::onCppDocumentContentsChanged()
void ModelManagerSupportClang::onCppDocumentContentsChangedOnTranslationUnit()
{
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateUnsavedFileIfNotCurrentDocument(document);
m_ipcCommunicator.updateTranslationUnitIfNotCurrentDocument(document);
}
void ModelManagerSupportClang::onCppDocumentReloadFinishedOnUnsavedFile(bool success)
{
if (success) {
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateUnsavedFile(document);
}
}
void ModelManagerSupportClang::onCppDocumentContentsChangedOnUnsavedFile()
{
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
m_ipcCommunicator.updateUnsavedFile(document);
}
void ModelManagerSupportClang::onAbstractEditorSupportContentsUpdated(const QString &filePath,
@@ -160,8 +194,7 @@ void ModelManagerSupportClang::onAbstractEditorSupportRemoved(const QString &fil
QTC_ASSERT(!filePath.isEmpty(), return);
if (!cppModelManager()->cppEditorDocument(filePath)) {
const QString projectPartId = Utils::projectPartIdForFile(filePath);
m_ipcCommunicator.unregisterFilesForEditor(
{ClangBackEnd::FileContainer(filePath, projectPartId)});
m_ipcCommunicator.unregisterUnsavedFilesForEditor({{filePath, projectPartId}});
}
}

View File

@@ -66,8 +66,10 @@ public:
private:
void onEditorOpened(Core::IEditor *editor);
void onCurrentEditorChanged(Core::IEditor *newCurrent);
void onCppDocumentReloadFinished(bool success);
void onCppDocumentContentsChanged();
void onCppDocumentReloadFinishedOnTranslationUnit(bool success);
void onCppDocumentContentsChangedOnTranslationUnit();
void onCppDocumentReloadFinishedOnUnsavedFile(bool success);
void onCppDocumentContentsChangedOnUnsavedFile();
void onAbstractEditorSupportContentsUpdated(const QString &filePath, const QByteArray &content);
void onAbstractEditorSupportRemoved(const QString &filePath);
@@ -75,6 +77,10 @@ private:
void onProjectPartsUpdated(ProjectExplorer::Project *project);
void onProjectPartsRemoved(const QStringList &projectPartIds);
void connectTextDocumentToTranslationUnit(TextEditor::TextDocument *textDocument);
void connectTextDocumentToUnsavedFiles(TextEditor::TextDocument *textDocument);
private:
IpcCommunicator m_ipcCommunicator;
ClangCompletionAssistProvider m_completionAssistProvider;
QPointer<Core::IEditor> m_previousCppEditor;

View File

@@ -60,6 +60,7 @@
#include <clangbackendipc/cmbregistertranslationunitsforeditormessage.h>
#include <clangbackendipc/cmbunregisterprojectsforeditormessage.h>
#include <clangbackendipc/cmbunregistertranslationunitsforeditormessage.h>
#include <clangbackendipc/registerunsavedfilesforeditormessage.h>
#include <utils/changeset.h>
#include <utils/qtcassert.h>
@@ -347,6 +348,23 @@ QString toString(const UnregisterProjectPartsForEditorMessage &message)
return out;
}
QString toString(const RegisterUnsavedFilesForEditorMessage &message)
{
QString out;
QTextStream ts(&out);
ts << "RegisterUnsavedFilesForEditorMessage\n"
<< toString(message.fileContainers());
return out;
return QLatin1String("RegisterUnsavedFilesForEditorMessage\n");
}
QString toString(const UnregisterUnsavedFilesForEditorMessage &)
{
return QLatin1String("UnregisterUnsavedFilesForEditorMessage\n");
}
QString toString(const CompleteCodeMessage &)
{
return QLatin1String("CompleteCodeMessage\n");
@@ -375,6 +393,12 @@ public:
void unregisterProjectPartsForEditor(const UnregisterProjectPartsForEditorMessage &message) override
{ senderLog.append(toString(message)); }
void registerUnsavedFilesForEditor(const RegisterUnsavedFilesForEditorMessage &message) override
{ senderLog.append(toString(message)); }
void unregisterUnsavedFilesForEditor(const UnregisterUnsavedFilesForEditorMessage &message) override
{ senderLog.append(toString(message)); }
void completeCode(const CompleteCodeMessage &message) override
{ senderLog.append(toString(message)); }