2013-12-10 14:37:32 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-12-10 14:37:32 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-12-10 14:37:32 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
#include "clangmodelmanagersupport.h"
|
|
|
|
|
|
|
|
|
|
#include "constants.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "clangeditordocumentprocessor.h"
|
2015-05-08 15:48:17 +02:00
|
|
|
#include "clangutils.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <cpptools/baseeditordocumentparser.h>
|
|
|
|
|
#include <cpptools/editordocumenthandle.h>
|
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
|
2015-06-16 11:56:00 +02:00
|
|
|
#include <clangbackendipc/cmbregisterprojectsforcodecompletioncommand.h>
|
|
|
|
|
#include <clangbackendipc/filecontainer.h>
|
|
|
|
|
#include <clangbackendipc/projectpartcontainer.h>
|
2015-05-08 15:48:17 +02:00
|
|
|
#include <utils/qtcassert.h>
|
2013-12-10 14:37:32 +01:00
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
|
|
|
|
using namespace ClangCodeModel;
|
|
|
|
|
using namespace ClangCodeModel::Internal;
|
|
|
|
|
|
2015-07-06 11:15:12 +02:00
|
|
|
static ModelManagerSupportClang *m_instance_forTestsOnly = 0;
|
2013-12-10 14:37:32 +01:00
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
static CppTools::CppModelManager *cppModelManager()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2015-05-08 15:48:17 +02:00
|
|
|
return CppTools::CppModelManager::instance();
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
ModelManagerSupportClang::ModelManagerSupportClang()
|
2015-06-16 13:59:08 +02:00
|
|
|
: m_completionAssistProvider(m_ipcCommunicator)
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2015-07-06 11:15:12 +02:00
|
|
|
QTC_CHECK(!m_instance_forTestsOnly);
|
|
|
|
|
m_instance_forTestsOnly = this;
|
2015-05-08 15:48:17 +02:00
|
|
|
|
|
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
connect(editorManager, &Core::EditorManager::currentEditorChanged,
|
|
|
|
|
this, &ModelManagerSupportClang::onCurrentEditorChanged);
|
|
|
|
|
connect(editorManager, &Core::EditorManager::editorOpened,
|
|
|
|
|
this, &ModelManagerSupportClang::onEditorOpened);
|
|
|
|
|
|
|
|
|
|
CppTools::CppModelManager *modelManager = cppModelManager();
|
2015-06-04 11:38:59 +02:00
|
|
|
connect(modelManager, &CppTools::CppModelManager::abstractEditorSupportContentsUpdated,
|
|
|
|
|
this, &ModelManagerSupportClang::onAbstractEditorSupportContentsUpdated);
|
|
|
|
|
connect(modelManager, &CppTools::CppModelManager::abstractEditorSupportRemoved,
|
|
|
|
|
this, &ModelManagerSupportClang::onAbstractEditorSupportRemoved);
|
2015-05-08 15:48:17 +02:00
|
|
|
connect(modelManager, &CppTools::CppModelManager::projectPartsUpdated,
|
|
|
|
|
this, &ModelManagerSupportClang::onProjectPartsUpdated);
|
|
|
|
|
connect(modelManager, &CppTools::CppModelManager::projectPartsRemoved,
|
|
|
|
|
this, &ModelManagerSupportClang::onProjectPartsRemoved);
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
ModelManagerSupportClang::~ModelManagerSupportClang()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2015-07-06 11:15:12 +02:00
|
|
|
m_instance_forTestsOnly = 0;
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
CppTools::CppCompletionAssistProvider *ModelManagerSupportClang::completionAssistProvider()
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2015-06-16 13:59:08 +02:00
|
|
|
return &m_completionAssistProvider;
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
|
|
2015-05-08 15:48:17 +02:00
|
|
|
CppTools::BaseEditorDocumentProcessor *ModelManagerSupportClang::editorDocumentProcessor(
|
2014-09-22 18:43:31 +02:00
|
|
|
TextEditor::TextDocument *baseTextDocument)
|
2013-12-10 14:37:32 +01:00
|
|
|
{
|
2015-05-08 15:48:17 +02:00
|
|
|
return new ClangEditorDocumentProcessor(this, baseTextDocument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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()) {
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.updateUnsavedFileFromCppEditorDocument(
|
2015-05-08 15:48:17 +02:00
|
|
|
m_previousCppEditor->document()->filePath().toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remember previous editor
|
|
|
|
|
if (newCurrent && cppModelManager()->isCppEditor(newCurrent))
|
|
|
|
|
m_previousCppEditor = newCurrent;
|
|
|
|
|
else
|
|
|
|
|
m_previousCppEditor.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onEditorOpened(Core::IEditor *editor)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(editor, return);
|
|
|
|
|
Core::IDocument *document = editor->document();
|
|
|
|
|
QTC_ASSERT(document, return);
|
|
|
|
|
TextEditor::TextDocument *textDocument = qobject_cast<TextEditor::TextDocument *>(document);
|
|
|
|
|
|
2015-07-08 15:08:13 +02:00
|
|
|
if (textDocument && cppModelManager()->isCppEditor(editor)) {
|
2015-05-08 15:48:17 +02:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
// TODO: Ensure that not fully loaded documents are updated?
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onCppDocumentReloadFinished(bool success)
|
|
|
|
|
{
|
|
|
|
|
if (!success)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.updateUnsavedFileIfNotCurrentDocument(document);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onCppDocumentContentsChanged()
|
|
|
|
|
{
|
|
|
|
|
Core::IDocument *document = qobject_cast<Core::IDocument *>(sender());
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.updateUnsavedFileIfNotCurrentDocument(document);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onAbstractEditorSupportContentsUpdated(const QString &filePath,
|
|
|
|
|
const QByteArray &content)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!filePath.isEmpty(), return);
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.updateUnsavedFile(filePath, content);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onAbstractEditorSupportRemoved(const QString &filePath)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!filePath.isEmpty(), return);
|
|
|
|
|
if (!cppModelManager()->cppEditorDocument(filePath)) {
|
|
|
|
|
const QString projectFilePath = Utils::projectFilePathForFile(filePath);
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.unregisterFilesForCodeCompletion(
|
2015-06-16 11:56:00 +02:00
|
|
|
{ClangBackEnd::FileContainer(filePath, projectFilePath)});
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onProjectPartsUpdated(ProjectExplorer::Project *project)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(project, return);
|
|
|
|
|
const CppTools::ProjectInfo projectInfo = cppModelManager()->projectInfo(project);
|
|
|
|
|
QTC_ASSERT(projectInfo.isValid(), return);
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.registerProjectsParts(projectInfo.projectParts());
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelManagerSupportClang::onProjectPartsRemoved(const QStringList &projectFiles)
|
|
|
|
|
{
|
2015-06-16 13:59:08 +02:00
|
|
|
m_ipcCommunicator.unregisterProjectPartsForCodeCompletion(projectFiles);
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
|
|
|
|
|
2015-07-06 11:15:12 +02:00
|
|
|
#ifdef QT_TESTLIB_LIB
|
|
|
|
|
ModelManagerSupportClang *ModelManagerSupportClang::instance_forTestsOnly()
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
2015-07-06 11:15:12 +02:00
|
|
|
return m_instance_forTestsOnly;
|
2015-05-08 15:48:17 +02:00
|
|
|
}
|
2015-07-06 11:15:12 +02:00
|
|
|
#endif
|
2015-05-08 15:48:17 +02:00
|
|
|
|
2015-06-16 13:59:08 +02:00
|
|
|
IpcCommunicator &ModelManagerSupportClang::ipcCommunicator()
|
2015-05-08 15:48:17 +02:00
|
|
|
{
|
|
|
|
|
return m_ipcCommunicator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ModelManagerSupportProviderClang::id() const
|
|
|
|
|
{
|
|
|
|
|
return QLatin1String(Constants::CLANG_MODELMANAGERSUPPORT_ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString ModelManagerSupportProviderClang::displayName() const
|
|
|
|
|
{
|
|
|
|
|
//: Display name
|
|
|
|
|
return QCoreApplication::translate("ClangCodeModel::Internal::ModelManagerSupport",
|
|
|
|
|
"Clang");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CppTools::ModelManagerSupport::Ptr ModelManagerSupportProviderClang::createModelManagerSupport()
|
|
|
|
|
{
|
|
|
|
|
return CppTools::ModelManagerSupport::Ptr(new ModelManagerSupportClang);
|
2013-12-10 14:37:32 +01:00
|
|
|
}
|