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 <texteditor/texteditorsettings.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QFileInfo>
|
|
||||||
#include <QLoggingCategory>
|
#include <QLoggingCategory>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
#include <QTextCursor>
|
#include <QTextCursor>
|
||||||
|
|
||||||
|
#include <utils/filepath.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@@ -81,17 +81,18 @@ bool FixitsRefactoringFile::apply()
|
|||||||
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(
|
ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(
|
||||||
CppEditor::Constants::CPP_SETTINGS_ID);
|
CppEditor::Constants::CPP_SETTINGS_ID);
|
||||||
|
|
||||||
// Apply changes
|
QHash<FilePath, QPair<ReplacementOperations, int>> operationsByFile;
|
||||||
std::unique_ptr<TextEditor::Indenter> indenter;
|
|
||||||
QString lastFilename;
|
|
||||||
ReplacementOperations operationsForFile;
|
|
||||||
|
|
||||||
for (int i=0; i < m_replacementOperations.size(); ++i) {
|
for (int i = 0; i < m_replacementOperations.size(); ++i) {
|
||||||
ReplacementOperation &op = *m_replacementOperations[i];
|
ReplacementOperation &op = *m_replacementOperations[i];
|
||||||
if (op.apply) {
|
if (!op.apply)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const FilePath filePath = FilePath::fromString(op.fileName);
|
||||||
|
|
||||||
// Check for permissions
|
// Check for permissions
|
||||||
if (!QFileInfo(op.fileName).isWritable())
|
if (!filePath.isWritableFile())
|
||||||
return false; // Error file not writable
|
return false;
|
||||||
|
|
||||||
qCDebug(fixitsLog) << " " << i << "Applying" << op;
|
qCDebug(fixitsLog) << " " << i << "Applying" << op;
|
||||||
|
|
||||||
@@ -99,21 +100,24 @@ bool FixitsRefactoringFile::apply()
|
|||||||
shiftAffectedReplacements(op, i + 1);
|
shiftAffectedReplacements(op, i + 1);
|
||||||
|
|
||||||
// Apply
|
// Apply
|
||||||
QTextDocument *doc = document(op.fileName);
|
QTextDocument * const 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
||||||
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
|
// Write file
|
||||||
|
Reference in New Issue
Block a user