Add syntax highlight for primitive data types

Syntax highlight rules for keywords are changed to highlight control
keywords and primitive data types separately.

Change-Id: Ifb25be7a97b92589030aa190641320c233dc7f2d
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
Anton Kalmykov
2014-06-21 14:31:06 +04:00
committed by Nikolai Kosjar
parent 2982a763ac
commit 496cfdd21d
12 changed files with 52 additions and 24 deletions

View File

@@ -16,6 +16,7 @@
<style name="Field"/> <style name="Field"/>
<style name="Function"/> <style name="Function"/>
<style name="Keyword" foreground="#ffff55"/> <style name="Keyword" foreground="#ffff55"/>
<style name="PrimitiveType" foreground="#ffff55"/>
<style name="Label" foreground="#ffff55"/> <style name="Label" foreground="#ffff55"/>
<style name="LineNumber" foreground="#888888" background="#232323"/> <style name="LineNumber" foreground="#888888" background="#232323"/>
<style name="Link" foreground="#0055ff"/> <style name="Link" foreground="#0055ff"/>

View File

@@ -16,6 +16,7 @@
<style name="Field"/> <style name="Field"/>
<style name="Function"/> <style name="Function"/>
<style name="Keyword" bold="true"/> <style name="Keyword" bold="true"/>
<style name="PrimitiveType" bold="true"/>
<style name="Label"/> <style name="Label"/>
<style name="LineNumber" foreground="#c7c4c1" background="#efebe7"/> <style name="LineNumber" foreground="#c7c4c1" background="#efebe7"/>
<style name="Link" foreground="#0000ff"/> <style name="Link" foreground="#0000ff"/>

View File

@@ -21,6 +21,7 @@
<style name="Field" bold="true"/> <style name="Field" bold="true"/>
<style name="Function"/> <style name="Function"/>
<style name="Keyword" foreground="#808bed"/> <style name="Keyword" foreground="#808bed"/>
<style name="PrimitiveType" foreground="#808bed"/>
<style name="Label" foreground="#e76000"/> <style name="Label" foreground="#e76000"/>
<style name="LineNumber" foreground="#8b8bcd" background="#2e2e2e"/> <style name="LineNumber" foreground="#8b8bcd" background="#2e2e2e"/>
<style name="Link" foreground="#409090"/> <style name="Link" foreground="#409090"/>

View File

@@ -10,6 +10,7 @@
<style name="Field" foreground="#660e7a" bold="true"/> <style name="Field" foreground="#660e7a" bold="true"/>
<style name="Function" foreground="#000000"/> <style name="Function" foreground="#000000"/>
<style name="Keyword" foreground="#000080" bold="true"/> <style name="Keyword" foreground="#000080" bold="true"/>
<style name="PrimitiveType" foreground="#000080" bold="true"/>
<style name="Label" foreground="#800000" bold="true"/> <style name="Label" foreground="#800000" bold="true"/>
<style name="Local" foreground="#000000"/> <style name="Local" foreground="#000000"/>
<style name="Number" foreground="#0000ff"/> <style name="Number" foreground="#0000ff"/>

View File

