Allow splitting terminal items in the central pane group (#22088)

Follow-up of https://github.com/zed-industries/zed/pull/22004
Closes https://github.com/zed-industries/zed/issues/22078

Release Notes:

- Fixed splitting terminal items in the center
This commit is contained in:
Kirill Bulatov 2024-12-16 19:23:01 +02:00 committed by GitHub
parent 88f7942f11
commit ff2ad63037
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 294 additions and 256 deletions

View file

@ -94,7 +94,6 @@ impl Project {
} }
} }
}; };
let ssh_details = self.ssh_details(cx);
let mut settings_location = None; let mut settings_location = None;
if let Some(path) = path.as_ref() { if let Some(path) = path.as_ref() {
@ -107,10 +106,57 @@ impl Project {
} }
let settings = TerminalSettings::get(settings_location, cx).clone(); let settings = TerminalSettings::get(settings_location, cx).clone();
cx.spawn(move |project, mut cx| async move {
let python_venv_directory = if let Some(path) = path.clone() {
project
.update(&mut cx, |this, cx| {
this.python_venv_directory(path, settings.detect_venv.clone(), cx)
})?
.await
} else {
None
};
project.update(&mut cx, |project, cx| {
project.create_terminal_with_venv(kind, python_venv_directory, window, cx)
})?
})
}
pub fn create_terminal_with_venv(
&mut self,
kind: TerminalKind,
python_venv_directory: Option<PathBuf>,
window: AnyWindowHandle,
cx: &mut ModelContext<Self>,
) -> Result<Model<Terminal>> {
let this = &mut *self;
let path: Option<Arc<Path>> = match &kind {
TerminalKind::Shell(path) => path.as_ref().map(|path| Arc::from(path.as_ref())),
TerminalKind::Task(spawn_task) => {
if let Some(cwd) = &spawn_task.cwd {
Some(Arc::from(cwd.as_ref()))
} else {
this.active_project_directory(cx)
}
}
};
let ssh_details = this.ssh_details(cx);
let mut settings_location = None;
if let Some(path) = path.as_ref() {
if let Some((worktree, _)) = this.find_worktree(path, cx) {
settings_location = Some(SettingsLocation {
worktree_id: worktree.read(cx).id(),
path,
});
}
}
let settings = TerminalSettings::get(settings_location, cx).clone();
let (completion_tx, completion_rx) = bounded(1); let (completion_tx, completion_rx) = bounded(1);
// Start with the environment that we might have inherited from the Zed CLI. // Start with the environment that we might have inherited from the Zed CLI.
let mut env = self let mut env = this
.environment .environment
.read(cx) .read(cx)
.get_cli_environment() .get_cli_environment()
@ -125,165 +171,141 @@ impl Project {
None None
}; };
cx.spawn(move |this, mut cx| async move { let mut python_venv_activate_command = None;
let python_venv_directory = if let Some(path) = path.clone() {
this.update(&mut cx, |this, cx| {
this.python_venv_directory(path, settings.detect_venv.clone(), cx)
})?
.await
} else {
None
};
let mut python_venv_activate_command = None;
let (spawn_task, shell) = match kind { let (spawn_task, shell) = match kind {
TerminalKind::Shell(_) => { TerminalKind::Shell(_) => {
if let Some(python_venv_directory) = python_venv_directory { if let Some(python_venv_directory) = &python_venv_directory {
python_venv_activate_command = this python_venv_activate_command =
.update(&mut cx, |this, _| { this.python_activate_command(python_venv_directory, &settings.detect_venv);
this.python_activate_command( }
&python_venv_directory,
&settings.detect_venv, match &ssh_details {
) Some((host, ssh_command)) => {
}) log::debug!("Connecting to a remote server: {ssh_command:?}");
.ok()
.flatten(); // Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed
// to properly display colors.
// We do not have the luxury of assuming the host has it installed,
// so we set it to a default that does not break the highlighting via ssh.
env.entry("TERM".to_string())
.or_insert_with(|| "xterm-256color".to_string());
let (program, args) =
wrap_for_ssh(&ssh_command, None, path.as_deref(), env, None);
env = HashMap::default();
(
Option::<TaskState>::None,
Shell::WithArguments {
program,
args,
title_override: Some(format!("{} — Terminal", host).into()),
},
)
} }
None => (None, settings.shell.clone()),
}
}
TerminalKind::Task(spawn_task) => {
let task_state = Some(TaskState {
id: spawn_task.id,
full_label: spawn_task.full_label,
label: spawn_task.label,
command_label: spawn_task.command_label,
hide: spawn_task.hide,
status: TaskStatus::Running,
show_summary: spawn_task.show_summary,
show_command: spawn_task.show_command,
completion_rx,
});
match &ssh_details { env.extend(spawn_task.env);
Some((host, ssh_command)) => {
log::debug!("Connecting to a remote server: {ssh_command:?}");
// Alacritty sets its terminfo to `alacritty`, this requiring hosts to have it installed if let Some(venv_path) = &python_venv_directory {
// to properly display colors. env.insert(
// We do not have the luxury of assuming the host has it installed, "VIRTUAL_ENV".to_string(),
// so we set it to a default that does not break the highlighting via ssh. venv_path.to_string_lossy().to_string(),
env.entry("TERM".to_string()) );
.or_insert_with(|| "xterm-256color".to_string()); }
let (program, args) = match &ssh_details {
wrap_for_ssh(ssh_command, None, path.as_deref(), env, None); Some((host, ssh_command)) => {
env = HashMap::default(); log::debug!("Connecting to a remote server: {ssh_command:?}");
( env.entry("TERM".to_string())
Option::<TaskState>::None, .or_insert_with(|| "xterm-256color".to_string());
Shell::WithArguments { let (program, args) = wrap_for_ssh(
program, &ssh_command,
args, Some((&spawn_task.command, &spawn_task.args)),
title_override: Some(format!("{} — Terminal", host).into()), path.as_deref(),
}, env,
) python_venv_directory.as_deref(),
);
env = HashMap::default();
(
task_state,
Shell::WithArguments {
program,
args,
title_override: Some(format!("{} — Terminal", host).into()),
},
)
}
None => {
if let Some(venv_path) = &python_venv_directory {
add_environment_path(&mut env, &venv_path.join("bin")).log_err();
} }
None => (None, settings.shell.clone()),
(
task_state,
Shell::WithArguments {
program: spawn_task.command,
args: spawn_task.args,
title_override: None,
},
)
} }
} }
TerminalKind::Task(spawn_task) => { }
let task_state = Some(TaskState { };
id: spawn_task.id, TerminalBuilder::new(
full_label: spawn_task.full_label, local_path.map(|path| path.to_path_buf()),
label: spawn_task.label, python_venv_directory,
command_label: spawn_task.command_label, spawn_task,
hide: spawn_task.hide, shell,
status: TaskStatus::Running, env,
show_summary: spawn_task.show_summary, settings.cursor_shape.unwrap_or_default(),
show_command: spawn_task.show_command, settings.alternate_scroll,
completion_rx, settings.max_scroll_history_lines,
}); ssh_details.is_some(),
window,
completion_tx,
cx,
)
.map(|builder| {
let terminal_handle = cx.new_model(|cx| builder.subscribe(cx));
env.extend(spawn_task.env); this.terminals
.local_handles
.push(terminal_handle.downgrade());
if let Some(venv_path) = &python_venv_directory { let id = terminal_handle.entity_id();
env.insert( cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
"VIRTUAL_ENV".to_string(), let handles = &mut project.terminals.local_handles;
venv_path.to_string_lossy().to_string(),
);
}
match &ssh_details { if let Some(index) = handles
Some((host, ssh_command)) => { .iter()
log::debug!("Connecting to a remote server: {ssh_command:?}"); .position(|terminal| terminal.entity_id() == id)
env.entry("TERM".to_string()) {
.or_insert_with(|| "xterm-256color".to_string()); handles.remove(index);
let (program, args) = wrap_for_ssh( cx.notify();
ssh_command,
Some((&spawn_task.command, &spawn_task.args)),
path.as_deref(),
env,
python_venv_directory,
);
env = HashMap::default();
(
task_state,
Shell::WithArguments {
program,
args,
title_override: Some(format!("{} — Terminal", host).into()),
},
)
}
None => {
if let Some(venv_path) = &python_venv_directory {
add_environment_path(&mut env, &venv_path.join("bin")).log_err();
}
(
task_state,
Shell::WithArguments {
program: spawn_task.command,
args: spawn_task.args,
title_override: None,
},
)
}
}
} }
}; })
let terminal = this.update(&mut cx, |this, cx| { .detach();
TerminalBuilder::new(
local_path.map(|path| path.to_path_buf()),
spawn_task,
shell,
env,
settings.cursor_shape.unwrap_or_default(),
settings.alternate_scroll,
settings.max_scroll_history_lines,
ssh_details.is_some(),
window,
completion_tx,
cx,
)
.map(|builder| {
let terminal_handle = cx.new_model(|cx| builder.subscribe(cx));
this.terminals if let Some(activate_command) = python_venv_activate_command {
.local_handles this.activate_python_virtual_environment(activate_command, &terminal_handle, cx);
.push(terminal_handle.downgrade()); }
terminal_handle
let id = terminal_handle.entity_id();
cx.observe_release(&terminal_handle, move |project, _terminal, cx| {
let handles = &mut project.terminals.local_handles;
if let Some(index) = handles
.iter()
.position(|terminal| terminal.entity_id() == id)
{
handles.remove(index);
cx.notify();
}
})
.detach();
if let Some(activate_command) = python_venv_activate_command {
this.activate_python_virtual_environment(
activate_command,
&terminal_handle,
cx,
);
}
terminal_handle
})
})?;
terminal
}) })
} }
@ -418,9 +440,9 @@ impl Project {
&self, &self,
command: String, command: String,
terminal_handle: &Model<Terminal>, terminal_handle: &Model<Terminal>,
cx: &mut ModelContext<Project>, cx: &mut AppContext,
) { ) {
terminal_handle.update(cx, |this, _| this.input_bytes(command.into_bytes())); terminal_handle.update(cx, |terminal, _| terminal.input_bytes(command.into_bytes()));
} }
pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> { pub fn local_terminal_handles(&self) -> &Vec<WeakModel<terminal::Terminal>> {
@ -433,7 +455,7 @@ pub fn wrap_for_ssh(
command: Option<(&String, &Vec<String>)>, command: Option<(&String, &Vec<String>)>,
path: Option<&Path>, path: Option<&Path>,
env: HashMap<String, String>, env: HashMap<String, String>,
venv_directory: Option<PathBuf>, venv_directory: Option<&Path>,
) -> (String, Vec<String>) { ) -> (String, Vec<String>) {
let to_run = if let Some((command, args)) = command { let to_run = if let Some((command, args)) = command {
let command = Cow::Borrowed(command.as_str()); let command = Cow::Borrowed(command.as_str());

View file

@ -324,6 +324,7 @@ impl TerminalBuilder {
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
pub fn new( pub fn new(
working_directory: Option<PathBuf>, working_directory: Option<PathBuf>,
python_venv_directory: Option<PathBuf>,
task: Option<TaskState>, task: Option<TaskState>,
shell: Shell, shell: Shell,
mut env: HashMap<String, String>, mut env: HashMap<String, String>,
@ -471,6 +472,7 @@ impl TerminalBuilder {
word_regex: RegexSearch::new(WORD_REGEX).unwrap(), word_regex: RegexSearch::new(WORD_REGEX).unwrap(),
vi_mode_enabled: false, vi_mode_enabled: false,
is_ssh_terminal, is_ssh_terminal,
python_venv_directory,
}; };
Ok(TerminalBuilder { Ok(TerminalBuilder {
@ -619,6 +621,7 @@ pub struct Terminal {
pub breadcrumb_text: String, pub breadcrumb_text: String,
pub pty_info: PtyProcessInfo, pub pty_info: PtyProcessInfo,
title_override: Option<SharedString>, title_override: Option<SharedString>,
pub python_venv_directory: Option<PathBuf>,
scroll_px: Pixels, scroll_px: Pixels,
next_link_id: usize, next_link_id: usize,
selection_phase: SelectionPhase, selection_phase: SelectionPhase,

View file

@ -251,7 +251,13 @@ async fn deserialize_pane_group(
let terminal = terminal.await.ok()?; let terminal = terminal.await.ok()?;
pane.update(cx, |pane, cx| { pane.update(cx, |pane, cx| {
let terminal_view = Box::new(cx.new_view(|cx| { let terminal_view = Box::new(cx.new_view(|cx| {
TerminalView::new(terminal, workspace.clone(), Some(workspace_id), cx) TerminalView::new(
terminal,
workspace.clone(),
Some(workspace_id),
project.downgrade(),
cx,
)
})); }));
pane.add_item(terminal_view, true, false, None, cx); pane.add_item(terminal_view, true, false, None, cx);
}) })

View file

@ -335,24 +335,13 @@ impl TerminalPanel {
self.serialize(cx); self.serialize(cx);
} }
pane::Event::Split(direction) => { pane::Event::Split(direction) => {
let new_pane = self.new_pane_with_cloned_active_terminal(cx); let Some(new_pane) = self.new_pane_with_cloned_active_terminal(cx) else {
return;
};
let pane = pane.clone(); let pane = pane.clone();
let direction = *direction; let direction = *direction;
cx.spawn(move |terminal_panel, mut cx| async move { self.center.split(&pane, &new_pane, direction).log_err();
let Some(new_pane) = new_pane.await else { cx.focus_view(&new_pane);
return;
};
terminal_panel
.update(&mut cx, |terminal_panel, cx| {
terminal_panel
.center
.split(&pane, &new_pane, direction)
.log_err();
cx.focus_view(&new_pane);
})
.ok();
})
.detach();
} }
pane::Event::Focus => { pane::Event::Focus => {
self.active_pane = pane.clone(); self.active_pane = pane.clone();
@ -365,63 +354,56 @@ impl TerminalPanel {
fn new_pane_with_cloned_active_terminal( fn new_pane_with_cloned_active_terminal(
&mut self, &mut self,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Option<View<Pane>>> { ) -> Option<View<Pane>> {
let Some(workspace) = self.workspace.clone().upgrade() else { let workspace = self.workspace.clone().upgrade()?;
return Task::ready(None); let workspace = workspace.read(cx);
}; let database_id = workspace.database_id();
let database_id = workspace.read(cx).database_id();
let weak_workspace = self.workspace.clone(); let weak_workspace = self.workspace.clone();
let project = workspace.read(cx).project().clone(); let project = workspace.project().clone();
let working_directory = self let (working_directory, python_venv_directory) = self
.active_pane .active_pane
.read(cx) .read(cx)
.active_item() .active_item()
.and_then(|item| item.downcast::<TerminalView>()) .and_then(|item| item.downcast::<TerminalView>())
.and_then(|terminal_view| { .map(|terminal_view| {
terminal_view let terminal = terminal_view.read(cx).terminal().read(cx);
.read(cx) (
.terminal() terminal
.read(cx) .working_directory()
.working_directory() .or_else(|| default_working_directory(workspace, cx)),
terminal.python_venv_directory.clone(),
)
}) })
.or_else(|| default_working_directory(workspace.read(cx), cx)); .unwrap_or((None, None));
let kind = TerminalKind::Shell(working_directory); let kind = TerminalKind::Shell(working_directory);
let window = cx.window_handle(); let window = cx.window_handle();
cx.spawn(move |terminal_panel, mut cx| async move { let terminal = project
let terminal = project .update(cx, |project, cx| {
.update(&mut cx, |project, cx| { project.create_terminal_with_venv(kind, python_venv_directory, window, cx)
project.create_terminal(kind, window, cx)
})
.log_err()?
.await
.log_err()?;
let terminal_view = Box::new(
cx.new_view(|cx| {
TerminalView::new(terminal.clone(), weak_workspace.clone(), database_id, cx)
})
.ok()?,
);
let pane = terminal_panel
.update(&mut cx, |terminal_panel, cx| {
let pane = new_terminal_pane(
weak_workspace,
project,
terminal_panel.active_pane.read(cx).is_zoomed(),
cx,
);
terminal_panel.apply_tab_bar_buttons(&pane, cx);
pane
})
.ok()?;
pane.update(&mut cx, |pane, cx| {
pane.add_item(terminal_view, true, true, None, cx);
}) })
.ok()?; .ok()?;
Some(pane) let terminal_view = Box::new(cx.new_view(|cx| {
}) TerminalView::new(
terminal.clone(),
weak_workspace.clone(),
database_id,
project.downgrade(),
cx,
)
}));
let pane = new_terminal_pane(
weak_workspace,
project,
self.active_pane.read(cx).is_zoomed(),
cx,
);
self.apply_tab_bar_buttons(&pane, cx);
pane.update(cx, |pane, cx| {
pane.add_item(terminal_view, true, true, None, cx);
});
Some(pane)
} }
pub fn open_terminal( pub fn open_terminal(
@ -724,6 +706,7 @@ impl TerminalPanel {
terminal.clone(), terminal.clone(),
workspace.weak_handle(), workspace.weak_handle(),
workspace.database_id(), workspace.database_id(),
workspace.project().downgrade(),
cx, cx,
) )
}); });
@ -739,17 +722,19 @@ impl TerminalPanel {
reveal_strategy: RevealStrategy, reveal_strategy: RevealStrategy,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Task<Result<Model<Terminal>>> { ) -> Task<Result<Model<Terminal>>> {
if !self.is_enabled(cx) {
return Task::ready(Err(anyhow!(
"terminal not yet supported for remote projects"
)));
}
let workspace = self.workspace.clone(); let workspace = self.workspace.clone();
self.pending_terminals_to_add += 1; self.pending_terminals_to_add += 1;
cx.spawn(|terminal_panel, mut cx| async move { cx.spawn(|terminal_panel, mut cx| async move {
let pane = terminal_panel.update(&mut cx, |this, _| this.active_pane.clone())?; if workspace.update(&mut cx, |workspace, cx| {
!is_enabled_in_workspace(workspace, cx)
})? {
anyhow::bail!("terminal not yet supported for remote projects");
}
let pane = terminal_panel.update(&mut cx, |terminal_panel, _| {
terminal_panel.pending_terminals_to_add += 1;
terminal_panel.active_pane.clone()
})?;
let project = workspace.update(&mut cx, |workspace, _| workspace.project().clone())?; let project = workspace.update(&mut cx, |workspace, _| workspace.project().clone())?;
let window = cx.window_handle(); let window = cx.window_handle();
let terminal = project let terminal = project
@ -763,6 +748,7 @@ impl TerminalPanel {
terminal.clone(), terminal.clone(),
workspace.weak_handle(), workspace.weak_handle(),
workspace.database_id(), workspace.database_id(),
workspace.project().downgrade(),
cx, cx,
) )
})); }));
@ -1218,25 +1204,19 @@ impl Render for TerminalPanel {
if let Some(pane) = panes.get(action.0).map(|p| (*p).clone()) { if let Some(pane) = panes.get(action.0).map(|p| (*p).clone()) {
cx.focus_view(&pane); cx.focus_view(&pane);
} else { } else {
let new_pane = terminal_panel.new_pane_with_cloned_active_terminal(cx); if let Some(new_pane) =
cx.spawn(|terminal_panel, mut cx| async move { terminal_panel.new_pane_with_cloned_active_terminal(cx)
if let Some(new_pane) = new_pane.await { {
terminal_panel terminal_panel
.update(&mut cx, |terminal_panel, cx| { .center
terminal_panel .split(
.center &terminal_panel.active_pane,
.split( &new_pane,
&terminal_panel.active_pane, SplitDirection::Right,
&new_pane, )
SplitDirection::Right, .log_err();
) cx.focus_view(&new_pane);
.log_err(); }
cx.focus_view(&new_pane);
})
.ok();
}
})
.detach();
} }
})) }))
.on_action(cx.listener( .on_action(cx.listener(

View file

@ -9,7 +9,7 @@ use gpui::{
anchored, deferred, div, impl_actions, AnyElement, AppContext, DismissEvent, EventEmitter, anchored, deferred, div, impl_actions, AnyElement, AppContext, DismissEvent, EventEmitter,
FocusHandle, FocusableView, KeyContext, KeyDownEvent, Keystroke, Model, MouseButton, FocusHandle, FocusableView, KeyContext, KeyDownEvent, Keystroke, Model, MouseButton,
MouseDownEvent, Pixels, Render, ScrollWheelEvent, Styled, Subscription, Task, View, MouseDownEvent, Pixels, Render, ScrollWheelEvent, Styled, Subscription, Task, View,
VisualContext, WeakView, VisualContext, WeakModel, WeakView,
}; };
use language::Bias; use language::Bias;
use persistence::TERMINAL_DB; use persistence::TERMINAL_DB;
@ -97,6 +97,7 @@ pub struct BlockContext<'a, 'b> {
pub struct TerminalView { pub struct TerminalView {
terminal: Model<Terminal>, terminal: Model<Terminal>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
project: WeakModel<Project>,
focus_handle: FocusHandle, focus_handle: FocusHandle,
//Currently using iTerm bell, show bell emoji in tab until input is received //Currently using iTerm bell, show bell emoji in tab until input is received
has_bell: bool, has_bell: bool,
@ -141,6 +142,7 @@ impl TerminalView {
terminal: Model<Terminal>, terminal: Model<Terminal>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
workspace_id: Option<WorkspaceId>, workspace_id: Option<WorkspaceId>,
project: WeakModel<Project>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let workspace_handle = workspace.clone(); let workspace_handle = workspace.clone();
@ -160,6 +162,7 @@ impl TerminalView {
Self { Self {
terminal, terminal,
workspace: workspace_handle, workspace: workspace_handle,
project,
has_bell: false, has_bell: false,
focus_handle, focus_handle,
context_menu: None, context_menu: None,
@ -1075,21 +1078,37 @@ impl Item for TerminalView {
fn clone_on_split( fn clone_on_split(
&self, &self,
_workspace_id: Option<WorkspaceId>, workspace_id: Option<WorkspaceId>,
_cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<View<Self>> { ) -> Option<View<Self>> {
//From what I can tell, there's no way to tell the current working let window = cx.window_handle();
//Directory of the terminal from outside the shell. There might be let terminal = self
//solutions to this, but they are non-trivial and require more IPC .project
.update(cx, |project, cx| {
let terminal = self.terminal().read(cx);
let working_directory = terminal
.working_directory()
.or_else(|| Some(project.active_project_directory(cx)?.to_path_buf()));
let python_venv_directory = terminal.python_venv_directory.clone();
project.create_terminal_with_venv(
TerminalKind::Shell(working_directory),
python_venv_directory,
window,
cx,
)
})
.ok()?
.log_err()?;
// Some(TerminalContainer::new( Some(cx.new_view(|cx| {
// Err(anyhow::anyhow!("failed to instantiate terminal")), TerminalView::new(
// workspace_id, terminal,
// cx, self.workspace.clone(),
// )) workspace_id,
self.project.clone(),
// TODO cx,
None )
}))
} }
fn is_dirty(&self, cx: &gpui::AppContext) -> bool { fn is_dirty(&self, cx: &gpui::AppContext) -> bool {
@ -1218,7 +1237,15 @@ impl SerializableItem for TerminalView {
})? })?
.await?; .await?;
cx.update(|cx| { cx.update(|cx| {
cx.new_view(|cx| TerminalView::new(terminal, workspace, Some(workspace_id), cx)) cx.new_view(|cx| {
TerminalView::new(
terminal,
workspace,
Some(workspace_id),
project.downgrade(),
cx,
)
})
}) })
}) })
} }

View file

@ -173,7 +173,7 @@ This could be useful for launching a terminal application that you want to use i
"bindings": { "bindings": {
"alt-g": [ "alt-g": [
"task::Spawn", "task::Spawn",
{ "task_name": "start lazygit", "target": "center" } { "task_name": "start lazygit", "reveal_target": "center" }
] ]
} }
} }