History

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

Kirill Bulatov committed 3 years ago

Commit 25ea07c2 parents 33b215a 804da681 file · +61 −19Browse files

Unified · Split

crates/project_panel/src/project_panel.rs+61−19
@@ -4,7 +4,7 @@ mod project_panel_settings; · 3 unmodified lines
44 use context_menu::{ContextMenu, ContextMenuItem};
55 use db::kvp::KEY_VALUE_STORE;
66 use drag_and_drop::{DragAndDrop, Draggable};
7−use editor::{Cancel, Editor};
7+use editor::{scroll::autoscroll::Autoscroll, Cancel, Editor};
88 use file_associations::FileAssociations;
99
1010 use futures::stream::StreamExt;
@@ -741,13 +741,20 @@ impl ProjectPanel { · 730 unmodified lines
741741 is_dir: entry.is_dir(),
742742 processing_filename: None,
743743 });
744− let filename = entry
744+ let file_name = entry
745745 .path
746746 .file_name()
747− .map_or(String::new(), |s| s.to_string_lossy().to_string());
747+ .map(|s| s.to_string_lossy())
748+ .unwrap_or_default()
749+ .to_string();
750+ let file_stem = entry.path.file_stem().map(|s| s.to_string_lossy());
751+ let selection_end =
752+ file_stem.map_or(file_name.len(), |file_stem| file_stem.len());
748753 self.filename_editor.update(cx, |editor, cx| {
749− editor.set_text(filename, cx);
750− editor.select_all(&Default::default(), cx);
754+ editor.set_text(file_name, cx);
755+ editor.change_selections(Some(Autoscroll::fit()), cx, |s| {
756+ s.select_ranges([0..selection_end])
757+ })
751758 });
752759 cx.focus(&self.filename_editor);
753760 self.update_visible_entries(None, cx);
@@ -1942,7 +1949,7 @@ mod tests { · 1188 unmodified lines
19421949 .update(cx, |panel, cx| {
19431950 panel
19441951 .filename_editor
1945− .update(cx, |editor, cx| editor.set_text("another-filename", cx));
1952+ .update(cx, |editor, cx| editor.set_text("another-filename.txt", cx));
19461953 panel.confirm(&Confirm, cx).unwrap()
19471954 })
19481955 .await
@@ -1956,14 +1963,14 @@ mod tests { · 7 unmodified lines
19561963 " v b",
19571964 " > 3",
19581965 " > 4",
1959− " another-filename <== selected",
1966+ " another-filename.txt <== selected",
19601967 " > C",
19611968 " .dockerignore",
19621969 " the-new-filename",
19631970 ]
19641971 );
19651972
1966− select_path(&panel, "root1/b/another-filename", cx);
1973+ select_path(&panel, "root1/b/another-filename.txt", cx);
19671974 panel.update(cx, |panel, cx| panel.rename(&Rename, cx));
19681975 assert_eq!(
19691976 visible_entries_as_strings(&panel, 0..10, cx),
@@ -1974,7 +1981,7 @@ mod tests { · 4 unmodified lines
19741981 " v b",
19751982 " > 3",
19761983 " > 4",
1977− " [EDITOR: 'another-filename'] <== selected",
1984+ " [EDITOR: 'another-filename.txt'] <== selected",
19781985 " > C",
19791986 " .dockerignore",
19801987 " the-new-filename",
@@ -1982,9 +1989,15 @@ mod tests { · 1 unmodified line
19821989 );
19831990
19841991 let confirm = panel.update(cx, |panel, cx| {
1985− panel
1986− .filename_editor
1987− .update(cx, |editor, cx| editor.set_text("a-different-filename", cx));
1992+ panel.filename_editor.update(cx, |editor, cx| {
1993+ let file_name_selections = editor.selections.all::<usize>(cx);
1994+ assert_eq!(file_name_selections.len(), 1, "File editing should have a single selection, but got: {file_name_selections:?}");
1995+ let file_name_selection = &file_name_selections[0];
1996+ assert_eq!(file_name_selection.start, 0, "Should select the file name from the start");
1997+ assert_eq!(file_name_selection.end, "another-filename".len(), "Should not select file extension");
1998+
1999+ editor.set_text("a-different-filename.tar.gz", cx)
2000+ });
19882001 panel.confirm(&Confirm, cx).unwrap()
19892002 });
19902003 assert_eq!(
@@ -1996,7 +2009,7 @@ mod tests { · 5 unmodified lines
19962009 " v b",
19972010 " > 3",
19982011 " > 4",
1999− " [PROCESSING: 'a-different-filename'] <== selected",
2012+ " [PROCESSING: 'a-different-filename.tar.gz'] <== selected",
20002013 " > C",
20012014 " .dockerignore",
20022015 " the-new-filename",
@@ -2013,13 +2026,42 @@ mod tests { · 10 unmodified lines
20132026 " v b",
20142027 " > 3",
20152028 " > 4",
2016− " a-different-filename <== selected",
2029+ " a-different-filename.tar.gz <== selected",
20172030 " > C",
20182031 " .dockerignore",
20192032 " the-new-filename",
20202033 ]
20212034 );
20222035
2036+ panel.update(cx, |panel, cx| panel.rename(&Rename, cx));
2037+ assert_eq!(
2038+ visible_entries_as_strings(&panel, 0..10, cx),
2039+ &[
2040+ "v root1",
2041+ " > .git",
2042+ " > a",
2043+ " v b",
2044+ " > 3",
2045+ " > 4",
2046+ " [EDITOR: 'a-different-filename.tar.gz'] <== selected",
2047+ " > C",
2048+ " .dockerignore",
2049+ " the-new-filename",
2050+ ]
2051+ );
2052+
2053+ panel.update(cx, |panel, cx| {
2054+ panel.filename_editor.update(cx, |editor, cx| {
2055+ let file_name_selections = editor.selections.all::<usize>(cx);
2056+ assert_eq!(file_name_selections.len(), 1, "File editing should have a single selection, but got: {file_name_selections:?}");
2057+ let file_name_selection = &file_name_selections[0];
2058+ assert_eq!(file_name_selection.start, 0, "Should select the file name from the start");
2059+ 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");
2060+
2061+ });
2062+ panel.cancel(&Cancel, cx)
2063+ });
2064+
20232065 panel.update(cx, |panel, cx| panel.new_directory(&NewDirectory, cx));
20242066 assert_eq!(
20252067 visible_entries_as_strings(&panel, 0..10, cx),
@@ -2031,7 +2073,7 @@ mod tests { · 5 unmodified lines
20312073 " > [EDITOR: ''] <== selected",
20322074 " > 3",
20332075 " > 4",
2034− " a-different-filename",
2076+ " a-different-filename.tar.gz",
20352077 " > C",
20362078 " .dockerignore",
20372079 ]
@@ -2054,7 +2096,7 @@ mod tests { · 16 unmodified lines
20542096 " > [PROCESSING: 'new-dir']",
20552097 " > 3 <== selected",
20562098 " > 4",
2057− " a-different-filename",
2099+ " a-different-filename.tar.gz",
20582100 " > C",
20592101 " .dockerignore",
20602102 ]
@@ -2071,7 +2113,7 @@ mod tests { · 10 unmodified lines
20712113 " > 3 <== selected",
20722114 " > 4",
20732115 " > new-dir",
2074− " a-different-filename",
2116+ " a-different-filename.tar.gz",
20752117 " > C",
20762118 " .dockerignore",
20772119 ]
@@ -2088,7 +2130,7 @@ mod tests { · 10 unmodified lines
20882130 " > [EDITOR: '3'] <== selected",
20892131 " > 4",
20902132 " > new-dir",
2091− " a-different-filename",
2133+ " a-different-filename.tar.gz",
20922134 " > C",
20932135 " .dockerignore",
20942136 ]
@@ -2106,7 +2148,7 @@ mod tests { · 11 unmodified lines
21062148 " > 3 <== selected",
21072149 " > 4",
21082150 " > new-dir",
2109− " a-different-filename",
2151+ " a-different-filename.tar.gz",
21102152 " > C",
21112153 " .dockerignore",
21122154 ]

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t