1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 18:15:07 +00:00

LibGUI: Add GTextDocument::text_in_range(GTextRange)

This function returns a String containing the text in a given range.
GTextEditor::selected_text() is now just a wrapper around this.
This commit is contained in:
Andreas Kling 2019-10-29 21:36:47 +01:00
parent c1efa4f336
commit bddba567b3
3 changed files with 21 additions and 12 deletions

View file

@ -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();
}