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_oo(CppCodeStyleSettings::currentProjectCodeStyleOverview())
, m_originalName(m_oo.prettyName(m_name))
, m_file(CppRefactoringChanges(snapshot()).cppFile(filePath()))
, m_file(interface.currentFile())
{
setDescription(Tr::tr("Assign to Local Variable"));
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -483,12 +483,11 @@ private:
void perform() override
{
CppRefactoringChanges refactoring(snapshot());
CppRefactoringFilePtr currentFile = refactoring.cppFile(filePath());
if (m_removeAllAtGlobalScope) {
removeAllUsingsAtGlobalScope(refactoring);
} else if (refactorFile(currentFile,
} else if (refactorFile(currentFile(),
refactoring.snapshot(),
currentFile->endOf(m_usingDirective),
currentFile()->endOf(m_usingDirective),
true)) {
processIncludes(refactoring, filePath());
}
@@ -505,7 +504,7 @@ private:
* @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
*/
bool refactorFile(CppRefactoringFilePtr &file,
bool refactorFile(const CppRefactoringFilePtr &file,
const Snapshot &snapshot,
int startSymbol,
bool removeUsing = false)

View File

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

View File

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