Commit 2324db9d authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Fix html code in completion list item

E.g. "dynamic_cast&lt>()" showed up as item.

Let CompletionChunksToTextConverter default to plain text format and
explicitly request HTML where needed.

Change-Id: Iebce85cb888a5bd697ffdce364118b6dc65a435d
Reviewed-by: default avatarMarco Bubke <marco.bubke@theqtcompany.com>
parent a071e52b
...@@ -103,7 +103,8 @@ QList<AssistProposalItem *> toAssistProposalItems(const CodeCompletions &complet ...@@ -103,7 +103,8 @@ QList<AssistProposalItem *> toAssistProposalItems(const CodeCompletions &complet
items.insert(name, item); items.insert(name, item);
item->setText(name); item->setText(name);
item->setOrder(ccr.priority()); item->setOrder(ccr.priority());
QString detail = CompletionChunksToTextConverter::convertToToolTip(ccr.chunks()); QString detail
= CompletionChunksToTextConverter::convertToToolTipWithHtml(ccr.chunks());
if (!ccr.briefComment().isEmpty()) if (!ccr.briefComment().isEmpty())
detail += QStringLiteral("\n\n") + ccr.briefComment().toString(); detail += QStringLiteral("\n\n") + ccr.briefComment().toString();
......
...@@ -118,7 +118,7 @@ bool CompletionChunksToTextConverter::hasPlaceholderPositions() const ...@@ -118,7 +118,7 @@ bool CompletionChunksToTextConverter::hasPlaceholderPositions() const
return m_placeholderPositions.size() > 0; return m_placeholderPositions.size() > 0;
} }
QString CompletionChunksToTextConverter::convertToFunctionSignature( QString CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(
const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks, const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks,
int parameterToEmphasize) int parameterToEmphasize)
{ {
...@@ -126,6 +126,7 @@ QString CompletionChunksToTextConverter::convertToFunctionSignature( ...@@ -126,6 +126,7 @@ QString CompletionChunksToTextConverter::convertToFunctionSignature(
converter.setAddPlaceHolderText(true); converter.setAddPlaceHolderText(true);
converter.setAddResultType(true); converter.setAddResultType(true);
converter.setTextFormat(TextFormat::Html);
converter.setAddOptional(true); converter.setAddOptional(true);
converter.setEmphasizeOptional(true); converter.setEmphasizeOptional(true);
...@@ -147,7 +148,7 @@ QString CompletionChunksToTextConverter::convertToName( ...@@ -147,7 +148,7 @@ QString CompletionChunksToTextConverter::convertToName(
return converter.text(); return converter.text();
} }
QString CompletionChunksToTextConverter::convertToToolTip( QString CompletionChunksToTextConverter::convertToToolTipWithHtml(
const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks) const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks)
{ {
CompletionChunksToTextConverter converter; CompletionChunksToTextConverter converter;
...@@ -155,6 +156,7 @@ QString CompletionChunksToTextConverter::convertToToolTip( ...@@ -155,6 +156,7 @@ QString CompletionChunksToTextConverter::convertToToolTip(
converter.setAddSpaces(true); converter.setAddSpaces(true);
converter.setAddExtraVerticalSpaceBetweenBraces(true); converter.setAddExtraVerticalSpaceBetweenBraces(true);
converter.setAddOptional(true); converter.setAddOptional(true);
converter.setTextFormat(TextFormat::Html);
converter.setEmphasizeOptional(true); converter.setEmphasizeOptional(true);
converter.setAddResultType(true); converter.setAddResultType(true);
...@@ -189,7 +191,7 @@ void CompletionChunksToTextConverter::parseDependendOnTheOptionalState( ...@@ -189,7 +191,7 @@ void CompletionChunksToTextConverter::parseDependendOnTheOptionalState(
void CompletionChunksToTextConverter::parseResultType(const Utf8String &resultTypeText) void CompletionChunksToTextConverter::parseResultType(const Utf8String &resultTypeText)
{ {
if (m_addResultType) if (m_addResultType)
m_text += resultTypeText.toString().toHtmlEscaped() + QChar(QChar::Space); m_text += inDesiredTextFormat(resultTypeText) + QChar(QChar::Space);
} }
void CompletionChunksToTextConverter::parseText(const Utf8String &text) void CompletionChunksToTextConverter::parseText(const Utf8String &text)
...@@ -199,14 +201,14 @@ void CompletionChunksToTextConverter::parseText(const Utf8String &text) ...@@ -199,14 +201,14 @@ void CompletionChunksToTextConverter::parseText(const Utf8String &text)
m_text += QChar(QChar::Space); m_text += QChar(QChar::Space);
} }
m_text += text.toString().toHtmlEscaped(); m_text += inDesiredTextFormat(text);
} }
void CompletionChunksToTextConverter::wrapInCursiveTagIfOptional( void CompletionChunksToTextConverter::wrapInCursiveTagIfOptional(
const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk)
{ {
if (m_addOptional) { if (m_addOptional) {
if (m_emphasizeOptional) { if (m_emphasizeOptional && m_textFormat == TextFormat::Html) {
if (!m_previousCodeCompletionChunk.isOptional() && codeCompletionChunk.isOptional()) if (!m_previousCodeCompletionChunk.isOptional() && codeCompletionChunk.isOptional())
m_text += QStringLiteral("<i>"); m_text += QStringLiteral("<i>");
else if (m_previousCodeCompletionChunk.isOptional() && !codeCompletionChunk.isOptional()) else if (m_previousCodeCompletionChunk.isOptional() && !codeCompletionChunk.isOptional())
...@@ -219,7 +221,7 @@ void CompletionChunksToTextConverter::parsePlaceHolder( ...@@ -219,7 +221,7 @@ void CompletionChunksToTextConverter::parsePlaceHolder(
const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk)
{ {
if (m_addPlaceHolderText) { if (m_addPlaceHolderText) {
appendText(codeCompletionChunk.text().toString().toHtmlEscaped(), appendText(inDesiredTextFormat(codeCompletionChunk.text()),
emphasizeCurrentPlaceHolder()); emphasizeCurrentPlaceHolder());
} }
...@@ -294,6 +296,14 @@ void CompletionChunksToTextConverter::addExtraVerticalSpaceBetweenBraces( ...@@ -294,6 +296,14 @@ void CompletionChunksToTextConverter::addExtraVerticalSpaceBetweenBraces(
} }
} }
QString CompletionChunksToTextConverter::inDesiredTextFormat(const Utf8String &text)
{
if (m_textFormat == TextFormat::Html)
return text.toString().toHtmlEscaped();
else
return text.toString();
}
bool CompletionChunksToTextConverter::emphasizeCurrentPlaceHolder() const bool CompletionChunksToTextConverter::emphasizeCurrentPlaceHolder() const
{ {
if (m_addPlaceHolderPositions) { if (m_addPlaceHolderPositions) {
...@@ -304,9 +314,14 @@ bool CompletionChunksToTextConverter::emphasizeCurrentPlaceHolder() const ...@@ -304,9 +314,14 @@ bool CompletionChunksToTextConverter::emphasizeCurrentPlaceHolder() const
return false; return false;
} }
void CompletionChunksToTextConverter::setTextFormat(TextFormat textFormat)
{
m_textFormat = textFormat;
}
void CompletionChunksToTextConverter::appendText(const QString &text, bool boldFormat) void CompletionChunksToTextConverter::appendText(const QString &text, bool boldFormat)
{ {
if (boldFormat) if (boldFormat && m_textFormat == TextFormat::Html)
m_text += QStringLiteral("<b>") + text + QStringLiteral("</b>"); m_text += QStringLiteral("<b>") + text + QStringLiteral("</b>");
else else
m_text += text; m_text += text;
......
...@@ -47,12 +47,17 @@ class CompletionChunksToTextConverter ...@@ -47,12 +47,17 @@ class CompletionChunksToTextConverter
public: public:
void parseChunks(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks); void parseChunks(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks);
enum class TextFormat {
Plain,
Html
};
void setTextFormat(TextFormat textFormat);
void setAddPlaceHolderText(bool addPlaceHolderText); void setAddPlaceHolderText(bool addPlaceHolderText);
void setAddPlaceHolderPositions(bool addPlaceHolderPositions); void setAddPlaceHolderPositions(bool addPlaceHolderPositions);
void setAddResultType(bool addResultType); void setAddResultType(bool addResultType);
void setAddSpaces(bool addSpaces); void setAddSpaces(bool addSpaces);
void setAddExtraVerticalSpaceBetweenBraces(bool addExtraVerticalSpaceBetweenBraces); void setAddExtraVerticalSpaceBetweenBraces(bool addExtraVerticalSpaceBetweenBraces);
void setEmphasizeOptional(bool emphasizeOptional); void setEmphasizeOptional(bool emphasizeOptional); // Only for Html format
void setAddOptional(bool addOptional); void setAddOptional(bool addOptional);
void setPlaceHolderToEmphasize(int placeHolderNumber); void setPlaceHolderToEmphasize(int placeHolderNumber);
...@@ -62,10 +67,13 @@ public: ...@@ -62,10 +67,13 @@ public:
const std::vector<int> &placeholderPositions() const; const std::vector<int> &placeholderPositions() const;
bool hasPlaceholderPositions() const; bool hasPlaceholderPositions() const;
static QString convertToFunctionSignature(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks,
int parameterToEmphasize = -1);
static QString convertToName(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks); static QString convertToName(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks);
static QString convertToToolTip(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks); static QString convertToKeywords(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks);
static QString convertToToolTipWithHtml(const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks);
static QString convertToFunctionSignatureWithHtml(
const ClangBackEnd::CodeCompletionChunks &codeCompletionChunks,
int parameterToEmphasize = -1);
private: private:
void parse(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk); void parse(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk);
void parseDependendOnTheOptionalState(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk); void parseDependendOnTheOptionalState(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk);
...@@ -78,7 +86,8 @@ private: ...@@ -78,7 +86,8 @@ private:
void addExtraVerticalSpaceBetweenBraces(); void addExtraVerticalSpaceBetweenBraces();
void addExtraVerticalSpaceBetweenBraces(const ClangBackEnd::CodeCompletionChunks::iterator &); void addExtraVerticalSpaceBetweenBraces(const ClangBackEnd::CodeCompletionChunks::iterator &);
void appendText(const QString &text, bool boldFormat = false); QString inDesiredTextFormat(const Utf8String &text) const;
void appendText(const QString &text, bool boldFormat = false); // Boldness only in Html format
bool canAddSpace() const; bool canAddSpace() const;
bool isNotOptionalOrAddOptionals(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) const; bool isNotOptionalOrAddOptionals(const ClangBackEnd::CodeCompletionChunk &codeCompletionChunk) const;
...@@ -90,6 +99,7 @@ private: ...@@ -90,6 +99,7 @@ private:
ClangBackEnd::CodeCompletionChunk m_previousCodeCompletionChunk; ClangBackEnd::CodeCompletionChunk m_previousCodeCompletionChunk;
QString m_text; QString m_text;
int m_placeHolderPositionToEmphasize = -1; int m_placeHolderPositionToEmphasize = -1;
TextFormat m_textFormat = TextFormat::Plain;
bool m_addPlaceHolderText = false; bool m_addPlaceHolderText = false;
bool m_addPlaceHolderPositions = false; bool m_addPlaceHolderPositions = false;
bool m_addResultType = false; bool m_addResultType = false;
......
...@@ -58,7 +58,8 @@ QString ClangFunctionHintModel::text(int index) const ...@@ -58,7 +58,8 @@ QString ClangFunctionHintModel::text(int index) const
{ {
const ClangBackEnd::CodeCompletionChunks chunks = m_functionSymbols.at(index).chunks(); const ClangBackEnd::CodeCompletionChunks chunks = m_functionSymbols.at(index).chunks();
const QString signatureWithEmphasizedCurrentParameter const QString signatureWithEmphasizedCurrentParameter
= CompletionChunksToTextConverter::convertToFunctionSignature(chunks, m_currentArgument + 1); = CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(chunks,
m_currentArgument + 1);
return signatureWithEmphasizedCurrentParameter; return signatureWithEmphasizedCurrentParameter;
} }
......
...@@ -131,7 +131,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneArgumen ...@@ -131,7 +131,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneArgumen
using ClangCodeModel::Internal::CompletionChunksToTextConverter; using ClangCodeModel::Internal::CompletionChunksToTextConverter;
ASSERT_THAT(converter.convertToFunctionSignature(completionChunks), ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks),
QStringLiteral("int Function(char x)")); QStringLiteral("int Function(char x)"));
} }
...@@ -143,7 +143,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneParamet ...@@ -143,7 +143,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneParamet
functionArgumentX, functionArgumentX,
rightParen}); rightParen});
ASSERT_THAT(converter.convertToFunctionSignature(completionChunks, 1), ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 1),
QStringLiteral("int Function(<b>char x</b>)")); QStringLiteral("int Function(<b>char x</b>)"));
} }
...@@ -155,7 +155,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneParamet ...@@ -155,7 +155,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithOneParamet
functionArgumentX, functionArgumentX,
rightParen}); rightParen});
ASSERT_THAT(converter.convertToFunctionSignature(completionChunks, -1), ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, -1),
QStringLiteral("int Function(char x)")); QStringLiteral("int Function(char x)"));
} }
...@@ -169,7 +169,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParamet ...@@ -169,7 +169,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParamet
functionArgumentY, functionArgumentY,
rightParen}); rightParen});
ASSERT_THAT(converter.convertToFunctionSignature(completionChunks, 2), ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 2),
QStringLiteral("int Function(char x, <b>int y</b>)")); QStringLiteral("int Function(char x, <b>int y</b>)"));
} }
...@@ -183,7 +183,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParamet ...@@ -183,7 +183,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTwoParamet
optionalFunctionArgumentY, optionalFunctionArgumentY,
rightParen}); rightParen});
ASSERT_THAT(converter.convertToFunctionSignature(completionChunks, 2), ASSERT_THAT(converter.convertToFunctionSignatureWithHtml(completionChunks, 2),
QStringLiteral("int Function(char x<i>, <b>int y</b></i>)")); QStringLiteral("int Function(char x<i>, <b>int y</b></i>)"));
} }
...@@ -197,7 +197,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateRe ...@@ -197,7 +197,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateRe
using ClangCodeModel::Internal::CompletionChunksToTextConverter; using ClangCodeModel::Internal::CompletionChunksToTextConverter;
ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignature(completionChunks), ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(completionChunks),
QStringLiteral("Foo&lt;int&gt; Function(char x)")); QStringLiteral("Foo&lt;int&gt; Function(char x)"));
} }
...@@ -211,7 +211,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateAr ...@@ -211,7 +211,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertToFunctionSignatureWithTemplateAr
using ClangCodeModel::Internal::CompletionChunksToTextConverter; using ClangCodeModel::Internal::CompletionChunksToTextConverter;
ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignature(completionChunks), ASSERT_THAT(CompletionChunksToTextConverter::convertToFunctionSignatureWithHtml(completionChunks),
QStringLiteral("int Function(const Foo&lt;int&gt; &amp;foo)")); QStringLiteral("int Function(const Foo&lt;int&gt; &amp;foo)"));
} }
...@@ -227,7 +227,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertFunctionWithOptionalParameter) ...@@ -227,7 +227,7 @@ TEST_F(CompletionChunksToTextConverter, ConvertFunctionWithOptionalParameter)
optionalFunctionArgumentZ, optionalFunctionArgumentZ,
rightParen}); rightParen});
ASSERT_THAT(Converter::convertToToolTip(completionChunks), ASSERT_THAT(Converter::convertToToolTipWithHtml(completionChunks),
QStringLiteral("int Function (char x<i>, int y, int z</i>)")); QStringLiteral("int Function (char x<i>, int y, int z</i>)"));
} }
...@@ -310,7 +310,7 @@ TEST_F(CompletionChunksToTextConverter, const_cast) ...@@ -310,7 +310,7 @@ TEST_F(CompletionChunksToTextConverter, const_cast)
converter.parseChunks(completionChunks); converter.parseChunks(completionChunks);
ASSERT_THAT(converter.text(), QStringLiteral("const_cast&lt;&gt;()")); ASSERT_THAT(converter.text(), QStringLiteral("const_cast<>()"));
} }
TEST_F(CompletionChunksToTextConverter, Throw) TEST_F(CompletionChunksToTextConverter, Throw)
...@@ -352,7 +352,7 @@ TEST_F(CompletionChunksToTextConverter, EnableIfT) ...@@ -352,7 +352,7 @@ TEST_F(CompletionChunksToTextConverter, EnableIfT)
converter.parseChunks(completionChunks); converter.parseChunks(completionChunks);
ASSERT_THAT(converter.text(), QStringLiteral("enable_if_t&lt;&gt;")); ASSERT_THAT(converter.text(), QStringLiteral("enable_if_t<>"));
} }
} }
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