Make editor indent a settings parameter
MODIFY
src/editor/codeeditor.cpp
src/editor/codeeditor.cpp
#include "csyntaxhighlighter.h"
#include "processorhandler.h"
+#include "ripessettings.h"
#include "rvsyntaxhighlighter.h"
namespace Ripes {
@@ -96,7 +97,12 @@ inline int indentationOf(const QString& text) {
}
void CodeEditor::keyPressEvent(QKeyEvent* e) {
- constexpr int indentAmt = 4;
+ const unsigned indentAmt = RipesSettings::value(RIPES_SETTING_INDENTAMT).toUInt();
+
+ /**
+ * The following is a collection of quality-of-life changes to the behaviour of the editor, mimmicking features
+ * generally found in IDEs.
+ */
if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter) {
const auto lineText = textCursor().block().text();
unsigned indent = 0;
@@ -104,7 +110,7 @@ void CodeEditor::keyPressEvent(QKeyEvent* e) {
indent = indentationOf(lineText);
// QoL 2: Further indent if last character was an indent start character
- const auto trimmedLineText = lineText.trimmed();
+ const auto trimmedLineText = textCursor().block().text().left(textCursor().positionInBlock()).trimmed();
if (!trimmedLineText.isEmpty()) {
if (QStringList{":", "(", "{", "["}.contains(QString(trimmedLineText.back()))) {
indent += indentAmt;
@@ -114,6 +120,15 @@ void CodeEditor::keyPressEvent(QKeyEvent* e) {
insertPlainText(QString(" ").repeated(indent));
} else if (e->key() == Qt::Key_Tab) {
insertPlainText(QString(" ").repeated(indentAmt));
+ } else if (e->key() == Qt::Key_Backspace) {
+ const auto preLineText = textCursor().block().text().left(textCursor().positionInBlock());
+ if (preLineText.endsWith(QString(" ").repeated(indentAmt))) {
+ for (unsigned i = 0; i < indentAmt; i++) {
+ textCursor().deletePreviousChar();
+ }
+ } else {
+ QPlainTextEdit::keyPressEvent(e);
+ }
} else {
QPlainTextEdit::keyPressEvent(e);
}

MODIFY
src/ripessettings.h
src/ripessettings.h
@@ -16,6 +16,7 @@ namespace Ripes {
#define RIPES_SETTING_CONSOLEBG ("console_bg_color")
#define RIPES_SETTING_CONSOLEFONTCOLOR ("console_font_color")
#define RIPES_SETTING_CONSOLEFONT ("console_font")
+#define RIPES_SETTING_INDENTAMT ("editor_indent")
// Program state preserving settings
#define RIPES_SETTING_SETTING_TAB ("settings_tab")
@@ -37,6 +38,7 @@ const static std::map<QString, QVariant> s_defaultSettings = {
{RIPES_SETTING_CONSOLEFONTCOLOR, QColor(Qt::black)},
{RIPES_SETTING_CONSOLEFONT, QVariant() /* Let Console define its own default font */},
{RIPES_SETTING_CONSOLEFONT, QColor(Qt::black)},
+ {RIPES_SETTING_INDENTAMT, 4},
// Program state preserving settings
{RIPES_SETTING_SETTING_TAB, 0},

MODIFY
src/settingsdialog.cpp
src/settingsdialog.cpp
@@ -117,6 +117,7 @@ SettingsDialog::SettingsDialog(QWidget* parent) : QDialog(parent), m_ui(new Ui::
// Create settings pages
addPage("Editor", createEditorPage());
+ addPage("Compiler", createCompilerPage());
addPage("Simulator", createSimulatorPage());
addPage("Environment", createEnvironmentPage());
@@ -131,7 +132,7 @@ void SettingsDialog::accept() {
QDialog::accept();
}
-QWidget* SettingsDialog::createEditorPage() {
+QWidget* SettingsDialog::createCompilerPage() {
auto [pageWidget, pageLayout] = constructPage();
// Setting: RIPES_SETTING_CCPATH
@@ -244,6 +245,18 @@ QWidget* SettingsDialog::createSimulatorPage() {
return pageWidget;
}
+QWidget* SettingsDialog::createEditorPage() {
+ auto [pageWidget, pageLayout] = constructPage();
+
+ auto [indentLabel, indentSpinbox] = createSettingsWidgets<QSpinBox>(RIPES_SETTING_INDENTAMT, "Indent size:");
+ indentSpinbox->setRange(0, 100);
+
+ pageLayout->addWidget(indentLabel, 0, 0);
+ pageLayout->addWidget(indentSpinbox, 0, 1);
+
+ return pageWidget;
+}
+
QWidget* SettingsDialog::createEnvironmentPage() {
auto* consoleGroupBox = new QGroupBox("Console");
auto* consoleLayout = new QGridLayout();

MODIFY
src/settingsdialog.h
src/settingsdialog.h
@@ -31,9 +31,10 @@ private:
QLabel* m_compileInfo = nullptr;
QLabel* m_compileInfoHeader = nullptr;
- QWidget* createEditorPage();
+ QWidget* createCompilerPage();
QWidget* createSimulatorPage();
QWidget* createEnvironmentPage();
+ QWidget* createEditorPage();
Ui::SettingsDialog* m_ui;

Make editor indent a settings parameter