void MarkupHighlighter::highlightBlock(const QString &text)
{

    if (FORMATTING.empty()){
        qWarning() << "Not given any formatting, so not highlighting.";
        return;
    }

    if (text.isEmpty()) return;

    int start=0, end=0, highlightLength = 0;

    QRegularExpression re;
    QTextCharFormat textFormat;
    for (QString exp : TOKENS.keys()){
        setCurrentBlockState(0);
        start = 0, end = 0;

        re = QRegularExpression(exp);

        if (!re.isValid()){
            QString message = "Invalid regular expression \""
                    + re.pattern() + "\" :" + re.errorString();
            qFatal("%s", message.toStdString().data());
            return;
        }

        if (previousBlockState() != 1)
            start = re.match(text).capturedStart();

        while (start >= 0){
            QRegularExpressionMatch match = re.match(text, start);
            end = match.capturedEnd();
            if (end == -1 || (end == start && end != 0)){
                setCurrentBlockState(1);
                highlightLength = text.length();
            } else {
                highlightLength = match.capturedLength();
            }
            QTextCharFormat baseFormat = currentBlock().blockFormat().toCharFormat();
            MarkupHighlighter::MarkupToken tok = TOKENS[exp];
            if (!FORMATTING.contains(tok)){
                qWarning() << "Could not find" << tok;
                break;
            }
            const StyleProxy *styleProxy = FORMATTING[tok];
            textFormat = styleProxy->toFormat(baseFormat);
            setFormat(start, highlightLength, textFormat);
            qDebug() << "highlightBlock";
            qDebug() << "Formatting"
                     << "token" << tok
                     << "with regex=" << exp
                     << ", string="
                     << text.mid(start, highlightLength)
                     << "\n"
                     << "  Bold?" << textFormat.font().bold() << "\n"
                     << "  Italic?" << textFormat.font().italic() << "\n"
                     << "  Size:" << textFormat.font().pointSize() << "\n"
                     << "  Background:" << textFormat.background().color();
            start = re.match(text, end).capturedStart();
            // This should not be 0 again. If it is, that means our search has
            // come up empty.
            if (start == 0)
                start = -1;
        }
    }
}
示例#2
0
void ProjectIntroPage::setProjectNameRegularExpression(const QRegularExpression &regEx)
{
    Q_ASSERT_X(regEx.isValid(), Q_FUNC_INFO, qPrintable(regEx.errorString()));
    d->m_projectNameValidator.setRegularExpression(regEx);
}