diff options
author | Falco Hirschenberger | 2022-06-30 09:16:18 +0000 |
---|---|---|
committer | GitHub | 2022-06-30 09:16:18 +0000 |
commit | ed89f8897eab84bf7614a718d5d1e3ec5c57086c (patch) | |
tree | 347bd0db6509b1c4a6e88f6ca7e461dc8a923215 /helix-core/src/path.rs | |
parent | 94fc41a41920fc705f01637e7902f06a1c32d998 (diff) |
Add workspace and document diagnostics picker (#2013)
* Add workspace and document diagnostics picker
fixes #1891
* Fix some of @archseer's annotations
* Add From<&Spans> impl for String
* More descriptive parameter names.
* Adding From<Cow<str>> impls for Span and Spans
* Add new keymap entries to docs
* Avoid some clones
* Fix api change
* Update helix-term/src/application.rs
Co-authored-by: Bjorn Ove Hay Andersen <bjrnove@gmail.com>
* Fix a clippy hint
* Sort diagnostics first by URL and then by severity.
* Sort diagnostics first by URL and then by severity.
* Ignore missing lsp severity entries
* Add truncated filepath
* Typo
* Strip cwd from paths and use url-path without schema
* Make tests a doctest
* Better variable names
Co-authored-by: Falco Hirschenberger <falco.hirschenberger@itwm.fraunhofer.de>
Co-authored-by: Bjorn Ove Hay Andersen <bjrnove@gmail.com>
Diffstat (limited to 'helix-core/src/path.rs')
-rw-r--r-- | helix-core/src/path.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/helix-core/src/path.rs b/helix-core/src/path.rs index cb50e136..d59a6baa 100644 --- a/helix-core/src/path.rs +++ b/helix-core/src/path.rs @@ -90,3 +90,54 @@ pub fn get_relative_path(path: &Path) -> PathBuf { }; fold_home_dir(path) } + +/// Returns a truncated filepath where the basepart of the path is reduced to the first +/// char of the folder and the whole filename appended. +/// +/// Also strip the current working directory from the beginning of the path. +/// Note that this function does not check if the truncated path is unambiguous. +/// +/// ``` +/// use helix_core::path::get_truncated_path; +/// use std::path::Path; +/// +/// assert_eq!( +/// get_truncated_path("/home/cnorris/documents/jokes.txt").as_path(), +/// Path::new("/h/c/d/jokes.txt") +/// ); +/// assert_eq!( +/// get_truncated_path("jokes.txt").as_path(), +/// Path::new("jokes.txt") +/// ); +/// assert_eq!( +/// get_truncated_path("/jokes.txt").as_path(), +/// Path::new("/jokes.txt") +/// ); +/// assert_eq!( +/// get_truncated_path("/h/c/d/jokes.txt").as_path(), +/// Path::new("/h/c/d/jokes.txt") +/// ); +/// assert_eq!(get_truncated_path("").as_path(), Path::new("")); +/// ``` +/// +pub fn get_truncated_path<P: AsRef<Path>>(path: P) -> PathBuf { + let cwd = std::env::current_dir().unwrap_or_default(); + let path = path + .as_ref() + .strip_prefix(cwd) + .unwrap_or_else(|_| path.as_ref()); + let file = path.file_name().unwrap_or_default(); + let base = path.parent().unwrap_or_else(|| Path::new("")); + let mut ret = PathBuf::new(); + for d in base { + ret.push( + d.to_string_lossy() + .chars() + .next() + .unwrap_or_default() + .to_string(), + ); + } + ret.push(file); + ret +} |