CppEditor: Convert AbstractEditorSupport interface to FilePath

Change-Id: I47439e154bc28d40e112b7eef46fa1f57a8b3fce
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-11-23 18:05:46 +01:00
parent f68db427ef
commit 196e73fa16
12 changed files with 95 additions and 74 deletions

View File

@@ -11,6 +11,8 @@
#include <utils/macroexpander.h>
#include <utils/templateengine.h>
using namespace Utils;
namespace CppEditor {
AbstractEditorSupport::AbstractEditorSupport(CppModelManager *modelmanager, QObject *parent) :
@@ -27,22 +29,23 @@ AbstractEditorSupport::~AbstractEditorSupport()
void AbstractEditorSupport::updateDocument()
{
++m_revision;
m_modelmanager->updateSourceFiles(QSet<QString>{fileName()});
m_modelmanager->updateSourceFiles(QSet<QString>{filePath().toString()});
}
void AbstractEditorSupport::notifyAboutUpdatedContents() const
{
m_modelmanager->emitAbstractEditorSupportContentsUpdated(fileName(), sourceFileName(), contents());
m_modelmanager->emitAbstractEditorSupportContentsUpdated(
filePath().toString(), sourceFilePath().toString(), contents());
}
QString AbstractEditorSupport::licenseTemplate(const QString &file, const QString &className)
QString AbstractEditorSupport::licenseTemplate(const FilePath &filePath, const QString &className)
{
const QString license = Internal::CppFileSettings::licenseTemplate();
Utils::MacroExpander expander;
expander.registerVariable("Cpp:License:FileName", tr("The file name."),
[file]() { return Utils::FilePath::fromString(file).fileName(); });
[filePath] { return filePath.fileName(); });
expander.registerVariable("Cpp:License:ClassName", tr("The class name."),
[className]() { return className; });
[className] { return className; });
return Utils::TemplateEngine::processText(&expander, license, nullptr);
}
@@ -52,5 +55,4 @@ bool AbstractEditorSupport::usePragmaOnce()
return Internal::CppEditorPlugin::usePragmaOnce();
}
} // namespace CppEditor
} // CppEditor

View File

@@ -5,29 +5,32 @@
#include "cppeditor_global.h"
#include <QString>
#include <utils/filepath.h>
#include <QObject>
namespace CppEditor {
class CppModelManager;
class CPPEDITOR_EXPORT AbstractEditorSupport : public QObject
{
Q_OBJECT
public:
explicit AbstractEditorSupport(CppModelManager *modelmanager, QObject *parent = nullptr);
~AbstractEditorSupport() override;
/// \returns the contents, encoded as UTF-8
virtual QByteArray contents() const = 0;
virtual QString fileName() const = 0;
virtual QString sourceFileName() const = 0;
virtual Utils::FilePath filePath() const = 0;
virtual Utils::FilePath sourceFilePath() const = 0;
void updateDocument();
void notifyAboutUpdatedContents() const;
unsigned revision() const { return m_revision; }
static QString licenseTemplate(const QString &file = QString(), const QString &className = QString());
static QString licenseTemplate(const Utils::FilePath &filePath = {}, const QString &className = {});
static bool usePragmaOnce();
private:
@@ -35,4 +38,4 @@ private:
unsigned m_revision;
};
} // namespace CppEditor
} // CppEditor

View File

@@ -63,7 +63,7 @@ public:
mm()->unregisterCppEditorDocument(m_registrationFilePath);
}
QString filePath() const override { return m_cppEditorDocument->filePath().toString(); }
FilePath filePath() const override { return m_cppEditorDocument->filePath(); }
QByteArray contents() const override { return m_cppEditorDocument->contentsText(); }
unsigned revision() const override { return m_cppEditorDocument->contentsRevision(); }

View File

