diff --git a/src/plugins/clangcodemodel/completionchunkstotextconverter.cpp b/src/plugins/clangcodemodel/completionchunkstotextconverter.cpp index c9342bdccd4..3c7c58c6a3c 100644 --- a/src/plugins/clangcodemodel/completionchunkstotextconverter.cpp +++ b/src/plugins/clangcodemodel/completionchunkstotextconverter.cpp @@ -136,9 +136,10 @@ void CompletionChunksToTextConverter::parseResultType(const Utf8String &resultTy void CompletionChunksToTextConverter::parseText(const Utf8String &text) { - if (m_addSpaces - && m_previousCodeCompletionChunk.kind() == ClangBackEnd::CodeCompletionChunk::RightBrace) + if (canAddSpace() + && m_previousCodeCompletionChunk.kind() == ClangBackEnd::CodeCompletionChunk::RightBrace) { m_text += QChar(QChar::Space); + } m_text += text.toString(); } @@ -163,7 +164,7 @@ void CompletionChunksToTextConverter::parsePlaceHolder(const ClangBackEnd::CodeC void CompletionChunksToTextConverter::parseLeftParen(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) { - if (m_addSpaces && m_previousCodeCompletionChunk.kind() != ClangBackEnd::CodeCompletionChunk::RightAngle) + if (canAddSpace()) m_text += QChar(QChar::Space); m_text += codeCompletionChunk.text().toString(); @@ -171,7 +172,7 @@ void CompletionChunksToTextConverter::parseLeftParen(const ClangBackEnd::CodeCom void CompletionChunksToTextConverter::parseLeftBrace(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) { - if (m_addSpaces) + if (canAddSpace()) m_text += QChar(QChar::Space); m_text += codeCompletionChunk.text().toString(); @@ -225,6 +226,13 @@ void CompletionChunksToTextConverter::addExtraVerticalSpaceBetweenBraces(const Q } } +bool CompletionChunksToTextConverter::canAddSpace() const +{ + return m_addSpaces + && m_previousCodeCompletionChunk.kind() != ClangBackEnd::CodeCompletionChunk::HorizontalSpace + && m_previousCodeCompletionChunk.kind() != ClangBackEnd::CodeCompletionChunk::RightAngle; +} + } // namespace Internal } // namespace ClangCodeModel diff --git a/src/plugins/clangcodemodel/completionchunkstotextconverter.h b/src/plugins/clangcodemodel/completionchunkstotextconverter.h index 9e2f6d5c6d4..32a9a040989 100644 --- a/src/plugins/clangcodemodel/completionchunkstotextconverter.h +++ b/src/plugins/clangcodemodel/completionchunkstotextconverter.h @@ -71,6 +71,8 @@ private: void addExtraVerticalSpaceBetweenBraces(); void addExtraVerticalSpaceBetweenBraces(const QVector::iterator &); + bool canAddSpace() const; + private: std::vector m_placeholderPositions; QVector m_codeCompletionChunks; diff --git a/tests/unit/unittest/completionchunkstotextconvertertest.cpp b/tests/unit/unittest/completionchunkstotextconvertertest.cpp index 0e51d63c735..bc2bb4219a3 100644 --- a/tests/unit/unittest/completionchunkstotextconvertertest.cpp +++ b/tests/unit/unittest/completionchunkstotextconvertertest.cpp @@ -76,6 +76,9 @@ protected: CodeCompletionChunk constCastName{CodeCompletionChunk::TypedText, Utf8StringLiteral("const_cast")}; CodeCompletionChunk leftAngle{CodeCompletionChunk::LeftAngle, Utf8StringLiteral("<")}; CodeCompletionChunk rightAngle{CodeCompletionChunk::RightAngle, Utf8StringLiteral(">")}; + CodeCompletionChunk elseName{CodeCompletionChunk::TypedText, Utf8StringLiteral("else")}; + CodeCompletionChunk ifName{CodeCompletionChunk::TypedText, Utf8StringLiteral("if")}; + CodeCompletionChunk horizontalSpace{CodeCompletionChunk::HorizontalSpace, Utf8StringLiteral(" ")}; CodeCompletionChunk optional{CodeCompletionChunk::Optional, Utf8String(), {comma, functionArgumentY, comma, functionArgumentZ}}; }; @@ -201,7 +204,6 @@ TEST_F(CompletionChunksToTextConverter, const_cast) converter.parseChunks(completionChunks); ASSERT_THAT(converter.text(), QStringLiteral("const_cast<>()")); - } TEST_F(CompletionChunksToTextConverter, Throw) @@ -213,6 +215,25 @@ TEST_F(CompletionChunksToTextConverter, Throw) ASSERT_THAT(completionName, QStringLiteral("throw")); } +TEST_F(CompletionChunksToTextConverter, ElseIf) +{ + QVector completionChunks({elseName, + horizontalSpace, + ifName, + horizontalSpace, + leftBrace, + verticalSpace, + statements, + verticalSpace, + rightBrace}); + + setupConverterForKeywords(); + + converter.parseChunks(completionChunks); + + ASSERT_THAT(converter.text(), QStringLiteral("else if {\n\n}")); +} + void CompletionChunksToTextConverter::setupConverterForKeywords() { converter.setAddPlaceHolderPositions(true);