Show correct number of characters selected (#16420)

This commit is contained in:
Kirill Bulatov 2024-08-18 02:24:32 +03:00 committed by GitHub
parent 8841d6faad
commit 5e6e465294
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 93 additions and 6 deletions

View file

@ -12,11 +12,11 @@ use ui::{
use util::paths::FILE_ROW_COLUMN_DELIMITER;
use workspace::{item::ItemHandle, StatusItemView, Workspace};
#[derive(Copy, Clone, Default, PartialOrd, PartialEq)]
struct SelectionStats {
lines: usize,
characters: usize,
selections: usize,
#[derive(Copy, Clone, Debug, Default, PartialOrd, PartialEq)]
pub(crate) struct SelectionStats {
pub lines: usize,
pub characters: usize,
pub selections: usize,
}
pub struct CursorPosition {
@ -44,7 +44,10 @@ impl CursorPosition {
self.selected_count.selections = editor.selections.count();
let mut last_selection: Option<Selection<usize>> = None;
for selection in editor.selections.all::<usize>(cx) {
self.selected_count.characters += selection.end - selection.start;
self.selected_count.characters += buffer
.text_for_range(selection.start..selection.end)
.map(|t| t.chars().count())
.sum::<usize>();
if last_selection
.as_ref()
.map_or(true, |last_selection| selection.id > last_selection.id)
@ -106,6 +109,11 @@ impl CursorPosition {
}
text.push(')');
}
#[cfg(test)]
pub(crate) fn selection_stats(&self) -> &SelectionStats {
&self.selected_count
}
}
impl Render for CursorPosition {