From 8017c1e17c7a7cc7a218ed8fbf05e8e5397e76cb Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 Mar 2019 11:08:36 +0100 Subject: [PATCH] GTextEditor: Let the Tab key insert up to 4 spaces. --- LibGUI/GTextEditor.cpp | 11 +++++++++++ LibGUI/GTextEditor.h | 1 + 2 files changed, 12 insertions(+) diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 8443c2c5941..bbaaface656 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -458,6 +458,17 @@ void GTextEditor::insert_at_cursor(char ch) set_cursor(m_cursor.line() + 1, 0); return; } + if (ch == '\t') { + int next_soft_tab_stop = ((m_cursor.column() + m_soft_tab_width) / m_soft_tab_width) * m_soft_tab_width; + int spaces_to_insert = next_soft_tab_stop - m_cursor.column(); + for (int i = 0; i < spaces_to_insert; ++i) { + current_line().insert(m_cursor.column(), ' '); + } + update_scrollbar_ranges(); + set_cursor(m_cursor.line(), next_soft_tab_stop); + update_cursor(); + return; + } current_line().insert(m_cursor.column(), ch); update_scrollbar_ranges(); set_cursor(m_cursor.line(), m_cursor.column() + 1); diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 28c30e709f2..de6e0355a5a 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -150,5 +150,6 @@ private: bool m_cursor_state { true }; bool m_in_drag_select { false }; int m_line_spacing { 2 }; + int m_soft_tab_width { 4 }; GTextRange m_selection; };