ClangTools: filepathify replacements

Change-Id: I22b6f4786cda39c79cf332dba4a6a7952fd5c8b7
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
David Schulz
2023-07-03 12:51:23 +02:00
parent 3c051a8b88
commit 56772fbcc6
5 changed files with 40 additions and 43 deletions

View File

@@ -31,7 +31,7 @@ using namespace Utils;
namespace ClangTools { namespace ClangTools {
namespace Internal { namespace Internal {
int FixitsRefactoringFile::position(const QString &filePath, unsigned line, unsigned column) const int FixitsRefactoringFile::position(const FilePath &filePath, unsigned line, unsigned column) const
{ {
QTC_ASSERT(line != 0, return -1); QTC_ASSERT(line != 0, return -1);
QTC_ASSERT(column != 0, return -1); QTC_ASSERT(column != 0, return -1);
@@ -66,10 +66,8 @@ bool FixitsRefactoringFile::apply()
if (!op.apply) if (!op.apply)
continue; continue;
const FilePath filePath = FilePath::fromString(op.fileName);
// Check for permissions // Check for permissions
if (!filePath.isWritableFile()) if (!op.filePath.isWritableFile())
return false; return false;
qCDebug(fixitsLog) << " " << i << "Applying" << op; qCDebug(fixitsLog) << " " << i << "Applying" << op;
@@ -78,19 +76,19 @@ bool FixitsRefactoringFile::apply()
shiftAffectedReplacements(op, i + 1); shiftAffectedReplacements(op, i + 1);
// Apply // Apply
QTextDocument * const doc = document(op.fileName); QTextDocument * const doc = document(op.filePath);
QTextCursor cursor(doc); QTextCursor cursor(doc);
cursor.setPosition(op.pos); cursor.setPosition(op.pos);
cursor.setPosition(op.pos + op.length, QTextCursor::KeepAnchor); cursor.setPosition(op.pos + op.length, QTextCursor::KeepAnchor);
cursor.insertText(op.text); cursor.insertText(op.text);
auto &opsForFile = operationsByFile[filePath]; auto &opsForFile = operationsByFile[op.filePath];
opsForFile.first.push_back(&op); opsForFile.first.push_back(&op);
opsForFile.second = i; opsForFile.second = i;
} }
// Format // Format
for (auto it = operationsByFile.cbegin(); it != operationsByFile.cend(); ++it) { for (auto it = operationsByFile.cbegin(); it != operationsByFile.cend(); ++it) {
QTextDocument * const doc = document(it.key().toString()); QTextDocument * const doc = document(it.key());
const std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(doc)); const std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(doc));
if (!indenter) if (!indenter)
continue; continue;
@@ -104,9 +102,7 @@ bool FixitsRefactoringFile::apply()
QString error; QString error;
for (auto it = m_documents.begin(); it != m_documents.end(); ++it) { for (auto it = m_documents.begin(); it != m_documents.end(); ++it) {
if (!m_textFileFormat.writeFile(FilePath::fromString(it.key()), if (!m_textFileFormat.writeFile(it.key(), it.value()->toPlainText(), &error)) {
it.value()->toPlainText(),
&error)) {
qCDebug(fixitsLog) << "ERROR: Could not write file" << it.key() << ":" << error; qCDebug(fixitsLog) << "ERROR: Could not write file" << it.key() << ":" << error;
return false; // Error writing file return false; // Error writing file
} }
@@ -135,26 +131,26 @@ void FixitsRefactoringFile::format(TextEditor::Indenter &indenter,
if (replacements.empty()) if (replacements.empty())
return; return;
shiftAffectedReplacements(operationsForFile.front()->fileName, shiftAffectedReplacements(operationsForFile.front()->filePath,
replacements, replacements,
firstOperationIndex + 1); firstOperationIndex + 1);
} }
QTextDocument *FixitsRefactoringFile::document(const QString &filePath) const QTextDocument *FixitsRefactoringFile::document(const FilePath &filePath) const
{ {
if (m_documents.find(filePath) == m_documents.end()) { if (m_documents.find(filePath) == m_documents.end()) {
QString fileContents; QString fileContents;
if (!filePath.isEmpty()) { if (!filePath.isEmpty()) {
QString error; QString error;
QTextCodec *defaultCodec = Core::EditorManager::defaultTextCodec(); QTextCodec *defaultCodec = Core::EditorManager::defaultTextCodec();
TextFileFormat::ReadResult result = TextFileFormat::readFile(FilePath::fromString( TextFileFormat::ReadResult result = TextFileFormat::readFile(filePath,
filePath),
defaultCodec, defaultCodec,
&fileContents, &fileContents,
&m_textFileFormat, &m_textFileFormat,
&error); &error);
if (result != TextFileFormat::ReadSuccess) { if (result != TextFileFormat::ReadSuccess) {
qCDebug(fixitsLog) << "ERROR: Could not read " << filePath << ":" << error; qCDebug(fixitsLog)
<< "ERROR: Could not read " << filePath.toUserOutput() << ":" << error;
m_textFileFormat.codec = nullptr; m_textFileFormat.codec = nullptr;
} }
} }
@@ -168,7 +164,7 @@ void FixitsRefactoringFile::shiftAffectedReplacements(const ReplacementOperation
{ {
for (int i = startIndex; i < m_replacementOperations.size(); ++i) { for (int i = startIndex; i < m_replacementOperations.size(); ++i) {
ReplacementOperation &current = *m_replacementOperations[i]; ReplacementOperation &current = *m_replacementOperations[i];
if (op.fileName != current.fileName) if (op.filePath != current.filePath)
continue; continue;
ReplacementOperation before = current; ReplacementOperation before = current;
@@ -182,13 +178,13 @@ void FixitsRefactoringFile::shiftAffectedReplacements(const ReplacementOperation
} }
} }
bool FixitsRefactoringFile::hasIntersection(const QString &fileName, bool FixitsRefactoringFile::hasIntersection(const FilePath &filePath,
const Text::Replacements &replacements, const Text::Replacements &replacements,
int startIndex) const int startIndex) const
{ {
for (int i = startIndex; i < m_replacementOperations.size(); ++i) { for (int i = startIndex; i < m_replacementOperations.size(); ++i) {
const ReplacementOperation &current = *m_replacementOperations[i]; const ReplacementOperation &current = *m_replacementOperations[i];
if (fileName != current.fileName) if (filePath != current.filePath)
continue; continue;
// Usually the number of replacements is from 1 to 3. // Usually the number of replacements is from 1 to 3.
@@ -205,13 +201,13 @@ bool FixitsRefactoringFile::hasIntersection(const QString &fileName,
return false; return false;
} }
void FixitsRefactoringFile::shiftAffectedReplacements(const QString &fileName, void FixitsRefactoringFile::shiftAffectedReplacements(const FilePath &filePath,
const Text::Replacements &replacements, const Text::Replacements &replacements,
int startIndex) int startIndex)
{ {
for (int i = startIndex; i < m_replacementOperations.size(); ++i) { for (int i = startIndex; i < m_replacementOperations.size(); ++i) {
ReplacementOperation &current = *m_replacementOperations[i]; ReplacementOperation &current = *m_replacementOperations[i];
if (fileName != current.fileName) if (filePath != current.filePath)
continue; continue;
for (const auto &replacement : replacements) { for (const auto &replacement : replacements) {

View File

@@ -21,7 +21,7 @@ public:
int pos = -1; int pos = -1;
int length = -1; int length = -1;
QString text; QString text;
QString fileName; Utils::FilePath filePath;
bool apply = false; bool apply = false;
}; };
using ReplacementOperations = QVector<ReplacementOperation *>; using ReplacementOperations = QVector<ReplacementOperation *>;
@@ -34,28 +34,28 @@ public:
FixitsRefactoringFile() = default; FixitsRefactoringFile() = default;
~FixitsRefactoringFile() { qDeleteAll(m_documents); } ~FixitsRefactoringFile() { qDeleteAll(m_documents); }
int position(const QString &filePath, unsigned line, unsigned column) const; int position(const Utils::FilePath &filePath, unsigned line, unsigned column) const;
void setReplacements(const ReplacementOperations &ops) { m_replacementOperations = ops; } void setReplacements(const ReplacementOperations &ops) { m_replacementOperations = ops; }
bool apply(); bool apply();
private: private:
QTextDocument *document(const QString &filePath) const; QTextDocument *document(const Utils::FilePath &filePath) const;
void shiftAffectedReplacements(const ReplacementOperation &op, int startIndex); void shiftAffectedReplacements(const ReplacementOperation &op, int startIndex);
void format(TextEditor::Indenter &indenter, void format(TextEditor::Indenter &indenter,
QTextDocument *doc, QTextDocument *doc,
const ReplacementOperations &operationsForFile, const ReplacementOperations &operationsForFile,
int firstOperationIndex); int firstOperationIndex);
void shiftAffectedReplacements(const QString &fileName, void shiftAffectedReplacements(const Utils::FilePath &filePath,
const Utils::Text::Replacements &replacements, const Utils::Text::Replacements &replacements,
int startIndex); int startIndex);
bool hasIntersection(const QString &fileName, bool hasIntersection(const Utils::FilePath &filePath,
const Utils::Text::Replacements &replacements, const Utils::Text::Replacements &replacements,
int startIndex) const; int startIndex) const;
mutable Utils::TextFileFormat m_textFileFormat; mutable Utils::TextFileFormat m_textFileFormat;
mutable QHash<QString, QTextDocument *> m_documents; mutable QHash<Utils::FilePath, QTextDocument *> m_documents;
ReplacementOperations m_replacementOperations; // Not owned. ReplacementOperations m_replacementOperations; // Not owned.
}; };

View File

@@ -221,14 +221,14 @@ public:
const Debugger::DiagnosticLocation start = step.ranges.first(); const Debugger::DiagnosticLocation start = step.ranges.first();
const Debugger::DiagnosticLocation end = step.ranges.last(); const Debugger::DiagnosticLocation end = step.ranges.last();
const int startPos = file.position(start.filePath.toString(), start.line, start.column); const int startPos = file.position(start.filePath, start.line, start.column);
const int endPos = file.position(start.filePath.toString(), end.line, end.column); const int endPos = file.position(start.filePath, end.line, end.column);
auto op = new ReplacementOperation; auto op = new ReplacementOperation;
op->pos = startPos; op->pos = startPos;
op->length = endPos - startPos; op->length = endPos - startPos;
op->text = step.message; op->text = step.message;
op->fileName = start.filePath.toString(); op->filePath = start.filePath;
op->apply = apply; op->apply = apply;
replacements += op; replacements += op;
@@ -278,14 +278,14 @@ public:
QVector<DiagnosticItem *> itemsInvalidated; QVector<DiagnosticItem *> itemsInvalidated;
fileInfo.file.setReplacements(ops); fileInfo.file.setReplacements(ops);
model->removeWatchedPath(ops.first()->fileName); model->removeWatchedPath(ops.first()->filePath);
if (fileInfo.file.apply()) { if (fileInfo.file.apply()) {
itemsApplied = itemsScheduled; itemsApplied = itemsScheduled;
} else { } else {
itemsFailedToApply = itemsScheduled; itemsFailedToApply = itemsScheduled;
itemsInvalidated = itemsSchedulable; itemsInvalidated = itemsSchedulable;
} }
model->addWatchedPath(ops.first()->fileName); model->addWatchedPath(ops.first()->filePath);
// Update DiagnosticItem state // Update DiagnosticItem state
for (DiagnosticItem *diagnosticItem : std::as_const(itemsScheduled)) for (DiagnosticItem *diagnosticItem : std::as_const(itemsScheduled))

View File

@@ -64,7 +64,7 @@ private:
ClangToolsDiagnosticModel::ClangToolsDiagnosticModel(QObject *parent) ClangToolsDiagnosticModel::ClangToolsDiagnosticModel(QObject *parent)
: ClangToolsDiagnosticModelBase(parent) : ClangToolsDiagnosticModelBase(parent)
, m_filesWatcher(std::make_unique<QFileSystemWatcher>()) , m_filesWatcher(std::make_unique<Utils::FileSystemWatcher>())
{ {
setRootItem(new Utils::StaticTreeItem(QString())); setRootItem(new Utils::StaticTreeItem(QString()));
connectFileWatcher(); connectFileWatcher();
@@ -104,7 +104,7 @@ void ClangToolsDiagnosticModel::addDiagnostics(const Diagnostics &diagnostics, b
if (!filePathItem) { if (!filePathItem) {
filePathItem = new FilePathItem(filePath); filePathItem = new FilePathItem(filePath);
rootItem()->appendChild(filePathItem); rootItem()->appendChild(filePathItem);
addWatchedPath(filePath.toString()); addWatchedPath(filePath);
} }
// Add to file path item // Add to file path item
@@ -149,14 +149,14 @@ void ClangToolsDiagnosticModel::updateItems(const DiagnosticItem *changedItem)
void ClangToolsDiagnosticModel::connectFileWatcher() void ClangToolsDiagnosticModel::connectFileWatcher()
{ {
connect(m_filesWatcher.get(), connect(m_filesWatcher.get(),
&QFileSystemWatcher::fileChanged, &Utils::FileSystemWatcher::fileChanged,
this, this,
&ClangToolsDiagnosticModel::onFileChanged); &ClangToolsDiagnosticModel::onFileChanged);
} }
void ClangToolsDiagnosticModel::clearAndSetupCache() void ClangToolsDiagnosticModel::clearAndSetupCache()
{ {
m_filesWatcher = std::make_unique<QFileSystemWatcher>(); m_filesWatcher = std::make_unique<Utils::FileSystemWatcher>();
connectFileWatcher(); connectFileWatcher();
stepsToItemsCache.clear(); stepsToItemsCache.clear();
} }
@@ -167,17 +167,17 @@ void ClangToolsDiagnosticModel::onFileChanged(const QString &path)
if (item->diagnostic().location.filePath == Utils::FilePath::fromString(path)) if (item->diagnostic().location.filePath == Utils::FilePath::fromString(path))
item->setFixItStatus(FixitStatus::Invalidated); item->setFixItStatus(FixitStatus::Invalidated);
}); });
removeWatchedPath(path); m_filesWatcher->removeFile(path);
} }
void ClangToolsDiagnosticModel::removeWatchedPath(const QString &path) void ClangToolsDiagnosticModel::removeWatchedPath(const Utils::FilePath &path)
{ {
m_filesWatcher->removePath(path); m_filesWatcher->removeFile(path);
} }
void ClangToolsDiagnosticModel::addWatchedPath(const QString &path) void ClangToolsDiagnosticModel::addWatchedPath(const Utils::FilePath &path)
{ {
m_filesWatcher->addPath(path); m_filesWatcher->addFile(path, Utils::FileSystemWatcher::WatchAllChanges);
} }
static QString lineColumnString(const Debugger::DiagnosticLocation &location) static QString lineColumnString(const Debugger::DiagnosticLocation &location)

View File

@@ -9,6 +9,7 @@
#include "clangtoolsutils.h" #include "clangtoolsutils.h"
#include <debugger/analyzer/detailederrorview.h> #include <debugger/analyzer/detailederrorview.h>
#include <utils/filesystemwatcher.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/treemodel.h> #include <utils/treemodel.h>
@@ -104,8 +105,8 @@ public:
QSet<QString> allChecks() const; QSet<QString> allChecks() const;
void clear(); void clear();
void removeWatchedPath(const QString &path); void removeWatchedPath(const Utils::FilePath &path);
void addWatchedPath(const QString &path); void addWatchedPath(const Utils::FilePath &path);
signals: signals:
void fixitStatusChanged(const QModelIndex &index, FixitStatus oldStatus, FixitStatus newStatus); void fixitStatusChanged(const QModelIndex &index, FixitStatus oldStatus, FixitStatus newStatus);
@@ -120,7 +121,7 @@ private:
QHash<Utils::FilePath, FilePathItem *> m_filePathToItem; QHash<Utils::FilePath, FilePathItem *> m_filePathToItem;
QSet<Diagnostic> m_diagnostics; QSet<Diagnostic> m_diagnostics;
std::map<QVector<ExplainingStep>, QVector<DiagnosticItem *>> stepsToItemsCache; std::map<QVector<ExplainingStep>, QVector<DiagnosticItem *>> stepsToItemsCache;
std::unique_ptr<QFileSystemWatcher> m_filesWatcher; std::unique_ptr<Utils::FileSystemWatcher> m_filesWatcher;
}; };
class FilterOptions { class FilterOptions {