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

View File

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

View File

@ -1,6 +1,28 @@
#include "SyntaxHighlighter.h" #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) : QSyntaxHighlighter(parent)
, commentStartExpression("/\\*") , commentStartExpression("/\\*")
, commentEndExpression("\\*/") , commentEndExpression("\\*/")
@ -55,7 +77,7 @@ SyntaxHighlighter::SyntaxHighlighter(QTextDocument *parent)
multiLineCommentFormat.setForeground(Qt::gray); multiLineCommentFormat.setForeground(Qt::gray);
} }
void SyntaxHighlighter::highlightBlock(const QString &text) void FallbackSyntaxHighlighter::highlightBlock(const QString &text)
{ {
for ( const auto &it : highlightingRules ) { for ( const auto &it : highlightingRules ) {
auto matchIterator = it.pattern.globalMatch(text); auto matchIterator = it.pattern.globalMatch(text);

View File

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

View File

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