Add support for relative terminal links (#7303)

Allow opening file paths relative to terminal's cwd


https://github.com/zed-industries/zed/assets/67913738/413a1107-541e-4c25-ae7c-cbe45469d452


Release Notes:

- Added support for opening file paths relative to terminal's cwd
([#7144](https://github.com/zed-industries/zed/issues/7144)).

---------

Co-authored-by: Kirill <kirill@zed.dev>
This commit is contained in:
Robin Pfäffle 2024-02-03 16:04:27 +01:00 committed by GitHub
parent 54aecd21ec
commit 06674a21f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 168 additions and 79 deletions

View file

@ -86,6 +86,15 @@ pub enum Event {
Open(MaybeNavigationTarget),
}
#[derive(Clone, Debug)]
pub struct PathLikeTarget {
/// File system path, absolute or relative, existing or not.
/// Might have line and column number(s) attached as `file.rs:1:23`
pub maybe_path: String,
/// Current working directory of the terminal
pub terminal_dir: Option<PathBuf>,
}
/// A string inside terminal, potentially useful as a URI that can be opened.
#[derive(Clone, Debug)]
pub enum MaybeNavigationTarget {
@ -93,7 +102,7 @@ pub enum MaybeNavigationTarget {
Url(String),
/// File system path, absolute or relative, existing or not.
/// Might have line and column number(s) attached as `file.rs:1:23`
PathLike(String),
PathLike(PathLikeTarget),
}
#[derive(Clone)]
@ -626,6 +635,12 @@ impl Terminal {
}
}
fn get_cwd(&self) -> Option<PathBuf> {
self.foreground_process_info
.as_ref()
.map(|info| info.cwd.clone())
}
///Takes events from Alacritty and translates them to behavior on this view
fn process_terminal_event(
&mut self,
@ -800,7 +815,10 @@ impl Terminal {
let target = if is_url {
MaybeNavigationTarget::Url(maybe_url_or_path)
} else {
MaybeNavigationTarget::PathLike(maybe_url_or_path)
MaybeNavigationTarget::PathLike(PathLikeTarget {
maybe_path: maybe_url_or_path,
terminal_dir: self.get_cwd(),
})
};
cx.emit(Event::Open(target));
} else {
@ -852,7 +870,10 @@ impl Terminal {
let navigation_target = if is_url {
MaybeNavigationTarget::Url(word)
} else {
MaybeNavigationTarget::PathLike(word)
MaybeNavigationTarget::PathLike(PathLikeTarget {
maybe_path: word,
terminal_dir: self.get_cwd(),
})
};
cx.emit(Event::NewNavigationTarget(Some(navigation_target)));
}