From 837cb2e3974b4b8d157be8234adcd11b226a7caf Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Mon, 16 Jan 2017 10:51:13 +0100 Subject: CppEditor: Do not highlight C++ and Qt keywords for C Task-number: QTCREATORBUG-2818 Task-number: QTCREATORBUG-18004 Change-Id: Ibca60a1711c827dd8b4c0486bff3d49b19a61d8b Reviewed-by: Orgad Shaneh --- src/libs/3rdparty/cplusplus/Token.h | 10 ++++++++++ src/plugins/cppeditor/cppeditordocument.cpp | 10 +++++++++- src/plugins/cppeditor/cpphighlighter.cpp | 19 ++++++++++--------- src/plugins/cppeditor/cpphighlighter.h | 8 +++++++- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/libs/3rdparty/cplusplus/Token.h b/src/libs/3rdparty/cplusplus/Token.h index c460f59582..7cf9fca3f3 100644 --- a/src/libs/3rdparty/cplusplus/Token.h +++ b/src/libs/3rdparty/cplusplus/Token.h @@ -407,6 +407,16 @@ struct LanguageFeatures return features; } + bool operator==(const LanguageFeatures &other) const + { + return flags == other.flags; + } + + bool operator!=(const LanguageFeatures &other) const + { + return flags != other.flags; + } + union { unsigned int flags; struct { diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index 3ae653dd95..44b06ac846 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -401,7 +401,15 @@ CppTools::BaseEditorDocumentProcessor *CppEditorDocument::processor() connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated, this, &CppEditorDocument::ifdefedOutBlocksUpdated); connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::cppDocumentUpdated, - this, &CppEditorDocument::cppDocumentUpdated); + [this](const CPlusPlus::Document::Ptr document) { + // Update syntax highlighter + auto *highlighter = qobject_cast(syntaxHighlighter()); + highlighter->setLanguageFeatures(document->languageFeatures()); + + // Forward signal + emit cppDocumentUpdated(document); + + }); connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::semanticInfoUpdated, this, &CppEditorDocument::semanticInfoUpdated); } diff --git a/src/plugins/cppeditor/cpphighlighter.cpp b/src/plugins/cppeditor/cpphighlighter.cpp index 1912f30ff1..f5d7f104f4 100644 --- a/src/plugins/cppeditor/cpphighlighter.cpp +++ b/src/plugins/cppeditor/cpphighlighter.cpp @@ -70,15 +70,8 @@ void CppHighlighter::highlightBlock(const QString &text) int braceDepth = initialBraceDepth; - // FIXME: Check defaults or get from document. - LanguageFeatures features; - features.cxx11Enabled = true; - features.cxxEnabled = true; - features.c99Enabled = true; - features.objCEnabled = true; - SimpleLexer tokenize; - tokenize.setLanguageFeatures(features); + tokenize.setLanguageFeatures(m_languageFeatures); int initialLexerState = lexerState; const Tokens tokens = tokenize(text, initialLexerState); @@ -215,7 +208,8 @@ void CppHighlighter::highlightBlock(const QString &text) } } else if (tk.isKeyword() - || CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars())) + || (m_languageFeatures.qtKeywordsEnabled + && CppTools::isQtKeyword(text.midRef(tk.utf16charsBegin(), tk.utf16chars()))) || tk.isObjCAtKeyword()) { setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat)); } else if (tk.isPrimitiveType()) { @@ -281,6 +275,13 @@ void CppHighlighter::highlightBlock(const QString &text) setCurrentBlockState((braceDepth << 8) | tokenize.state()); } +void CppHighlighter::setLanguageFeatures(const LanguageFeatures &languageFeatures) +{ + if (languageFeatures != m_languageFeatures) { + m_languageFeatures = languageFeatures; + rehighlight(); + } +} bool CppHighlighter::isPPKeyword(const QStringRef &text) const { diff --git a/src/plugins/cppeditor/cpphighlighter.h b/src/plugins/cppeditor/cpphighlighter.h index fd9ba578a5..a67336c77c 100644 --- a/src/plugins/cppeditor/cpphighlighter.h +++ b/src/plugins/cppeditor/cpphighlighter.h @@ -27,6 +27,8 @@ #include +#include + #include namespace CppEditor { @@ -42,7 +44,8 @@ class CppHighlighter : public TextEditor::SyntaxHighlighter public: CppHighlighter(QTextDocument *document = 0); - virtual void highlightBlock(const QString &text); + void setLanguageFeatures(const CPlusPlus::LanguageFeatures &languageFeatures); + void highlightBlock(const QString &text) override; private: void highlightWord(QStringRef word, int position, int length); @@ -53,6 +56,9 @@ private: int length); bool isPPKeyword(const QStringRef &text) const; + +private: + CPlusPlus::LanguageFeatures m_languageFeatures = CPlusPlus::LanguageFeatures::defaultFeatures(); }; } // namespace Internal -- cgit v1.2.3