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:
Orgad Shaneh
2018-07-01 11:03:09 +03:00
committed by Orgad Shaneh
parent 58df07bc76
commit e5a8958e31
4 changed files with 40 additions and 2 deletions

View File

@@ -85,6 +85,7 @@ namespace {
static const QLatin1String kDetect2Chars("Detect2Chars");
static const QLatin1String kAnyChar("AnyChar");
static const QLatin1String kStringDetect("StringDetect");
static const QLatin1String kWordDetect("WordDetect");
static const QLatin1String kRegExpr("RegExpr");
static const QLatin1String kKeyword("keyword");
static const QLatin1String kInt("Int");
@@ -153,6 +154,8 @@ bool HighlightDefinitionHandler::startElement(const QString &,
anyCharStarted(atts);
else if (qName == kStringDetect)
stringDetectedStarted(atts);
else if (qName == kWordDetect)
wordDetectStarted(atts);
else if (qName == kRegExpr)
regExprStarted(atts);
else if (qName == kKeyword)
@@ -189,7 +192,8 @@ bool HighlightDefinitionHandler::endElement(const QString &, const QString &, co
m_currentList->addKeyword(m_currentKeyword.trimmed());
m_processingKeyword = false;
} 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 == kHlCChar || qName == kRangeDetect || qName == kLineContinue ||
qName == kDetectSpaces || qName == kDetectIdentifier) {
@@ -333,6 +337,15 @@ void HighlightDefinitionHandler::stringDetectedStarted(const QXmlAttributes &att
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)
{
RegExprRule *rule = new RegExprRule;

View File

@@ -67,6 +67,7 @@ private:
void detect2CharsStarted(const QXmlAttributes &atts);
void anyCharStarted(const QXmlAttributes &atts);
void stringDetectedStarted(const QXmlAttributes &atts);
void wordDetectStarted(const QXmlAttributes &atts);
void regExprStarted(const QXmlAttributes &atts);
void keywordStarted(const QXmlAttributes &atts);
void intStarted(const QXmlAttributes &atts);

View File

@@ -171,6 +171,21 @@ bool StringDetectRule::doMatchSucceed(const QString &text,
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
RegExprRule::~RegExprRule()
{

View File

@@ -99,7 +99,7 @@ public:
void setString(const QString &s);
void setInsensitive(const QString &insensitive);
private:
protected:
virtual bool doMatchSucceed(const QString &text,
const int length,
ProgressData *progress);
@@ -111,6 +111,15 @@ private:
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
{
public: