forked from qt-creator/qt-creator
GenericHighlighter: Support WordDetect
Added in Kate 3.5 (KDE 4.5). Similar to StringDetect, but only matches whole word (\b<string>\b) Already used in some of the built-in highlighters. Change-Id: I03ae5e1db917e777a21bb96d9a31cc7771287f39 Reviewed-by: Eike Ziller <eike.ziller@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
58df07bc76
commit
e5a8958e31
@@ -85,6 +85,7 @@ namespace {
|
|||||||
static const QLatin1String kDetect2Chars("Detect2Chars");
|
static const QLatin1String kDetect2Chars("Detect2Chars");
|
||||||
static const QLatin1String kAnyChar("AnyChar");
|
static const QLatin1String kAnyChar("AnyChar");
|
||||||
static const QLatin1String kStringDetect("StringDetect");
|
static const QLatin1String kStringDetect("StringDetect");
|
||||||
|
static const QLatin1String kWordDetect("WordDetect");
|
||||||
static const QLatin1String kRegExpr("RegExpr");
|
static const QLatin1String kRegExpr("RegExpr");
|
||||||
static const QLatin1String kKeyword("keyword");
|
static const QLatin1String kKeyword("keyword");
|
||||||
static const QLatin1String kInt("Int");
|
static const QLatin1String kInt("Int");
|
||||||
@@ -153,6 +154,8 @@ bool HighlightDefinitionHandler::startElement(const QString &,
|
|||||||
anyCharStarted(atts);
|
anyCharStarted(atts);
|
||||||
else if (qName == kStringDetect)
|
else if (qName == kStringDetect)
|
||||||
stringDetectedStarted(atts);
|
stringDetectedStarted(atts);
|
||||||
|
else if (qName == kWordDetect)
|
||||||
|
wordDetectStarted(atts);
|
||||||
else if (qName == kRegExpr)
|
else if (qName == kRegExpr)
|
||||||
regExprStarted(atts);
|
regExprStarted(atts);
|
||||||
else if (qName == kKeyword)
|
else if (qName == kKeyword)
|
||||||
@@ -189,7 +192,8 @@ bool HighlightDefinitionHandler::endElement(const QString &, const QString &, co
|
|||||||
m_currentList->addKeyword(m_currentKeyword.trimmed());
|
m_currentList->addKeyword(m_currentKeyword.trimmed());
|
||||||
m_processingKeyword = false;
|
m_processingKeyword = false;
|
||||||
} else if (qName == kDetectChar || qName == kDetect2Chars || qName == kAnyChar ||
|
} else if (qName == kDetectChar || qName == kDetect2Chars || qName == kAnyChar ||
|
||||||
qName == kStringDetect || qName == kRegExpr || qName == kKeyword || qName == kInt ||
|
qName == kStringDetect || qName == kWordDetect || qName == kRegExpr ||
|
||||||
|
qName == kKeyword || qName == kInt ||
|
||||||
qName == kFloat || qName == kHlCOct || qName == kHlCHex || qName == kHlCStringChar ||
|
qName == kFloat || qName == kHlCOct || qName == kHlCHex || qName == kHlCStringChar ||
|
||||||
qName == kHlCChar || qName == kRangeDetect || qName == kLineContinue ||
|
qName == kHlCChar || qName == kRangeDetect || qName == kLineContinue ||
|
||||||
qName == kDetectSpaces || qName == kDetectIdentifier) {
|
qName == kDetectSpaces || qName == kDetectIdentifier) {
|
||||||
@@ -333,6 +337,15 @@ void HighlightDefinitionHandler::stringDetectedStarted(const QXmlAttributes &att
|
|||||||
ruleElementStarted(atts, QSharedPointer<Rule>(rule));
|
ruleElementStarted(atts, QSharedPointer<Rule>(rule));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HighlightDefinitionHandler::wordDetectStarted(const QXmlAttributes &atts)
|
||||||
|
{
|
||||||
|
WordDetectRule *rule = new WordDetectRule;
|
||||||
|
rule->setString(atts.value(kString));
|
||||||
|
rule->setInsensitive(atts.value(kInsensitive));
|
||||||
|
rule->setActive(atts.value(kDynamic));
|
||||||
|
ruleElementStarted(atts, QSharedPointer<Rule>(rule));
|
||||||
|
}
|
||||||
|
|
||||||
void HighlightDefinitionHandler::regExprStarted(const QXmlAttributes &atts)
|
void HighlightDefinitionHandler::regExprStarted(const QXmlAttributes &atts)
|
||||||
{
|
{
|
||||||
RegExprRule *rule = new RegExprRule;
|
RegExprRule *rule = new RegExprRule;
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ private:
|
|||||||
void detect2CharsStarted(const QXmlAttributes &atts);
|
void detect2CharsStarted(const QXmlAttributes &atts);
|
||||||
void anyCharStarted(const QXmlAttributes &atts);
|
void anyCharStarted(const QXmlAttributes &atts);
|
||||||
void stringDetectedStarted(const QXmlAttributes &atts);
|
void stringDetectedStarted(const QXmlAttributes &atts);
|
||||||
|
void wordDetectStarted(const QXmlAttributes &atts);
|
||||||
void regExprStarted(const QXmlAttributes &atts);
|
void regExprStarted(const QXmlAttributes &atts);
|
||||||
void keywordStarted(const QXmlAttributes &atts);
|
void keywordStarted(const QXmlAttributes &atts);
|
||||||
void intStarted(const QXmlAttributes &atts);
|
void intStarted(const QXmlAttributes &atts);
|
||||||
|
|||||||
@@ -171,6 +171,21 @@ bool StringDetectRule::doMatchSucceed(const QString &text,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool WordDetectRule::doMatchSucceed(const QString &text, const int length, ProgressData *progress)
|
||||||
|
{
|
||||||
|
const int offset = progress->offset();
|
||||||
|
if (length - offset < m_length)
|
||||||
|
return false;
|
||||||
|
if (offset > 0 && !definition()->isDelimiter(text.at(offset - 1)))
|
||||||
|
return false;
|
||||||
|
if (text.midRef(offset, m_string.size()).compare(m_string, m_caseSensitivity) != 0)
|
||||||
|
return false;
|
||||||
|
if (length > offset + m_string.size() && !definition()->isDelimiter(text.at(offset + m_string.size())))
|
||||||
|
return false;
|
||||||
|
progress->incrementOffset(m_length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// RegExpr
|
// RegExpr
|
||||||
RegExprRule::~RegExprRule()
|
RegExprRule::~RegExprRule()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public:
|
|||||||
void setString(const QString &s);
|
void setString(const QString &s);
|
||||||
void setInsensitive(const QString &insensitive);
|
void setInsensitive(const QString &insensitive);
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
virtual bool doMatchSucceed(const QString &text,
|
virtual bool doMatchSucceed(const QString &text,
|
||||||
const int length,
|
const int length,
|
||||||
ProgressData *progress);
|
ProgressData *progress);
|
||||||
@@ -111,6 +111,15 @@ private:
|
|||||||
Qt::CaseSensitivity m_caseSensitivity = Qt::CaseSensitive;
|
Qt::CaseSensitivity m_caseSensitivity = Qt::CaseSensitive;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class WordDetectRule : public StringDetectRule
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
virtual bool doMatchSucceed(const QString &text,
|
||||||
|
const int length,
|
||||||
|
ProgressData *progress);
|
||||||
|
virtual WordDetectRule *doClone() const { return new WordDetectRule(*this); }
|
||||||
|
};
|
||||||
|
|
||||||
class RegExprRule : public DynamicRule
|
class RegExprRule : public DynamicRule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
Reference in New Issue
Block a user