summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/TextEditor.cpp
diff options
context:
space:
mode:
authorhuttongrabiel <huttonthomas@icloud.com>2022-06-18 12:03:49 -0700
committerSam Atkins <atkinssj@gmail.com>2022-07-08 11:47:56 +0100
commit9369610bf4097824b08df7fa89f6cfebc7c0706e (patch)
treec539894d80f50a8e3ab6b02fbce5b9cb382fcfda /Userland/Libraries/LibGUI/TextEditor.cpp
parent2fbaa7996c9f9fdbfcebf3af95fab88a89de62ac (diff)
downloadserenity-9369610bf4097824b08df7fa89f6cfebc7c0706e.zip
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.
Diffstat (limited to 'Userland/Libraries/LibGUI/TextEditor.cpp')
-rw-r--r--Userland/Libraries/LibGUI/TextEditor.cpp21
1 files changed, 21 insertions, 0 deletions
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<UnindentSelection>(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);