Skip to content

Commit

Permalink
make syntax highlighting customizeable (#448)
Browse files Browse the repository at this point in the history
fixes #434
  • Loading branch information
lievenhey authored Dec 12, 2022
1 parent 1f54407 commit ff86112
Show file tree
Hide file tree
Showing 10 changed files with 311 additions and 58 deletions.
5 changes: 2 additions & 3 deletions src/models/disassemblymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
#include "highlighter.hpp"
#include "sourcecodemodel.h"

DisassemblyModel::DisassemblyModel(QObject* parent)
DisassemblyModel::DisassemblyModel(KSyntaxHighlighting::Repository* repository, QObject* parent)
: QAbstractTableModel(parent)
, m_document(new QTextDocument(this))
, m_highlighter(new Highlighter(m_document, this))
, m_highlighter(new Highlighter(m_document, repository, this))
{
m_document->setUndoRedoEnabled(false);
m_highlighter->setDefinitionForName(QStringLiteral("GNU Assembler"));
}

DisassemblyModel::~DisassemblyModel() = default;
Expand Down
12 changes: 11 additions & 1 deletion src/models/disassemblymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
class QTextDocument;
class Highlighter;

namespace KSyntaxHighlighting {
class Definition;
class Repository;
}

class DisassemblyModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit DisassemblyModel(QObject* parent = nullptr);
explicit DisassemblyModel(KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
~DisassemblyModel();

void setDisassembly(const DisassemblyOutput& disassemblyOutput, const Data::CallerCalleeResults& results);
Expand All @@ -39,6 +44,11 @@ class DisassemblyModel : public QAbstractTableModel
Data::FileLine fileLineForIndex(const QModelIndex& index) const;
QModelIndex indexForFileLine(const Data::FileLine& line) const;

Highlighter* highlighter() const
{
return m_highlighter;
}

enum Columns
{
AddrColumn,
Expand Down
32 changes: 17 additions & 15 deletions src/models/highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
#include <KSyntaxHighlighting/Theme>
#endif

Highlighter::Highlighter(QTextDocument* document, QObject* parent)
Highlighter::Highlighter(QTextDocument* document, KSyntaxHighlighting::Repository* repository, QObject* parent)
: QObject(parent)
#if KF5SyntaxHighlighting_FOUND
, m_repository(std::make_unique<KSyntaxHighlighting::Repository>())
, m_highlighter(new KSyntaxHighlighting::SyntaxHighlighter(document))
, m_repository(repository)
#endif
{
#if !KF5SyntaxHighlighting_FOUND
Q_UNUSED(repository);
#endif
document->setDefaultFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

// if qApp is used, UBSAN complains
Expand All @@ -36,23 +39,18 @@ Highlighter::Highlighter(QTextDocument* document, QObject* parent)

Highlighter::~Highlighter() = default;

void Highlighter::setDefinitionForFilename(const QString& filename)
void Highlighter::setDefinition(const KSyntaxHighlighting::Definition& definition)
{
#if KF5SyntaxHighlighting_FOUND
const auto def = m_repository->definitionForFileName(filename);
m_highlighter->setDefinition(def);
#else
Q_UNUSED(filename);
#endif
}
// don't reparse if definition hasn't changed
if (m_currentDefinition == definition.name())
return;

void Highlighter::setDefinitionForName(const QString& name)
{
#if KF5SyntaxHighlighting_FOUND
const auto def = m_repository->definitionForName(name);
m_highlighter->setDefinition(def);
m_highlighter->setDefinition(definition);
m_currentDefinition = definition.name();
emit definitionChanged(m_currentDefinition);
#else
Q_UNUSED(name);
Q_UNUSED(definition);
#endif
}

Expand All @@ -68,6 +66,10 @@ bool Highlighter::eventFilter(QObject* /*watched*/, QEvent* event)
void Highlighter::updateColorTheme()
{
#if KF5SyntaxHighlighting_FOUND
if (!m_repository) {
return;
}

KSyntaxHighlighting::Repository::DefaultTheme theme;
if (QPalette().base().color().lightness() < 128) {
theme = KSyntaxHighlighting::Repository::DarkTheme;
Expand Down
21 changes: 17 additions & 4 deletions src/models/highlighter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@ class QTextDocument;

namespace KSyntaxHighlighting {
class SyntaxHighlighter;
class Definition;
class Repository;
}

class Highlighter : public QObject
{
Q_OBJECT
public:
Highlighter(QTextDocument* document, QObject* parent = nullptr);
Highlighter(QTextDocument* document, KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
~Highlighter();

void setDefinitionForFilename(const QString& filename);
void setDefinitionForName(const QString& name);
void setDefinition(const KSyntaxHighlighting::Definition& definition);

QString definition() const
{
#if KF5SyntaxHighlighting_FOUND
return m_currentDefinition;
#else
return {};
#endif
}

signals:
void definitionChanged(const QString& definition);

protected:
bool eventFilter(QObject* watched, QEvent* event) override;
Expand All @@ -37,7 +49,8 @@ class Highlighter : public QObject
void updateColorTheme();

#if KF5SyntaxHighlighting_FOUND
std::unique_ptr<KSyntaxHighlighting::Repository> m_repository;
KSyntaxHighlighting::SyntaxHighlighter* m_highlighter = nullptr;
KSyntaxHighlighting::Repository* m_repository = nullptr;
QString m_currentDefinition;
#endif
};
5 changes: 2 additions & 3 deletions src/models/sourcecodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

#include "highlighter.hpp"

SourceCodeModel::SourceCodeModel(QObject* parent)
SourceCodeModel::SourceCodeModel(KSyntaxHighlighting::Repository* repository, QObject* parent)
: QAbstractTableModel(parent)
, m_document(new QTextDocument(this))
, m_highlighter(new Highlighter(m_document, this))
, m_highlighter(new Highlighter(m_document, repository, this))
{
m_document->setUndoRedoEnabled(false);
qRegisterMetaType<QTextLine>();
Expand Down Expand Up @@ -64,7 +64,6 @@ void SourceCodeModel::setDisassembly(const DisassemblyOutput& disassemblyOutput,
const auto sourceCode = QString::fromUtf8(file.readAll());

m_document->clear();
m_highlighter->setDefinitionForFilename(disassemblyOutput.mainSourceFileName);

m_document->setPlainText(sourceCode);
m_document->setTextWidth(m_document->idealWidth());
Expand Down
12 changes: 11 additions & 1 deletion src/models/sourcecodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ class QTextDocument;

class Highlighter;

namespace KSyntaxHighlighting {
class Repository;
class Definition;
}

Q_DECLARE_METATYPE(QTextLine)

class SourceCodeModel : public QAbstractTableModel
{
Q_OBJECT
public:
explicit SourceCodeModel(QObject* parent = nullptr);
explicit SourceCodeModel(KSyntaxHighlighting::Repository* repository, QObject* parent = nullptr);
~SourceCodeModel();

void clear();
Expand All @@ -39,6 +44,11 @@ class SourceCodeModel : public QAbstractTableModel
Data::FileLine fileLineForIndex(const QModelIndex& index) const;
QModelIndex indexForFileLine(const Data::FileLine& line) const;

Highlighter* highlighter() const
{
return m_highlighter;
}

enum Columns
{
SourceCodeLineNumber,
Expand Down
60 changes: 58 additions & 2 deletions src/resultsdisassemblypage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
#include "parsers/perf/perfparser.h"
#include "resultsutil.h"

#if KF5SyntaxHighlighting_FOUND
#include "highlighter.hpp"
#include <KSyntaxHighlighting/definition.h>
#include <KSyntaxHighlighting/repository.h>
#include <QCompleter>
#include <QStringListModel>
#endif

#include "data.h"
#include "models/codedelegate.h"
#include "models/costdelegate.h"
Expand All @@ -41,8 +49,14 @@
ResultsDisassemblyPage::ResultsDisassemblyPage(QWidget* parent)
: QWidget(parent)
, ui(new Ui::ResultsDisassemblyPage)
, m_disassemblyModel(new DisassemblyModel(this))
, m_sourceCodeModel(new SourceCodeModel(this))
#if KF5SyntaxHighlighting_FOUND
, m_repository(new KSyntaxHighlighting::Repository)
, m_disassemblyModel(new DisassemblyModel(m_repository.get(), this))
, m_sourceCodeModel(new SourceCodeModel(m_repository.get(), this))
#else
, m_disassemblyModel(new DisassemblyModel(nullptr, this))
, m_sourceCodeModel(new SourceCodeModel(nullptr, this))
#endif
, m_disassemblyCostDelegate(new CostDelegate(DisassemblyModel::CostRole, DisassemblyModel::TotalCostRole, this))
, m_sourceCodeCostDelegate(new CostDelegate(SourceCodeModel::CostRole, SourceCodeModel::TotalCostRole, this))
, m_disassemblyDelegate(new CodeDelegate(DisassemblyModel::RainbowLineNumberRole, DisassemblyModel::HighlightRole,
Expand Down Expand Up @@ -123,6 +137,42 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(QWidget* parent)
}
}
});

#if KF5SyntaxHighlighting_FOUND
QStringList schemes;

const auto definitions = m_repository->definitions();
schemes.reserve(definitions.size());

std::transform(definitions.begin(), definitions.end(), std::back_inserter(schemes),
[](const auto& definition) { return definition.name(); });

auto definitionModel = new QStringListModel(this);
definitionModel->setStringList(schemes);

auto connectCompletion = [definitionModel, schemes, this](QComboBox* box, auto* model) {
auto completer = new QCompleter(this);
completer->setModel(definitionModel);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
box->setCompleter(completer);
box->setModel(definitionModel);
box->setCurrentText(model->highlighter()->definition());

connect(box, qOverload<int>(&QComboBox::activated), this, [this, model, box]() {
model->highlighter()->setDefinition(m_repository->definitionForName(box->currentText()));
});

connect(model->highlighter(), &Highlighter::definitionChanged,
[box](const QString& definition) { box->setCurrentText(definition); });
};

connectCompletion(ui->sourceCodeComboBox, m_sourceCodeModel);
connectCompletion(ui->assemblyComboBox, m_disassemblyModel);
#else
ui->customSourceCodeHighlighting->setVisible(false);
ui->customAssemblyHighlighting->setVisible(false);
#endif
}

ResultsDisassemblyPage::~ResultsDisassemblyPage() = default;
Expand Down Expand Up @@ -186,6 +236,12 @@ void ResultsDisassemblyPage::showDisassembly(const DisassemblyOutput& disassembl
m_disassemblyModel->clear();
m_sourceCodeModel->clear();

#if KF5SyntaxHighlighting_FOUND
m_sourceCodeModel->highlighter()->setDefinition(
m_repository->definitionForFileName(disassemblyOutput.mainSourceFileName));
m_disassemblyModel->highlighter()->setDefinition(m_repository->definitionForName(QStringLiteral("GNU Assembler")));
#endif

const auto& entry = m_callerCalleeResults.entry(m_curSymbol);

ui->filenameLabel->setText(disassemblyOutput.mainSourceFileName);
Expand Down
8 changes: 8 additions & 0 deletions src/resultsdisassemblypage.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include "data.h"
#include "hotspot-config.h"
#include "models/costdelegate.h"
#include <QWidget>

Expand All @@ -34,6 +35,10 @@ struct DisassemblyOutput;
class DisassemblyModel;
class SourceCodeModel;

namespace KSyntaxHighlighting {
class Repository;
}

class ResultsDisassemblyPage : public QWidget
{
Q_OBJECT
Expand All @@ -58,6 +63,9 @@ class ResultsDisassemblyPage : public QWidget
void showDisassembly(const DisassemblyOutput& disassemblyOutput);

QScopedPointer<Ui::ResultsDisassemblyPage> ui;
#if KF5SyntaxHighlighting_FOUND
QScopedPointer<KSyntaxHighlighting::Repository> m_repository;
#endif
// Model
DisassemblyModel* m_disassemblyModel;
SourceCodeModel* m_sourceCodeModel;
Expand Down
Loading

0 comments on commit ff86112

Please sign in to comment.