Skip to content

Commit

Permalink
make syntax highlighting customizeable
Browse files Browse the repository at this point in the history
fixes #434
  • Loading branch information
lievenhey committed Dec 7, 2022
1 parent 976b6a7 commit b23f460
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 58 deletions.
10 changes: 7 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 Expand Up @@ -223,3 +222,8 @@ QModelIndex DisassemblyModel::indexForFileLine(const Data::FileLine& fileLine) c
return {};
return index(bestMatch, 0);
}

void DisassemblyModel::setSyntaxHighlightDefinition(const KSyntaxHighlighting::Definition& definition)
{
m_highlighter->setDefinition(definition);
}
13 changes: 12 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 All @@ -59,6 +69,7 @@ class DisassemblyModel : public QAbstractTableModel

public slots:
void updateHighlighting(int line);
void setSyntaxHighlightDefinition(const KSyntaxHighlighting::Definition& definition);

private:
QTextDocument* m_document;
Expand Down
30 changes: 15 additions & 15 deletions src/models/highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@
#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_ASSERT(repository);
#else
Q_UNUSED(repository);
#endif
document->setDefaultFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

// if qApp is used, UBSAN complains
Expand All @@ -36,23 +41,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 Down
12 changes: 8 additions & 4 deletions src/models/highlighter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ 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);

signals:
void definitionChanged(const QString& definition);

protected:
bool eventFilter(QObject* watched, QEvent* event) override;
Expand All @@ -37,7 +40,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
};
10 changes: 7 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 Expand Up @@ -231,3 +230,8 @@ void SourceCodeModel::setSysroot(const QString& sysroot)
{
m_sysroot = sysroot;
}

void SourceCodeModel::setSyntaxHighlightDefinition(const KSyntaxHighlighting::Definition& definition)
{
m_highlighter->setDefinition(definition);
}
13 changes: 12 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 All @@ -59,6 +69,7 @@ class SourceCodeModel : public QAbstractTableModel
public slots:
void updateHighlighting(int line);
void setSysroot(const QString& sysroot);
void setSyntaxHighlightDefinition(const KSyntaxHighlighting::Definition& definition);

private:
QString m_sysroot;
Expand Down
58 changes: 56 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,39 @@ ResultsDisassemblyPage::ResultsDisassemblyPage(QWidget* parent)
}
}
});

#if KF5SyntaxHighlighting_FOUND
QStringList schemes;

for (const auto& entry : m_repository->definitions()) {
schemes.push_back(entry.name());
}

auto connectCompletion = [schemes, this](QComboBox* box, auto* model) {
auto m = new QStringListModel(this);
m->setStringList(schemes);

auto completer = new QCompleter(this);
completer->setModel(m);
completer->setCaseSensitivity(Qt::CaseInsensitive);
completer->setCompletionMode(QCompleter::PopupCompletion);
box->setCompleter(completer);

connect(box, qOverload<int>(&QComboBox::activated), this, [this, model, box]() {
model->setSyntaxHighlightDefinition(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 +233,13 @@ void ResultsDisassemblyPage::showDisassembly(const DisassemblyOutput& disassembl
m_disassemblyModel->clear();
m_sourceCodeModel->clear();

#if KF5SyntaxHighlighting_FOUND
auto sourceCodeDefinition = m_repository->definitionForFileName(disassemblyOutput.mainSourceFileName);

m_sourceCodeModel->setSyntaxHighlightDefinition(sourceCodeDefinition);
m_disassemblyModel->setSyntaxHighlightDefinition(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 b23f460

Please sign in to comment.