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

Replace home directory with the tilde substitution
This commit is contained in:
Petros Amoiridis
2023-03-30 18:12:39 +03:00
committed by GitHub
6 changed files with 54 additions and 5 deletions
Generated
+1
View File
@@ -5015,6 +5015,7 @@ dependencies = [
"settings", "settings",
"smol", "smol",
"text", "text",
"util",
"workspace", "workspace",
] ]
+1
View File
@@ -21,3 +21,4 @@ workspace = { path = "../workspace" }
ordered-float = "2.1.1" ordered-float = "2.1.1"
postage = { workspace = true } postage = { workspace = true }
smol = "1.2" smol = "1.2"
util = { path = "../util"}
@@ -61,6 +61,7 @@ impl HighlightedWorkspaceLocation {
.paths() .paths()
.iter() .iter()
.map(|path| { .map(|path| {
let path = util::paths::compact(&path);
let highlighted_text = Self::highlights_for_path( let highlighted_text = Self::highlights_for_path(
path.as_ref(), path.as_ref(),
&string_match.positions, &string_match.positions,
+1 -1
View File
@@ -6,7 +6,7 @@ publish = false
[lib] [lib]
path = "src/util.rs" path = "src/util.rs"
doctest = false doctest = true
[features] [features]
test-support = ["tempdir", "git2"] test-support = ["tempdir", "git2"]
+47 -1
View File
@@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::{Path, PathBuf};
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory"); 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"); 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()
}
}
+3 -3
View File
@@ -87,21 +87,21 @@ pub fn marked_text_ranges_by(
/// 1. To mark a range of text, surround it with the `«` and `»` angle brackets, /// 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. /// which can be typed on a US keyboard with the `alt-|` and `alt-shift-|` keys.
/// ///
/// ``` /// ```text
/// foo «selected text» bar /// foo «selected text» bar
/// ``` /// ```
/// ///
/// 2. To mark a single position in the text, use the `ˇ` caron, /// 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. /// which can be typed on a US keyboard with the `alt-shift-t` key.
/// ///
/// ``` /// ```text
/// the cursors are hereˇ and hereˇ. /// the cursors are hereˇ and hereˇ.
/// ``` /// ```
/// ///
/// 3. To mark a range whose direction is meaningful (like a selection), /// 3. To mark a range whose direction is meaningful (like a selection),
/// put a caron character beside one of its bounds, on the inside: /// put a caron character beside one of its bounds, on the inside:
/// ///
/// ``` /// ```text
/// one «ˇreversed» selection and one «forwardˇ» selection /// one «ˇreversed» selection and one «forwardˇ» selection
/// ``` /// ```
pub fn marked_text_ranges( pub fn marked_text_ranges(