forked from qt-creator/qt-creator
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:
@@ -43,8 +43,8 @@ bool CppParser::selectedForBuilding(const FilePath &fileName)
|
|||||||
QByteArray CppParser::getFileContent(const FilePath &filePath) const
|
QByteArray CppParser::getFileContent(const FilePath &filePath) const
|
||||||
{
|
{
|
||||||
QByteArray fileContent;
|
QByteArray fileContent;
|
||||||
if (m_workingCopy.contains(filePath)) {
|
if (const auto source = m_workingCopy.source(filePath)) {
|
||||||
fileContent = m_workingCopy.source(filePath);
|
fileContent = *source;
|
||||||
} else {
|
} else {
|
||||||
QString error;
|
QString error;
|
||||||
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
const QTextCodec *codec = Core::EditorManager::defaultTextCodec();
|
||||||
|
|||||||
@@ -144,8 +144,8 @@ void BuiltinEditorDocumentParser::updateImpl(const QPromise<void> &promise,
|
|||||||
QSet<Utils::FilePath> toRemove;
|
QSet<Utils::FilePath> toRemove;
|
||||||
for (const Document::Ptr &doc : std::as_const(state.snapshot)) {
|
for (const Document::Ptr &doc : std::as_const(state.snapshot)) {
|
||||||
const Utils::FilePath filePath = doc->filePath();
|
const Utils::FilePath filePath = doc->filePath();
|
||||||
if (workingCopy.contains(filePath)) {
|
if (const auto entry = workingCopy.get(filePath)) {
|
||||||
if (workingCopy.get(filePath).second != doc->editorRevision())
|
if (entry->second != doc->editorRevision())
|
||||||
addFileAndDependencies(&state.snapshot, &toRemove, filePath);
|
addFileAndDependencies(&state.snapshot, &toRemove, filePath);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -302,12 +302,13 @@ void BuiltinEditorDocumentProcessor::onCodeWarningsUpdated(
|
|||||||
|
|
||||||
SemanticInfo::Source BuiltinEditorDocumentProcessor::createSemanticInfoSource(bool force) const
|
SemanticInfo::Source BuiltinEditorDocumentProcessor::createSemanticInfoSource(bool force) const
|
||||||
{
|
{
|
||||||
const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy();
|
QByteArray source;
|
||||||
return SemanticInfo::Source(filePath().toString(),
|
int revision = 0;
|
||||||
workingCopy.source(filePath()),
|
if (const auto entry = CppModelManager::instance()->workingCopy().get(filePath())) {
|
||||||
workingCopy.revision(filePath()),
|
source = entry->first;
|
||||||
m_documentSnapshot,
|
revision = entry->second;
|
||||||
force);
|
}
|
||||||
|
return SemanticInfo::Source(filePath().toString(), source, revision, m_documentSnapshot, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace CppEditor
|
} // namespace CppEditor
|
||||||
|
|||||||
@@ -105,8 +105,8 @@ namespace Internal {
|
|||||||
static QByteArray getSource(const Utils::FilePath &fileName,
|
static QByteArray getSource(const Utils::FilePath &fileName,
|
||||||
const WorkingCopy &workingCopy)
|
const WorkingCopy &workingCopy)
|
||||||
{
|
{
|
||||||
if (workingCopy.contains(fileName)) {
|
if (const auto source = workingCopy.source(fileName)) {
|
||||||
return workingCopy.source(fileName);
|
return *source;
|
||||||
} else {
|
} else {
|
||||||
QString fileContents;
|
QString fileContents;
|
||||||
Utils::TextFileFormat format;
|
Utils::TextFileFormat format;
|
||||||
|
|||||||
@@ -649,7 +649,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed()
|
|||||||
QVERIFY(editor);
|
QVERIFY(editor);
|
||||||
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
|
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
|
||||||
QVERIFY(mm->isCppEditor(editor));
|
QVERIFY(mm->isCppEditor(editor));
|
||||||
QVERIFY(mm->workingCopy().contains(file));
|
QVERIFY(mm->workingCopy().get(file));
|
||||||
|
|
||||||
// Wait until the file is refreshed
|
// Wait until the file is refreshed
|
||||||
helper.waitForRefreshedSourceFiles();
|
helper.waitForRefreshedSourceFiles();
|
||||||
@@ -659,7 +659,7 @@ void ModelManagerTest::testGcIfLastCppeditorClosed()
|
|||||||
helper.waitForFinishedGc();
|
helper.waitForFinishedGc();
|
||||||
|
|
||||||
// Check: File is removed from the snapshpt
|
// Check: File is removed from the snapshpt
|
||||||
QVERIFY(!mm->workingCopy().contains(file));
|
QVERIFY(!mm->workingCopy().get(file));
|
||||||
QVERIFY(!mm->snapshot().contains(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
|
// Wait until the file is refreshed and check whether it is in the working copy
|
||||||
helper.waitForRefreshedSourceFiles();
|
helper.waitForRefreshedSourceFiles();
|
||||||
|
|
||||||
QVERIFY(mm->workingCopy().contains(file));
|
QVERIFY(mm->workingCopy().get(file));
|
||||||
|
|
||||||
// Run the garbage collector
|
// Run the garbage collector
|
||||||
mm->GC();
|
mm->GC();
|
||||||
|
|
||||||
// Check: File is still there
|
// Check: File is still there
|
||||||
QVERIFY(mm->workingCopy().contains(file));
|
QVERIFY(mm->workingCopy().get(file));
|
||||||
QVERIFY(mm->snapshot().contains(file));
|
QVERIFY(mm->snapshot().contains(file));
|
||||||
|
|
||||||
// Close editor
|
// Close editor
|
||||||
@@ -1090,7 +1090,7 @@ void ModelManagerTest::testRenameIncludesInEditor()
|
|||||||
});
|
});
|
||||||
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
|
QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
|
||||||
QVERIFY(modelManager->isCppEditor(editor));
|
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
|
// Test the renaming of a header file where a pragma once guard is present
|
||||||
QVERIFY(Core::FileUtils::renameFile(headerWithPragmaOnce,
|
QVERIFY(Core::FileUtils::renameFile(headerWithPragmaOnce,
|
||||||
|
|||||||
@@ -57,8 +57,8 @@ CppRefactoringFilePtr CppRefactoringChanges::file(const FilePath &filePath) cons
|
|||||||
CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const
|
CppRefactoringFileConstPtr CppRefactoringChanges::fileNoEditor(const FilePath &filePath) const
|
||||||
{
|
{
|
||||||
QTextDocument *document = nullptr;
|
QTextDocument *document = nullptr;
|
||||||
if (data()->m_workingCopy.contains(filePath))
|
if (const auto source = data()->m_workingCopy.source(filePath))
|
||||||
document = new QTextDocument(QString::fromUtf8(data()->m_workingCopy.source(filePath)));
|
document = new QTextDocument(QString::fromUtf8(*source));
|
||||||
CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
|
CppRefactoringFilePtr result(new CppRefactoringFile(document, filePath));
|
||||||
result->m_data = m_data;
|
result->m_data = m_data;
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,8 @@ inline const CPlusPlus::Macro revision(const WorkingCopy &workingCopy,
|
|||||||
const CPlusPlus::Macro ¯o)
|
const CPlusPlus::Macro ¯o)
|
||||||
{
|
{
|
||||||
CPlusPlus::Macro newMacro(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;
|
return newMacro;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,10 +190,9 @@ bool CppSourceProcessor::getFileContents(const FilePath &absoluteFilePath,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Get from working copy
|
// Get from working copy
|
||||||
if (m_workingCopy.contains(absoluteFilePath)) {
|
if (const auto entry = m_workingCopy.get(absoluteFilePath)) {
|
||||||
const QPair<QByteArray, unsigned> entry = m_workingCopy.get(absoluteFilePath);
|
*contents = entry->first;
|
||||||
*contents = entry.first;
|
*revision = entry->second;
|
||||||
*revision = entry.second;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +216,7 @@ bool CppSourceProcessor::checkFile(const FilePath &absoluteFilePath) const
|
|||||||
{
|
{
|
||||||
if (absoluteFilePath.isEmpty()
|
if (absoluteFilePath.isEmpty()
|
||||||
|| m_included.contains(absoluteFilePath)
|
|| m_included.contains(absoluteFilePath)
|
||||||
|| m_workingCopy.contains(absoluteFilePath)) {
|
|| m_workingCopy.get(absoluteFilePath)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -281,7 +281,7 @@ FilePath CppSourceProcessor::resolveFile_helper(const FilePath &filePath,
|
|||||||
} else {
|
} else {
|
||||||
path = FilePath::fromString(headerPathsIt->path) / fileName;
|
path = FilePath::fromString(headerPathsIt->path) / fileName;
|
||||||
}
|
}
|
||||||
if (m_workingCopy.contains(path) || checkFile(path))
|
if (m_workingCopy.get(path) || checkFile(path))
|
||||||
return path;
|
return path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -468,8 +468,8 @@ void CppSourceProcessor::sourceNeeded(int line, const FilePath &filePath, Includ
|
|||||||
document->setUtf8Source(preprocessedCode);
|
document->setUtf8Source(preprocessedCode);
|
||||||
document->keepSourceAndAST();
|
document->keepSourceAndAST();
|
||||||
document->tokenize();
|
document->tokenize();
|
||||||
document->check(m_workingCopy.contains(document->filePath()) ? Document::FullCheck
|
document->check(m_workingCopy.get(document->filePath()) ? Document::FullCheck
|
||||||
: Document::FastCheck);
|
: Document::FastCheck);
|
||||||
|
|
||||||
m_documentFinished(document);
|
m_documentFinished(document);
|
||||||
|
|
||||||
|
|||||||
@@ -135,13 +135,13 @@ bool CppToolsJsExtension::hasQObjectParent(const QString &klassName) const
|
|||||||
// Find class in AST.
|
// Find class in AST.
|
||||||
const CPlusPlus::Snapshot snapshot = CppModelManager::instance()->snapshot();
|
const CPlusPlus::Snapshot snapshot = CppModelManager::instance()->snapshot();
|
||||||
const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy();
|
const WorkingCopy workingCopy = CppModelManager::instance()->workingCopy();
|
||||||
QByteArray source = workingCopy.source(item->filePath());
|
std::optional<QByteArray> source = workingCopy.source(item->filePath());
|
||||||
if (source.isEmpty()) {
|
if (!source) {
|
||||||
const Utils::expected_str<QByteArray> contents = item->filePath().fileContents();
|
const Utils::expected_str<QByteArray> contents = item->filePath().fileContents();
|
||||||
QTC_ASSERT_EXPECTED(contents, return false);
|
QTC_ASSERT_EXPECTED(contents, return false);
|
||||||
source = *contents;
|
source = *contents;
|
||||||
}
|
}
|
||||||
const auto doc = snapshot.preprocessedDocument(source, item->filePath());
|
const auto doc = snapshot.preprocessedDocument(*source, item->filePath());
|
||||||
if (!doc)
|
if (!doc)
|
||||||
return false;
|
return false;
|
||||||
doc->check();
|
doc->check();
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ bool VerifyCleanCppModelManager::isClean(bool testOnlyForCleanedProjects)
|
|||||||
if (!testOnlyForCleanedProjects) {
|
if (!testOnlyForCleanedProjects) {
|
||||||
RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty());
|
RETURN_FALSE_IF_NOT(mm->snapshot().isEmpty());
|
||||||
RETURN_FALSE_IF_NOT(mm->workingCopy().size() == 1);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,4 +20,18 @@ namespace CppEditor {
|
|||||||
|
|
||||||
WorkingCopy::WorkingCopy() = default;
|
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
|
} // namespace CppEditor
|
||||||
|
|||||||
@@ -11,6 +11,8 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QPair>
|
#include <QPair>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace CppEditor {
|
namespace CppEditor {
|
||||||
|
|
||||||
class CPPEDITOR_EXPORT WorkingCopy
|
class CPPEDITOR_EXPORT WorkingCopy
|
||||||
@@ -21,17 +23,12 @@ public:
|
|||||||
void insert(const Utils::FilePath &fileName, const QByteArray &source, unsigned revision = 0)
|
void insert(const Utils::FilePath &fileName, const QByteArray &source, unsigned revision = 0)
|
||||||
{ _elements.insert(fileName, {source, revision}); }
|
{ _elements.insert(fileName, {source, revision}); }
|
||||||
|
|
||||||
bool contains(const Utils::FilePath &fileName) const
|
std::optional<QByteArray> source(const Utils::FilePath &fileName) const;
|
||||||
{ return _elements.contains(fileName); }
|
|
||||||
|
|
||||||
QByteArray source(const Utils::FilePath &fileName) const
|
|
||||||
{ return _elements.value(fileName).first; }
|
|
||||||
|
|
||||||
unsigned revision(const Utils::FilePath &fileName) const
|
unsigned revision(const Utils::FilePath &fileName) const
|
||||||
{ return _elements.value(fileName).second; }
|
{ return _elements.value(fileName).second; }
|
||||||
|
|
||||||
QPair<QByteArray, unsigned> get(const Utils::FilePath &fileName) const
|
std::optional<QPair<QByteArray, unsigned>> get(const Utils::FilePath &fileName) const;
|
||||||
{ return _elements.value(fileName); }
|
|
||||||
|
|
||||||
using Table = QHash<Utils::FilePath, QPair<QByteArray, unsigned> >;
|
using Table = QHash<Utils::FilePath, QPair<QByteArray, unsigned> >;
|
||||||
const Table &elements() const
|
const Table &elements() const
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ TestActionsTestCase::TestActionsTestCase(const Actions &tokenActions, const Acti
|
|||||||
|
|
||||||
QCOMPARE(DocumentModel::openedDocuments().size(), 1);
|
QCOMPARE(DocumentModel::openedDocuments().size(), 1);
|
||||||
QVERIFY(m_modelManager->isCppEditor(editor));
|
QVERIFY(m_modelManager->isCppEditor(editor));
|
||||||
QVERIFY(m_modelManager->workingCopy().contains(filePath));
|
QVERIFY(m_modelManager->workingCopy().get(filePath));
|
||||||
|
|
||||||
// Rehighlight
|
// Rehighlight
|
||||||
waitForRehighlightedSemanticDocument(editorWidget);
|
waitForRehighlightedSemanticDocument(editorWidget);
|
||||||
|
|||||||
@@ -2466,8 +2466,8 @@ static CPlusPlus::Document::Ptr getParsedDocument(const FilePath &filePath,
|
|||||||
const CPlusPlus::Snapshot &snapshot)
|
const CPlusPlus::Snapshot &snapshot)
|
||||||
{
|
{
|
||||||
QByteArray src;
|
QByteArray src;
|
||||||
if (workingCopy.contains(filePath))
|
if (const auto source = workingCopy.source(filePath))
|
||||||
src = workingCopy.source(filePath);
|
src = *source;
|
||||||
else
|
else
|
||||||
src = QString::fromLocal8Bit(filePath.fileContents().value_or(QByteArray())).toUtf8();
|
src = QString::fromLocal8Bit(filePath.fileContents().value_or(QByteArray())).toUtf8();
|
||||||
|
|
||||||
|
|||||||
@@ -456,8 +456,8 @@ static Document::Ptr getParsedDocument(const FilePath &filePath,
|
|||||||
Snapshot &snapshot)
|
Snapshot &snapshot)
|
||||||
{
|
{
|
||||||
QByteArray src;
|
QByteArray src;
|
||||||
if (workingCopy.contains(filePath)) {
|
if (const auto source = workingCopy.source(filePath)) {
|
||||||
src = workingCopy.source(filePath);
|
src = *source;
|
||||||
} else {
|
} else {
|
||||||
Utils::FileReader reader;
|
Utils::FileReader reader;
|
||||||
if (reader.fetch(filePath)) // ### FIXME error reporting
|
if (reader.fetch(filePath)) // ### FIXME error reporting
|
||||||
|
|||||||
Reference in New Issue
Block a user