Commit 5756ba29 authored by Waqar Ahmed's avatar Waqar Ahmed
Browse files

Diff: Fix comment block highlighting in hunks

parent cbe04fba
Pipeline #230782 passed with stage
in 12 minutes and 30 seconds
......@@ -14,9 +14,17 @@
#include <QTextBlock>
#include <KLocalizedString>
#include <KSyntaxHighlighting/Definition>
#include <KSyntaxHighlighting/Format>
#include <KSyntaxHighlighting/State>
#include <KTextEditor/Editor>
DiffSyntaxHighlighter::DiffSyntaxHighlighter(QTextDocument *parent, DiffWidget *diffWidget)
: KSyntaxHighlighting::SyntaxHighlighter(parent)
, m_diffWidget(diffWidget)
{
}
void DiffSyntaxHighlighter::applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format)
{
if (format.textStyle() == KSyntaxHighlighting::Theme::TextStyle::Error) {
......@@ -25,6 +33,21 @@ void DiffSyntaxHighlighter::applyFormat(int offset, int length, const KSyntaxHig
KSyntaxHighlighting::SyntaxHighlighter::applyFormat(offset, length, format);
}
void DiffSyntaxHighlighter::highlightBlock(const QString &text)
{
// Delete user data i.e., the stored state in the block
// when we encounter a hunk to avoid issues like everything
// is commented because previous hunk ended with an unclosed
// comment block
if (m_diffWidget->isHunk(currentBlock().blockNumber())) {
auto prevBlock = currentBlock().previous();
const auto prevData = prevBlock.userData();
delete prevData;
prevBlock.setUserData(new QTextBlockUserData);
}
KSyntaxHighlighting::SyntaxHighlighter::highlightBlock(text);
}
DiffEditor::DiffEditor(DiffParams::Flags f, QWidget *parent)
: QPlainTextEdit(parent)
, m_lineNumArea(new LineNumArea(this))
......
......@@ -15,13 +15,17 @@
class DiffSyntaxHighlighter final : public KSyntaxHighlighting::SyntaxHighlighter
{
public:
using KSyntaxHighlighting::SyntaxHighlighter::SyntaxHighlighter;
void applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format) override;
DiffSyntaxHighlighter(QTextDocument *parent, class DiffWidget *diffWidget);
void highlightBlock(const QString &text) override;
void applyFormat(int offset, int length, const KSyntaxHighlighting::Format &format) override;
void applyFolding(int, int, KSyntaxHighlighting::FoldingRegion) override
{
// no folding
}
private:
class DiffWidget *const m_diffWidget;
};
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
......
......@@ -35,8 +35,8 @@ DiffWidget::DiffWidget(DiffParams p, QWidget *parent)
layout->addWidget(m_left);
layout->addWidget(m_right);
leftHl = new DiffSyntaxHighlighter(m_left->document());
rightHl = new DiffSyntaxHighlighter(m_right->document());
leftHl = new DiffSyntaxHighlighter(m_left->document(), this);
rightHl = new DiffSyntaxHighlighter(m_right->document(), this);
leftHl->setTheme(KTextEditor::Editor::instance()->theme());
rightHl->setTheme(KTextEditor::Editor::instance()->theme());
......
......@@ -64,8 +64,8 @@ private:
class DiffEditor *m_left;
class DiffEditor *m_right;
KSyntaxHighlighting::SyntaxHighlighter *leftHl;
KSyntaxHighlighting::SyntaxHighlighter *rightHl;
KSyntaxHighlighting::AbstractHighlighter *leftHl;
KSyntaxHighlighting::AbstractHighlighter *rightHl;
DiffStyle m_style = SideBySide;
DiffParams m_params;
QByteArray m_rawDiff; // Raw diff saved as is
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment