diff options
author | Norbert Preining <norbert@preining.info> | 2024-05-02 03:02:55 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2024-05-02 03:02:55 +0000 |
commit | 68c5442089b7c2e61ee14fc3efed490e291a244a (patch) | |
tree | 91d0e68d495b53cb52bb60a3e234da061741067d /support/texlab/crates/base-db/src/util/expand.rs | |
parent | 6ac3dbed4b71022bd6bb0cc25e8fc3df81198498 (diff) |
CTAN sync 202405020302
Diffstat (limited to 'support/texlab/crates/base-db/src/util/expand.rs')
-rw-r--r-- | support/texlab/crates/base-db/src/util/expand.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/support/texlab/crates/base-db/src/util/expand.rs b/support/texlab/crates/base-db/src/util/expand.rs new file mode 100644 index 0000000000..a315737115 --- /dev/null +++ b/support/texlab/crates/base-db/src/util/expand.rs @@ -0,0 +1,31 @@ +use std::{borrow::Cow, path::PathBuf}; + +use url::Url; + +use crate::deps::HOME_DIR; + +pub fn expand_relative_path( + path: &str, + current_dir: &Url, + workspace_folders: &[PathBuf], +) -> Result<Url, url::ParseError> { + let home_dir = HOME_DIR.as_ref().and_then(|dir| dir.to_str()); + + let workspace_folder = workspace_folders.iter().find_map(|folder| { + let current_dir = current_dir.to_file_path().ok()?; + if current_dir.starts_with(folder) { + Some(folder.to_str()?) + } else { + None + } + }); + + let expand_var = |variable: &str| match variable { + "userHome" => home_dir.map(Cow::Borrowed), + "workspaceFolder" => Some(Cow::Borrowed(workspace_folder.unwrap_or("."))), + _ => std::env::var(variable).ok().map(Cow::Owned), + }; + + let path = shellexpand::full_with_context_no_errors(&path, || home_dir, expand_var); + current_dir.join(&path) +} |