task: Add ZED_RELATIVE_DIR task variable (#31657)

This is my first contribution to zed, let me know if I missed anything.

There is no corresponding issue/discussion.

`$ZED_RELATIVE_DIR` can be used in cases where a task's command's
filesystem namespace (e.g. inside a container) is different than the
host, where absolute paths cannot work.

I modified `relative_path` to `relative_file` after the addition of
`relative_dir`.

For top-level files, where `relative_file.parent() == Some("")`, I use
`"."` for `$ZED_RELATIVE_DIR`, which is a valid relative path in both
*nix and windows.

Thank you for building zed, and open-sourcing it. I hope to contribute
more as I use it as my primary editor.

Release Notes:

- Added ZED_RELATIVE_DIR (path to current file's directory relative to
worktree root) task variable.
This commit is contained in:
Dhruvin Gandhi 2025-05-29 15:20:36 +05:30 committed by GitHub
parent b4af61edfe
commit ae076fa415
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 2 deletions

View file

@ -847,11 +847,21 @@ impl ContextProvider for BasicContextProvider {
);
if let Some(full_path) = current_file.as_ref() {
let relative_path = pathdiff::diff_paths(full_path, worktree_path);
if let Some(relative_path) = relative_path {
if let Some(relative_file) = relative_path {
task_variables.insert(
VariableName::RelativeFile,
relative_path.to_sanitized_string(),
relative_file.to_sanitized_string(),
);
if let Some(relative_dir) = relative_file.parent() {
task_variables.insert(
VariableName::RelativeDir,
if relative_dir.as_os_str().is_empty() {
String::from(".")
} else {
relative_dir.to_sanitized_string()
},
);
}
}
}
}