regex - QSyntaxHighlighter 文档中的多行注释显示不正确

标签 regex qt qt4 syntax-highlighting

引用Syntax Highlighter Example由QT官网提供,我尝试在我的应用程序中实现(实际上你可以称之为复制粘贴)相同的多行注释逻辑。作为引用,这是多行注释突出显示的代码:

构造函数内部:

quotationFormat.setForeground(QColor(164, 14, 14));
rule.pattern = QRegExp("\".*\"");
rule.format = quotationFormat;
highlightingRules.append(rule);

charFormat.setForeground(QColor(164, 14, 14));
rule.pattern = QRegExp("\'.*\'");
rule.format = charFormat;
highlightingRules.append(rule);

singleLineCommentFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegExp("//[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
multiLineCommentFormat.setForeground(Qt::darkGreen);

commentStartExpression = QRegExp("/\\*");
commentEndExpression = QRegExp("\\*/");

在 highlightBlock() 函数中:

foreach (const HighlightingRule &rule, highlightingRules) {
    QRegExp expression(rule.pattern);
    int index = expression.indexIn(text);
    while (index >= 0) {
        int length = expression.matchedLength();
        setFormat(index, length, rule.format);
        index = expression.indexIn(text, index + length);
    }
}
setCurrentBlockState(0);

int startIndex = 0;
if (previousBlockState() != 1)
    startIndex = commentStartExpression.indexIn(text);

while (startIndex >= 0) {
    int endIndex = commentEndExpression.indexIn(text, startIndex);
    int commentLength;
    if (endIndex == -1) {
        setCurrentBlockState(1);
        commentLength = text.length() - startIndex;
    } else {
        commentLength = endIndex - startIndex
                        + commentEndExpression.matchedLength();
    }
    setFormat(startIndex, commentLength, multiLineCommentFormat);
    startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
}

但是当/* 出现在引号内时,我仍然面临问题。它以多行注释颜色(绿色)显示到最后(/*.png"] })引用以下示例文档内容:

{
    "code": [
        "./Code/Main.js"
    ],

    "textures": [
        "./Content/*.png"
    ]
}

我不是正则表达式的高手,但我猜想正则表达式有问题。

最佳答案

凭借一些运气和努力,我找到了一个解决方法(但它不会涵盖所有情况)

    int MySyntaxHighlighter::getCommentStartIndex(const QString &text, const int offset)
    {
        int startIndex = -1;

        qDebug() << "offset: " << offset;

        if(text.length() > 1 && offset < (text.length() - 1)) {

            int commentStartIndex = commentStartExpression.indexIn(text, offset);
            qDebug() << "commentStartIndex: " << commentStartIndex;

            QRegExp quotationExpression(quotationRule.pattern);

            int quotationStartIndex = quotationExpression.indexIn(text, offset);
            qDebug() << "quotationStartIndex: " << quotationStartIndex;

            if (quotationStartIndex >= 0) {

                int quotationLength = quotationExpression.matchedLength();
                qDebug() << "quotationLength: " << quotationLength;

                if(commentStartIndex > quotationStartIndex &&
                        commentStartIndex < (quotationStartIndex + quotationLength)) {

                    startIndex = getCommentStartIndex(text, (commentStartIndex + 2));
                } else {
                    startIndex = commentStartIndex;
                }
            } else if(commentStartIndex >= 0) {
                startIndex = commentStartIndex;
            } else {
                startIndex = -1;
            }
        }

        qDebug() << "startIndex: " << startIndex;

        return startIndex;
    }

    void MySyntaxHighlighter::highlightBlock(const QString &text)
    {
        setCurrentBlockState(0);

        qDebug() << "text: " << text;
        qDebug() << "previousBlockState(): " << previousBlockState();

        int startIndex = 0;
        if (previousBlockState() != 1) {
//        startIndex = commentEndExpression.indexIn(text);
            startIndex = getCommentStartIndex(text, 0);
        }

        while (startIndex >= 0) {
            int endIndex = commentEndExpression.indexIn(text, startIndex);
            int commentLength;
            if (endIndex == -1) {
                setCurrentBlockState(1);
                commentLength = text.length() - startIndex;
            } else {
                commentLength = endIndex - startIndex
                                + commentEndExpression.matchedLength();
            }
            setFormat(startIndex, commentLength, multiLineCommentFormat);

    //        startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
            startIndex = getCommentStartIndex(text, startIndex + commentLength);
        }    
    }

如果有人找到更好的解决方案,请与我分享。

关于regex - QSyntaxHighlighter 文档中的多行注释显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26650827/

相关文章:

regex - 如何从 Perl 的正则表达式中排除特定模式?

RegEx 确保字符串至少包含一个小写字符、大写字符、数字和符号

连接到 MySQL 的 C++ 程序

c++ - QtConcurrent::map 不接受参数

c++ - 非主线程中的QApplication

c++ - Qt 中缺少链接器

regex - Perl 正则表达式用自己替换数字,只是高一个

python - 根据不同长度的分隔符拆分 Pandas 字符串列

c++ - 更改单个 QTabWidget 选项卡的颜色

c++ - Qt : C++ dynamic parameter GUI for plugin parameters