From effb18102de00bfd3ad6afdd6f6b504cfc054bbd Mon Sep 17 00:00:00 2001 From: Joel Blanco Berg Date: Sat, 14 Dec 2024 15:46:27 +0100 Subject: [PATCH] Fix CodeEditor indent bug (#7922) Also faster mousewheeling --- src/surge-xt/gui/overlays/LuaEditors.cpp | 27 ++++++++++++++++++------ src/surge-xt/gui/overlays/LuaEditors.h | 1 + 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/surge-xt/gui/overlays/LuaEditors.cpp b/src/surge-xt/gui/overlays/LuaEditors.cpp index e8ae74b3cf3..9f5353dff8e 100644 --- a/src/surge-xt/gui/overlays/LuaEditors.cpp +++ b/src/surge-xt/gui/overlays/LuaEditors.cpp @@ -791,15 +791,24 @@ void SurgeCodeEditorComponent::caretPositionMoved() } } +void SurgeCodeEditorComponent::mouseWheelMove(const juce::MouseEvent &e, + const juce::MouseWheelDetails &wheel) +{ + juce::MouseWheelDetails w(wheel); + w.deltaY *= 4; + CodeEditorComponent::mouseWheelMove(e, w); +} + // Handles auto indentation void SurgeCodeEditorComponent::handleReturnKey() { - auto pos = this->getCaretPos(); auto txt = pos.getLineText(); int tabs = 0; - + int indexInLine = pos.getIndexInLine(); + int actualCharactersBeforeCaret = 0; + bool indent = false; for (int i = 0; i < txt.length(); i++) { if (txt.substring(i, i + 1) == " ") @@ -812,7 +821,7 @@ void SurgeCodeEditorComponent::handleReturnKey() } else { - bool indent = false; + auto trimmedTxt = txt.trim(); if (txt.substring(i, i + 8) == "function") @@ -839,10 +848,17 @@ void SurgeCodeEditorComponent::handleReturnKey() break; } + + if (i < indexInLine) + { + actualCharactersBeforeCaret = tabs; + } } this->insertTextAtCaret("\n"); - this->insertTextAtCaret(std::string(tabs, ' ')); + this->insertTextAtCaret(std::string( + std::min(actualCharactersBeforeCaret + (indent == true ? this->getTabSize() : 0), tabs), + ' ')); } struct EditorColors @@ -1135,7 +1151,6 @@ bool CodeEditorContainerWithApply::autoCompleteStringDeclaration(juce::String st mainEditor->insertTextAtCaret(str); } return true; - // sdfsd } void CodeEditorContainerWithApply::paint(juce::Graphics &g) { g.fillAll(juce::Colours::black); } @@ -3003,4 +3018,4 @@ WavetableScriptEditor::getPreCloseChickenBoxMessage() } } // namespace Overlays -} // namespace Surge \ No newline at end of file +} // namespace Surge diff --git a/src/surge-xt/gui/overlays/LuaEditors.h b/src/surge-xt/gui/overlays/LuaEditors.h index 4e6f842da6c..75771744a01 100644 --- a/src/surge-xt/gui/overlays/LuaEditors.h +++ b/src/surge-xt/gui/overlays/LuaEditors.h @@ -199,6 +199,7 @@ class SurgeCodeEditorComponent : public juce::CodeEditorComponent virtual void paint(juce::Graphics &) override; virtual void setSearch(CodeEditorSearch &s); virtual void setGotoLine(GotoLine &s); + void mouseWheelMove(const juce::MouseEvent &e, const juce::MouseWheelDetails &d) override; SurgeCodeEditorComponent(juce::CodeDocument &d, juce::CodeTokeniser *t, Surge::GUI::Skin::ptr_t &skin);