参考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"
]
}
我不是正则表达式的大师,但我想Regex有问题。
经过一些运气和努力,我找到了一个变通方法(仍然无法涵盖所有情况)
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);
}
}
如果有人找到了更好的解决方案,请与我分享。