diff --git a/src/pegmarkdownhighlighter.cpp b/src/pegmarkdownhighlighter.cpp index 1923a26c..bd8fc310 100644 --- a/src/pegmarkdownhighlighter.cpp +++ b/src/pegmarkdownhighlighter.cpp @@ -7,6 +7,7 @@ #include "pegparser.h" #include "vconfigmanager.h" #include "utils/vutils.h" +#include "utils/veditutils.h" extern VConfigManager *g_config; @@ -110,7 +111,7 @@ void PegMarkdownHighlighter::highlightBlock(const QString &p_text) updateCodeBlockState(result, blockNum, p_text); if (currentBlockState() == HighlightBlockState::CodeBlock) { - highlightCodeBlock(result, blockNum); + highlightCodeBlock(result, blockNum, p_text); highlightCodeBlockColorColumn(p_text); } @@ -339,8 +340,17 @@ void PegMarkdownHighlighter::updateCodeBlockState(const QSharedPointer &p_result, - int p_blockNum) + int p_blockNum, + const QString &p_text) { + // Brush the indentation spaces. + if (currentBlockState() == HighlightBlockState::CodeBlock) { + int spaces = VEditUtils::fetchIndentation(p_text); + if (spaces > 0) { + setFormat(0, spaces, m_codeBlockFormat); + } + } + if (p_result->m_codeBlocksHighlights.size() > p_blockNum) { const QVector &units = p_result->m_codeBlocksHighlights[p_blockNum]; if (!units.isEmpty()) { diff --git a/src/pegmarkdownhighlighter.h b/src/pegmarkdownhighlighter.h index a73b5863..78747a14 100644 --- a/src/pegmarkdownhighlighter.h +++ b/src/pegmarkdownhighlighter.h @@ -85,7 +85,8 @@ private: // Highlight fenced code block according to VCodeBlockHighlightHelper result. void highlightCodeBlock(const QSharedPointer &p_result, - int p_blockNum); + int p_blockNum, + const QString &p_text); // Highlight color column in code block. void highlightCodeBlockColorColumn(const QString &p_text); diff --git a/src/utils/veditutils.cpp b/src/utils/veditutils.cpp index b36a8f53..5f2a1b08 100644 --- a/src/utils/veditutils.cpp +++ b/src/utils/veditutils.cpp @@ -944,6 +944,18 @@ QString VEditUtils::fetchIndentSpaces(const QTextBlock &p_block) return regExp.capturedTexts()[1]; } +int VEditUtils::fetchIndentation(const QString &p_text) +{ + int idx = 0; + for (; idx < p_text.size(); ++idx) { + if (!p_text[idx].isSpace()) { + break; + } + } + + return idx; +} + void VEditUtils::insertBlock(QTextCursor &p_cursor, bool p_above) { diff --git a/src/utils/veditutils.h b/src/utils/veditutils.h index 112b2699..9c4e283c 100644 --- a/src/utils/veditutils.h +++ b/src/utils/veditutils.h @@ -184,6 +184,8 @@ public: // Return the leading spaces of @p_block. static QString fetchIndentSpaces(const QTextBlock &p_block); + static int fetchIndentation(const QString &p_text); + // Insert a block above/below current block. Move the cursor to the start of // the new block after insertion. static void insertBlock(QTextCursor &p_cursor,