From 05e4159530e313eac862696a0c78fb1ce6e91fc7 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Thu, 12 Apr 2018 19:32:04 +0800 Subject: [PATCH] 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". --- src/vcodeblockhighlighthelper.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/vcodeblockhighlighthelper.cpp b/src/vcodeblockhighlighthelper.cpp index a31f509c..c6551579 100644 --- a/src/vcodeblockhighlighthelper.cpp +++ b/src/vcodeblockhighlighthelper.cpp @@ -111,8 +111,20 @@ static void matchTokenRelaxed(const QString &p_text, const QString &p_tokenStr, int &p_index, int &p_start, int &p_end) { 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'. regStr.replace(QRegExp("\n(?!$)"), "\\s+"); + QRegExp regExp(regStr); p_start = p_text.indexOf(regExp, p_index); if (p_start == -1) {