forked from qt-creator/qt-creator
ClangTools: Fix fix-it application
The logic for applying formatting was completely broken and had obviously never been tested with a formatter that does anything. Fixes: QTCREATORBUG-26420 Change-Id: Iffc1784b225969b86b221afb16c06e8e0d053b91 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -36,11 +36,11 @@
|
||||
#include <texteditor/texteditorsettings.h>
|
||||
|
||||
#include <QDebug>
|
||||
#include <QFileInfo>
|
||||
#include <QLoggingCategory>
|
||||
#include <QTextBlock>
|
||||
#include <QTextCursor>
|
||||
|
||||
#include <utils/filepath.h>
|
||||
#include <utils/qtcassert.h>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -81,17 +81,18 @@ bool FixitsRefactoringFile::apply()
|
||||
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(
|
||||
CppEditor::Constants::CPP_SETTINGS_ID);
|
||||
|
||||
// Apply changes
|
||||
std::unique_ptr<TextEditor::Indenter> indenter;
|
||||
QString lastFilename;
|
||||
ReplacementOperations operationsForFile;
|
||||
QHash<FilePath, QPair<ReplacementOperations, int>> operationsByFile;
|
||||
|
||||
for (int i = 0; i < m_replacementOperations.size(); ++i) {
|
||||
ReplacementOperation &op = *m_replacementOperations[i];
|
||||
if (op.apply) {
|
||||
if (!op.apply)
|
||||
continue;
|
||||
|
||||
const FilePath filePath = FilePath::fromString(op.fileName);
|
||||
|
||||
// Check for permissions
|
||||
if (!QFileInfo(op.fileName).isWritable())
|
||||
return false; // Error file not writable
|
||||
if (!filePath.isWritableFile())
|
||||
return false;
|
||||
|
||||
qCDebug(fixitsLog) << " " << i << "Applying" << op;
|
||||
|
||||
@@ -99,21 +100,24 @@ bool FixitsRefactoringFile::apply()
|
||||
shiftAffectedReplacements(op, i + 1);
|
||||
|
||||
// Apply
|
||||
QTextDocument *doc = document(op.fileName);
|
||||
if (lastFilename != op.fileName) {
|
||||
if (indenter)
|
||||
format(*indenter, doc, operationsForFile, i);
|
||||
operationsForFile.clear();
|
||||
indenter = std::unique_ptr<TextEditor::Indenter>(factory->createIndenter(doc));
|
||||
indenter->setFileName(Utils::FilePath::fromString(op.fileName));
|
||||
}
|
||||
|
||||
QTextDocument * const doc = document(op.fileName);
|
||||
QTextCursor cursor(doc);
|
||||
cursor.setPosition(op.pos);
|
||||
cursor.setPosition(op.pos + op.length, QTextCursor::KeepAnchor);
|
||||
cursor.insertText(op.text);
|
||||
operationsForFile.push_back(&op);
|
||||
auto &opsForFile = operationsByFile[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());
|
||||
const std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(doc));
|
||||
if (!indenter)
|
||||
continue;
|
||||
indenter->setFileName(it.key());
|
||||
format(*indenter, doc, it.value().first, it.value().second);
|
||||
}
|
||||
|
||||
// Write file
|
||||
|
Reference in New Issue
Block a user