From aeb2cfbc4fe7685550fb6b10ef7146c98c19e821 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh <orgad.shaneh@audiocodes.com> Date: Wed, 26 Mar 2014 18:54:04 +0200 Subject: [PATCH] CppTools: Fix indentation for concatenated strings Change-Id: I801a56804e7873be75a9ebc085dc3f4a2d64ea35 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com> --- src/plugins/cpptools/cppcodeformatter.cpp | 15 +++++++++++++++ src/plugins/cpptools/cppcodeformatter.h | 3 ++- .../codeformatter/tst_codeformatter.cpp | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/plugins/cpptools/cppcodeformatter.cpp b/src/plugins/cpptools/cppcodeformatter.cpp index c2ac2fb88d8..9ee2fa8815c 100644 --- a/src/plugins/cpptools/cppcodeformatter.cpp +++ b/src/plugins/cpptools/cppcodeformatter.cpp @@ -564,6 +564,13 @@ void CodeFormatter::recalculateStateAfter(const QTextBlock &block) case cpp_macro_cont: break; + case string_open: + if (!m_currentToken.isStringLiteral()) { + leave(); + continue; + } + break; + default: qWarning() << "Unhandled state" << m_currentState.top().type; break; @@ -819,6 +826,9 @@ bool CodeFormatter::tryExpression(bool alsoExpression) break; } + if (m_currentToken.isStringLiteral()) + newState = string_open; + if (newState != -1) { if (alsoExpression) enter(expression); @@ -1425,6 +1435,11 @@ void QtStyleCodeFormatter::onEnter(int newState, int *indentDepth, int *savedInd case cpp_macro_cont: *indentDepth = m_tabSettings.m_indentSize; break; + + case string_open: + *indentDepth = tokenPosition; + *paddingDepth = 0; + break; } // ensure padding and indent are >= 0 diff --git a/src/plugins/cpptools/cppcodeformatter.h b/src/plugins/cpptools/cppcodeformatter.h index a19e688e58d..c36a4f0df59 100644 --- a/src/plugins/cpptools/cppcodeformatter.h +++ b/src/plugins/cpptools/cppcodeformatter.h @@ -180,7 +180,8 @@ public: // must be public to make Q_GADGET introspection work lambda_statement_expected, lambda_instroducer, // when '=', '&' or ',' occurred within '[]' lambda_declarator, // just after ']' when previous state is lambda_introducer - lambda_statement // just after '{' when previous state is lambda_declarator or lambda_declarator_or_expression + lambda_statement, // just after '{' when previous state is lambda_declarator or lambda_declarator_or_expression + string_open }; Q_ENUMS(StateType) diff --git a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp index fadfccfdff8..891d26d6969 100644 --- a/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp +++ b/tests/auto/cplusplus/codeformatter/tst_codeformatter.cpp @@ -128,6 +128,7 @@ private Q_SLOTS: void attributeInAccessSpecifier(); void braceReturn(); void staticVarDeclWithTypeDecl(); + void strings(); }; struct Line { @@ -2154,6 +2155,23 @@ void tst_CodeFormatter::staticVarDeclWithTypeDecl() checkIndent(data); } +void tst_CodeFormatter::strings() +{ + QList<Line> data; + data << Line("char *a = \"foo\"") + << Line(" \"bar\" \"why\"") + << Line(" \"baz\";") + << Line("void foo()") + << Line("{") + << Line(" func(1, 2, \"foo\"") + << Line(" \"bar\"") + << Line(" \"baz\", 4,") + << Line(" ~ 5, 6);") + << Line("}") + ; + checkIndent(data); +} + QTEST_MAIN(tst_CodeFormatter) #include "tst_codeformatter.moc" -- GitLab