forked from qt-creator/qt-creator
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 <orgads@gmail.com>
This commit is contained in:
10
src/libs/3rdparty/cplusplus/Token.h
vendored
10
src/libs/3rdparty/cplusplus/Token.h
vendored
@@ -407,6 +407,16 @@ struct LanguageFeatures
|
|||||||
return features;
|
return features;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool operator==(const LanguageFeatures &other) const
|
||||||
|
{
|
||||||
|
return flags == other.flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const LanguageFeatures &other) const
|
||||||
|
{
|
||||||
|
return flags != other.flags;
|
||||||
|
}
|
||||||
|
|
||||||
union {
|
union {
|
||||||
unsigned int flags;
|
unsigned int flags;
|
||||||
struct {
|
struct {
|
||||||
|
|||||||
@@ -401,7 +401,15 @@ CppTools::BaseEditorDocumentProcessor *CppEditorDocument::processor()
|
|||||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated,
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::ifdefedOutBlocksUpdated,
|
||||||
this, &CppEditorDocument::ifdefedOutBlocksUpdated);
|
this, &CppEditorDocument::ifdefedOutBlocksUpdated);
|
||||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::cppDocumentUpdated,
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::cppDocumentUpdated,
|
||||||
this, &CppEditorDocument::cppDocumentUpdated);
|
[this](const CPlusPlus::Document::Ptr document) {
|
||||||
|
// Update syntax highlighter
|
||||||
|
auto *highlighter = qobject_cast<CppHighlighter *>(syntaxHighlighter());
|
||||||
|
highlighter->setLanguageFeatures(document->languageFeatures());
|
||||||
|
|
||||||
|
// Forward signal
|
||||||
|
emit cppDocumentUpdated(document);
|
||||||
|
|
||||||
|
});
|
||||||
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::semanticInfoUpdated,
|
connect(m_processor.data(), &CppTools::BaseEditorDocumentProcessor::semanticInfoUpdated,
|
||||||
this, &CppEditorDocument::semanticInfoUpdated);
|
this, &CppEditorDocument::semanticInfoUpdated);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,15 +70,8 @@ void CppHighlighter::highlightBlock(const QString &text)
|
|||||||
|
|
||||||
int braceDepth = initialBraceDepth;
|
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;
|
SimpleLexer tokenize;
|
||||||
tokenize.setLanguageFeatures(features);
|
tokenize.setLanguageFeatures(m_languageFeatures);
|
||||||
|
|
||||||
int initialLexerState = lexerState;
|
int initialLexerState = lexerState;
|
||||||
const Tokens tokens = tokenize(text, initialLexerState);
|
const Tokens tokens = tokenize(text, initialLexerState);
|
||||||
@@ -215,7 +208,8 @@ void CppHighlighter::highlightBlock(const QString &text)
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (tk.isKeyword()
|
} 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()) {
|
|| tk.isObjCAtKeyword()) {
|
||||||
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat));
|
setFormat(tk.utf16charsBegin(), tk.utf16chars(), formatForCategory(CppKeywordFormat));
|
||||||
} else if (tk.isPrimitiveType()) {
|
} else if (tk.isPrimitiveType()) {
|
||||||
@@ -281,6 +275,13 @@ void CppHighlighter::highlightBlock(const QString &text)
|
|||||||
setCurrentBlockState((braceDepth << 8) | tokenize.state());
|
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
|
bool CppHighlighter::isPPKeyword(const QStringRef &text) const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -27,6 +27,8 @@
|
|||||||
|
|
||||||
#include <texteditor/syntaxhighlighter.h>
|
#include <texteditor/syntaxhighlighter.h>
|
||||||
|
|
||||||
|
#include <cplusplus/Token.h>
|
||||||
|
|
||||||
#include <QTextCharFormat>
|
#include <QTextCharFormat>
|
||||||
|
|
||||||
namespace CppEditor {
|
namespace CppEditor {
|
||||||
@@ -42,7 +44,8 @@ class CppHighlighter : public TextEditor::SyntaxHighlighter
|
|||||||
public:
|
public:
|
||||||
CppHighlighter(QTextDocument *document = 0);
|
CppHighlighter(QTextDocument *document = 0);
|
||||||
|
|
||||||
virtual void highlightBlock(const QString &text);
|
void setLanguageFeatures(const CPlusPlus::LanguageFeatures &languageFeatures);
|
||||||
|
void highlightBlock(const QString &text) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void highlightWord(QStringRef word, int position, int length);
|
void highlightWord(QStringRef word, int position, int length);
|
||||||
@@ -53,6 +56,9 @@ private:
|
|||||||
int length);
|
int length);
|
||||||
|
|
||||||
bool isPPKeyword(const QStringRef &text) const;
|
bool isPPKeyword(const QStringRef &text) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CPlusPlus::LanguageFeatures m_languageFeatures = CPlusPlus::LanguageFeatures::defaultFeatures();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
|
|||||||
Reference in New Issue
Block a user