mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +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
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