@@ -1128,12 +1128,12 @@ BaseEditorDocumentProcessor *CppModelManager::cppEditorDocumentProcessor(const Q
void CppModelManager::registerCppEditorDocument(CppEditorDocumentHandle *editorDocument)
{
QTC_ASSERT(editorDocument, return);
const QString filePath = editorDocument->filePath();
const FilePath filePath = editorDocument->filePath();
QTC_ASSERT(!filePath.isEmpty(), return);
QMutexLocker locker(&d->m_cppEditorDocumentsMutex);
QTC_ASSERT(d->m_cppEditorDocuments.value(filePath, 0) == 0, return);
d->m_cppEditorDocuments.insert(filePath, editorDocument);
QTC_ASSERT(d->m_cppEditorDocuments.value(filePath.toString(), 0) == 0, return);
d->m_cppEditorDocuments.insert(filePath.toString(), editorDocument);
}
void CppModelManager::unregisterCppEditorDocument(const QString &filePath)
@@ -1204,7 +1204,7 @@ WorkingCopy CppModelManager::buildWorkingCopyList()
}
for (AbstractEditorSupport *es : std::as_const(d->m_extraEditorSupports))
workingCopy.insert(es->fileName(), es->contents(), es->revision());
workingCopy.insert(es->filePath(), es->contents(), es->revision());
// Add the project configuration file
QByteArray conf = codeModelConfiguration();
@@ -1922,21 +1922,22 @@ void CppModelManager::GC()
return;
// Collect files of opened editors and editor supports (e.g. ui code model)
QStringList filesInEditorSupports;
FilePaths filesInEditorSupports;
const QList<CppEditorDocumentHandle *> editorDocuments = cppEditorDocuments();
for (const CppEditorDocumentHandle *editorDocument : editorDocuments)
filesInEditorSupports << editorDocument->filePath();
const QSet<AbstractEditorSupport *> abstractEditorSupportList = abstractEditorSupports();
for (AbstractEditorSupport *abstractEditorSupport : abstractEditorSupportList)
filesInEditorSupports << abstractEditorSupport->fileName();
filesInEditorSupports << abstractEditorSupport->filePath();
Snapshot currentSnapshot = snapshot();
QSet<Utils::FilePath> reachableFiles;
// The configuration file is part of the project files, which is just fine.
// If single files are open, without any project, then there is no need to
// keep the configuration file around.
FilePaths todo = transform(filesInEditorSupports + projectFiles(), &FilePath::fromString);
FilePaths todo = filesInEditorSupports;
todo += transform(projectFiles(), &FilePath::fromString);
// Collect all files that are reachable from the project files
while (!todo.isEmpty()) {

View File

@@ -6,9 +6,10 @@
#include "cppeditor_global.h"
#include "senddocumenttracker.h"
#include <QString>
namespace Utils { class FilePath; }
namespace CppEditor {
class BaseEditorDocumentProcessor;
class CPPEDITOR_EXPORT CppEditorDocumentHandle
@@ -25,7 +26,7 @@ public:
void setRefreshReason(const RefreshReason &refreshReason);
// For the Working Copy
virtual QString filePath() const = 0;
virtual Utils::FilePath filePath() const = 0;
virtual QByteArray contents() const = 0;
virtual unsigned revision() const = 0;
@@ -41,4 +42,4 @@ private:
RefreshReason m_refreshReason = None;
};
} // namespace CppEditor
} // CppEditor

View File

