PegHighlighter: brush code block indentation

This commit is contained in:
Le Tan 2018-07-13 20:32:34 +08:00
parent f5bbc1ccd7
commit 926e053d37
4 changed files with 28 additions and 3 deletions

View File

@ -7,6 +7,7 @@
#include "pegparser.h" #include "pegparser.h"
#include "vconfigmanager.h" #include "vconfigmanager.h"
#include "utils/vutils.h" #include "utils/vutils.h"
#include "utils/veditutils.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
@ -110,7 +111,7 @@ void PegMarkdownHighlighter::highlightBlock(const QString &p_text)
updateCodeBlockState(result, blockNum, p_text); updateCodeBlockState(result, blockNum, p_text);
if (currentBlockState() == HighlightBlockState::CodeBlock) { if (currentBlockState() == HighlightBlockState::CodeBlock) {
highlightCodeBlock(result, blockNum); highlightCodeBlock(result, blockNum, p_text);
highlightCodeBlockColorColumn(p_text); highlightCodeBlockColorColumn(p_text);
} }
@ -339,8 +340,17 @@ void PegMarkdownHighlighter::updateCodeBlockState(const QSharedPointer<PegHighli
} }
void PegMarkdownHighlighter::highlightCodeBlock(const QSharedPointer<PegHighlighterResult> &p_result, void PegMarkdownHighlighter::highlightCodeBlock(const QSharedPointer<PegHighlighterResult> &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) { if (p_result->m_codeBlocksHighlights.size() > p_blockNum) {
const QVector<HLUnitStyle> &units = p_result->m_codeBlocksHighlights[p_blockNum]; const QVector<HLUnitStyle> &units = p_result->m_codeBlocksHighlights[p_blockNum];
if (!units.isEmpty()) { if (!units.isEmpty()) {

View File

@ -85,7 +85,8 @@ private:
// Highlight fenced code block according to VCodeBlockHighlightHelper result. // Highlight fenced code block according to VCodeBlockHighlightHelper result.
void highlightCodeBlock(const QSharedPointer<PegHighlighterResult> &p_result, void highlightCodeBlock(const QSharedPointer<PegHighlighterResult> &p_result,
int p_blockNum); int p_blockNum,
const QString &p_text);
// Highlight color column in code block. // Highlight color column in code block.
void highlightCodeBlockColorColumn(const QString &p_text); void highlightCodeBlockColorColumn(const QString &p_text);

View File

@ -944,6 +944,18 @@ QString VEditUtils::fetchIndentSpaces(const QTextBlock &p_block)
return regExp.capturedTexts()[1]; 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, void VEditUtils::insertBlock(QTextCursor &p_cursor,
bool p_above) bool p_above)
{ {

View File

@ -184,6 +184,8 @@ public:
// Return the leading spaces of @p_block. // Return the leading spaces of @p_block.
static QString fetchIndentSpaces(const QTextBlock &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 // Insert a block above/below current block. Move the cursor to the start of
// the new block after insertion. // the new block after insertion.
static void insertBlock(QTextCursor &p_cursor, static void insertBlock(QTextCursor &p_cursor,