CppEditor: Remove WorkingCopy::contains()

Most uses involved a double look-up.

Change-Id: Ifeb62ea2361222ed0faad749f44a59735c8d6930
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2023-05-02 17:55:44 +02:00
parent e15b384944
commit a03bea81c7
14 changed files with 56 additions and 44 deletions

View File

@@ -43,8 +43,8 @@ bool CppParser::selectedForBuilding(const FilePath &fileName)
QByteArray CppParser::getFileContent(const FilePath &filePath) const
{
QByteArray fileContent;
if (m_workingCopy.contains(filePath)) {
fileContent = m_workingCopy.source(filePath);
if (const auto source = m_workingCopy.source(filePath)) {
fileContent = *source;
} else {
QString error;
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();

View File

@@ -144,8 +144,8 @@ void BuiltinEditorDocumentParser::updateImpl(const QPromise<void> &promise,
QSet<Utils::FilePath> toRemove;
for (const Document::Ptr &doc : std::as_const(state.snapshot)) {
const Utils::FilePath filePath = doc->filePath();
if (workingCopy.contains(filePath)) {
if (workingCopy.get(filePath).second != doc->editorRevision())
if (const auto entry = workingCopy.get(filePath)) {
if (entry->second != doc->editorRevision())
addFileAndDependencies(&state.snapshot, &toRemove, filePath);
continue;
}

View File

@@ -302,12 +302,13 @@ void BuiltinEditorDocumentProcessor::onCodeWarningsUpdated(
SemanticInfo::Source BuiltinEditorDocumentProcessor::createSemanticInfoSource(bool force) const
{
const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy();
return SemanticInfo::Source(filePath().toString(),
workingCopy.source(filePath()),
workingCopy.revision(filePath()),
m_documentSnapshot,
force);
QByteArray source;
int revision = 0;
if (const auto entry = CppModelManager::instance()->workingCopy().get(filePath())) {
source = entry->first;
revision = entry->second;
}
return SemanticInfo::Source(filePath().toString(), source, revision, m_documentSnapshot, force);
}
} // namespace CppEditor

View File

@@ -105,8 +105,8 @@ namespace Internal {
static QByteArray getSource(const Utils::FilePath &fileName,
const WorkingCopy &workingCopy)
{
if (workingCopy.contains(fileName)) {
return workingCopy.source(fileName);
if (const auto source = workingCopy.source(fileName)) {
return *source;
} else {
QString fileContents;
Utils::TextFileFormat format;

View File

@@ -649,7 +649,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed()
QVERIFY(editor);
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
QVERIFY(mm->isCppEditor(editor));
QVERIFY(mm->workingCopy().contains(file));
QVERIFY(mm->workingCopy().get(file));
// Wait until the file is refreshed
helper.waitForRefreshedSourceFiles();
@@ -659,7 +659,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed()
helper.waitForFinishedGc();
// Check: File is removed from the snapshpt
QVERIFY(!mm->workingCopy().contains(file));
QVERIFY(!mm->workingCopy().get(file));
QVERIFY(!mm->snapshot().contains(file));
}
@@ -684,13 +684,13 @@ void ModelManagerTest::testDontGcOpenedFiles()
// Wait until the file is refreshed and check whether it is in the working copy
helper.waitForRefreshedSourceFiles();
QVERIFY(mm->workingCopy().contains(file));
QVERIFY(mm->workingCopy().get(file));
// Run the garbage collector
mm->GC();
// Check: File is still there
QVERIFY(mm->workingCopy().contains(file));
QVERIFY(mm->workingCopy().get(file));
QVERIFY(mm->snapshot().contains(file));
// Close editor
@@ -1090,7 +1090,7 @@ void ModelManagerTest::testRenameIncludesInEditor()
});
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
QVERIFY(modelManager->isCppEditor(editor));
QVERIFY(modelManager->workingCopy().contains(mainFile));
QVERIFY(modelManager->workingCopy().get(mainFile));
// Test the renaming of a header file where a pragma once guard is present
QVERIFY(Core::FileUtils::renameFile(headerWithPragmaOnce,

View File

@@ -57,8 +57,8 @@ CppRefactoringFilePtr CppRefactoringChanges::file(const FilePath &filePath) cons
CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const
{
QTextDocument *document = nullptr;
if (data()->m_workingCopy.contains(filePath))
document = new QTextDocument(QString::fromUtf8(data()->m_workingCopy.source(filePath)));
if (const auto source = data()->m_workingCopy.source(filePath))
document = new QTextDocument(QString::fromUtf8(*source));
CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
result->m_data = m_data;

View File

@@ -81,7 +81,8 @@ inline const CPlusPlus::Macro revision(const WorkingCopy &workingCopy,
const CPlusPlus::Macro &macro)
{
CPlusPlus::Macro newMacro(macro);
newMacro.setFileRevision(workingCopy.get(macro.filePath()).second);
if (const auto entry = workingCopy.get(macro.filePath()))
newMacro.setFileRevision(entry->second);
return newMacro;
}
@@ -189,10 +190,9 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath,
return false;
// Get from working copy
if (m_workingCopy.contains(absoluteFilePath)) {
const QPair<QByteArray, unsigned> entry = m_workingCopy.get(absoluteFilePath);
*contents = entry.first;
*revision = entry.second;
if (const auto entry = m_workingCopy.get(absoluteFilePath)) {
*contents = entry->first;
*revision = entry->second;
return true;
}
@@ -216,7 +216,7 @@ bool CppSourceProcessor::checkFile(const FilePath &absoluteFilePath) const
{
if (absoluteFilePath.isEmpty()
|| m_included.contains(absoluteFilePath)
|| m_workingCopy.contains(absoluteFilePath)) {
|| m_workingCopy.get(absoluteFilePath)) {
return true;
}
@@ -281,7 +281,7 @@ FilePath CppSourceProcessor::resolveFile_helper(const FilePath &filePath,
} else {
path = FilePath::fromString(headerPathsIt->path) / fileName;
}
if (m_workingCopy.contains(path) || checkFile(path))
if (m_workingCopy.get(path) || checkFile(path))
return path;
}
}
@@ -468,7 +468,7 @@ void CppSourceProcessor::sourceNeeded(int line, const FilePath &filePath, Includ
document->setUtf8Source(preprocessedCode);
document->keepSourceAndAST();
document->tokenize();
document->check(m_workingCopy.contains(document->filePath()) ? Document::FullCheck
document->check(m_workingCopy.get(document->filePath()) ? Document::FullCheck
: Document::FastCheck);
m_documentFinished(document);

View File

@@ -135,13 +135,13 @@ bool CppToolsJsExtension::hasQObjectParent(const QString &klassName) const
// Find class in AST.
const CPlusPlus::Snapshot snapshot = CppModelManager::instance()->snapshot();
const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy();
QByteArray source = workingCopy.source(item->filePath());
if (source.isEmpty()) {
std::optional<QByteArray> source = workingCopy.source(item->filePath());
if (!source) {
const Utils::expected_str<QByteArray> contents = item->filePath().fileContents();
QTC_ASSERT_EXPECTED(contents, return false);
source = *contents;
}
const auto doc = snapshot.preprocessedDocument(source, item->filePath());
const auto doc = snapshot.preprocessedDocument(*source, item->filePath());
if (!doc)
return false;
doc->check();

View File

@@ -158,7 +158,7 @@ bool VerifyCleanCppModelManager::isClean(bool testOnlyForCleanedProjects)
if (!testOnlyForCleanedProjects) {
RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty());
RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1);
RETURN_FALSE_IF_NOT(mm->workingCopy().contains(mm->configurationFileName()));
RETURN_FALSE_IF_NOT(mm->workingCopy().get(mm->configurationFileName()));
}
return true;
}

View File

@@ -20,4 +20,18 @@ namespace CppEditor {
WorkingCopy::WorkingCopy() = default;
std::optional<QByteArray> WorkingCopy::source(const Utils::FilePath &fileName) const
{
if (const auto value = get(fileName))
return value->first;
return {};
}
std::optional<QPair<QByteArray, unsigned>> WorkingCopy::get(const Utils::FilePath &fileName) const
{
const auto it = _elements.constFind(fileName);
if (it == _elements.constEnd())
return {};
return it.value();
}
} // namespace CppEditor

View File

@@ -11,6 +11,8 @@
#include <QString>
#include <QPair>
#include <optional>
namespace CppEditor {
class CPPEDITOR_EXPORT WorkingCopy
@@ -21,17 +23,12 @@ public:
void insert(const Utils::FilePath &fileName, const QByteArray &source, unsigned revision = 0)
{ _elements.insert(fileName, {source, revision}); }
bool contains(const Utils::FilePath &fileName) const
{ return _elements.contains(fileName); }
QByteArray source(const Utils::FilePath &fileName) const
{ return _elements.value(fileName).first; }
std::optional<QByteArray> source(const Utils::FilePath &fileName) const;
unsigned revision(const Utils::FilePath &fileName) const
{ return _elements.value(fileName).second; }
QPair<QByteArray, unsigned> get(const Utils::FilePath &fileName) const
{ return _elements.value(fileName); }
std::optional<QPair<QByteArray, unsigned>> get(const Utils::FilePath &fileName) const;
using Table = QHash<Utils::FilePath, QPair<QByteArray, unsigned> >;
const Table &elements() const

View File

@@ -163,7 +163,7 @@ TestActionsTestCase::TestActionsTestCase(const Actions &tokenActions, const Acti
QCOMPARE(DocumentModel::openedDocuments().size(), 1);
QVERIFY(m_modelManager->isCppEditor(editor));
QVERIFY(m_modelManager->workingCopy().contains(filePath));
QVERIFY(m_modelManager->workingCopy().get(filePath));
// Rehighlight
waitForRehighlightedSemanticDocument(editorWidget);

View File

@@ -2466,8 +2466,8 @@ static CPlusPlus::Document::Ptr getParsedDocument(const FilePath &filePath,
const CPlusPlus::Snapshot &snapshot)
{
QByteArray src;
if (workingCopy.contains(filePath))
src = workingCopy.source(filePath);
if (const auto source = workingCopy.source(filePath))
src = *source;
else
src = QString::fromLocal8Bit(filePath.fileContents().value_or(QByteArray())).toUtf8();

View File

@@ -456,8 +456,8 @@ static Document::Ptr getParsedDocument(const FilePath &filePath,
Snapshot &snapshot)
{
QByteArray src;
if (workingCopy.contains(filePath)) {
src = workingCopy.source(filePath);
if (const auto source = workingCopy.source(filePath)) {
src = *source;
} else {
Utils::FileReader reader;
if (reader.fetch(filePath)) // ### FIXME error reporting