@@ -46,21 +46,20 @@ const char *token_names[] = {
("|="), ("||"), ("+"), ("+="), ("++"), ("#"), ("##"), ("?"), ("}"), ("|="), ("||"), ("+"), ("+="), ("++"), ("#"), ("##"), ("?"), ("}"),
("]"), (")"), (";"), ("*"), ("*="), ("~"), ("~="), ("]"), (")"), (";"), ("*"), ("*="), ("~"), ("~="),
("alignas"), ("alignof"), ("asm"), ("auto"), ("bool"), ("break"), ("case"), ("catch"), ("alignas"), ("alignof"), ("asm"), ("auto"), ("break"), ("case"), ("catch"),
("char"), ("char16_t"), ("char32_t"),
("class"), ("const"), ("const_cast"), ("constexpr"), ("continue"), ("class"), ("const"), ("const_cast"), ("constexpr"), ("continue"),
("decltype"), ("default"), ("decltype"), ("default"),
("delete"), ("do"), ("double"), ("dynamic_cast"), ("else"), ("enum"), ("delete"), ("do"), ("dynamic_cast"), ("else"), ("enum"),
("explicit"), ("export"), ("extern"), ("false"), ("float"), ("for"), ("explicit"), ("export"), ("extern"), ("false"), ("for"),
("friend"), ("goto"), ("if"), ("inline"), ("int"), ("long"), ("friend"), ("goto"), ("if"), ("inline"),
("mutable"), ("namespace"), ("new"), ("noexcept"), ("mutable"), ("namespace"), ("new"), ("noexcept"),
("nullptr"), ("operator"), ("private"), ("nullptr"), ("operator"), ("private"),
("protected"), ("public"), ("register"), ("reinterpret_cast"), ("protected"), ("public"), ("register"), ("reinterpret_cast"),
("return"), ("short"), ("signed"), ("sizeof"), ("static"),("static_assert"), ("return"), ("sizeof"), ("static"), ("static_assert"),
("static_cast"), ("struct"), ("switch"), ("template"), ("this"), ("thread_local"), ("static_cast"), ("struct"), ("switch"), ("template"), ("this"), ("thread_local"),
("throw"), ("true"), ("try"), ("typedef"), ("typeid"), ("typename"), ("throw"), ("true"), ("try"), ("typedef"), ("typeid"), ("typename"),
("union"), ("unsigned"), ("using"), ("virtual"), ("void"), ("union"), ("using"), ("virtual"),
("volatile"), ("wchar_t"), ("while"), ("volatile"), ("while"),
// gnu // gnu
("__attribute__"), ("__thread"), ("__typeof__"), ("__attribute__"), ("__thread"), ("__typeof__"),
@@ -72,6 +71,10 @@ const char *token_names[] = {
("@protected"), ("@protocol"), ("@public"), ("@required"), ("@selector"), ("@protected"), ("@protocol"), ("@public"), ("@required"), ("@selector"),
("@synchronized"), ("@synthesize"), ("@throw"), ("@try"), ("@synchronized"), ("@synthesize"), ("@throw"), ("@try"),
// Primitive types
("bool"), ("char"), ("char16_t"), ("char32_t"), ("double"), ("float"), ("int"),
("long"), ("short"), ("signed"), ("unsigned"), ("void"), ("wchar_t"),
// Qt keywords // Qt keywords
("emit"), ("SIGNAL"), ("SLOT"), ("Q_SIGNAL"), ("Q_SLOT"), ("signals"), ("slots"), ("emit"), ("SIGNAL"), ("SLOT"), ("Q_SIGNAL"), ("Q_SLOT"), ("signals"), ("slots"),
("Q_FOREACH"), ("Q_D"), ("Q_Q"), ("Q_FOREACH"), ("Q_D"), ("Q_Q"),

View File

@@ -119,13 +119,9 @@ enum Kind {
T_ALIGNOF, T_ALIGNOF,
T_ASM, T_ASM,
T_AUTO, T_AUTO,
T_BOOL,
T_BREAK, T_BREAK,
T_CASE, T_CASE,
T_CATCH, T_CATCH,
T_CHAR,
T_CHAR16_T,
T_CHAR32_T,
T_CLASS, T_CLASS,
T_CONST, T_CONST,
T_CONST_CAST, T_CONST_CAST,
@@ -135,7 +131,6 @@ enum Kind {
T_DEFAULT, T_DEFAULT,
T_DELETE, T_DELETE,
T_DO, T_DO,
T_DOUBLE,
T_DYNAMIC_CAST, T_DYNAMIC_CAST,
T_ELSE, T_ELSE,
T_ENUM, T_ENUM,
@@ -143,14 +138,11 @@ enum Kind {
T_EXPORT, T_EXPORT,
T_EXTERN, T_EXTERN,
T_FALSE, T_FALSE,
T_FLOAT,
T_FOR, T_FOR,
T_FRIEND, T_FRIEND,
T_GOTO, T_GOTO,
T_IF, T_IF,
T_INLINE, T_INLINE,
T_INT,
T_LONG,
T_MUTABLE, T_MUTABLE,
T_NAMESPACE, T_NAMESPACE,
T_NEW, T_NEW,
@@ -163,8 +155,6 @@ enum Kind {
T_REGISTER, T_REGISTER,
T_REINTERPRET_CAST, T_REINTERPRET_CAST,
T_RETURN, T_RETURN,
T_SHORT,
T_SIGNED,
T_SIZEOF, T_SIZEOF,
T_STATIC, T_STATIC,
T_STATIC_ASSERT, T_STATIC_ASSERT,
@@ -181,12 +171,9 @@ enum Kind {
T_TYPEID, T_TYPEID,
T_TYPENAME, T_TYPENAME,
T_UNION, T_UNION,
T_UNSIGNED,
T_USING, T_USING,
T_VIRTUAL, T_VIRTUAL,
T_VOID,
T_VOLATILE, T_VOLATILE,
T_WCHAR_T,
T_WHILE, T_WHILE,
T___ATTRIBUTE__, T___ATTRIBUTE__,
@@ -223,6 +210,23 @@ enum Kind {
T_LAST_OBJC_AT_KEYWORD = T_AT_TRY, T_LAST_OBJC_AT_KEYWORD = T_AT_TRY,
// Primitive types
T_FIRST_PRIMITIVE,
T_BOOL = T_FIRST_PRIMITIVE,
T_CHAR,
T_CHAR16_T,
T_CHAR32_T,
T_DOUBLE,
T_FLOAT,
T_INT,
T_LONG,
T_SHORT,
T_SIGNED,
T_UNSIGNED,
T_VOID,
T_WCHAR_T,
T_LAST_PRIMITIVE = T_WCHAR_T,
T_FIRST_QT_KEYWORD, T_FIRST_QT_KEYWORD,
// Qt keywords // Qt keywords
@@ -247,6 +251,7 @@ enum Kind {
T_Q_DECLARE_INTERFACE, T_Q_DECLARE_INTERFACE,
T_Q_OBJECT, T_Q_OBJECT,
T_Q_GADGET, T_Q_GADGET,
T_LAST_KEYWORD = T_Q_GADGET, T_LAST_KEYWORD = T_Q_GADGET,
// aliases // aliases
@@ -320,7 +325,10 @@ public:
{ return f.kind >= T_FIRST_OPERATOR && f.kind <= T_LAST_OPERATOR; } { return f.kind >= T_FIRST_OPERATOR && f.kind <= T_LAST_OPERATOR; }
inline bool isKeyword() const inline bool isKeyword() const
{ return f.kind >= T_FIRST_KEYWORD && f.kind < T_FIRST_QT_KEYWORD; } { return f.kind >= T_FIRST_KEYWORD && f.kind < T_FIRST_PRIMITIVE; }
inline bool isPrimitiveType() const
{ return f.kind >= T_FIRST_PRIMITIVE && f.kind <= T_LAST_PRIMITIVE; }
inline bool isComment() const inline bool isComment() const
{ return f.kind == T_COMMENT || f.kind == T_DOXY_COMMENT || { return f.kind == T_COMMENT || f.kind == T_DOXY_COMMENT ||

View File

@@ -43,6 +43,7 @@ enum CppFormats {
CppStringFormat, CppStringFormat,
CppTypeFormat, CppTypeFormat,
CppKeywordFormat, CppKeywordFormat,
CppPrimitiveTypeFormat,
CppOperatorFormat, CppOperatorFormat,
CppPreprocessorFormat, CppPreprocessorFormat,
CppLabelFormat, CppLabelFormat,

View File

@@ -52,6 +52,7 @@ CppHighlighter::CppHighlighter(QTextDocument *document) :
<< TextEditor::C_STRING << TextEditor::C_STRING
<< TextEditor::C_TYPE << TextEditor::C_TYPE
<< TextEditor::C_KEYWORD << TextEditor::C_KEYWORD
<< TextEditor::C_PRIMITIVE_TYPE
<< TextEditor::C_OPERATOR << TextEditor::C_OPERATOR
<< TextEditor::C_PREPROCESSOR << TextEditor::C_PREPROCESSOR
<< TextEditor::C_LABEL << TextEditor::C_LABEL
@@ -220,6 +221,9 @@ void CppHighlighter::highlightBlock(const QString &text)
|| CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars())) || CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))
|| tk.isObjCAtKeyword()) { || tk.isObjCAtKeyword()) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat)); setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat));
} else if (tk.isPrimitiveType()) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(),
formatForCategory(CppPrimitiveTypeFormat));
} else if (tk.isOperator()) { } else if (tk.isOperator()) {
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppOperatorFormat)); setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppOperatorFormat));
} else if (i == 0 && tokens.size() > 1 && tk.is(T_IDENTIFIER) && tokens.at(1).is(T_COLON)) { } else if (i == 0 && tokens.size() > 1 && tk.is(T_IDENTIFIER) && tokens.at(1).is(T_COLON)) {

View File

@@ -1727,6 +1727,10 @@ void CppCompletionAssistProcessor::addKeywords()
// keyword completion items. // keyword completion items.
for (int i = T_FIRST_KEYWORD; i < keywordLimit; ++i) for (int i = T_FIRST_KEYWORD; i < keywordLimit; ++i)
addCompletionItem(QLatin1String(Token::name(i)), m_icons.keywordIcon(), KeywordsOrder); addCompletionItem(QLatin1String(Token::name(i)), m_icons.keywordIcon(), KeywordsOrder);
// primitive type completion items.
for (int i = T_FIRST_PRIMITIVE; i <= T_LAST_PRIMITIVE; ++i)
addCompletionItem(QLatin1String(Token::name(i)), m_icons.keywordIcon(), KeywordsOrder);
} }
void CppCompletionAssistProcessor::addMacros(const QString &fileName, const CPlusPlus::Snapshot &snapshot) void CppCompletionAssistProcessor::addMacros(const QString &fileName, const CPlusPlus::Snapshot &snapshot)

