mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +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: https://github.com/SerenityOS/serenity/commit/8146543a437 Pull-request: https://github.com/SerenityOS/serenity/pull/7473
3 changed files with 25 additions and 13 deletions
|
@ -765,20 +765,8 @@ void MainWidget::update_statusbar()
|
|||
builder.appendff("Line: {}, Column: {}", m_editor->cursor().line() + 1, m_editor->cursor().column());
|
||||
|
||||
if (m_editor->has_selection()) {
|
||||
int word_count = 0;
|
||||
bool in_word = false;
|
||||
String selected_text = m_editor->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++;
|
||||
auto word_count = m_editor->number_of_selected_words();
|
||||
builder.appendff(" Selected: {} {} ({} {})", selected_text.length(), selected_text.length() == 1 ? "character" : "characters", word_count, word_count != 1 ? "words" : "word");
|
||||
}
|
||||
m_statusbar->set_text(builder.to_string());
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -115,6 +115,7 @@ public:
|
|||
bool write_to_file(const String& path);
|
||||
bool has_selection() const { return m_selection.is_valid(); }
|
||||
String selected_text() const;
|
||||
size_t number_of_selected_words() const;
|
||||
void set_selection(const TextRange&);
|
||||
void clear_selection();
|
||||
bool can_undo() const { return document().can_undo(); }
|
||||
|
|
Loading…
Reference in a new issue