diff options
author | Julian Eigmüller <j.eigmueller17@gmail.com> | 2022-10-28 14:42:32 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-11-09 21:27:47 +0100 |
commit | 0ea399d8d691d4446f632f54a22942277edcb5e1 (patch) | |
tree | 9745edc1f9b20af9a3e6d70efd8caa5ffada6cd5 /Userland/Libraries/LibGUI | |
parent | 03d0c7a5b66683be81778d6d9abe0e0255463206 (diff) | |
download | serenity-0ea399d8d691d4446f632f54a22942277edcb5e1.zip |
LibGUI: Shift+Tab unindents line
Previously, pressing Shift+Tab would indent the line if no selection was
given. While with a selection, it would be unindented. With this change,
pressing Shift+Tab with no selection unindents the current line.
For this, add unindent_line() helper function. This function unindents the
current line by at most one tab width if it starts with whitespace,
regardless of cursor position.
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.cpp | 17 | ||||
-rw-r--r-- | Userland/Libraries/LibGUI/TextEditor.h | 1 |
2 files changed, 18 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index dfdedb3d08..6e53fec67b 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -918,6 +918,11 @@ void TextEditor::keydown_event(KeyEvent& event) indent_selection(); return; } + } else { + if (event.modifiers() == Mod_Shift) { + unindent_line(); + return; + } } } @@ -1050,6 +1055,18 @@ void TextEditor::unindent_selection() } } +void TextEditor::unindent_line() +{ + if (current_line().first_non_whitespace_column() != 0) { + auto const unindent_size = current_line().leading_spaces() < m_soft_tab_width ? current_line().leading_spaces() : m_soft_tab_width; + auto const temp_column = m_cursor.column(); + + execute<UnindentSelection>(unindent_size, TextRange({ m_cursor.line(), 0 }, { m_cursor.line(), line(m_cursor.line()).length() })); + + set_cursor({ m_cursor.line(), temp_column <= unindent_size ? 0 : temp_column - unindent_size }); + } +} + void TextEditor::delete_previous_word() { TextRange to_erase(document().first_word_before(m_cursor, true), m_cursor); diff --git a/Userland/Libraries/LibGUI/TextEditor.h b/Userland/Libraries/LibGUI/TextEditor.h index 9a9b69f0a8..e6f4d13f31 100644 --- a/Userland/Libraries/LibGUI/TextEditor.h +++ b/Userland/Libraries/LibGUI/TextEditor.h @@ -158,6 +158,7 @@ public: bool is_indenting_selection(); void indent_selection(); void unindent_selection(); + void unindent_line(); Function<void()> on_change; Function<void(bool modified)> on_modified_change; |