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

View File

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

View File

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

View File

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

View File

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