From ccb5f2302db7269592aaa0a2952d69bac8303737 Mon Sep 17 00:00:00 2001 From: David Schulz Date: Fri, 3 May 2019 08:48:25 +0200 Subject: [PATCH] TextEditor: fix folding regression in generic highlighter Fixes: QTCREATORBUG-22346 Change-Id: Ib35a70da77ffaa3b84e1d85a855625e2086625da Reviewed-by: Eike Ziller --- src/plugins/texteditor/highlighter.cpp | 50 +++++++++++++++++++++++--- src/plugins/texteditor/highlighter.h | 1 + 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/plugins/texteditor/highlighter.cpp b/src/plugins/texteditor/highlighter.cpp index 5b60b885ab4..07ec2fcf964 100644 --- a/src/plugins/texteditor/highlighter.cpp +++ b/src/plugins/texteditor/highlighter.cpp @@ -27,6 +27,7 @@ #include "highlightersettings.h" #include "textdocumentlayout.h" +#include "tabsettings.h" #include "texteditorsettings.h" #include @@ -35,6 +36,7 @@ #include #include +#include #include #include @@ -272,10 +274,16 @@ void Highlighter::highlightBlock(const QString &text) { if (!definition().isValid()) return; - QTextBlock block = currentBlock(); - KSyntaxHighlighting::State state = TextDocumentLayout::userData(block)->syntaxState(); + const QTextBlock block = currentBlock(); + KSyntaxHighlighting::State state; + setCurrentBlockState(qMax(0, previousBlockState())); + if (TextBlockUserData *data = TextDocumentLayout::testUserData(block)) { + state = data->syntaxState(); + data->setFoldingStartIncluded(false); + data->setFoldingEndIncluded(false); + } state = highlightLine(text, state); - block = block.next(); + const QTextBlock nextBlock = block.next(); Parentheses parentheses; int pos = 0; @@ -288,8 +296,12 @@ void Highlighter::highlightBlock(const QString &text) } TextDocumentLayout::setParentheses(currentBlock(), parentheses); - if (block.isValid()) - TextDocumentLayout::userData(block)->setSyntaxState(state); + if (nextBlock.isValid()) { + TextBlockUserData *data = TextDocumentLayout::userData(nextBlock); + data->setSyntaxState(state); + data->setFoldingIndent(currentBlockState()); + } + formatSpaces(text); } @@ -297,3 +309,31 @@ void Highlighter::applyFormat(int offset, int length, const KSyntaxHighlighting: { setFormat(offset, length, formatForCategory(format.textStyle())); } + +void Highlighter::applyFolding(int offset, + int length, + KSyntaxHighlighting::FoldingRegion region) +{ + if (!region.isValid()) + return; + const QTextBlock &block = currentBlock(); + const QString &text = block.text(); + TextBlockUserData *data = TextDocumentLayout::userData(currentBlock()); + const bool fromStart = TabSettings::firstNonSpace(text) == offset; + const bool toEnd = (offset + length) == (text.length() - TabSettings::trailingWhitespaces(text)); + if (region.type() == KSyntaxHighlighting::FoldingRegion::Begin) { + setCurrentBlockState(currentBlockState() + 1); + // if there is only a folding begin in the line move the current block into the fold + if (fromStart && toEnd) { + data->setFoldingIndent(currentBlockState()); + data->setFoldingStartIncluded(true); + } + } else if (region.type() == KSyntaxHighlighting::FoldingRegion::End) { + setCurrentBlockState(qMax(0, currentBlockState() - 1)); + // if the folding end is at the end of the line move the current block into the fold + if (toEnd) + data->setFoldingEndIncluded(true); + else + data->setFoldingIndent(currentBlockState()); + } +} diff --git a/src/plugins/texteditor/highlighter.h b/src/plugins/texteditor/highlighter.h index 92e7e136c18..8777bf006fc 100644 --- a/src/plugins/texteditor/highlighter.h +++ b/src/plugins/texteditor/highlighter.h @@ -66,6 +66,7 @@ public: protected: void highlightBlock(const QString &text) override; void applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format) override; + void applyFolding(int offset, int length, KSyntaxHighlighting::FoldingRegion region) override; }; } // namespace TextEditor