summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/TextDocument.cpp
diff options
context:
space:
mode:
authorhuttongrabiel <huttonthomas@icloud.com>2022-06-18 12:02:51 -0700
committerSam Atkins <atkinssj@gmail.com>2022-07-08 11:47:56 +0100
commit2fbaa7996c9f9fdbfcebf3af95fab88a89de62ac (patch)
tree082799b85b6dacd387c798328c6e2f754f3670d7 /Userland/Libraries/LibGUI/TextDocument.cpp
parent80705a72bde015c824126ea37e55337439b89b76 (diff)
downloadserenity-2fbaa7996c9f9fdbfcebf3af95fab88a89de62ac.zip
LibGUI: Indent selected text on tab press
If selected text is less than a whole line, usual delete/replace takes place. Otherwise, if the selected text is a whole line or spans multiple lines, the selection will be indented.
Diffstat (limited to 'Userland/Libraries/LibGUI/TextDocument.cpp')
-rw-r--r--Userland/Libraries/LibGUI/TextDocument.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/TextDocument.cpp b/Userland/Libraries/LibGUI/TextDocument.cpp
index 10a8f76cb3..cc0f60d1ad 100644
--- a/Userland/Libraries/LibGUI/TextDocument.cpp
+++ b/Userland/Libraries/LibGUI/TextDocument.cpp
@@ -932,6 +932,33 @@ String ReplaceAllTextCommand::action_text() const
return m_action_text;
}
+IndentSelection::IndentSelection(TextDocument& document, size_t tab_width, TextRange const& range)
+ : TextDocumentUndoCommand(document)
+ , m_tab_width(tab_width)
+ , m_range(range)
+{
+}
+
+void IndentSelection::redo()
+{
+ auto const tab = String::repeated(' ', m_tab_width);
+
+ for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
+ m_document.insert_at({ i, 0 }, tab, m_client);
+ }
+
+ m_document.set_all_cursors(m_range.start());
+}
+
+void IndentSelection::undo()
+{
+ for (size_t i = m_range.start().line(); i <= m_range.end().line(); i++) {
+ m_document.remove({ { i, 0 }, { i, m_tab_width } });
+ }
+
+ m_document.set_all_cursors(m_range.start());
+}
+
TextPosition TextDocument::insert_at(TextPosition const& position, StringView text, Client const* client)
{
TextPosition cursor = position;