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 5, 2022
1 parent 976b6a7 commit 135d1bd
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 56 deletions.
12 changes: 9 additions & 3 deletions src/models/disassemblymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
#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"));
// TODO
// m_highlighter->setDefinitionForName(QStringLiteral("GNU Assembler"));
}

DisassemblyModel::~DisassemblyModel() = default;
Expand Down Expand Up @@ -223,3 +224,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);
}
8 changes: 7 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 = nullptr, QObject* parent = nullptr);
~DisassemblyModel();

void setDisassembly(const DisassemblyOutput& disassemblyOutput, const Data::CallerCalleeResults& results);
Expand Down Expand Up @@ -59,6 +64,7 @@ class DisassemblyModel : public QAbstractTableModel

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

private:
QTextDocument* m_document;
Expand Down
27 changes: 12 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 == 0
Q_UNUSED(repository);
#endif
document->setDefaultFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));

// if qApp is used, UBSAN complains
Expand All @@ -36,23 +39,17 @@ 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();
#else
Q_UNUSED(name);
Q_UNUSED(definition);
#endif
}

Expand Down
9 changes: 5 additions & 4 deletions src/models/highlighter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ 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);

protected:
bool eventFilter(QObject* watched, QEvent* event) override;
Expand All @@ -37,7 +37,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
};
11 changes: 8 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,7 @@ void SourceCodeModel::setDisassembly(const DisassemblyOutput& disassemblyOutput,
const auto sourceCode = QString::fromUtf8(file.readAll());

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

m_document->setPlainText(sourceCode);
m_document->setTextWidth(m_document->idealWidth());
Expand Down Expand Up @@ -231,3 +231,8 @@ void SourceCodeModel::setSysroot(const QString& sysroot)
{
m_sysroot = sysroot;
}

void SourceCodeModel::setSyntaxHighlightDefinition(const KSyntaxHighlighting::Definition& definition)
{
m_highlighter->setDefinition(definition);
}
8 changes: 7 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 = nullptr, QObject* parent = nullptr);
~SourceCodeModel();

void clear();
Expand Down Expand Up @@ -59,6 +64,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 @@ -25,9 +25,17 @@

#include <KRecursiveFilterProxyModel>

#include "hotspot-config.h"
#include "parsers/perf/perfparser.h"
#include "resultsutil.h"

#if KF5SyntaxHighlighting_FOUND
#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,11 @@
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)
#endif
, m_disassemblyModel(new DisassemblyModel(m_repository.get(), this))
, m_sourceCodeModel(new SourceCodeModel(m_repository.get(), this))
, 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 +134,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(completer, qOverload<const QModelIndex&>(&QCompleter::activated), this,
[this, model](const QModelIndex& entry) {
model->setSyntaxHighlightDefinition(m_repository->definitionForName(entry.data().toString()));
});
connect(box, qOverload<int>(&QComboBox::activated), this, [this, model, box]() {
model->setSyntaxHighlightDefinition(m_repository->definitionForName(box->currentText()));
});
};

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 +230,16 @@ void ResultsDisassemblyPage::showDisassembly(const DisassemblyOutput& disassembl
m_disassemblyModel->clear();
m_sourceCodeModel->clear();

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

ui->assemblyComboBox->setCurrentText(QStringLiteral("GNU Assembler"));
ui->sourceCodeComboBox->setCurrentText(sourceCodeDefinition.name());

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
5 changes: 5 additions & 0 deletions src/resultsdisassemblypage.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct DisassemblyOutput;
class DisassemblyModel;
class SourceCodeModel;

namespace KSyntaxHighlighting {
class Repository;
}

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

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

0 comments on commit 135d1bd

Please sign in to comment.