View File

@@ -62,6 +62,7 @@ const char *nameForStyle(TextStyle style)
case C_VIRTUAL_METHOD: return "VirtualMethod"; case C_VIRTUAL_METHOD: return "VirtualMethod";
case C_FUNCTION: return "Function"; case C_FUNCTION: return "Function";
case C_KEYWORD: return "Keyword"; case C_KEYWORD: return "Keyword";
case C_PRIMITIVE_TYPE: return "PrimitiveType";
case C_OPERATOR: return "Operator"; case C_OPERATOR: return "Operator";
case C_PREPROCESSOR: return "Preprocessor"; case C_PREPROCESSOR: return "Preprocessor";
case C_LABEL: return "Label"; case C_LABEL: return "Label";

View File

@@ -59,6 +59,7 @@ enum TextStyle {
C_VIRTUAL_METHOD, C_VIRTUAL_METHOD,
C_FUNCTION, C_FUNCTION,
C_KEYWORD, C_KEYWORD,
C_PRIMITIVE_TYPE,
C_OPERATOR, C_OPERATOR,
C_PREPROCESSOR, C_PREPROCESSOR,
C_LABEL, C_LABEL,

View File

@@ -139,6 +139,8 @@ TextEditorSettings::TextEditorSettings(QObject *parent)
Qt::darkBlue)); Qt::darkBlue));
formatDescr.append(FormatDescription(C_STRING, tr("String"), formatDescr.append(FormatDescription(C_STRING, tr("String"),
tr("Character and string literals."), Qt::darkGreen)); tr("Character and string literals."), Qt::darkGreen));
formatDescr.append(FormatDescription(C_PRIMITIVE_TYPE, tr("Primitive Type"),
tr("Primitive data types"), Qt::darkYellow));
formatDescr.append(FormatDescription(C_TYPE, tr("Type"), tr("Name of a type."), formatDescr.append(FormatDescription(C_TYPE, tr("Type"), tr("Name of a type."),
Qt::darkMagenta)); Qt::darkMagenta));
formatDescr.append(FormatDescription(C_LOCAL, tr("Local"), tr("Local variables."))); formatDescr.append(FormatDescription(C_LOCAL, tr("Local"), tr("Local variables.")));
@@ -204,8 +206,8 @@ TextEditorSettings::TextEditorSettings(QObject *parent)
jsGlobalFormat)); jsGlobalFormat));
formatDescr.append(FormatDescription(C_KEYWORD, tr("Keyword"), formatDescr.append(FormatDescription(C_KEYWORD, tr("Keyword"),
tr("Reserved keywords of the programming language."), tr("Reserved keywords of the programming language except "
Qt::darkYellow)); "keywords denoting primitive types."), Qt::darkYellow));
formatDescr.append(FormatDescription(C_OPERATOR, tr("Operator"), formatDescr.append(FormatDescription(C_OPERATOR, tr("Operator"),
tr("Operators (for example operator++ or operator-=)."))); tr("Operators (for example operator++ or operator-=).")));
formatDescr.append(FormatDescription(C_PREPROCESSOR, tr("Preprocessor"), formatDescr.append(FormatDescription(C_PREPROCESSOR, tr("Preprocessor"),