CppEditor: Fix highlighting non-plain character literals

Only string literals were considered so far. Properly consider character
literals as well.

Fixes: QTCREATORBUG-31342
Change-Id: I880546c1384f48ba9f81a6c6716e50fb7781cc11
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
Christian Kandeler
2024-08-01 15:24:44 +02:00
parent 1977d1fa19
commit 908da97d2d
2 changed files with 19 additions and 3 deletions

View File

@@ -439,6 +439,9 @@ void CppHighlighter::highlightStringLiteral(QStringView text, const CPlusPlus::T
case T_UTF8_STRING_LITERAL:
case T_UTF16_STRING_LITERAL:
case T_UTF32_STRING_LITERAL:
case T_WIDE_CHAR_LITERAL:
case T_UTF16_CHAR_LITERAL:
case T_UTF32_CHAR_LITERAL:
break;
default:
if (!tk.userDefinedLiteral()) { // Simple case: No prefix, no suffix.
@@ -448,16 +451,17 @@ void CppHighlighter::highlightStringLiteral(QStringView text, const CPlusPlus::T
}
}
const char quote = tk.isStringLiteral() ? '"' : '\'';
int stringOffset = 0;
if (!tk.f.joined) {
stringOffset = text.indexOf('"', tk.utf16charsBegin());
stringOffset = text.indexOf(quote, tk.utf16charsBegin());
QTC_ASSERT(stringOffset > 0, return);
setFormat(tk.utf16charsBegin(), stringOffset - tk.utf16charsBegin(),
formatForCategory(C_KEYWORD));
}
int operatorOffset = tk.utf16charsBegin() + tk.utf16chars();
if (tk.userDefinedLiteral()) {
const int closingQuoteOffset = text.lastIndexOf('"', operatorOffset);
const int closingQuoteOffset = text.lastIndexOf(quote, operatorOffset);
QTC_ASSERT(closingQuoteOffset >= tk.utf16charsBegin(), return);
operatorOffset = closingQuoteOffset + 1;
}
@@ -465,7 +469,10 @@ void CppHighlighter::highlightStringLiteral(QStringView text, const CPlusPlus::T
formatForCategory(C_STRING));
if (const int operatorLength = tk.utf16charsBegin() + tk.utf16chars() - operatorOffset;
operatorLength > 0) {
setFormat(operatorOffset, operatorLength, formatForCategory(C_OPERATOR));
setFormat(
operatorOffset,
operatorLength,
formatForCategory(tk.userDefinedLiteral() ? C_OVERLOADED_OPERATOR : C_OPERATOR));
}
}
@@ -586,6 +593,12 @@ void CppHighlighterTest::test_data()
<< 49 << 1 << 49 << 1 << C_STRING;
QTest::newRow("multi-line raw string literal with consecutive closing parens (suffix)")
<< 49 << 2 << 49 << 3 << C_KEYWORD;
QTest::newRow("wide char literal with user-defined suffix (prefix)")
<< 73 << 16 << 73 << 16 << C_KEYWORD;
QTest::newRow("wide char literal with user-defined suffix (content)")
<< 73 << 17 << 73 << 18 << C_STRING;
QTest::newRow("wide char literal with user-defined suffix (suffix)")
<< 73 << 20 << 73 << 22 << C_OVERLOADED_OPERATOR;
}
void CppHighlighterTest::test()

View File

@@ -68,3 +68,6 @@ static void parenTest2()
}
}
wchar_t operator ""_wc(const wchar_t c) { return c; }
const auto c = L'c'_wc;