Use KSyntaxHighlighting (#1645)

This commit is contained in:
Florian Märkl 2019-07-11 15:21:54 +02:00 committed by GitHub
parent c7d582f00a
commit db3c34a9cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 107 additions and 9 deletions

View File

@ -7,12 +7,14 @@ endif()
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(DisallowInSource) include(DisallowInSource)
include(Utils)
set(CUTTER_PYTHON_MIN 3.5) set(CUTTER_PYTHON_MIN 3.5)
option(CUTTER_ENABLE_PYTHON "Enable Python integration. Requires Python >= ${CUTTER_PYTHON_MIN}." OFF) option(CUTTER_ENABLE_PYTHON "Enable Python integration. Requires Python >= ${CUTTER_PYTHON_MIN}." OFF)
option(CUTTER_ENABLE_PYTHON_BINDINGS "Enable generating Python bindings with Shiboken2. Unused if CUTTER_ENABLE_PYTHON=OFF." OFF) option(CUTTER_ENABLE_PYTHON_BINDINGS "Enable generating Python bindings with Shiboken2. Unused if CUTTER_ENABLE_PYTHON=OFF." OFF)
option(CUTTER_ENABLE_CRASH_REPORTS "Enable crash report system. Unused if CUTTER_ENABLE_CRASH_REPORTS=OFF" OFF) option(CUTTER_ENABLE_CRASH_REPORTS "Enable crash report system. Unused if CUTTER_ENABLE_CRASH_REPORTS=OFF" OFF)
tri_option(CUTTER_ENABLE_KSYNTAXHIGHLIGHTING "Use KSyntaxHighlighting" AUTO)
if(NOT CUTTER_ENABLE_PYTHON) if(NOT CUTTER_ENABLE_PYTHON)
set(CUTTER_ENABLE_PYTHON_BINDINGS OFF) set(CUTTER_ENABLE_PYTHON_BINDINGS OFF)
@ -95,6 +97,22 @@ if(CUTTER_ENABLE_PYTHON)
endif() endif()
endif() endif()
if(CUTTER_ENABLE_KSYNTAXHIGHLIGHTING)
if(CUTTER_ENABLE_KSYNTAXHIGHLIGHTING STREQUAL AUTO)
find_package(KF5SyntaxHighlighting)
if(KF5SyntaxHighlighting_FOUND)
set(KSYNTAXHIGHLIGHTING_STATUS ON)
else()
set(KSYNTAXHIGHLIGHTING_STATUS "OFF (KSyntaxHighlighting not found)")
endif()
else()
find_package(KF5SyntaxHighlighting REQUIRED)
set(KSYNTAXHIGHLIGHTING_STATUS ON)
endif()
else()
set(KSYNTAXHIGHLIGHTING_STATUS OFF)
endif()
message(STATUS "") message(STATUS "")
@ -103,6 +121,7 @@ message(STATUS "Options:")
message(STATUS "- Python: ${CUTTER_ENABLE_PYTHON}") message(STATUS "- Python: ${CUTTER_ENABLE_PYTHON}")
message(STATUS "- Python Bindings: ${CUTTER_ENABLE_PYTHON_BINDINGS}") message(STATUS "- Python Bindings: ${CUTTER_ENABLE_PYTHON_BINDINGS}")
message(STATUS "- Crash Handling: ${CUTTER_ENABLE_CRASH_REPORTS}") message(STATUS "- Crash Handling: ${CUTTER_ENABLE_CRASH_REPORTS}")
message(STATUS "- KSyntaxHighlighting: ${KSYNTAXHIGHLIGHTING_STATUS}")
message(STATUS "") message(STATUS "")
@ -184,4 +203,8 @@ if(CUTTER_ENABLE_PYTHON)
endif() endif()
endif() endif()
if(TARGET KF5::SyntaxHighlighting)
target_link_libraries(Cutter KF5::SyntaxHighlighting)
target_compile_definitions(Cutter PRIVATE CUTTER_ENABLE_KSYNTAXHIGHLIGHTING)
endif()

6
src/cmake/Utils.cmake Normal file
View File

@ -0,0 +1,6 @@
# Like option(), but the value can also be AUTO
macro(tri_option name desc default)
set("${name}" "${default}" CACHE STRING "${desc}")
set_property(CACHE "${name}" PROPERTY STRINGS AUTO ON OFF)
endmacro()

View File

@ -7,7 +7,15 @@
#include <QApplication> #include <QApplication>
#include <QLibraryInfo> #include <QLibraryInfo>
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
#include <KSyntaxHighlighting/repository.h>
#include <KSyntaxHighlighting/theme.h>
#include <KSyntaxHighlighting/syntaxhighlighter.h>
#include <KSyntaxHighlighting/definition.h>
#endif
#include "common/ColorThemeWorker.h" #include "common/ColorThemeWorker.h"
#include "common/SyntaxHighlighter.h"
/* Map with names of themes associated with its color palette /* Map with names of themes associated with its color palette
* (Dark or Light), so for dark interface themes will be shown only Dark color themes * (Dark or Light), so for dark interface themes will be shown only Dark color themes
@ -133,6 +141,9 @@ Configuration::Configuration() : QObject(), nativePalette(qApp->palette())
.arg(s.fileName()) .arg(s.fileName())
); );
} }
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
kSyntaxHighlightingRepository = nullptr;
#endif
} }
Configuration *Configuration::instance() Configuration *Configuration::instance()
@ -147,6 +158,10 @@ void Configuration::loadInitial()
setInterfaceTheme(getInterfaceTheme()); setInterfaceTheme(getInterfaceTheme());
setColorTheme(getColorTheme()); setColorTheme(getColorTheme());
applySavedAsmOptions(); applySavedAsmOptions();
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
kSyntaxHighlightingRepository = new KSyntaxHighlighting::Repository();
#endif
} }
QString Configuration::getDirProjects() QString Configuration::getDirProjects()
@ -397,6 +412,40 @@ const CutterInterfaceTheme *Configuration::getCurrentTheme()
return &cutterInterfaceThemesList()[i]; return &cutterInterfaceThemesList()[i];
} }
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
KSyntaxHighlighting::Repository *Configuration::getKSyntaxHighlightingRepository()
{
return kSyntaxHighlightingRepository;
}
KSyntaxHighlighting::Theme Configuration::getKSyntaxHighlightingTheme()
{
auto repo = getKSyntaxHighlightingRepository();
if (!repo) {
return KSyntaxHighlighting::Theme();
}
return repo->defaultTheme(
getCurrentTheme()->flag & DarkFlag
? KSyntaxHighlighting::Repository::DefaultTheme::DarkTheme
: KSyntaxHighlighting::Repository::DefaultTheme::LightTheme);
}
#endif
QSyntaxHighlighter *Configuration::createSyntaxHighlighter(QTextDocument *document)
{
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
auto syntaxHighlighter = new KSyntaxHighlighting::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);
#endif
}
QString Configuration::getLogoFile() QString Configuration::getLogoFile()
{ {
return windowColorIsDark() return windowColorIsDark()

View File

@ -8,6 +8,16 @@
#define Config() (Configuration::instance()) #define Config() (Configuration::instance())
#define ConfigColor(x) Config()->getColor(x) #define ConfigColor(x) Config()->getColor(x)
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
namespace KSyntaxHighlighting {
class Repository;
class Theme;
}
#endif
class QSyntaxHighlighter;
class QTextDocument;
enum ColorFlags { enum ColorFlags {
LightFlag = 1, LightFlag = 1,
DarkFlag = 2 DarkFlag = 2
@ -27,6 +37,10 @@ private:
QSettings s; QSettings s;
static Configuration *mPtr; static Configuration *mPtr;
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
KSyntaxHighlighting::Repository *kSyntaxHighlightingRepository;
#endif
// Colors // Colors
void loadBaseThemeNative(); void loadBaseThemeNative();
void loadBaseThemeDark(); void loadBaseThemeDark();
@ -77,6 +91,12 @@ public:
const CutterInterfaceTheme *getCurrentTheme(); const CutterInterfaceTheme *getCurrentTheme();
#ifdef CUTTER_ENABLE_KSYNTAXHIGHLIGHTING
KSyntaxHighlighting::Repository *getKSyntaxHighlightingRepository();
KSyntaxHighlighting::Theme getKSyntaxHighlightingTheme();
#endif
QSyntaxHighlighter *createSyntaxHighlighter(QTextDocument *document);
QString getDirProjects(); QString getDirProjects();
void setDirProjects(const QString &dir); void setDirProjects(const QString &dir);

View File

@ -15,7 +15,7 @@ TypesInteractionDialog::TypesInteractionDialog(QWidget *parent, bool readOnly) :
{ {
ui->setupUi(this); ui->setupUi(this);
ui->plainTextEdit->setPlainText(""); ui->plainTextEdit->setPlainText("");
syntaxHighLighter = new SyntaxHighlighter(ui->plainTextEdit->document()); syntaxHighLighter = Config()->createSyntaxHighlighter(ui->plainTextEdit->document());
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
ui->plainTextEdit->setReadOnly(readOnly); ui->plainTextEdit->setReadOnly(readOnly);
} }

View File

@ -7,7 +7,8 @@
namespace Ui { namespace Ui {
class TypesInteractionDialog; class TypesInteractionDialog;
} }
class SyntaxHighlighter;
class QSyntaxHighlighter;
class TypesInteractionDialog : public QDialog class TypesInteractionDialog : public QDialog
{ {
@ -48,7 +49,7 @@ private slots:
private: private:
std::unique_ptr<Ui::TypesInteractionDialog> ui; std::unique_ptr<Ui::TypesInteractionDialog> ui;
SyntaxHighlighter *syntaxHighLighter; QSyntaxHighlighter *syntaxHighLighter;
signals: signals:
/** /**

View File

@ -1,20 +1,19 @@
#include "PseudocodeWidget.h" #include "PseudocodeWidget.h"
#include "ui_PseudocodeWidget.h" #include "ui_PseudocodeWidget.h"
#include <QTextEdit>
#include "common/Configuration.h" #include "common/Configuration.h"
#include "common/Helpers.h" #include "common/Helpers.h"
#include "common/SyntaxHighlighter.h"
#include "common/TempConfig.h" #include "common/TempConfig.h"
#include <QTextEdit>
PseudocodeWidget::PseudocodeWidget(MainWindow *main, QAction *action) : PseudocodeWidget::PseudocodeWidget(MainWindow *main, QAction *action) :
MemoryDockWidget(CutterCore::MemoryWidgetType::Pseudocode, main, action), MemoryDockWidget(CutterCore::MemoryWidgetType::Pseudocode, main, action),
ui(new Ui::PseudocodeWidget) ui(new Ui::PseudocodeWidget)
{ {
ui->setupUi(this); ui->setupUi(this);
syntaxHighLighter = new SyntaxHighlighter(ui->textEdit->document()); syntaxHighlighter = Config()->createSyntaxHighlighter(ui->textEdit->document());
setupFonts(); setupFonts();
colorsUpdatedSlot(); colorsUpdatedSlot();

View File

@ -11,7 +11,7 @@ class PseudocodeWidget;
} }
class QTextEdit; class QTextEdit;
class SyntaxHighlighter; class QSyntaxHighlighter;
class PseudocodeWidget : public MemoryDockWidget class PseudocodeWidget : public MemoryDockWidget
{ {
@ -30,7 +30,7 @@ private:
enum DecompilerComboBoxValues { DecompilerCBR2Dec, DecompilerCBPdc }; enum DecompilerComboBoxValues { DecompilerCBR2Dec, DecompilerCBPdc };
std::unique_ptr<Ui::PseudocodeWidget> ui; std::unique_ptr<Ui::PseudocodeWidget> ui;
SyntaxHighlighter *syntaxHighLighter; QSyntaxHighlighter *syntaxHighlighter;
void doRefresh(RVA addr); void doRefresh(RVA addr);
void setupFonts(); void setupFonts();