forked from qt-creator/qt-creator
CppEditor: Make "Add curly braces" quickfix more robust
Anchor the opening brace at the closing parenthesis, rather than at the statement. This way, we won't get troubled by macro weirdness in the statement part. Fixes: QTCREATORBUG-13921 Change-Id: I05af24d1642e6b62c78bb2f47a1ef0b1fea326d0 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -197,6 +197,8 @@ private slots:
|
||||
void test_quickfix_ExtractLiteralAsParameter_memberFunction_separateFiles();
|
||||
void test_quickfix_ExtractLiteralAsParameter_notTriggeringForInvalidCode();
|
||||
|
||||
void test_quickfix_addCurlyBraces();
|
||||
|
||||
void test_quickfix_InsertVirtualMethods_data();
|
||||
void test_quickfix_InsertVirtualMethods();
|
||||
void test_quickfix_InsertVirtualMethods_implementationFile();
|
||||
|
@@ -5417,6 +5417,31 @@ void CppEditorPlugin::test_quickfix_ExtractLiteralAsParameter_notTriggeringForIn
|
||||
QuickFixOperationTest(testDocuments, &factory);
|
||||
}
|
||||
|
||||
void CppEditorPlugin::test_quickfix_addCurlyBraces()
|
||||
{
|
||||
QList<QuickFixTestDocument::Ptr> testDocuments;
|
||||
const QByteArray original = R"delim(
|
||||
void MyObject::f()
|
||||
{
|
||||
@if (true)
|
||||
emit mySig();
|
||||
}
|
||||
)delim";
|
||||
const QByteArray expected = R"delim(
|
||||
void MyObject::f()
|
||||
{
|
||||
if (true) {
|
||||
emit mySig();
|
||||
}
|
||||
}
|
||||
)delim";
|
||||
|
||||
testDocuments << QuickFixTestDocument::create("file.cpp", original, expected);
|
||||
AddBracesToIf factory;
|
||||
QuickFixOperationTest(testDocuments, &factory);
|
||||
|
||||
}
|
||||
|
||||
void CppEditorPlugin::test_quickfix_ConvertQt4Connect_connectOutOfClass()
|
||||
{
|
||||
QByteArray prefix =
|
||||
|
@@ -691,7 +691,8 @@ namespace {
|
||||
class AddBracesToIfOp: public CppQuickFixOperation
|
||||
{
|
||||
public:
|
||||
AddBracesToIfOp(const CppQuickFixInterface &interface, int priority, StatementAST *statement)
|
||||
AddBracesToIfOp(const CppQuickFixInterface &interface, int priority,
|
||||
const IfStatementAST *statement)
|
||||
: CppQuickFixOperation(interface, priority)
|
||||
, _statement(statement)
|
||||
{
|
||||
@@ -705,10 +706,10 @@ public:
|
||||
|
||||
ChangeSet changes;
|
||||
|
||||
const int start = currentFile->endOf(_statement->firstToken() - 1);
|
||||
const int start = currentFile->endOf(_statement->rparen_token);
|
||||
changes.insert(start, QLatin1String(" {"));
|
||||
|
||||
const int end = currentFile->endOf(_statement->lastToken() - 1);
|
||||
const int end = currentFile->endOf(_statement->statement->lastToken() - 1);
|
||||
changes.insert(end, QLatin1String("\n}"));
|
||||
|
||||
currentFile->setChangeSet(changes);
|
||||
@@ -717,7 +718,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
StatementAST *_statement;
|
||||
const IfStatementAST * const _statement;
|
||||
};
|
||||
|
||||
} // anonymous namespace
|
||||
@@ -731,7 +732,7 @@ void AddBracesToIf::match(const CppQuickFixInterface &interface, QuickFixOperati
|
||||
IfStatementAST *ifStatement = path.at(index)->asIfStatement();
|
||||
if (ifStatement && interface.isCursorOn(ifStatement->if_token) && ifStatement->statement
|
||||
&& !ifStatement->statement->asCompoundStatement()) {
|
||||
result << new AddBracesToIfOp(interface, index, ifStatement->statement);
|
||||
result << new AddBracesToIfOp(interface, index, ifStatement);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -742,7 +743,7 @@ void AddBracesToIf::match(const CppQuickFixInterface &interface, QuickFixOperati
|
||||
if (ifStatement && ifStatement->statement
|
||||
&& interface.isCursorOn(ifStatement->statement)
|
||||
&& !ifStatement->statement->asCompoundStatement()) {
|
||||
result << new AddBracesToIfOp(interface, index, ifStatement->statement);
|
||||
result << new AddBracesToIfOp(interface, index, ifStatement);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user