@@ -7,12 +7,15 @@
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/buildconfiguration.h>
#include <projectexplorer/buildmanager.h>
#include <projectexplorer/extracompiler.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <projectexplorer/target.h>
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
@@ -22,6 +25,7 @@
using namespace ProjectExplorer;
using namespace CPlusPlus;
using namespace Utils;
namespace CppEditor {
@@ -46,16 +50,16 @@ private:
};
GeneratedCodeModelSupport::GeneratedCodeModelSupport(CppModelManager *modelmanager,
ProjectExplorer::ExtraCompiler *generator,
const Utils::FilePath &generatedFile) :
AbstractEditorSupport(modelmanager, generator), m_generatedFileName(generatedFile),
ExtraCompiler *generator,
const FilePath &generatedFile) :
AbstractEditorSupport(modelmanager, generator), m_generatedFilePath(generatedFile),
m_generator(generator)
{
QLoggingCategory log("qtc.cppeditor.generatedcodemodelsupport", QtWarningMsg);
qCDebug(log) << "ctor GeneratedCodeModelSupport for" << m_generator->source()
<< generatedFile;
connect(m_generator, &ProjectExplorer::ExtraCompiler::contentsChanged,
connect(m_generator, &ExtraCompiler::contentsChanged,
this, &GeneratedCodeModelSupport::onContentsChanged, Qt::QueuedConnection);
onContentsChanged(generatedFile);
}
@@ -63,14 +67,14 @@ GeneratedCodeModelSupport::GeneratedCodeModelSupport(CppModelManager *modelmanag
GeneratedCodeModelSupport::~GeneratedCodeModelSupport()
{
CppModelManager::instance()->emitAbstractEditorSupportRemoved(
m_generatedFileName.toString());
m_generatedFilePath.toString());
QLoggingCategory log("qtc.cppeditor.generatedcodemodelsupport", QtWarningMsg);
qCDebug(log) << "dtor ~generatedcodemodelsupport for" << m_generatedFileName;
qCDebug(log) << "dtor ~generatedcodemodelsupport for" << m_generatedFilePath;
}
void GeneratedCodeModelSupport::onContentsChanged(const Utils::FilePath &file)
void GeneratedCodeModelSupport::onContentsChanged(const FilePath &file)
{
if (file == m_generatedFileName) {
if (file == m_generatedFilePath) {
notifyAboutUpdatedContents();
updateDocument();
}
@@ -78,20 +82,20 @@ void GeneratedCodeModelSupport::onContentsChanged(const Utils::FilePath &file)
QByteArray GeneratedCodeModelSupport::contents() const
{
return m_generator->content(m_generatedFileName);
return m_generator->content(m_generatedFilePath);
}
QString GeneratedCodeModelSupport::fileName() const
FilePath GeneratedCodeModelSupport::filePath() const
{
return m_generatedFileName.toString();
return m_generatedFilePath;
}
QString GeneratedCodeModelSupport::sourceFileName() const
FilePath GeneratedCodeModelSupport::sourceFilePath() const
{
return m_generator->source().toString();
return m_generator->source();
}
void GeneratedCodeModelSupport::update(const QList<ProjectExplorer::ExtraCompiler *> &generators)
void GeneratedCodeModelSupport::update(const QList<ExtraCompiler *> &generators)
{
static QObjectCache extraCompilerCache;
@@ -102,10 +106,10 @@ void GeneratedCodeModelSupport::update(const QList<ProjectExplorer::ExtraCompile
continue;
extraCompilerCache.insert(generator);
generator->forEachTarget([mm, generator](const Utils::FilePath &generatedFile) {
generator->forEachTarget([mm, generator](const FilePath &generatedFile) {
new GeneratedCodeModelSupport(mm, generator, generatedFile);
});
}
}
} // namespace CppEditor
} // CppEditor

View File

@@ -6,15 +6,7 @@
#include "cppeditor_global.h"
#include "abstracteditorsupport.h"
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/extracompiler.h>
#include <QDateTime>
#include <QHash>
#include <QSet>
namespace Core { class IEditor; }
namespace ProjectExplorer { class Project; }
namespace ProjectExplorer { class ExtraCompiler; }
namespace CppEditor {
@@ -30,14 +22,14 @@ public:
/// \returns the contents encoded in UTF-8.
QByteArray contents() const override;
QString fileName() const override; // The generated file
QString sourceFileName() const override;
Utils::FilePath filePath() const override; // The generated file
Utils::FilePath sourceFilePath() const override;
static void update(const QList<ProjectExplorer::ExtraCompiler *> &generators);
private:
void onContentsChanged(const Utils::FilePath &file);
Utils::FilePath m_generatedFileName;
Utils::FilePath m_generatedFilePath;
ProjectExplorer::ExtraCompiler *m_generator;
};

View File

@@ -17,16 +17,17 @@
#include <QFileInfo>
#include <QDebug>
using namespace Utils;
static const char uiMemberC[] = "ui";
static const char uiNamespaceC[] = "Ui";
namespace Designer {
namespace Internal {
// Generation code
// Write out how to access the Ui class in the source code.
static inline void writeUiMemberAccess(const QtSupport::CodeGenSettings &fp, QTextStream &str)
static void writeUiMemberAccess(const QtSupport::CodeGenSettings &fp, QTextStream &str)
{
switch (fp.embedding) {
case QtSupport::CodeGenSettings::PointerAggregatedUiClass:
@@ -40,8 +41,6 @@ static inline void writeUiMemberAccess(const QtSupport::CodeGenSettings &fp, QTe
}
}
} // namespace Internal
bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParameters &parameters,
QString *header, QString *source, int indentation)
{
@@ -72,9 +71,11 @@ bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParamete
const QString unqualifiedClassName = namespaceList.takeLast();
const QString headerLicense =
CppEditor::AbstractEditorSupport::licenseTemplate(parameters.headerFile, parameters.className);
CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(parameters.headerFile), parameters.className);
const QString sourceLicense =
CppEditor::AbstractEditorSupport::licenseTemplate(parameters.sourceFile, parameters.className);
CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(parameters.sourceFile), parameters.className);
// Include guards
const QString guard = Utils::headerGuard(parameters.headerFile, namespaceList);
@@ -165,7 +166,7 @@ bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParamete
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass)
sourceStr << ",\n" << namespaceIndent << indent << uiMemberC << "(new " << uiClassName << ")";
sourceStr << '\n' << namespaceIndent << "{\n" << namespaceIndent << indent;
Internal::writeUiMemberAccess(generationParameters, sourceStr);
writeUiMemberAccess(generationParameters, sourceStr);
sourceStr << "setupUi(this);\n" << namespaceIndent << "}\n";
// Deleting destructor for ptr
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass) {
@@ -181,7 +182,7 @@ bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParamete
<< namespaceIndent << indent << formBaseClass << "::changeEvent(e);\n"
<< namespaceIndent << indent << "switch (e->type()) {\n" << namespaceIndent << indent << "case QEvent::LanguageChange:\n"
<< namespaceIndent << indent << indent;
Internal::writeUiMemberAccess(generationParameters, sourceStr);
writeUiMemberAccess(generationParameters, sourceStr);
sourceStr << "retranslateUi(this);\n"
<< namespaceIndent << indent << indent << "break;\n"
<< namespaceIndent << indent << "default:\n"

View File

@@ -18,14 +18,15 @@
#include <QRegularExpression>
#include <QSet>
using namespace Utils;
namespace QmakeProjectManager::Internal {
static QString headerGuard(const QString &header)
{
return header.toUpper().replace(QRegularExpression("[^A-Z0-9]+"), QString("_"));
}
namespace QmakeProjectManager {
namespace Internal {
struct ProjectContents {
QString tmpl;
QString library;
@@ -100,7 +101,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (pluginHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile pluginHeader(baseDir + wo.pluginHeaderFile);
pluginHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(wo.pluginHeaderFile, wo.pluginClassName)
pluginHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(wo.pluginHeaderFile), wo.pluginClassName)
+ pluginHeaderContents);
rc.push_back(pluginHeader);
@@ -128,7 +130,8 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (pluginSourceContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile pluginSource(baseDir + wo.pluginSourceFile);
pluginSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(wo.pluginSourceFile, wo.pluginClassName)
pluginSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(wo.pluginSourceFile), wo.pluginClassName)
+ pluginSourceContents);
if (i == 0 && widgetCount == 1) // Open first widget unless collection
pluginSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
@@ -174,7 +177,9 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (widgetHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile widgetHeader(baseDir + wo.widgetHeaderFile);
widgetHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(wo.widgetHeaderFile, wo.widgetClassName)
widgetHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(wo.widgetHeaderFile),
wo.widgetClassName)
+ widgetHeaderContents);
rc.push_back(widgetHeader);
@@ -184,7 +189,9 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (widgetSourceContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile widgetSource(baseDir + wo.widgetSourceFile);
widgetSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(wo.widgetSourceFile, wo.widgetClassName)
widgetSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(wo.widgetSourceFile),
wo.widgetClassName)
+ widgetSourceContents);
rc.push_back(widgetSource);
}
@@ -219,7 +226,9 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (collectionHeaderContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile collectionHeader(baseDir + options.collectionHeaderFile);
collectionHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(options.collectionHeaderFile, options.collectionClassName)
collectionHeader.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(options.collectionHeaderFile),
options.collectionClassName)
+ collectionHeaderContents);
rc.push_back(collectionHeader);
@@ -235,7 +244,9 @@ QList<Core::GeneratedFile> PluginGenerator::generatePlugin(const GenerationPara
if (collectionSourceFileContents.isEmpty())
return QList<Core::GeneratedFile>();
Core::GeneratedFile collectionSource(baseDir + options.collectionSourceFile);
collectionSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(options.collectionSourceFile, options.collectionClassName)
collectionSource.setContents(CppEditor::AbstractEditorSupport::licenseTemplate(
FilePath::fromString(options.collectionSourceFile),
options.collectionClassName)
+ collectionSourceFileContents);
collectionSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
rc.push_back(collectionSource);
@@ -336,5 +347,4 @@ QString PluginGenerator::cStringQuote(QString s)
return s;
}
}
}
} // QmakeProjectManager::Internal

