diff options
-rw-r--r-- | Libraries/LibGUI/GTextDocument.cpp | 18 | ||||
-rw-r--r-- | Libraries/LibGUI/GTextDocument.h | 2 | ||||
-rw-r--r-- | Libraries/LibGUI/GTextEditor.cpp | 13 |
3 files changed, 21 insertions, 12 deletions
diff --git a/Libraries/LibGUI/GTextDocument.cpp b/Libraries/LibGUI/GTextDocument.cpp index 12a71e2a8f..fcaeb33b8e 100644 --- a/Libraries/LibGUI/GTextDocument.cpp +++ b/Libraries/LibGUI/GTextDocument.cpp @@ -1,3 +1,4 @@ +#include <AK/StringBuilder.h> #include <LibGUI/GTextDocument.h> #include <ctype.h> @@ -165,3 +166,20 @@ void GTextDocument::update_views(Badge<GTextDocumentLine>) for (auto* client : m_clients) client->document_did_change(); } + +String GTextDocument::text_in_range(const GTextRange& a_range) const +{ + auto range = a_range.normalized(); + + StringBuilder builder; + for (int i = range.start().line(); i <= range.end().line(); ++i) { + auto& line = lines()[i]; + int selection_start_column_on_line = range.start().line() == i ? range.start().column() : 0; + int selection_end_column_on_line = range.end().line() == i ? range.end().column() : line.length(); + builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line); + if (i != range.end().line()) + builder.append('\n'); + } + + return builder.to_string(); +} diff --git a/Libraries/LibGUI/GTextDocument.h b/Libraries/LibGUI/GTextDocument.h index 47ad556370..3578f3d551 100644 --- a/Libraries/LibGUI/GTextDocument.h +++ b/Libraries/LibGUI/GTextDocument.h @@ -59,6 +59,8 @@ public: void update_views(Badge<GTextDocumentLine>); + String text_in_range(const GTextRange&) const; + private: explicit GTextDocument(Client* client); diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index 9b991eaff6..148c60f584 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -1002,18 +1002,7 @@ String GTextEditor::selected_text() const if (!has_selection()) return {}; - auto selection = normalized_selection(); - StringBuilder builder; - for (int i = selection.start().line(); i <= selection.end().line(); ++i) { - auto& line = lines()[i]; - int selection_start_column_on_line = selection.start().line() == i ? selection.start().column() : 0; - int selection_end_column_on_line = selection.end().line() == i ? selection.end().column() : line.length(); - builder.append(line.characters() + selection_start_column_on_line, selection_end_column_on_line - selection_start_column_on_line); - if (i != selection.end().line()) - builder.append('\n'); - } - - return builder.to_string(); + return document().text_in_range(m_selection); } void GTextEditor::delete_selection() |