aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo8it2024-02-29 23:18:12 +0000
committerBlaž Hrastnik2024-03-19 05:39:46 +0000
commite91ec8e880062c1822d08f47dac48dffeab4548f (patch)
treee9a57487a9da580bf54428e0a8eb1be692e7c1af
parent6ed93b6e49df2499cd32cc4e4f6dcfe6d416c907 (diff)
Optimize getting a relative path
-rw-r--r--helix-stdx/src/path.rs35
-rw-r--r--helix-term/src/ui/statusline.rs2
-rw-r--r--helix-view/src/document.rs2
3 files changed, 23 insertions, 16 deletions
diff --git a/helix-stdx/src/path.rs b/helix-stdx/src/path.rs
index 42d7a9b6..c514f9f2 100644
--- a/helix-stdx/src/path.rs
+++ b/helix-stdx/src/path.rs
@@ -10,17 +10,21 @@ use crate::env::current_working_dir;
/// Replaces users home directory from `path` with tilde `~` if the directory
/// is available, otherwise returns the path unchanged.
-pub fn fold_home_dir(path: &Path) -> PathBuf {
+pub fn fold_home_dir<'a, P>(path: P) -> Cow<'a, Path>
+where
+ P: Into<Cow<'a, Path>>,
+{
+ let path = path.into();
if let Ok(home) = home_dir() {
if let Ok(stripped) = path.strip_prefix(&home) {
let mut path = OsString::with_capacity(2 + stripped.as_os_str().len());
path.push("~/");
path.push(stripped);
- return PathBuf::from(path);
+ return Cow::Owned(PathBuf::from(path));
}
}
- path.to_path_buf()
+ path
}
/// Expands tilde `~` into users home directory if available, otherwise returns the path
@@ -129,18 +133,21 @@ pub fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
normalize(path)
}
-pub fn get_relative_path(path: impl AsRef<Path>) -> PathBuf {
- let path = PathBuf::from(path.as_ref());
- let path = if path.is_absolute() {
+pub fn get_relative_path<'a, P>(path: P) -> Cow<'a, Path>
+where
+ P: Into<Cow<'a, Path>>,
+{
+ let path = path.into();
+ if path.is_absolute() {
let cwdir = normalize(current_working_dir());
- normalize(&path)
- .strip_prefix(cwdir)
- .map(PathBuf::from)
- .unwrap_or(path)
- } else {
- path
- };
- fold_home_dir(&path)
+ if let Ok(stripped) = normalize(&path).strip_prefix(cwdir) {
+ return Cow::Owned(PathBuf::from(stripped));
+ }
+
+ return fold_home_dir(path);
+ }
+
+ path
}
/// Returns a truncated filepath where the basepart of the path is reduced to the first
diff --git a/helix-term/src/ui/statusline.rs b/helix-term/src/ui/statusline.rs
index c3464067..7437cbd0 100644
--- a/helix-term/src/ui/statusline.rs
+++ b/helix-term/src/ui/statusline.rs
@@ -482,7 +482,7 @@ where
let rel_path = context.doc.relative_path();
let path = rel_path
.as_ref()
- .and_then(|p| p.as_path().file_name().map(|s| s.to_string_lossy()))
+ .and_then(|p| p.file_name().map(|s| s.to_string_lossy()))
.unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
format!(" {} ", path)
};
diff --git a/helix-view/src/document.rs b/helix-view/src/document.rs
index 4e7b1de9..5f3595ee 100644
--- a/helix-view/src/document.rs
+++ b/helix-view/src/document.rs
@@ -1685,7 +1685,7 @@ impl Document {
&self.selections
}
- pub fn relative_path(&self) -> Option<PathBuf> {
+ pub fn relative_path(&self) -> Option<Cow<Path>> {
self.path
.as_deref()
.map(helix_stdx::path::get_relative_path)