diff --git a/Cargo.lock b/Cargo.lock index ee107643f18804d7abd1c6b0ed4aa52b1636756f..5de83278f2f081ef606f46d68ad4542912ddcd58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5015,6 +5015,7 @@ dependencies = [ "settings", "smol", "text", + "util", "workspace", ] diff --git a/crates/recent_projects/Cargo.toml b/crates/recent_projects/Cargo.toml index e8d851fa083ab85ef6a05f53c3a5eb07ab8e4667..c056b6885250822be9e877226cdf89c0db2ed5e3 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 8e75b291a0748627ce47c570c607eb2d2163358d..a71a7dd2d7de2fad414f44957995879d243d4cb3 100644 --- a/crates/recent_projects/src/highlighted_workspace_location.rs +++ b/crates/recent_projects/src/highlighted_workspace_location.rs @@ -61,6 +61,7 @@ impl HighlightedWorkspaceLocation { .paths() .iter() .map(|path| { + let path = util::paths::compact(&path); let highlighted_text = Self::highlights_for_path( path.as_ref(), &string_match.positions, diff --git a/crates/util/Cargo.toml b/crates/util/Cargo.toml index b13b8af956c0f174c1897a920d47555bde119543..8a77cc358ca3b1497ad44f70b8dd3da080c9e413 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"] diff --git a/crates/util/src/paths.rs b/crates/util/src/paths.rs index 63c3c6d884a4e91fdeaff3766adc97a99072ec24..3fccb0c896e405ec80303fef1a5027ecc7c3c57f 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,49 @@ 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. +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() + } +} diff --git a/crates/util/src/test/marked_text.rs b/crates/util/src/test/marked_text.rs index c2aaca383158db66833db52be667d95997bee9b6..dc91e7b5b6bd63565ec85842a111ec4e75be2f87 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(