View File

@@ -6,10 +6,13 @@
#include "qmakeproject.h"
#include "qmakeprojectmanagertr.h"
#include <projectexplorer/extracompiler.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
#include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h>
#include <resourceeditor/resourcenode.h>
#include <utils/algorithm.h>

View File

@@ -17,6 +17,7 @@
#include <cppeditor/cppeditorconstants.h>
#include <projectexplorer/editorconfiguration.h>
#include <projectexplorer/extracompiler.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/target.h>
@@ -31,7 +32,6 @@
#include <utils/QtConcurrentTools>
#include <utils/algorithm.h>
#include <utils/filesystemwatcher.h>
#include <utils/fileutils.h>
#include <utils/mimeutils.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>

View File

@@ -9,6 +9,7 @@
#include <coreplugin/idocument.h>
#include <cppeditor/generatedcodemodelsupport.h>
#include <projectexplorer/projectnodes.h>
#include <utils/textfileformat.h>
#include <QFutureWatcher>
@@ -20,12 +21,15 @@
#include <memory>
namespace ProjectExplorer { class BuildConfiguration; }
namespace ProjectExplorer {
class BuildConfiguration;
class ExtraCompilerFactory;
} // ProjectExplorer
namespace Utils {
class FilePath;
class FileSystemWatcher;
} // namespace Utils;
} // Utils;
namespace QtSupport { class ProFileReader; }