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
commit 007aa92581
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 5 deletions

1
Cargo.lock generated
View file

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

View file

@ -21,3 +21,4 @@ workspace = { path = "../workspace" }
ordered-float = "2.1.1"
postage = { workspace = true }
smol = "1.2"
util = { path = "../util"}

View file

@ -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,

View file

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

View file

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

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,
/// 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(