CppEditor: Make more use of CppQuickFixInterface::currentFile()

A CppRefactoringFile is created for us when quickfixes are collected, so
make use of it instead of creating a new one.

Change-Id: I133b43f92d1fbea82aa25df025cfc257691de2a6
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2024-05-22 12:46:28 +02:00
parent 8577ab8bcb
commit 5cc44b8cab
18 changed files with 139 additions and 199 deletions

View File

@@ -37,7 +37,7 @@ public:
, m_name(name) , m_name(name)
, m_oo(CppCodeStyleSettings::currentProjectCodeStyleOverview()) , m_oo(CppCodeStyleSettings::currentProjectCodeStyleOverview())
, m_originalName(m_oo.prettyName(m_name)) , m_originalName(m_oo.prettyName(m_name))
, m_file(CppRefactoringChanges(snapshot()).cppFile(filePath())) , m_file(interface.currentFile())
{ {
setDescription(Tr::tr("Assign to Local Variable")); setDescription(Tr::tr("Assign to Local Variable"));
} }

View File

@@ -248,8 +248,7 @@ private:
QTC_ASSERT(!parts.isEmpty(), return); QTC_ASSERT(!parts.isEmpty(), return);
const QStringList namespaces = parts.mid(0, parts.length() - 1); const QStringList namespaces = parts.mid(0, parts.length() - 1);
CppRefactoringChanges refactoring(snapshot()); CppRefactoringFilePtr file = currentFile();
CppRefactoringFilePtr file = refactoring.cppFile(filePath());
NSVisitor visitor(file.data(), namespaces, m_symbolPos); NSVisitor visitor(file.data(), namespaces, m_symbolPos);
visitor.accept(file->cppDocument()->translationUnit()->ast()); visitor.accept(file->cppDocument()->translationUnit()->ast());
@@ -311,8 +310,7 @@ AddIncludeForUndefinedIdentifierOp::AddIncludeForUndefinedIdentifierOp(
void AddIncludeForUndefinedIdentifierOp::perform() void AddIncludeForUndefinedIdentifierOp::perform()
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringFilePtr file = currentFile();
CppRefactoringFilePtr file = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
insertNewIncludeDirective(m_include, file, semanticInfo().doc, changes); insertNewIncludeDirective(m_include, file, semanticInfo().doc, changes);

View File

@@ -82,16 +82,13 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
int start = currentFile->endOf(compoundStatement->lbrace_token); int start = currentFile()->endOf(compoundStatement->lbrace_token);
changes.insert(start, QLatin1String("\ncase ") changes.insert(start, QLatin1String("\ncase ")
+ values.join(QLatin1String(":\nbreak;\ncase ")) + values.join(QLatin1String(":\nbreak;\ncase "))
+ QLatin1String(":\nbreak;")); + QLatin1String(":\nbreak;"));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
CompoundStatementAST *compoundStatement; CompoundStatementAST *compoundStatement;

View File

@@ -41,7 +41,7 @@ public:
, m_identifierAST(identifierAST) , m_identifierAST(identifierAST)
, m_symbol(symbol) , m_symbol(symbol)
, m_refactoring(snapshot()) , m_refactoring(snapshot())
, m_file(m_refactoring.cppFile(filePath())) , m_file(currentFile())
, m_document(interface.semanticInfo().doc) , m_document(interface.semanticInfo().doc)
{ {
setDescription( setDescription(

View File

@@ -28,13 +28,10 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
changes.replace(start, end, replacement); changes.replace(start, end, replacement);
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:

View File

@@ -35,10 +35,8 @@ public:
private: private:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); currentFile()->setChangeSet(m_changes);
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath()); currentFile()->apply();
currentFile->setChangeSet(m_changes);
currentFile->apply();
} }
const ChangeSet m_changes; const ChangeSet m_changes;

View File

@@ -241,15 +241,12 @@ private:
public: public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); const int startPos = currentFile()->startOf(m_literal);
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath()); const int endPos = currentFile()->endOf(m_literal);
const int startPos = currentFile->startOf(m_literal);
const int endPos = currentFile->endOf(m_literal);
StringLiteralAST *stringLiteral = m_literal->asStringLiteral(); StringLiteralAST *stringLiteral = m_literal->asStringLiteral();
QTC_ASSERT(stringLiteral, return); QTC_ASSERT(stringLiteral, return);
const QByteArray oldContents(currentFile->tokenAt(stringLiteral->literal_token). const QByteArray oldContents(currentFile()->tokenAt(stringLiteral->literal_token).
identifier->chars()); identifier->chars());
QByteArrayList newContents; QByteArrayList newContents;
if (m_escape) if (m_escape)
@@ -278,8 +275,8 @@ public:
changes.insert(endPos, "\"" + str + "\""); changes.insert(endPos, "\"" + str + "\"");
replace = false; replace = false;
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -302,13 +299,10 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
const int startPos = currentFile->startOf(m_literal); const int startPos = currentFile()->startOf(m_literal);
const int endPos = currentFile->endOf(m_literal); const int endPos = currentFile()->endOf(m_literal);
// kill leading '@'. No need to adapt endPos, that is done by ChangeSet // kill leading '@'. No need to adapt endPos, that is done by ChangeSet
if (m_actions & RemoveObjectiveCAction) if (m_actions & RemoveObjectiveCAction)
@@ -326,7 +320,7 @@ public:
if (m_actions & ConvertEscapeSequencesToCharAction) { if (m_actions & ConvertEscapeSequencesToCharAction) {
StringLiteralAST *stringLiteral = m_literal->asStringLiteral(); StringLiteralAST *stringLiteral = m_literal->asStringLiteral();
QTC_ASSERT(stringLiteral, return ;); QTC_ASSERT(stringLiteral, return ;);
const QByteArray oldContents(currentFile->tokenAt(stringLiteral->literal_token).identifier->chars()); const QByteArray oldContents(currentFile()->tokenAt(stringLiteral->literal_token).identifier->chars());
const QByteArray newContents = stringToCharEscapeSequences(oldContents); const QByteArray newContents = stringToCharEscapeSequences(oldContents);
QTC_ASSERT(!newContents.isEmpty(), return ;); QTC_ASSERT(!newContents.isEmpty(), return ;);
if (oldContents != newContents) if (oldContents != newContents)
@@ -337,7 +331,7 @@ public:
if (m_actions & ConvertEscapeSequencesToStringAction) { if (m_actions & ConvertEscapeSequencesToStringAction) {
NumericLiteralAST *charLiteral = m_literal->asNumericLiteral(); // char 'c' constants are numerical. NumericLiteralAST *charLiteral = m_literal->asNumericLiteral(); // char 'c' constants are numerical.
QTC_ASSERT(charLiteral, return ;); QTC_ASSERT(charLiteral, return ;);
const QByteArray oldContents(currentFile->tokenAt(charLiteral->literal_token).identifier->chars()); const QByteArray oldContents(currentFile()->tokenAt(charLiteral->literal_token).identifier->chars());
const QByteArray newContents = charToStringEscapeSequences(oldContents); const QByteArray newContents = charToStringEscapeSequences(oldContents);
QTC_ASSERT(!newContents.isEmpty(), return ;); QTC_ASSERT(!newContents.isEmpty(), return ;);
if (oldContents != newContents) if (oldContents != newContents)
@@ -358,8 +352,8 @@ public:
changes.insert(startPos, leading); changes.insert(startPos, leading);
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -382,21 +376,18 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
if (qlatin1Call) { if (qlatin1Call) {
changes.replace(currentFile->startOf(qlatin1Call), currentFile->startOf(stringLiteral), changes.replace(currentFile()->startOf(qlatin1Call), currentFile()->startOf(stringLiteral),
QLatin1String("@")); QLatin1String("@"));
changes.remove(currentFile->endOf(stringLiteral), currentFile->endOf(qlatin1Call)); changes.remove(currentFile()->endOf(stringLiteral), currentFile()->endOf(qlatin1Call));
} else { } else {
changes.insert(currentFile->startOf(stringLiteral), QLatin1String("@")); changes.insert(currentFile()->startOf(stringLiteral), QLatin1String("@"));
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:

View File

@@ -42,9 +42,6 @@ public:
private: private:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
QString newName = m_isAllUpper ? m_name.toLower() : m_name; QString newName = m_isAllUpper ? m_name.toLower() : m_name;
for (int i = 1; i < newName.length(); ++i) { for (int i = 1; i < newName.length(); ++i) {
const QChar c = newName.at(i); const QChar c = newName.at(i);
@@ -57,9 +54,9 @@ private:
} }
if (m_test) { if (m_test) {
ChangeSet changeSet; ChangeSet changeSet;
changeSet.replace(currentFile->range(m_nameAst), newName); changeSet.replace(currentFile()->range(m_nameAst), newName);
currentFile->setChangeSet(changeSet); currentFile()->setChangeSet(changeSet);
currentFile->apply(); currentFile()->apply();
} else { } else {
editor()->renameUsages(newName); editor()->renameUsages(newName);
} }

View File

@@ -235,33 +235,29 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
QString declaration = getDeclaration(); QString declaration = getDeclaration();
if (!declaration.isEmpty()) { if (!declaration.isEmpty()) {
ChangeSet changes; ChangeSet changes;
changes.replace(currentFile->startOf(binaryAST), changes.replace(currentFile()->startOf(binaryAST),
currentFile->endOf(simpleNameAST), currentFile()->endOf(simpleNameAST),
declaration); declaration);
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
} }
private: private:
QString getDeclaration() QString getDeclaration()
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview(); Overview oo = CppCodeStyleSettings::currentProjectCodeStyleOverview();
const auto settings = CppQuickFixProjectsSettings::getQuickFixSettings( const auto settings = CppQuickFixProjectsSettings::getQuickFixSettings(
ProjectTree::currentProject()); ProjectTree::currentProject());
if (currentFile->cppDocument()->languageFeatures().cxx11Enabled && settings->useAuto) if (currentFile()->cppDocument()->languageFeatures().cxx11Enabled && settings->useAuto)
return "auto " + oo.prettyName(simpleNameAST->name); return "auto " + oo.prettyName(simpleNameAST->name);
return declFromExpr(binaryAST->right_expression, nullptr, simpleNameAST, snapshot(), return declFromExpr(binaryAST->right_expression, nullptr, simpleNameAST, snapshot(),
context(), currentFile, false); context(), currentFile(), false);
} }
const BinaryExpressionAST *binaryAST; const BinaryExpressionAST *binaryAST;

View File

@@ -75,9 +75,8 @@ public:
void perform() override void perform() override
{ {
QTC_ASSERT(!m_funcReturn || !m_relevantDecls.isEmpty(), return); QTC_ASSERT(!m_funcReturn || !m_relevantDecls.isEmpty(), return);
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
CppRefactoringChanges refactoring(snapshot());
ExtractFunctionOptions options; ExtractFunctionOptions options;
if (m_functionNameGetter) if (m_functionNameGetter)
options.funcName = m_functionNameGetter(); options.funcName = m_functionNameGetter();
@@ -172,7 +171,7 @@ public:
funcDecl.append(QLatin1String(" const")); funcDecl.append(QLatin1String(" const"));
} }
funcDef.append(QLatin1String("\n{\n")); funcDef.append(QLatin1String("\n{\n"));
QString extract = currentFile->textOf(m_extractionStart, m_extractionEnd); QString extract = currentFile()->textOf(m_extractionStart, m_extractionEnd);
extract.replace(QChar::ParagraphSeparator, QLatin1String("\n")); extract.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
if (!extract.endsWith(QLatin1Char('\n')) && m_funcReturn) if (!extract.endsWith(QLatin1Char('\n')) && m_funcReturn)
extract.append(QLatin1Char('\n')); extract.append(QLatin1Char('\n'));
@@ -187,24 +186,24 @@ public:
} }
funcDef.append(QLatin1String("\n}\n\n")); funcDef.append(QLatin1String("\n}\n\n"));
funcDef.replace(QChar::ParagraphSeparator, QLatin1String("\n")); funcDef.replace(QChar::ParagraphSeparator, QLatin1String("\n"));
funcDef.prepend(inlinePrefix(currentFile->filePath())); funcDef.prepend(inlinePrefix(currentFile()->filePath()));
funcCall.append(QLatin1Char(';')); funcCall.append(QLatin1Char(';'));
// Do not insert right between the function and an associated comment. // Do not insert right between the function and an associated comment.
int position = currentFile->startOf(m_refFuncDef); int position = currentFile()->startOf(m_refFuncDef);
const QList<Token> functionDoc = commentsForDeclaration( const QList<Token> functionDoc = commentsForDeclaration(
m_refFuncDef->symbol, m_refFuncDef, *currentFile->document(), m_refFuncDef->symbol, m_refFuncDef, *currentFile()->document(),
currentFile->cppDocument()); currentFile()->cppDocument());
if (!functionDoc.isEmpty()) { if (!functionDoc.isEmpty()) {
position = currentFile->cppDocument()->translationUnit()->getTokenPositionInDocument( position = currentFile()->cppDocument()->translationUnit()->getTokenPositionInDocument(
functionDoc.first(), currentFile->document()); functionDoc.first(), currentFile()->document());
} }
ChangeSet change; ChangeSet change;
change.insert(position, funcDef); change.insert(position, funcDef);
change.replace(m_extractionStart, m_extractionEnd, funcCall); change.replace(m_extractionStart, m_extractionEnd, funcCall);
currentFile->setChangeSet(change); currentFile()->setChangeSet(change);
currentFile->apply(); currentFile()->apply();
// Write declaration, if necessary. // Write declaration, if necessary.
if (matchingClass) { if (matchingClass) {

View File

@@ -176,42 +176,41 @@ public:
FunctionDeclaratorAST *functionDeclaratorOfDefinition FunctionDeclaratorAST *functionDeclaratorOfDefinition
= functionDeclarator(m_functionDefinition); = functionDeclarator(m_functionDefinition);
const CppRefactoringChanges refactoring(snapshot()); const CppRefactoringChanges refactoring(snapshot());
const CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath()); deduceTypeNameOfLiteral(currentFile()->cppDocument());
deduceTypeNameOfLiteral(currentFile->cppDocument());
ChangeSet changes; ChangeSet changes;
if (NumericLiteralAST *concreteLiteral = m_literal->asNumericLiteral()) { if (NumericLiteralAST *concreteLiteral = m_literal->asNumericLiteral()) {
m_literalInfo = ReplaceLiterals<NumericLiteralAST>(currentFile, &changes, m_literalInfo = ReplaceLiterals<NumericLiteralAST>(currentFile(), &changes,
concreteLiteral) concreteLiteral)
.apply(m_functionDefinition->function_body); .apply(m_functionDefinition->function_body);
} else if (StringLiteralAST *concreteLiteral = m_literal->asStringLiteral()) { } else if (StringLiteralAST *concreteLiteral = m_literal->asStringLiteral()) {
m_literalInfo = ReplaceLiterals<StringLiteralAST>(currentFile, &changes, m_literalInfo = ReplaceLiterals<StringLiteralAST>(currentFile(), &changes,
concreteLiteral) concreteLiteral)
.apply(m_functionDefinition->function_body); .apply(m_functionDefinition->function_body);
} else if (BoolLiteralAST *concreteLiteral = m_literal->asBoolLiteral()) { } else if (BoolLiteralAST *concreteLiteral = m_literal->asBoolLiteral()) {
m_literalInfo = ReplaceLiterals<BoolLiteralAST>(currentFile, &changes, m_literalInfo = ReplaceLiterals<BoolLiteralAST>(currentFile(), &changes,
concreteLiteral) concreteLiteral)
.apply(m_functionDefinition->function_body); .apply(m_functionDefinition->function_body);
} }
const FoundDeclaration functionDeclaration const FoundDeclaration functionDeclaration
= findDeclaration(refactoring, m_functionDefinition); = findDeclaration(refactoring, m_functionDefinition);
appendFunctionParameter(functionDeclaratorOfDefinition, currentFile, &changes, appendFunctionParameter(functionDeclaratorOfDefinition, currentFile(), &changes,
!functionDeclaration.ast); !functionDeclaration.ast);
if (functionDeclaration.ast) { if (functionDeclaration.ast) {
if (currentFile->filePath() != functionDeclaration.file->filePath()) { if (currentFile()->filePath() != functionDeclaration.file->filePath()) {
ChangeSet declChanges; ChangeSet declChanges;
appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges, appendFunctionParameter(functionDeclaration.ast, functionDeclaration.file, &declChanges,
true); true);
functionDeclaration.file->setChangeSet(declChanges); functionDeclaration.file->setChangeSet(declChanges);
functionDeclaration.file->apply(); functionDeclaration.file->apply();
} else { } else {
appendFunctionParameter(functionDeclaration.ast, currentFile, &changes, appendFunctionParameter(functionDeclaration.ast, currentFile(), &changes,
true); true);
} }
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
QTextCursor c = currentFile->cursor(); QTextCursor c = currentFile()->cursor();
c.setPosition(c.position() - parameterName().length()); c.setPosition(c.position() - parameterName().length());
editor()->setTextCursor(c); editor()->setTextCursor(c);
editor()->renameSymbolUnderCursor(); editor()->renameSymbolUnderCursor();

View File

@@ -35,17 +35,14 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
changes.flip(currentFile->range(binary->left_expression), changes.flip(currentFile()->range(binary->left_expression),
currentFile->range(binary->right_expression)); currentFile()->range(binary->right_expression));
if (!replacement.isEmpty()) if (!replacement.isEmpty())
changes.replace(currentFile->range(binary->binary_op_token), replacement); changes.replace(currentFile()->range(binary->binary_op_token), replacement);
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -86,22 +83,19 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
if (negation) { if (negation) {
// can't remove parentheses since that might break precedence // can't remove parentheses since that might break precedence
changes.remove(currentFile->range(negation->unary_op_token)); changes.remove(currentFile()->range(negation->unary_op_token));
} else if (nested) { } else if (nested) {
changes.insert(currentFile->startOf(nested), QLatin1String("!")); changes.insert(currentFile()->startOf(nested), QLatin1String("!"));
} else { } else {
changes.insert(currentFile->startOf(binary), QLatin1String("!(")); changes.insert(currentFile()->startOf(binary), QLatin1String("!("));
changes.insert(currentFile->endOf(binary), QLatin1String(")")); changes.insert(currentFile()->endOf(binary), QLatin1String(")"));
} }
changes.replace(currentFile->range(binary->binary_op_token), replacement); changes.replace(currentFile()->range(binary->binary_op_token), replacement);
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -131,20 +125,17 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
changes.replace(currentFile->range(pattern->binary_op_token), QLatin1String("||")); changes.replace(currentFile()->range(pattern->binary_op_token), QLatin1String("||"));
changes.remove(currentFile->range(left->unary_op_token)); changes.remove(currentFile()->range(left->unary_op_token));
changes.remove(currentFile->range(right->unary_op_token)); changes.remove(currentFile()->range(right->unary_op_token));
const int start = currentFile->startOf(pattern); const int start = currentFile()->startOf(pattern);
const int end = currentFile->endOf(pattern); const int end = currentFile()->endOf(pattern);
changes.insert(start, QLatin1String("!(")); changes.insert(start, QLatin1String("!("));
changes.insert(end, QLatin1String(")")); changes.insert(end, QLatin1String(")"));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
}; };

View File

@@ -469,7 +469,7 @@ private:
return; return;
const CppRefactoringChanges refactoring(interface.snapshot()); const CppRefactoringChanges refactoring(interface.snapshot());
const CppRefactoringFilePtr defFile = refactoring.cppFile(interface.filePath()); const CppRefactoringFilePtr defFile = interface.currentFile();
const ChangeSet::Range defRange = defFile->range(completeDefAST); const ChangeSet::Range defRange = defFile->range(completeDefAST);
// Determine declaration (file, range, text); // Determine declaration (file, range, text);

View File

@@ -37,16 +37,13 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); int targetEndPos = currentFile()->endOf(m_targetParam);
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
int targetEndPos = currentFile->endOf(m_targetParam);
Utils::ChangeSet changes; Utils::ChangeSet changes;
changes.flip(currentFile->startOf(m_currentParam), currentFile->endOf(m_currentParam), changes.flip(currentFile()->startOf(m_currentParam), currentFile()->endOf(m_currentParam),
currentFile->startOf(m_targetParam), targetEndPos); currentFile()->startOf(m_targetParam), targetEndPos);
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->setOpenEditor(false, targetEndPos); currentFile()->setOpenEditor(false, targetEndPos);
currentFile->apply(); currentFile()->apply();
} }
private: private:

