From 9369610bf4097824b08df7fa89f6cfebc7c0706e Mon Sep 17 00:00:00 2001 From: huttongrabiel Date: Sat, 18 Jun 2022 12:03:49 -0700 Subject: LibGUI: Unindent selected text on shift+tab press Selected text is unindented when Shift+Tab is pressed. Select text, indent it with Tab, then unindent with Shift+Tab. --- Userland/Libraries/LibGUI/TextEditor.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'Userland/Libraries/LibGUI/TextEditor.cpp') diff --git a/Userland/Libraries/LibGUI/TextEditor.cpp b/Userland/Libraries/LibGUI/TextEditor.cpp index f61aabff25..698935fe75 100644 --- a/Userland/Libraries/LibGUI/TextEditor.cpp +++ b/Userland/Libraries/LibGUI/TextEditor.cpp @@ -869,6 +869,10 @@ void TextEditor::keydown_event(KeyEvent& event) if (event.key() == KeyCode::Key_Tab) { if (has_selection()) { + if (event.modifiers() == Mod_Shift) { + unindent_selection(); + return; + } if (is_indenting_selection()) { indent_selection(); return; @@ -988,6 +992,23 @@ void TextEditor::indent_selection() } } +void TextEditor::unindent_selection() +{ + auto const selection_start = m_selection.start() > m_selection.end() ? m_selection.end() : m_selection.start(); + auto const selection_end = m_selection.end() > m_selection.start() ? m_selection.end() : m_selection.start(); + + if (current_line().first_non_whitespace_column() != 0) { + if (current_line().first_non_whitespace_column() > m_soft_tab_width && selection_start.column() != 0) { + m_selection.set_start({ selection_start.line(), selection_start.column() - m_soft_tab_width }); + m_selection.set_end({ selection_end.line(), selection_end.column() - m_soft_tab_width }); + } else if (selection_start.column() != 0) { + m_selection.set_start({ selection_start.line(), selection_start.column() - current_line().leading_spaces() }); + m_selection.set_end({ selection_end.line(), selection_end.column() - current_line().leading_spaces() }); + } + execute(m_soft_tab_width, TextRange(selection_start, selection_end)); + } +} + void TextEditor::delete_previous_word() { TextRange to_erase(document().first_word_before(m_cursor, true), m_cursor); -- cgit v1.2.3