bug-fix: fix leading spaces issue of VCodeBlockHighlightHelper

For example, a token " abc" in text "def\n abc", the leading space may
be consumed by the former "\n".
This commit is contained in:
Le Tan 2018-04-12 19:32:04 +08:00
parent 2c527201b8
commit 05e4159530

View File

@ -111,8 +111,20 @@ static void matchTokenRelaxed(const QString &p_text, const QString &p_tokenStr,
int &p_index, int &p_start, int &p_end) int &p_index, int &p_start, int &p_end)
{ {
QString regStr = QRegExp::escape(p_tokenStr); QString regStr = QRegExp::escape(p_tokenStr);
// Remove the leading spaces.
int nonSpaceIdx = 0;
while (nonSpaceIdx < regStr.size() && regStr[nonSpaceIdx].isSpace()) {
++nonSpaceIdx;
}
if (nonSpaceIdx > 0 && nonSpaceIdx < regStr.size()) {
regStr.remove(0, nonSpaceIdx);
}
// Do not replace the ending '\n'. // Do not replace the ending '\n'.
regStr.replace(QRegExp("\n(?!$)"), "\\s+"); regStr.replace(QRegExp("\n(?!$)"), "\\s+");
QRegExp regExp(regStr); QRegExp regExp(regStr);
p_start = p_text.indexOf(regExp, p_index); p_start = p_text.indexOf(regExp, p_index);
if (p_start == -1) { if (p_start == -1) {