Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

terminal: Resolve path hyperlinks using per-line cwd (#52454)

Saber Oueslati184e124bba43
diff --git a/crates/terminal/src/pty_info.rs b/crates/terminal/src/pty_info.rs
index 8808ef5c73e..35c814585e8 100644
--- a/crates/terminal/src/pty_info.rs
+++ b/crates/terminal/src/pty_info.rs
@@ -263,7 +263,7 @@ impl PtyProcessInfo {
return;
}
let this = self.clone();
- let has_changed = cx.background_executor().spawn(async move {
+ let change_task = cx.background_executor().spawn(async move {
let previous = this.current.read().clone();
let current = this.load();
let has_changed = match (previous.as_ref(), current.as_ref()) {
@@ -272,14 +272,26 @@ impl PtyProcessInfo {
_ => true,
};
if has_changed {
- *this.current.write() = current;
+ *this.current.write() = current.clone();
}
- has_changed
+ let changed_cwd = match (previous.as_ref(), current.as_ref()) {
+ (Some(prev), Some(now)) if prev.cwd != now.cwd => Some(now.cwd.clone()),
+ (None, Some(now)) => Some(now.cwd.clone()),
+ _ => None,
+ };
+ (has_changed, changed_cwd)
});
let this = Arc::downgrade(self);
*self.task.lock() = Some(cx.spawn(async move |term, cx| {
- if has_changed.await {
- term.update(cx, |_, cx| cx.emit(Event::TitleChanged)).ok();
+ let (has_changed, new_cwd) = change_task.await;
+ if has_changed {
+ term.update(cx, |terminal, cx| {
+ if let Some(cwd) = new_cwd {
+ terminal.record_cwd_change(cwd);
+ }
+ cx.emit(Event::TitleChanged);
+ })
+ .ok();
}
if let Some(this) = this.upgrade() {
this.task.lock().take();
diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs
index 7a3f0795f77..a3e6ee421f8 100644
--- a/crates/terminal/src/terminal.rs
+++ b/crates/terminal/src/terminal.rs
@@ -15,6 +15,7 @@ use futures::{
channel::mpsc::{UnboundedReceiver, unbounded},
};
+use alacritty_terminal::grid::Dimensions as _;
use itertools::Itertools as _;
use mappings::mouse::{
alt_scroll, grid_point, grid_point_and_side, mouse_button_report, mouse_moved_report,
@@ -695,7 +696,7 @@ pub struct PathLikeTarget {
/// 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>,
+ pub working_directory: Option<PathBuf>,
}
/// A string inside terminal, potentially useful as a URI that can be opened.
@@ -1032,6 +1033,8 @@ impl TerminalBuilder {
event_loop_task: Task::ready(Ok(())),
background_executor: background_executor.clone(),
path_style,
+ cwd_history: Vec::new(),
+ pending_cwd_boundary: None,
#[cfg(any(test, feature = "test-support"))]
input_log: Vec::new(),
#[cfg(any(test, feature = "test-support"))]
@@ -1305,6 +1308,20 @@ impl TerminalBuilder {
event_loop_task: Task::ready(Ok(())),
background_executor,
path_style,
+ cwd_history: if is_remote_terminal {
+ Vec::new()
+ } else {
+ working_directory
+ .as_ref()
+ .map(|working_directory| {
+ vec![CwdHistoryEntry {
+ scrollback_position: i32::MIN,
+ working_directory: working_directory.clone(),
+ }]
+ })
+ .unwrap_or_default()
+ },
+ pending_cwd_boundary: None,
#[cfg(any(test, feature = "test-support"))]
input_log: Vec::new(),
#[cfg(any(test, feature = "test-support"))]
@@ -1500,12 +1517,21 @@ pub struct Terminal {
event_loop_task: Task<Result<(), anyhow::Error>>,
background_executor: BackgroundExecutor,
path_style: PathStyle,
+ cwd_history: Vec<CwdHistoryEntry>,
+ pending_cwd_boundary: Option<i32>,
#[cfg(any(test, feature = "test-support"))]
input_log: Vec<Vec<u8>>,
#[cfg(any(test, feature = "test-support"))]
pty_write_log: std::cell::RefCell<Vec<Vec<u8>>>,
}
+#[derive(Clone, Debug, Eq, PartialEq)]
+struct CwdHistoryEntry {
+ /// Line offset in the retained scrollback buffer.
+ scrollback_position: i32,
+ working_directory: PathBuf,
+}
+
struct CopyTemplate {
shell: Shell,
env: HashMap<String, String>,
@@ -1660,6 +1686,8 @@ impl Terminal {
let new_bounds = normalize_terminal_bounds(new_bounds);
trace!("Resizing: new_bounds={new_bounds:?}");
+ let columns_changed =
+ self.last_content.terminal_bounds.num_columns() != new_bounds.num_columns();
self.last_content.terminal_bounds = new_bounds;
if let TerminalType::Pty { pty_tx, .. } = &self.terminal_type {
@@ -1667,6 +1695,9 @@ impl Terminal {
}
resize(term, new_bounds);
+ if columns_changed {
+ self.reset_cwd_history();
+ }
// If there are matches we need to emit a wake up event to
// invalidate the matches and recalculate their locations
// in the new terminal layout
@@ -1677,6 +1708,7 @@ impl Terminal {
InternalEvent::Clear => {
trace!("Clearing");
clear_saved_screen(term);
+ self.reset_cwd_history();
cx.emit(Event::Wakeup);
}
InternalEvent::Scroll(scroll) => {
@@ -1777,7 +1809,8 @@ impl Terminal {
self.path_style,
) {
Some(hyperlink) => {
- self.process_hyperlink(hyperlink, *open, cx);
+ let history_size = term.history_size();
+ self.process_hyperlink(hyperlink, *open, history_size, cx);
}
None => {
self.last_content.last_hovered_word = None;
@@ -1786,18 +1819,29 @@ impl Terminal {
}
}
InternalEvent::ProcessHyperlink(hyperlink, open) => {
- self.process_hyperlink(hyperlink.clone(), *open, cx);
+ // history_size must be read here since process_hyperlink cannot lock term
+ // (sync() already holds the lock when dispatching events)
+ let history_size = term.history_size();
+ self.process_hyperlink(hyperlink.clone(), *open, history_size, cx);
}
}
}
- fn process_hyperlink(&mut self, hyperlink: HyperlinkMatch, open: bool, cx: &mut Context<Self>) {
+ fn process_hyperlink(
+ &mut self,
+ hyperlink: HyperlinkMatch,
+ open: bool,
+ history_size: usize,
+ cx: &mut Context<Self>,
+ ) {
let HyperlinkMatch {
text: maybe_url_or_path,
is_url,
range,
} = hyperlink;
let prev_hovered_word = self.last_content.last_hovered_word.take();
+ let match_line = range.start().line;
+ let working_directory = self.cwd_at_line(match_line, history_size);
let target = if is_url {
if let Some(path) = maybe_url_or_path.strip_prefix("file://") {
@@ -1807,7 +1851,7 @@ impl Terminal {
MaybeNavigationTarget::PathLike(PathLikeTarget {
maybe_path: decoded_path,
- terminal_dir: self.working_directory(),
+ working_directory,
})
} else {
MaybeNavigationTarget::Url(maybe_url_or_path.clone())
@@ -1815,7 +1859,7 @@ impl Terminal {
} else {
MaybeNavigationTarget::PathLike(PathLikeTarget {
maybe_path: maybe_url_or_path.clone(),
- terminal_dir: self.working_directory(),
+ working_directory,
})
};
@@ -2142,14 +2186,23 @@ impl Terminal {
let mut term = self.term.lock_unfair();
clear_saved_screen(&mut term);
self.last_content = make_content(&term, &self.last_content);
+ drop(term);
+ self.reset_cwd_history();
cx.emit(Event::Wakeup);
}
fn write_input(&mut self, input: impl Into<Cow<'static, [u8]>>) {
+ let input = input.into();
+ if !self.is_remote_terminal && input.contains(&b'\r') {
+ let term = self.term.lock_unfair();
+ self.pending_cwd_boundary = Some(Self::scrollback_position(
+ term.grid().cursor.point.line.0,
+ term.history_size(),
+ ));
+ }
+
self.events.push_back(InternalEvent::Scroll(Scroll::Bottom));
self.events.push_back(InternalEvent::SetSelection(None));
-
- let input = input.into();
#[cfg(any(test, feature = "test-support"))]
self.input_log.push(input.to_vec());
@@ -2792,6 +2845,57 @@ impl Terminal {
}
}
+ pub(crate) fn record_cwd_change(&mut self, new_working_directory: PathBuf) {
+ if self.is_remote_terminal {
+ return;
+ }
+
+ let scrollback_position = self.pending_cwd_boundary.take().unwrap_or_else(|| {
+ let term = self.term.lock_unfair();
+ Self::scrollback_position(term.grid().cursor.point.line.0, term.history_size())
+ });
+ self.cwd_history.push(CwdHistoryEntry {
+ scrollback_position,
+ working_directory: new_working_directory,
+ });
+ }
+
+ fn reset_cwd_history(&mut self) {
+ self.pending_cwd_boundary = None;
+ self.cwd_history = self
+ .working_directory()
+ .map(|working_directory| {
+ vec![CwdHistoryEntry {
+ scrollback_position: i32::MIN,
+ working_directory,
+ }]
+ })
+ .unwrap_or_default();
+ }
+
+ fn cwd_at_line(&self, line: i32, history_size: usize) -> Option<PathBuf> {
+ // Once the scrollback cap is reached, evictions move retained lines without changing
+ // `history_size`, so stored row offsets no longer identify their original lines.
+ if self.is_remote_terminal
+ || self.cwd_history.is_empty()
+ || history_size >= self.term_config.scrolling_history
+ {
+ return self.working_directory();
+ }
+ let scrollback_position = Self::scrollback_position(line, history_size);
+ self.cwd_history
+ .iter()
+ .rev()
+ .find(|entry| entry.scrollback_position <= scrollback_position)
+ .map(|entry| entry.working_directory.clone())
+ .or_else(|| self.working_directory())
+ }
+
+ fn scrollback_position(line: i32, history_size: usize) -> i32 {
+ let history_size = i32::try_from(history_size).unwrap_or(i32::MAX);
+ history_size.saturating_add(line)
+ }
+
pub fn title(&self, truncate: bool) -> String {
const MAX_CHARS: usize = 25;
match &self.task {
@@ -4362,10 +4466,17 @@ mod tests {
});
let wrote = terminal.update(cx, |terminal, cx| {
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 42,
+ working_directory: PathBuf::from("/stale/cwd"),
+ });
terminal.write_init_command_after_startup(b"agent\r".to_vec(), cx)
});
assert!(wrote);
- let content = terminal.update(cx, |terminal, _| terminal.get_content());
+ let (content, cwd_history) = terminal.update(cx, |terminal, _| {
+ (terminal.get_content(), terminal.cwd_history.clone())
+ });
+ assert!(cwd_history.is_empty());
assert!(
!content.contains("startup output"),
"startup output should be cleared internally before writing the init command"
@@ -5208,4 +5319,151 @@ mod tests {
}
}
}
+
+ fn make_display_only_terminal() -> Terminal {
+ let dispatcher = gpui::TestDispatcher::new(rand::random());
+ let executor = gpui::BackgroundExecutor::new(std::sync::Arc::new(dispatcher));
+ TerminalBuilder::new_display_only(
+ SettingsCursorShape::default(),
+ AlternateScroll::On,
+ None,
+ 0,
+ &executor,
+ PathStyle::local(),
+ )
+ .terminal
+ }
+
+ #[test]
+ fn test_cwd_at_line_empty_history_returns_none() {
+ let terminal = make_display_only_terminal();
+ assert_eq!(terminal.cwd_at_line(0, 0), None);
+ }
+
+ #[test]
+ fn test_cwd_at_line_returns_cwd_for_line_at_or_after_recorded_position() {
+ let mut terminal = make_display_only_terminal();
+ let working_directory_a = PathBuf::from("/home/user/project_a");
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 5,
+ working_directory: working_directory_a.clone(),
+ });
+
+ // click_pos = history_size(5) + line(3) = 8 >= 5
+ assert_eq!(
+ terminal.cwd_at_line(3, 5),
+ Some(working_directory_a.clone())
+ );
+ // click_pos = history_size(5) + line(0) = 5 == 5 (exact match)
+ assert_eq!(terminal.cwd_at_line(0, 5), Some(working_directory_a));
+ }
+
+ #[test]
+ fn test_cwd_at_line_ignores_history_at_scrollback_cap() {
+ let mut terminal = make_display_only_terminal();
+ terminal.term_config.scrolling_history = 10;
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 0,
+ working_directory: PathBuf::from("/stale/cwd"),
+ });
+
+ assert_eq!(terminal.cwd_at_line(-5, 10), None);
+ }
+
+ #[test]
+ fn test_cwd_at_line_returns_none_when_line_is_before_any_recorded_cwd() {
+ let mut terminal = make_display_only_terminal();
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 10,
+ working_directory: PathBuf::from("/home/user/project_a"),
+ });
+
+ // click_pos = 0 + 3 = 3 < 10, no match, falls back to working_directory (None)
+ assert_eq!(terminal.cwd_at_line(3, 0), None);
+ }
+
+ #[test]
+ fn test_cwd_at_line_selects_most_recent_cwd_before_click() {
+ let mut terminal = make_display_only_terminal();
+ let working_directory_a = PathBuf::from("/home/user/project_a");
+ let working_directory_b = PathBuf::from("/home/user/project_b");
+ let working_directory_c = PathBuf::from("/home/user/project_c");
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 0,
+ working_directory: working_directory_a.clone(),
+ });
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 10,
+ working_directory: working_directory_b.clone(),
+ });
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 20,
+ working_directory: working_directory_c.clone(),
+ });
+
+ // click_pos=5: between 0 and 10, working_directory_a
+ assert_eq!(terminal.cwd_at_line(5, 0), Some(working_directory_a));
+ // click_pos=15: between 10 and 20, working_directory_b
+ assert_eq!(terminal.cwd_at_line(15, 0), Some(working_directory_b));
+ // click_pos=25: after 20, working_directory_c
+ assert_eq!(terminal.cwd_at_line(25, 0), Some(working_directory_c));
+ }
+
+ #[test]
+ fn test_record_cwd_change_stores_entry_at_current_cursor_position() {
+ let mut terminal = make_display_only_terminal();
+ let working_directory = PathBuf::from("/tmp/test");
+ terminal.record_cwd_change(working_directory.clone());
+
+ assert_eq!(terminal.cwd_history.len(), 1);
+ let entry = &terminal.cwd_history[0];
+ assert_eq!(entry.scrollback_position, 0);
+ assert_eq!(entry.working_directory, working_directory);
+ }
+
+ #[test]
+ fn test_record_cwd_change_uses_command_boundary() {
+ let mut terminal = make_display_only_terminal();
+ terminal.write_input(b"\r".to_vec());
+ assert_eq!(terminal.pending_cwd_boundary, Some(0));
+
+ let working_directory = PathBuf::from("/tmp/test");
+ terminal.record_cwd_change(working_directory.clone());
+
+ assert_eq!(terminal.pending_cwd_boundary, None);
+ assert_eq!(
+ terminal.cwd_history,
+ vec![CwdHistoryEntry {
+ scrollback_position: 0,
+ working_directory,
+ }]
+ );
+ }
+
+ #[test]
+ fn test_remote_terminal_does_not_record_local_cwd() {
+ let mut terminal = make_display_only_terminal();
+ terminal.is_remote_terminal = true;
+ terminal.write_input(b"\r".to_vec());
+ terminal.record_cwd_change(PathBuf::from("/local/ssh/cwd"));
+
+ assert_eq!(terminal.pending_cwd_boundary, None);
+ assert!(terminal.cwd_history.is_empty());
+ assert_eq!(terminal.cwd_at_line(0, 0), None);
+ }
+
+ #[test]
+ fn test_reset_cwd_history_discards_stale_coordinates() {
+ let mut terminal = make_display_only_terminal();
+ terminal.cwd_history.push(CwdHistoryEntry {
+ scrollback_position: 42,
+ working_directory: PathBuf::from("/tmp/test"),
+ });
+ terminal.pending_cwd_boundary = Some(43);
+
+ terminal.reset_cwd_history();
+
+ assert!(terminal.cwd_history.is_empty());
+ assert_eq!(terminal.pending_cwd_boundary, None);
+ }
}
diff --git a/crates/terminal_view/src/terminal_path_like_target.rs b/crates/terminal_view/src/terminal_path_like_target.rs
index 6a4a943b143..0b7ec675eff 100644
--- a/crates/terminal_view/src/terminal_path_like_target.rs
+++ b/crates/terminal_view/src/terminal_path_like_target.rs
@@ -47,7 +47,7 @@ fn possible_hover_target(
workspace,
PathMatching::Heuristic,
&path_like_target.maybe_path,
- path_like_target.terminal_dir.as_deref(),
+ path_like_target.working_directory.as_deref(),
cx,
);
#[cfg(test)]
@@ -55,7 +55,7 @@ fn possible_hover_target(
workspace,
PathMatching::Heuristic,
&path_like_target.maybe_path,
- path_like_target.terminal_dir.as_deref(),
+ path_like_target.working_directory.as_deref(),
cx,
background_path_checks,
);
@@ -126,7 +126,7 @@ fn possibly_open_target(
&workspace,
PathMatching::Heuristic,
&path_like_target.maybe_path,
- path_like_target.terminal_dir.as_deref(),
+ path_like_target.working_directory.as_deref(),
cx,
)
}
@@ -136,7 +136,7 @@ fn possibly_open_target(
&workspace,
PathMatching::Heuristic,
&path_like_target.maybe_path,
- path_like_target.terminal_dir.as_deref(),
+ path_like_target.working_directory.as_deref(),
cx,
background_path_checks,
)
@@ -268,7 +268,7 @@ mod tests {
) -> (Option<HoverTarget>, Option<OpenTarget>),
maybe_path: &str,
tooltip: &str,
- terminal_dir: Option<PathBuf>,
+ working_directory: Option<PathBuf>,
background_path_checks: BackgroundPathChecks,
open_target_found_by: OpenTargetFoundBy,
file: &str,
@@ -282,7 +282,7 @@ mod tests {
},
PathLikeTarget {
maybe_path: maybe_path.to_string(),
- terminal_dir,
+ working_directory,
},
background_path_checks,
)