mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-04 23:30:20 +00:00
LibGUI+TextEditor: Add the calculation of selected words
This moves the calculation of selected words that was originally in the TextEditor application to TextEditor in LibGUI. This allows all applications with text editors to get this number without having to calculating it themselves.
This commit is contained in:
parent
074813e441
commit
8146543a43
Notes:
sideshowbarker
2024-07-18 17:20:41 +09:00
Author: https://github.com/ry-sev
Commit: 8146543a43
Pull-request: https://github.com/SerenityOS/serenity/pull/7473
3 changed files with 25 additions and 13 deletions
|
@ -1217,6 +1217,29 @@ String TextEditor::selected_text() const
|
|||
return document().text_in_range(m_selection);
|
||||
}
|
||||
|
||||
size_t TextEditor::number_of_selected_words() const
|
||||
{
|
||||
if (!has_selection())
|
||||
return 0;
|
||||
|
||||
size_t word_count = 0;
|
||||
bool in_word = false;
|
||||
auto selected_text = this->selected_text();
|
||||
for (char c : selected_text) {
|
||||
if (in_word && isspace(c)) {
|
||||
in_word = false;
|
||||
word_count++;
|
||||
continue;
|
||||
}
|
||||
if (!in_word && !isspace(c))
|
||||
in_word = true;
|
||||
}
|
||||
if (in_word)
|
||||
word_count++;
|
||||
|
||||
return word_count;
|
||||
}
|
||||
|
||||
void TextEditor::delete_selection()
|
||||
{
|
||||
auto selection = normalized_selection();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue