QtNovelUI/WordsIDE/keywordshighlighter.cpp

51 lines
1.3 KiB
C++

#include "keywordshightlighter.h"
#include <QDebug>
using namespace Parse;
using namespace Enhancement;
KeywordsHightlighter::KeywordsHightlighter(QObject *parent)
: QSyntaxHighlighter(parent), parse_core(nullptr)
{
}
void KeywordsHightlighter::reset(Parse::Result::DocCore *base)
{
this->parse_core = base;
}
void KeywordsHightlighter::highlightBlock(const QString &text)
{
if(parse_core == nullptr)
return;
QTextCharFormat unknowns;
unknowns.setForeground(QBrush(Qt::gray));
QTextCharFormat generate;
generate.setForeground(QBrush(Qt::blue));
QTextCharFormat error;
error.setForeground(QBrush(Qt::red));
auto block = currentBlock();
auto words = parse_core->getWords(block.blockNumber());
for(auto &w : words){
QList<ErrorMessage> xerrors;
if(w->host() == parse_core->unknowns()){
unknowns.setUnderlineStyle(QTextCharFormat::UnderlineStyle::WaveUnderline);
unknowns.setUnderlineColor(QColor(Qt::red));
setFormat(w->column()-1, w->toString().length(), unknowns);
}
else if(!w->host()->check(xerrors)){
setFormat(w->column()-1, w->toString().length(), error);
}
else{
setFormat(w->column()-1, w->toString().length(), generate);
}
}
}