View File

@@ -41,10 +41,8 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); currentFile()->setChangeSet(m_change);
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath()); currentFile()->apply();
currentFile->setChangeSet(m_change);
currentFile->apply();
} }
private: private:

View File

@@ -483,12 +483,11 @@ private:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
if (m_removeAllAtGlobalScope) { if (m_removeAllAtGlobalScope) {
removeAllUsingsAtGlobalScope(refactoring); removeAllUsingsAtGlobalScope(refactoring);
} else if (refactorFile(currentFile, } else if (refactorFile(currentFile(),
refactoring.snapshot(), refactoring.snapshot(),
currentFile->endOf(m_usingDirective), currentFile()->endOf(m_usingDirective),
true)) { true)) {
processIncludes(refactoring, filePath()); processIncludes(refactoring, filePath());
} }
@@ -505,7 +504,7 @@ private:
* @param removeUsing if the using directive is in this file, remove it * @param removeUsing if the using directive is in this file, remove it
* @return true if the using statement is global and there is no other global using namespace * @return true if the using statement is global and there is no other global using namespace
*/ */
bool refactorFile(CppRefactoringFilePtr &file, bool refactorFile(const CppRefactoringFilePtr &file,
const Snapshot &snapshot, const Snapshot &snapshot,
int startSymbol, int startSymbol,
bool removeUsing = false) bool removeUsing = false)

