Commit 30da7ad3 authored by Nikolai Kosjar's avatar Nikolai Kosjar

C++: Pass only reasonable ASTs to pointer declaration formatter

Change-Id: Ide829a8084d3fef79f252dc7724bd90ce8ebba04
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent c6a7fb8c
......@@ -102,6 +102,7 @@ private slots: // quickfix tests
void test_quickfix_GenerateGetterSetter_notTriggeringOnMemberFunction();
void test_quickfix_GenerateGetterSetter_notTriggeringOnMemberArray();
void test_quickfix_GenerateGetterSetter_notTriggeringWhenGetterOrSetterExist();
void test_quickfix_ReformatPointerDeclaration();
#endif // WITH_TESTS
private:
......
......@@ -43,6 +43,7 @@
#include <cppeditor/cppplugin.h>
#include <cppeditor/cppquickfixassistant.h>
#include <cppeditor/cppquickfix.h>
#include <cppeditor/cppquickfixes.h>
#include <extensionsystem/pluginmanager.h>
#include <texteditor/basetextdocument.h>
#include <texteditor/codeassist/basicproposalitemlistmodel.h>
......@@ -543,3 +544,14 @@ void CppPlugin::test_quickfix_GenerateGetterSetter_notTriggeringWhenGetterOrSett
GenerateGetterSetter factory(/*testMode=*/ true);
data.run(&factory, expected, /*changesExpected=*/ false);
}
/// Check: Just a basic test since the main functionality is tested in
/// cpppointerdeclarationformatter_test.cpp
void CppPlugin::test_quickfix_ReformatPointerDeclaration()
{
TestCase data("char@*s;");
QByteArray expected = "char *s;\n";
ReformatPointerDeclaration factory;
data.run(&factory, expected);
}
......@@ -1859,6 +1859,64 @@ private:
Utils::ChangeSet m_change;
};
/// Filter the results of ASTPath.
/// The resulting list contains the supported AST types only once.
/// For this, the results of ASTPath are iterated in reverse order.
class ReformatPointerDeclarationASTPathResultsFilter
{
public:
ReformatPointerDeclarationASTPathResultsFilter()
: m_hasSimpleDeclaration(false)
, m_hasFunctionDefinition(false)
, m_hasParameterDeclaration(false)
, m_hasIfStatement(false)
, m_hasWhileStatement(false)
, m_hasForStatement(false)
, m_hasForeachStatement(false) {}
QList<AST*> filter(const QList<AST*> &astPathList)
{
QList<AST*> filtered;
for (int i = astPathList.size() - 1; i >= 0; --i) {
AST *ast = astPathList.at(i);
if (! m_hasSimpleDeclaration && ast->asSimpleDeclaration()) {
m_hasSimpleDeclaration = true;
filtered.append(ast);
} else if (! m_hasFunctionDefinition && ast->asFunctionDefinition()) {
m_hasFunctionDefinition = true;
filtered.append(ast);
} else if (! m_hasParameterDeclaration && ast->asParameterDeclaration()) {
m_hasParameterDeclaration = true;
filtered.append(ast);
} else if (! m_hasIfStatement && ast->asIfStatement()) {
m_hasIfStatement = true;
filtered.append(ast);
} else if (! m_hasWhileStatement && ast->asWhileStatement()) {
m_hasWhileStatement = true;
filtered.append(ast);
} else if (! m_hasForStatement && ast->asForStatement()) {
m_hasForStatement = true;
filtered.append(ast);
} else if (! m_hasForeachStatement && ast->asForeachStatement()) {
m_hasForeachStatement = true;
filtered.append(ast);
}
}
return filtered;
}
private:
bool m_hasSimpleDeclaration;
bool m_hasFunctionDefinition;
bool m_hasParameterDeclaration;
bool m_hasIfStatement;
bool m_hasWhileStatement;
bool m_hasForStatement;
bool m_hasForeachStatement;
};
void ReformatPointerDeclaration::match(const CppQuickFixInterface &interface,
QuickFixOperations &result)
{
......@@ -1885,9 +1943,9 @@ void ReformatPointerDeclaration::match(const CppQuickFixInterface &interface,
new ReformatPointerDeclarationOp(interface, change)));
}
} else {
for (int index = path.size() - 1; index >= 0; --index) {
AST *ast = path.at(index);
const QList<AST *> suitableASTs
= ReformatPointerDeclarationASTPathResultsFilter().filter(path);
foreach (AST *ast, suitableASTs) {
change = formatter.format(ast);
if (! change.isEmpty()) {
result.append(QuickFixOperation::Ptr(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment