Commit 85609aff authored by Francois Ferrand's avatar Francois Ferrand Committed by Leandro Melo

CppHighlighter: highlight macro references.

Change-Id: I7c90957aa67e03a109af0a722160d4e1c759d716
Reviewed-by: default avatarLeandro Melo <leandro.melo@nokia.com>
parent 85ce5aba
......@@ -1743,6 +1743,8 @@ void CPPEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_VIRTUAL_METHOD));
m_semanticHighlightFormatMap[SemanticInfo::LabelUse] =
fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_LABEL));
m_semanticHighlightFormatMap[SemanticInfo::MacroUse] =
fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_PREPROCESSOR));
m_keywordFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_KEYWORD));
// only set the background, we do not want to modify foreground properties set by the syntax highlighter or the link
......
......@@ -35,6 +35,7 @@
#include <Token.h>
#include <cplusplus/SimpleLexer.h>
#include <cpptools/cpptoolsreuse.h>
#include <texteditor/basetextdocumentlayout.h>
#include <QTextDocument>
......@@ -199,7 +200,7 @@ void CppHighlighter::highlightBlock(const QString &text)
initialState = 0;
}
} else if (tk.isKeyword() || isQtKeyword(text.midRef(tk.begin(), tk.length())) || tk.isObjCAtKeyword())
} else if (tk.isKeyword() || CppTools::isQtKeyword(text.midRef(tk.begin(), tk.length())) || tk.isObjCAtKeyword())
setFormat(tk.begin(), tk.length(), m_formats[CppKeywordFormat]);
else if (tk.isOperator())
......@@ -338,51 +339,6 @@ bool CppHighlighter::isPPKeyword(const QStringRef &text) const
return false;
}
bool CppHighlighter::isQtKeyword(const QStringRef &text) const
{
switch (text.length()) {
case 4:
switch (text.at(0).toLatin1()) {
case 'e':
if (text == QLatin1String("emit"))
return true;
break;
case 'S':
if (text == QLatin1String("SLOT"))
return true;
break;
}
break;
case 5:
if (text.at(0) == QLatin1Char('s') && text == QLatin1String("slots"))
return true;
break;
case 6:
if (text.at(0) == QLatin1Char('S') && text == QLatin1String("SIGNAL"))
return true;
break;
case 7:
switch (text.at(0).toLatin1()) {
case 's':
if (text == QLatin1String("signals"))
return true;
break;
case 'f':
if (text == QLatin1String("foreach") || text == QLatin1String("forever"))
return true;
break;
}
break;
default:
break;
}
return false;
}
void CppHighlighter::highlightLine(const QString &text, int position, int length,
const QTextCharFormat &format)
{
......
......@@ -68,7 +68,6 @@ private:
int length);
bool isPPKeyword(const QStringRef &text) const;
bool isQtKeyword(const QStringRef &text) const;
QTextCharFormat m_formats[NumCppFormats];
};
......
......@@ -33,7 +33,7 @@
#include "cppchecksymbols.h"
#include "cpplocalsymbols.h"
#include <cplusplus/Overview.h>
#include <cplusplus/SimpleLexer.h>
#include <Names.h>
#include <Literals.h>
......@@ -284,16 +284,21 @@ protected:
} // end of anonymous namespace
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context)
static bool sortByLinePredicate(const CheckSymbols::Use &lhs, const CheckSymbols::Use &rhs)
{
return lhs.line < rhs.line;
}
CheckSymbols::Future CheckSymbols::go(Document::Ptr doc, const LookupContext &context, const QList<CheckSymbols::Use> &macroUses)
{
QTC_ASSERT(doc, return Future());
return (new CheckSymbols(doc, context))->start();
return (new CheckSymbols(doc, context, macroUses))->start();
}
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context)
CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context, const QList<CheckSymbols::Use> &macroUses)
: ASTVisitor(doc->translationUnit()), _doc(doc), _context(context)
, _lineOfLastUsage(0)
, _lineOfLastUsage(0), _macroUses(macroUses)
{
CollectSymbols collectTypes(doc, context.snapshot());
......@@ -311,11 +316,13 @@ CheckSymbols::~CheckSymbols()
void CheckSymbols::run()
{
qSort(_macroUses.begin(), _macroUses.end(), sortByLinePredicate);
_diagnosticMessages.clear();
if (! isCanceled()) {
if (_doc->translationUnit()) {
accept(_doc->translationUnit()->ast());
_usages << QVector<Use>::fromList(_macroUses);
flush();
}
}
......@@ -876,6 +883,9 @@ void CheckSymbols::addUse(const Use &use)
}
}
while (!_macroUses.isEmpty() && _macroUses.first().line <= use.line)
_usages.append(_macroUses.takeFirst());
_lineOfLastUsage = qMax(_lineOfLastUsage, use.line);
_usages.append(use);
}
......@@ -1115,11 +1125,6 @@ bool CheckSymbols::maybeVirtualMethod(const Name *name) const
return false;
}
static bool sortByLinePredicate(const CheckSymbols::Use &lhs, const CheckSymbols::Use &rhs)
{
return lhs.line < rhs.line;
}
void CheckSymbols::flush()
{
_lineOfLastUsage = 0;
......
......@@ -72,7 +72,7 @@ public:
return future;
}
static Future go(Document::Ptr doc, const LookupContext &context);
static Future go(Document::Ptr doc, const LookupContext &context, const QList<Use> &macroUses);
static QMap<int, QVector<Use> > chunks(const QFuture<Use> &future, int from, int to)
{
......@@ -94,7 +94,7 @@ protected:
using ASTVisitor::visit;
using ASTVisitor::endVisit;
CheckSymbols(Document::Ptr doc, const LookupContext &context);
CheckSymbols(Document::Ptr doc, const LookupContext &context, const QList<Use> &macroUses);
bool hasVirtualDestructor(Class *klass) const;
bool hasVirtualDestructor(ClassOrNamespace *binding) const;
......@@ -174,6 +174,7 @@ private:
QList<AST *> _astStack;
QVector<Use> _usages;
unsigned _lineOfLastUsage;
QList<Use> _macroUses;
};
} // namespace CPlusPlus
......
......@@ -34,6 +34,10 @@
#include "cpphighlightingsupportinternal.h"
#include <cplusplus/LookupContext.h>
#include <cplusplus/SimpleLexer.h>
#include <cplusplus/Token.h>
#include <cpptools/cpptoolsreuse.h>
#include <texteditor/itexteditor.h>
using namespace CPlusPlus;
using namespace CppTools;
......@@ -52,8 +56,32 @@ QFuture<CppHighlightingSupport::Use> CppHighlightingSupportInternal::highlightin
const Document::Ptr &doc,
const Snapshot &snapshot) const
{
//Get macro uses
QList<CheckSymbols::Use> macroUses;
foreach (Document::MacroUse macro, doc->macroUses()) {
const QString name = QString::fromUtf8(macro.macro().name());
//Filter out QtKeywords
if (isQtKeyword(QStringRef(&name)))
continue;
//Filter out C++ keywords
SimpleLexer tokenize;
tokenize.setQtMocRunEnabled(false);
tokenize.setObjCEnabled(false);
const QList<Token> tokens = tokenize(name);
if (tokens.length() && (tokens.at(0).isKeyword() || tokens.at(0).isObjCAtKeyword()))
continue;
int line, column;
editor()->convertPosition(macro.begin(), &line, &column);
++column; //Highlighting starts at (column-1) --> compensate here
CheckSymbols::Use use(line, column, name.size(), SemanticInfo::MacroUse);
macroUses.append(use);
}
LookupContext context(doc, snapshot);
return CheckSymbols::go(doc, context);
return CheckSymbols::go(doc, context, macroUses);
}
CppHighlightingSupportInternalFactory::~CppHighlightingSupportInternalFactory()
......
......@@ -52,7 +52,8 @@ public:
FieldUse,
StaticUse,
VirtualMethodUse,
LabelUse
LabelUse,
MacroUse
};
typedef TextEditor::SemanticHighlighter::Result Use;
......
......@@ -40,6 +40,7 @@
#include <QSet>
#include <QTextDocument>
#include <QTextCursor>
#include <QStringRef>
using namespace CPlusPlus;
......@@ -131,5 +132,49 @@ bool isValidIdentifier(const QString &s)
return true;
}
bool isQtKeyword(const QStringRef &text)
{
switch (text.length()) {
case 4:
switch (text.at(0).toLatin1()) {
case 'e':
if (text == QLatin1String("emit"))
return true;
break;
case 'S':
if (text == QLatin1String("SLOT"))
return true;
break;
}
break;
case 5:
if (text.at(0) == QLatin1Char('s') && text == QLatin1String("slots"))
return true;
break;
case 6:
if (text.at(0) == QLatin1Char('S') && text == QLatin1String("SIGNAL"))
return true;
break;
case 7:
switch (text.at(0).toLatin1()) {
case 's':
if (text == QLatin1String("signals"))
return true;
break;
case 'f':
if (text == QLatin1String("foreach") || text == QLatin1String("forever"))
return true;
break;
}
break;
default:
break;
}
return false;
}
} // CppTools
......@@ -36,6 +36,7 @@
#include "cpptools_global.h"
QT_FORWARD_DECLARE_CLASS(QTextCursor)
QT_FORWARD_DECLARE_CLASS(QStringRef)
namespace CPlusPlus {
class Symbol;
......@@ -52,6 +53,8 @@ bool CPPTOOLS_EXPORT isOwnershipRAIIType(CPlusPlus::Symbol *symbol,
bool CPPTOOLS_EXPORT isValidIdentifier(const QString &s);
bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
} // CppTools
#endif // CPPTOOLSREUSE_H
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