From 87c1b190a8c38d1e6aa648160bae67225e8c7f44 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 28 Mar 2023 17:21:59 +0300 Subject: [PATCH 01/10] Replace home directory with the tilde substitution --- Cargo.lock | 1 + crates/recent_projects/Cargo.toml | 1 + .../src/highlighted_workspace_location.rs | 12 ++++++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02b27566e4..e24d7a9e60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5014,6 +5014,7 @@ dependencies = [ "settings", "smol", "text", + "util", "workspace", ] diff --git a/crates/recent_projects/Cargo.toml b/crates/recent_projects/Cargo.toml index e8d851fa08..c056b68852 100644 --- a/crates/recent_projects/Cargo.toml +++ b/crates/recent_projects/Cargo.toml @@ -21,3 +21,4 @@ workspace = { path = "../workspace" } ordered-float = "2.1.1" postage = { workspace = true } smol = "1.2" +util = { path = "../util"} diff --git a/crates/recent_projects/src/highlighted_workspace_location.rs b/crates/recent_projects/src/highlighted_workspace_location.rs index 8e75b291a0..b414b84f4a 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -1,4 +1,4 @@ -use std::path::Path; +use std::path::{Path, PathBuf}; use fuzzy::StringMatch; use gpui::{ @@ -61,8 +61,16 @@ impl HighlightedWorkspaceLocation { .paths() .iter() .map(|path| { + let mut full_path = PathBuf::new(); + if path.starts_with(util::paths::HOME.as_path()) { + full_path.push("~"); + full_path.push(path.strip_prefix(util::paths::HOME.as_path()).unwrap()); + } else { + full_path.push(path) + } + let highlighted_text = Self::highlights_for_path( - path.as_ref(), + full_path.as_ref(), &string_match.positions, path_start_offset, ); From a1284396992d1d11b62ad47c5d456026c8929b33 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Tue, 28 Mar 2023 19:23:52 +0300 Subject: [PATCH 02/10] Move code into the platform Co-Authored-By: Joseph T. Lyons <19867440+JosephTLyons@users.noreply.github.com> --- crates/gpui/src/platform.rs | 1 + crates/gpui/src/platform/mac/platform.rs | 31 +++++++++++++++++++ crates/gpui/src/platform/test.rs | 4 +++ .../src/highlighted_workspace_location.rs | 17 +++++----- crates/recent_projects/src/recent_projects.rs | 1 + 5 files changed, 44 insertions(+), 10 deletions(-) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 538b46ee77..482de94162 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -68,6 +68,7 @@ pub trait Platform: Send + Sync { fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; fn open_url(&self, url: &str); + fn convert_to_shortened_path(&self, path: &Path) -> PathBuf; fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>; fn read_credentials(&self, url: &str) -> Result)>>; diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index ab4fd873c6..d9ac7237ce 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -674,6 +674,18 @@ impl platform::Platform for MacPlatform { } } + fn convert_to_shortened_path(&self, path: &Path) -> PathBuf { + match path.strip_prefix(util::paths::HOME.as_path()) { + Ok(relative_path) => { + let mut shortened_path = PathBuf::new(); + shortened_path.push("~"); + shortened_path.push(relative_path); + shortened_path + } + Err(_) => path.to_path_buf(), + } + } + fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { let url = CFString::from(url); let username = CFString::from(username); @@ -1113,4 +1125,23 @@ mod tests { platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) }; platform } + + #[test] + fn test_convert_to_shortened_path() { + let platform = build_platform(); + + let full_path: PathBuf = [ + util::paths::HOME.to_string_lossy().to_string(), + "a".to_string(), + "b".to_string(), + "c".to_string(), + ] + .iter() + .collect(); + + let shortened_path_actual = platform.convert_to_shortened_path(&full_path); + let shortened_path_expected = PathBuf::from("~/a/b/c"); + + assert_eq!(shortened_path_actual, shortened_path_expected); + } } diff --git a/crates/gpui/src/platform/test.rs b/crates/gpui/src/platform/test.rs index a3532dd96e..6b1cdf5e05 100644 --- a/crates/gpui/src/platform/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -175,6 +175,10 @@ impl super::Platform for Platform { fn open_url(&self, _: &str) {} + fn convert_to_shortened_path(&self, _path: &Path) -> PathBuf { + PathBuf::new() + } + fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> { Ok(()) } diff --git a/crates/recent_projects/src/highlighted_workspace_location.rs b/crates/recent_projects/src/highlighted_workspace_location.rs index b414b84f4a..bee7594d33 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -1,4 +1,4 @@ -use std::path::{Path, PathBuf}; +use std::path::Path; use fuzzy::StringMatch; use gpui::{ @@ -55,20 +55,17 @@ pub struct HighlightedWorkspaceLocation { } impl HighlightedWorkspaceLocation { - pub fn new(string_match: &StringMatch, location: &WorkspaceLocation) -> Self { + pub fn new( + string_match: &StringMatch, + location: &WorkspaceLocation, + cx: &gpui::AppContext, + ) -> Self { let mut path_start_offset = 0; let (names, paths): (Vec<_>, Vec<_>) = location .paths() .iter() .map(|path| { - let mut full_path = PathBuf::new(); - if path.starts_with(util::paths::HOME.as_path()) { - full_path.push("~"); - full_path.push(path.strip_prefix(util::paths::HOME.as_path()).unwrap()); - } else { - full_path.push(path) - } - + let full_path = cx.platform().convert_to_shortened_path(&path); let highlighted_text = Self::highlights_for_path( full_path.as_ref(), &string_match.positions, diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs index e73d0b4fb5..81afd694d0 100644 --- a/crates/recent_projects/src/recent_projects.rs +++ b/crates/recent_projects/src/recent_projects.rs @@ -192,6 +192,7 @@ impl PickerDelegate for RecentProjectsView { let highlighted_location = HighlightedWorkspaceLocation::new( &string_match, &self.workspace_locations[string_match.candidate_id], + &cx, ); Flex::column() From d5f53111e849b0e2b0bc30de5bac5c3506fa76ef Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Wed, 29 Mar 2023 17:31:55 +0300 Subject: [PATCH 03/10] Enable doctests in util crate --- crates/util/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/util/Cargo.toml b/crates/util/Cargo.toml index b13b8af956..8a77cc358c 100644 --- a/crates/util/Cargo.toml +++ b/crates/util/Cargo.toml @@ -6,7 +6,7 @@ publish = false [lib] path = "src/util.rs" -doctest = false +doctest = true [features] test-support = ["tempdir", "git2"] From 69989d0463011f6101fbd32f837b46260f4e7f00 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Wed, 29 Mar 2023 17:32:27 +0300 Subject: [PATCH 04/10] Introduce compact function in util create --- crates/util/src/paths.rs | 54 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index 63c3c6d884..b04c12a204 100644 --- a/crates/util/src/paths.rs +++ b/crates/util/src/paths.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; lazy_static::lazy_static! { pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory"); @@ -23,3 +23,55 @@ pub mod legacy { pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json"); } } + +/// Compacts a given file path by replacing the user's home directory +/// prefix with a tilde (`~`). +/// +/// # Arguments +/// +/// * `path` - A reference to a `Path` representing the file path to compact. +/// +/// # Examples +/// +/// ``` +/// use std::path::{Path, PathBuf}; +/// use util::paths::compact; +/// let path: PathBuf = [ +/// util::paths::HOME.to_string_lossy().to_string(), +/// "some_file.txt".to_string(), +/// ] +/// .iter() +/// .collect(); +/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") { +/// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt")); +/// } else { +/// assert_eq!(compact(&path).to_str(), path.to_str()); +/// } +/// ``` +/// +/// # Returns +/// +/// * A `PathBuf` containing the compacted file path. If the input path +/// does not have the user's home directory prefix, or if we are not on +/// Linux or macOS, the original path is returned unchanged. +/// +/// # Errors +/// +/// This function will not produce any errors, but in case the input path +/// cannot be stripped of the home directory prefix, the original path +/// will be returned unchanged. +pub fn compact(path: &Path) -> PathBuf { + if cfg!(target_os = "linux") || cfg!(target_os = "macos") { + match path.strip_prefix(HOME.as_path()) { + Ok(relative_path) => { + let mut shortened_path = PathBuf::new(); + shortened_path.push("~"); + shortened_path.push(relative_path); + shortened_path + } + Err(_) => path.to_path_buf(), + } + } else { + path.to_path_buf() + } +} From b4593cd90b5584517a3102bfdd2336a4a5c4b3eb Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Wed, 29 Mar 2023 17:40:39 +0300 Subject: [PATCH 05/10] Use util::paths::compact --- .../recent_projects/src/highlighted_workspace_location.rs | 8 ++------ crates/recent_projects/src/recent_projects.rs | 1 - 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/recent_projects/src/highlighted_workspace_location.rs b/crates/recent_projects/src/highlighted_workspace_location.rs index bee7594d33..ca8b8b2828 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -55,17 +55,13 @@ pub struct HighlightedWorkspaceLocation { } impl HighlightedWorkspaceLocation { - pub fn new( - string_match: &StringMatch, - location: &WorkspaceLocation, - cx: &gpui::AppContext, - ) -> Self { + pub fn new(string_match: &StringMatch, location: &WorkspaceLocation) -> Self { let mut path_start_offset = 0; let (names, paths): (Vec<_>, Vec<_>) = location .paths() .iter() .map(|path| { - let full_path = cx.platform().convert_to_shortened_path(&path); + let full_path = util::paths::compact(&path); let highlighted_text = Self::highlights_for_path( full_path.as_ref(), &string_match.positions, diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs index 81afd694d0..e73d0b4fb5 100644 --- a/crates/recent_projects/src/recent_projects.rs +++ b/crates/recent_projects/src/recent_projects.rs @@ -192,7 +192,6 @@ impl PickerDelegate for RecentProjectsView { let highlighted_location = HighlightedWorkspaceLocation::new( &string_match, &self.workspace_locations[string_match.candidate_id], - &cx, ); Flex::column() From b15632bd453bd2f24e6589aa29b1d56e14aa4960 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Wed, 29 Mar 2023 17:41:01 +0300 Subject: [PATCH 06/10] Remove convert_to_shortened_path from gpui platform --- crates/gpui/src/platform.rs | 1 - crates/gpui/src/platform/mac/platform.rs | 31 ------------------------ crates/gpui/src/platform/test.rs | 4 --- 3 files changed, 36 deletions(-) diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 482de94162..538b46ee77 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -68,7 +68,6 @@ pub trait Platform: Send + Sync { fn write_to_clipboard(&self, item: ClipboardItem); fn read_from_clipboard(&self) -> Option; fn open_url(&self, url: &str); - fn convert_to_shortened_path(&self, path: &Path) -> PathBuf; fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()>; fn read_credentials(&self, url: &str) -> Result)>>; diff --git a/crates/gpui/src/platform/mac/platform.rs b/crates/gpui/src/platform/mac/platform.rs index d9ac7237ce..ab4fd873c6 100644 --- a/crates/gpui/src/platform/mac/platform.rs +++ b/crates/gpui/src/platform/mac/platform.rs @@ -674,18 +674,6 @@ impl platform::Platform for MacPlatform { } } - fn convert_to_shortened_path(&self, path: &Path) -> PathBuf { - match path.strip_prefix(util::paths::HOME.as_path()) { - Ok(relative_path) => { - let mut shortened_path = PathBuf::new(); - shortened_path.push("~"); - shortened_path.push(relative_path); - shortened_path - } - Err(_) => path.to_path_buf(), - } - } - fn write_credentials(&self, url: &str, username: &str, password: &[u8]) -> Result<()> { let url = CFString::from(url); let username = CFString::from(username); @@ -1125,23 +1113,4 @@ mod tests { platform.pasteboard = unsafe { NSPasteboard::pasteboardWithUniqueName(nil) }; platform } - - #[test] - fn test_convert_to_shortened_path() { - let platform = build_platform(); - - let full_path: PathBuf = [ - util::paths::HOME.to_string_lossy().to_string(), - "a".to_string(), - "b".to_string(), - "c".to_string(), - ] - .iter() - .collect(); - - let shortened_path_actual = platform.convert_to_shortened_path(&full_path); - let shortened_path_expected = PathBuf::from("~/a/b/c"); - - assert_eq!(shortened_path_actual, shortened_path_expected); - } } diff --git a/crates/gpui/src/platform/test.rs b/crates/gpui/src/platform/test.rs index 6b1cdf5e05..a3532dd96e 100644 --- a/crates/gpui/src/platform/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -175,10 +175,6 @@ impl super::Platform for Platform { fn open_url(&self, _: &str) {} - fn convert_to_shortened_path(&self, _path: &Path) -> PathBuf { - PathBuf::new() - } - fn write_credentials(&self, _: &str, _: &str, _: &[u8]) -> Result<()> { Ok(()) } From 9ef3e45bcd55ba27287085a63c4247aad33841a1 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Thu, 30 Mar 2023 11:35:17 +0300 Subject: [PATCH 07/10] Update crates/recent_projects/src/highlighted_workspace_location.rs Co-authored-by: Antonio Scandurra --- crates/recent_projects/src/highlighted_workspace_location.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/recent_projects/src/highlighted_workspace_location.rs b/crates/recent_projects/src/highlighted_workspace_location.rs index ca8b8b2828..5c3505cccd 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -61,7 +61,7 @@ impl HighlightedWorkspaceLocation { .paths() .iter() .map(|path| { - let full_path = util::paths::compact(&path); + let path = util::paths::compact(&path); let highlighted_text = Self::highlights_for_path( full_path.as_ref(), &string_match.positions, From 18c6c7ebb7af44c4d42db928c2a4b44d9e0bd185 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Thu, 30 Mar 2023 14:03:59 +0300 Subject: [PATCH 08/10] Fix error --- crates/recent_projects/src/highlighted_workspace_location.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/recent_projects/src/highlighted_workspace_location.rs b/crates/recent_projects/src/highlighted_workspace_location.rs index 5c3505cccd..a71a7dd2d7 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -63,7 +63,7 @@ impl HighlightedWorkspaceLocation { .map(|path| { let path = util::paths::compact(&path); let highlighted_text = Self::highlights_for_path( - full_path.as_ref(), + path.as_ref(), &string_match.positions, path_start_offset, ); From adc5ef911fbd3b9a61feb79971f2161c075945fc Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Thu, 30 Mar 2023 14:04:37 +0300 Subject: [PATCH 09/10] Remove the Errors section from rust docs The section does not add anything that we don't already know. Co-Authored-By: Antonio Scandurra --- crates/util/src/paths.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index b04c12a204..3fccb0c896 100644 --- a/crates/util/src/paths.rs +++ b/crates/util/src/paths.rs @@ -54,12 +54,6 @@ pub mod legacy { /// * A `PathBuf` containing the compacted file path. If the input path /// does not have the user's home directory prefix, or if we are not on /// Linux or macOS, the original path is returned unchanged. -/// -/// # Errors -/// -/// This function will not produce any errors, but in case the input path -/// cannot be stripped of the home directory prefix, the original path -/// will be returned unchanged. pub fn compact(path: &Path) -> PathBuf { if cfg!(target_os = "linux") || cfg!(target_os = "macos") { match path.strip_prefix(HOME.as_path()) { From dc51735112d3649dc44372bb6b0edeceb3ab4f70 Mon Sep 17 00:00:00 2001 From: Petros Amoiridis Date: Thu, 30 Mar 2023 17:57:14 +0300 Subject: [PATCH 10/10] Fix doctests --- crates/util/src/test/marked_text.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/util/src/test/marked_text.rs b/crates/util/src/test/marked_text.rs index c2aaca3831..dc91e7b5b6 100644 --- a/crates/util/src/test/marked_text.rs +++ b/crates/util/src/test/marked_text.rs @@ -87,21 +87,21 @@ pub fn marked_text_ranges_by( /// 1. To mark a range of text, surround it with the `«` and `»` angle brackets, /// which can be typed on a US keyboard with the `alt-|` and `alt-shift-|` keys. /// -/// ``` +/// ```text /// foo «selected text» bar /// ``` /// /// 2. To mark a single position in the text, use the `ˇ` caron, /// which can be typed on a US keyboard with the `alt-shift-t` key. /// -/// ``` +/// ```text /// the cursors are hereˇ and hereˇ. /// ``` /// /// 3. To mark a range whose direction is meaningful (like a selection), /// put a caron character beside one of its bounds, on the inside: /// -/// ``` +/// ```text /// one «ˇreversed» selection and one «forwardˇ» selection /// ``` pub fn marked_text_ranges(