View File

@@ -77,36 +77,33 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
for (Statement * const statement : m_statements) { for (Statement * const statement : m_statements) {
const int start = currentFile->endOf(tokenToInsertOpeningBraceAfter(statement)); const int start = currentFile()->endOf(tokenToInsertOpeningBraceAfter(statement));
changes.insert(start, QLatin1String(" {")); changes.insert(start, QLatin1String(" {"));
if constexpr (std::is_same_v<Statement, DoStatementAST>) { if constexpr (std::is_same_v<Statement, DoStatementAST>) {
const int end = currentFile->startOf(statement->while_token); const int end = currentFile()->startOf(statement->while_token);
changes.insert(end, QLatin1String("} ")); changes.insert(end, QLatin1String("} "));
} else if constexpr (std::is_same_v<Statement, IfStatementAST>) { } else if constexpr (std::is_same_v<Statement, IfStatementAST>) {
if (statement->else_statement) { if (statement->else_statement) {
changes.insert(currentFile->startOf(statement->else_token), "} "); changes.insert(currentFile()->startOf(statement->else_token), "} ");
} else { } else {
changes.insert(currentFile->endOf(statement->statement->lastToken() - 1), changes.insert(currentFile()->endOf(statement->statement->lastToken() - 1),
"\n}"); "\n}");
} }
} else { } else {
const int end = currentFile->endOf(statement->statement->lastToken() - 1); const int end = currentFile()->endOf(statement->statement->lastToken() - 1);
changes.insert(end, QLatin1String("\n}")); changes.insert(end, QLatin1String("\n}"));
} }
} }
if (m_elseStatement) { if (m_elseStatement) {
changes.insert(currentFile->endOf(m_elseToken), " {"); changes.insert(currentFile()->endOf(m_elseToken), " {");
changes.insert(currentFile->endOf(m_elseStatement->lastToken() - 1), "\n}"); changes.insert(currentFile()->endOf(m_elseStatement->lastToken() - 1), "\n}");
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -174,19 +171,16 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
changes.copy(currentFile->range(core), currentFile->startOf(condition)); changes.copy(currentFile()->range(core), currentFile()->startOf(condition));
int insertPos = currentFile->startOf(pattern); int insertPos = currentFile()->startOf(pattern);
changes.move(currentFile->range(condition), insertPos); changes.move(currentFile()->range(condition), insertPos);
changes.insert(insertPos, QLatin1String(";\n")); changes.insert(insertPos, QLatin1String(";\n"));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
ASTMatcher matcher; ASTMatcher matcher;
@@ -214,22 +208,19 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
changes.insert(currentFile->startOf(condition), QLatin1String("(")); changes.insert(currentFile()->startOf(condition), QLatin1String("("));
changes.insert(currentFile->endOf(condition), QLatin1String(") != 0")); changes.insert(currentFile()->endOf(condition), QLatin1String(") != 0"));
int insertPos = currentFile->startOf(pattern); int insertPos = currentFile()->startOf(pattern);
const int conditionStart = currentFile->startOf(condition); const int conditionStart = currentFile()->startOf(condition);
changes.move(conditionStart, currentFile->startOf(core), insertPos); changes.move(conditionStart, currentFile()->startOf(core), insertPos);
changes.copy(currentFile->range(core), insertPos); changes.copy(currentFile()->range(core), insertPos);
changes.insert(insertPos, QLatin1String(";\n")); changes.insert(insertPos, QLatin1String(";\n"));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
ASTMatcher matcher; ASTMatcher matcher;
@@ -253,60 +244,57 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot()); const Token binaryToken = currentFile()->tokenAt(condition->binary_op_token);
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
const Token binaryToken = currentFile->tokenAt(condition->binary_op_token);
if (binaryToken.is(T_AMPER_AMPER)) if (binaryToken.is(T_AMPER_AMPER))
splitAndCondition(currentFile); splitAndCondition();
else else
splitOrCondition(currentFile); splitOrCondition();
} }
void splitAndCondition(CppRefactoringFilePtr currentFile) const void splitAndCondition() const
{ {
ChangeSet changes; ChangeSet changes;
int startPos = currentFile->startOf(pattern); int startPos = currentFile()->startOf(pattern);
changes.insert(startPos, QLatin1String("if (")); changes.insert(startPos, QLatin1String("if ("));
changes.move(currentFile->range(condition->left_expression), startPos); changes.move(currentFile()->range(condition->left_expression), startPos);
changes.insert(startPos, QLatin1String(") {\n")); changes.insert(startPos, QLatin1String(") {\n"));
const int lExprEnd = currentFile->endOf(condition->left_expression); const int lExprEnd = currentFile()->endOf(condition->left_expression);
changes.remove(lExprEnd, currentFile->startOf(condition->right_expression)); changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
changes.insert(currentFile->endOf(pattern), QLatin1String("\n}")); changes.insert(currentFile()->endOf(pattern), QLatin1String("\n}"));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
void splitOrCondition(CppRefactoringFilePtr currentFile) const void splitOrCondition() const
{ {
ChangeSet changes; ChangeSet changes;
StatementAST *ifTrueStatement = pattern->statement; StatementAST *ifTrueStatement = pattern->statement;
CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement(); CompoundStatementAST *compoundStatement = ifTrueStatement->asCompoundStatement();
int insertPos = currentFile->endOf(ifTrueStatement); int insertPos = currentFile()->endOf(ifTrueStatement);
if (compoundStatement) if (compoundStatement)
changes.insert(insertPos, QLatin1String(" ")); changes.insert(insertPos, QLatin1String(" "));
else else
changes.insert(insertPos, QLatin1String("\n")); changes.insert(insertPos, QLatin1String("\n"));
changes.insert(insertPos, QLatin1String("else if (")); changes.insert(insertPos, QLatin1String("else if ("));
const int rExprStart = currentFile->startOf(condition->right_expression); const int rExprStart = currentFile()->startOf(condition->right_expression);
changes.move(rExprStart, currentFile->startOf(pattern->rparen_token), insertPos); changes.move(rExprStart, currentFile()->startOf(pattern->rparen_token), insertPos);
changes.insert(insertPos, QLatin1String(")")); changes.insert(insertPos, QLatin1String(")"));
const int rParenEnd = currentFile->endOf(pattern->rparen_token); const int rParenEnd = currentFile()->endOf(pattern->rparen_token);
changes.copy(rParenEnd, currentFile->endOf(pattern->statement), insertPos); changes.copy(rParenEnd, currentFile()->endOf(pattern->statement), insertPos);
const int lExprEnd = currentFile->endOf(condition->left_expression); const int lExprEnd = currentFile()->endOf(condition->left_expression);
changes.remove(lExprEnd, currentFile->startOf(condition->right_expression)); changes.remove(lExprEnd, currentFile()->startOf(condition->right_expression));
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private:
@@ -333,9 +321,7 @@ public:
{ {
QTC_ASSERT(m_forAst, return); QTC_ASSERT(m_forAst, return);
const Utils::FilePath filePath = currentFile()->filePath(); const CppRefactoringFilePtr file = currentFile();
const CppRefactoringChanges refactoring(snapshot());
const CppRefactoringFilePtr file = refactoring.cppFile(filePath);
ChangeSet change; ChangeSet change;
// Optimize post (in|de)crement operator to pre (in|de)crement operator // Optimize post (in|de)crement operator to pre (in|de)crement operator

View File

@@ -43,15 +43,12 @@ public:
void perform() override void perform() override
{ {
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
ChangeSet changes; ChangeSet changes;
SpecifierListAST *specifiers = declaration->decl_specifier_list; SpecifierListAST *specifiers = declaration->decl_specifier_list;
int declSpecifiersStart = currentFile->startOf(specifiers->firstToken()); int declSpecifiersStart = currentFile()->startOf(specifiers->firstToken());
int declSpecifiersEnd = currentFile->endOf(specifiers->lastToken() - 1); int declSpecifiersEnd = currentFile()->endOf(specifiers->lastToken() - 1);
int insertPos = currentFile->endOf(declaration->semicolon_token); int insertPos = currentFile()->endOf(declaration->semicolon_token);
DeclaratorAST *prevDeclarator = declaration->declarator_list->value; DeclaratorAST *prevDeclarator = declaration->declarator_list->value;
@@ -61,17 +58,17 @@ public:
changes.insert(insertPos, QLatin1String("\n")); changes.insert(insertPos, QLatin1String("\n"));
changes.copy(declSpecifiersStart, declSpecifiersEnd, insertPos); changes.copy(declSpecifiersStart, declSpecifiersEnd, insertPos);
changes.insert(insertPos, QLatin1String(" ")); changes.insert(insertPos, QLatin1String(" "));
changes.move(currentFile->range(declarator), insertPos); changes.move(currentFile()->range(declarator), insertPos);
changes.insert(insertPos, QLatin1String(";")); changes.insert(insertPos, QLatin1String(";"));
const int prevDeclEnd = currentFile->endOf(prevDeclarator); const int prevDeclEnd = currentFile()->endOf(prevDeclarator);
changes.remove(prevDeclEnd, currentFile->startOf(declarator)); changes.remove(prevDeclEnd, currentFile()->startOf(declarator));
prevDeclarator = declarator; prevDeclarator = declarator;
} }
currentFile->setChangeSet(changes); currentFile()->setChangeSet(changes);
currentFile->apply(); currentFile()->apply();
} }
private: private: