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

@ -221,6 +221,8 @@ impl Render for GoToLine {
#[cfg(test)]
mod tests {
use super::*;
use cursor_position::{CursorPosition, SelectionStats};
use editor::actions::SelectAll;
use gpui::{TestAppContext, VisualTestContext};
use indoc::indoc;
use project::{FakeFs, Project};
@ -335,6 +337,83 @@ mod tests {
assert_single_caret_at_row(&editor, expected_highlighted_row, cx);
}
#[gpui::test]
async fn test_unicode_characters_selection(cx: &mut TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
"/dir",
json!({
"a.rs": "ēlo"
}),
)
.await;
let project = Project::test(fs, ["/dir".as_ref()], cx).await;
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
workspace.update(cx, |workspace, cx| {
let cursor_position = cx.new_view(|_| CursorPosition::new(workspace));
workspace.status_bar().update(cx, |status_bar, cx| {
status_bar.add_right_item(cursor_position, cx);
});
});
let worktree_id = workspace.update(cx, |workspace, cx| {
workspace.project().update(cx, |project, cx| {
project.worktrees(cx).next().unwrap().read(cx).id()
})
});
let _buffer = project
.update(cx, |project, cx| project.open_local_buffer("/dir/a.rs", cx))
.await
.unwrap();
let editor = workspace
.update(cx, |workspace, cx| {
workspace.open_path((worktree_id, "a.rs"), None, true, cx)
})
.await
.unwrap()
.downcast::<Editor>()
.unwrap();
workspace.update(cx, |workspace, cx| {
assert_eq!(
&SelectionStats {
lines: 0,
characters: 0,
selections: 1,
},
workspace
.status_bar()
.read(cx)
.item_of_type::<CursorPosition>()
.expect("missing cursor position item")
.read(cx)
.selection_stats(),
"No selections should be initially"
);
});
editor.update(cx, |editor, cx| editor.select_all(&SelectAll, cx));
workspace.update(cx, |workspace, cx| {
assert_eq!(
&SelectionStats {
lines: 1,
characters: 3,
selections: 1,
},
workspace
.status_bar()
.read(cx)
.item_of_type::<CursorPosition>()
.expect("missing cursor position item")
.read(cx)
.selection_stats(),
"After selecting a text with multibyte unicode characters, the character count should be correct"
);
});
}
fn open_go_to_line_view(
workspace: &View<Workspace>,
cx: &mut VisualTestContext,