Merge pull request #2323 from zed-industries/petros/z-402-recent-projects-replace-expanded-home

Petros Amoiridis created

Replace home directory with the tilde substitution

Change summary

Cargo.lock                                                   |  1 
crates/recent_projects/Cargo.toml                            |  1 
crates/recent_projects/src/highlighted_workspace_location.rs |  1 
crates/util/Cargo.toml                                       |  2 
crates/util/src/paths.rs                                     | 48 +++++
crates/util/src/test/marked_text.rs                          |  6 
6 files changed, 54 insertions(+), 5 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -5015,6 +5015,7 @@ dependencies = [
  "settings",
  "smol",
  "text",
+ "util",
  "workspace",
 ]
 

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"}

crates/util/Cargo.toml 🔗

@@ -6,7 +6,7 @@ publish = false
 
 [lib]
 path = "src/util.rs"
-doctest = false
+doctest = true
 
 [features]
 test-support = ["tempdir", "git2"]

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()
+    }
+}

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(