Make Syntax Highlighter react to Theme Changes (#1650)

This commit is contained in:
Florian Märkl 2019-07-12 15:37:19 +02:00 committed by GitHub
parent 8ec780db22
commit 7c605e3fed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 11 deletions

View File

@ -10,7 +10,6 @@
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
#include <KSyntaxHighlighting/repository.h>
#include <KSyntaxHighlighting/theme.h>
#include <KSyntaxHighlighting/syntaxhighlighter.h>
#include <KSyntaxHighlighting/definition.h>
#endif
@ -400,6 +399,9 @@ void Configuration::setInterfaceTheme(int theme)
emit interfaceThemeChanged();
emit colorsUpdated();
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
emit kSyntaxHighlightingThemeChanged();
#endif
}
const CutterInterfaceTheme *Configuration::getCurrentTheme()
@ -434,15 +436,14 @@ KSyntaxHighlighting::Theme Configuration::getKSyntaxHighlightingTheme()
QSyntaxHighlighter *Configuration::createSyntaxHighlighter(QTextDocument *document)
{
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
auto syntaxHighlighter = new KSyntaxHighlighting::SyntaxHighlighter(document);
auto syntaxHighlighter = new SyntaxHighlighter(document);
auto repo = getKSyntaxHighlightingRepository();
if (repo) {
syntaxHighlighter->setDefinition(repo->definitionForName("C"));
syntaxHighlighter->setTheme(repo->defaultTheme(KSyntaxHighlighting::Repository::DefaultTheme::DarkTheme));
}
return syntaxHighlighter;
#else
return new SyntaxHighlighter(document);
return new FallbackSyntaxHighlighter(document);
#endif
}

View File

@ -152,6 +152,9 @@ signals:
void fontsUpdated();
void colorsUpdated();
void interfaceThemeChanged();
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
void kSyntaxHighlightingThemeChanged();
#endif
};
#endif // CONFIGURATION_H

View File

@ -1,6 +1,28 @@
#include "SyntaxHighlighter.h"
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
#include "Configuration.h"
#include <KSyntaxHighlighting/theme.h>
SyntaxHighlighter::SyntaxHighlighter(QTextDocument *document) : KSyntaxHighlighting::SyntaxHighlighter(document)
{
connect(Config(), &Configuration::kSyntaxHighlightingThemeChanged, this, &SyntaxHighlighter::updateTheme);
updateTheme();
}
void SyntaxHighlighter::updateTheme()
{
setTheme(Config()->getKSyntaxHighlightingTheme());
rehighlight();
}
#endif
FallbackSyntaxHighlighter::FallbackSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
, commentStartExpression("/\\*")
, commentEndExpression("\\*/")
@ -55,7 +77,7 @@ SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
multiLineCommentFormat.setForeground(Qt::gray);
}
void SyntaxHighlighter::highlightBlock(const QString &text)
void FallbackSyntaxHighlighter::highlightBlock(const QString &text)
{
for ( const auto &it : highlightingRules ) {
auto matchIterator = it.pattern.globalMatch(text);

View File

@ -1,4 +1,5 @@
#pragma once
#ifndef SYNTAXHIGHLIGHTER_H
#define SYNTAXHIGHLIGHTER_H
#include <QSyntaxHighlighter>
#include <QVector>
@ -6,14 +7,33 @@
#include <QRegularExpression>
#include <QTextCharFormat>
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
class SyntaxHighlighter : public QSyntaxHighlighter
#include <KSyntaxHighlighting/syntaxhighlighter.h>
class SyntaxHighlighter : public KSyntaxHighlighting::SyntaxHighlighter
{
Q_OBJECT
public:
SyntaxHighlighter(QTextDocument *parent = nullptr);
virtual ~SyntaxHighlighter() = default;
SyntaxHighlighter(QTextDocument *document);
private slots:
void updateTheme();
};
#endif
/**
* SyntaxHighlighter to be used when KSyntaxHighlighting is not available
*/
class FallbackSyntaxHighlighter : public QSyntaxHighlighter
{
Q_OBJECT
public:
FallbackSyntaxHighlighter(QTextDocument *parent = nullptr);
virtual ~FallbackSyntaxHighlighter() = default;
protected:
void highlightBlock(const QString &text) override;
@ -31,3 +51,5 @@ private:
QTextCharFormat multiLineCommentFormat;
};
#endif

View File

@ -14,7 +14,7 @@
#include "common/CutterSeekable.h"
class QTextEdit;
class SyntaxHighlighter;
class FallbackSyntaxHighlighter;
class DisassemblerGraphView : public GraphView
{