When renaming in project panel, select file names without extensions (#2776)

Closes
https://github.com/zed-industries/community/issues/1789#issuecomment-1646061712

<img width="196" alt="Screenshot 2023-07-21 at 23 23 47"
src="https://github.com/zed-industries/zed/assets/2690773/f5c7025b-6dc8-4f0c-81e5-3cc98a3a9c8b">
<img width="197" alt="Screenshot 2023-07-21 at 23 23 52"
src="https://github.com/zed-industries/zed/assets/2690773/596f8ab0-15e0-4285-be34-ce4c276b686f">

When renaming in project panel, select file names without extensions.

Release Notes:

- Improved project panel rename by selecting file names without
extensions
This commit is contained in:
Kirill Bulatov 2023-07-21 23:30:55 +03:00 committed by GitHub
commit 25ea07cd41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ mod project_panel_settings;
use context_menu::{ContextMenu, ContextMenuItem};
use db::kvp::KEY_VALUE_STORE;
use drag_and_drop::{DragAndDrop, Draggable};
use editor::{Cancel, Editor};
use editor::{scroll::autoscroll::Autoscroll, Cancel, Editor};
use file_associations::FileAssociations;
use futures::stream::StreamExt;
@ -741,13 +741,20 @@ impl ProjectPanel {
is_dir: entry.is_dir(),
processing_filename: None,
});
let filename = entry
let file_name = entry
.path
.file_name()
.map_or(String::new(), |s| s.to_string_lossy().to_string());
.map(|s| s.to_string_lossy())
.unwrap_or_default()
.to_string();
let file_stem = entry.path.file_stem().map(|s| s.to_string_lossy());
let selection_end =
file_stem.map_or(file_name.len(), |file_stem| file_stem.len());
self.filename_editor.update(cx, |editor, cx| {
editor.set_text(filename, cx);
editor.select_all(&Default::default(), cx);
editor.set_text(file_name, cx);
editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
s.select_ranges([0..selection_end])
})
});
cx.focus(&self.filename_editor);
self.update_visible_entries(None, cx);
@ -1942,7 +1949,7 @@ mod tests {
.update(cx, |panel, cx| {
panel
.filename_editor
.update(cx, |editor, cx| editor.set_text("another-filename", cx));
.update(cx, |editor, cx| editor.set_text("another-filename.txt", cx));
panel.confirm(&Confirm, cx).unwrap()
})
.await
@ -1956,14 +1963,14 @@ mod tests {
" v b",
" > 3",
" > 4",
" another-filename <== selected",
" another-filename.txt <== selected",
" > C",
" .dockerignore",
" the-new-filename",
]
);
select_path(&panel, "root1/b/another-filename", cx);
select_path(&panel, "root1/b/another-filename.txt", cx);
panel.update(cx, |panel, cx| panel.rename(&Rename, cx));
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
@ -1974,7 +1981,7 @@ mod tests {
" v b",
" > 3",
" > 4",
" [EDITOR: 'another-filename'] <== selected",
" [EDITOR: 'another-filename.txt'] <== selected",
" > C",
" .dockerignore",
" the-new-filename",
@ -1982,9 +1989,15 @@ mod tests {
);
let confirm = panel.update(cx, |panel, cx| {
panel
.filename_editor
.update(cx, |editor, cx| editor.set_text("a-different-filename", cx));
panel.filename_editor.update(cx, |editor, cx| {
let file_name_selections = editor.selections.all::<usize>(cx);
assert_eq!(file_name_selections.len(), 1, "File editing should have a single selection, but got: {file_name_selections:?}");
let file_name_selection = &file_name_selections[0];
assert_eq!(file_name_selection.start, 0, "Should select the file name from the start");
assert_eq!(file_name_selection.end, "another-filename".len(), "Should not select file extension");
editor.set_text("a-different-filename.tar.gz", cx)
});
panel.confirm(&Confirm, cx).unwrap()
});
assert_eq!(
@ -1996,7 +2009,7 @@ mod tests {
" v b",
" > 3",
" > 4",
" [PROCESSING: 'a-different-filename'] <== selected",
" [PROCESSING: 'a-different-filename.tar.gz'] <== selected",
" > C",
" .dockerignore",
" the-new-filename",
@ -2013,13 +2026,42 @@ mod tests {
" v b",
" > 3",
" > 4",
" a-different-filename <== selected",
" a-different-filename.tar.gz <== selected",
" > C",
" .dockerignore",
" the-new-filename",
]
);
panel.update(cx, |panel, cx| panel.rename(&Rename, cx));
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
&[
"v root1",
" > .git",
" > a",
" v b",
" > 3",
" > 4",
" [EDITOR: 'a-different-filename.tar.gz'] <== selected",
" > C",
" .dockerignore",
" the-new-filename",
]
);
panel.update(cx, |panel, cx| {
panel.filename_editor.update(cx, |editor, cx| {
let file_name_selections = editor.selections.all::<usize>(cx);
assert_eq!(file_name_selections.len(), 1, "File editing should have a single selection, but got: {file_name_selections:?}");
let file_name_selection = &file_name_selections[0];
assert_eq!(file_name_selection.start, 0, "Should select the file name from the start");
assert_eq!(file_name_selection.end, "a-different-filename.tar".len(), "Should not select file extension, but still may select anything up to the last dot");
});
panel.cancel(&Cancel, cx)
});
panel.update(cx, |panel, cx| panel.new_directory(&NewDirectory, cx));
assert_eq!(
visible_entries_as_strings(&panel, 0..10, cx),
@ -2031,7 +2073,7 @@ mod tests {
" > [EDITOR: ''] <== selected",
" > 3",
" > 4",
" a-different-filename",
" a-different-filename.tar.gz",
" > C",
" .dockerignore",
]
@ -2054,7 +2096,7 @@ mod tests {
" > [PROCESSING: 'new-dir']",
" > 3 <== selected",
" > 4",
" a-different-filename",
" a-different-filename.tar.gz",
" > C",
" .dockerignore",
]
@ -2071,7 +2113,7 @@ mod tests {
" > 3 <== selected",
" > 4",
" > new-dir",
" a-different-filename",
" a-different-filename.tar.gz",
" > C",
" .dockerignore",
]
@ -2088,7 +2130,7 @@ mod tests {
" > [EDITOR: '3'] <== selected",
" > 4",
" > new-dir",
" a-different-filename",
" a-different-filename.tar.gz",
" > C",
" .dockerignore",
]
@ -2106,7 +2148,7 @@ mod tests {
" > 3 <== selected",
" > 4",
" > new-dir",
" a-different-filename",
" a-different-filename.tar.gz",
" > C",
" .dockerignore",
]