Improve Linux terminal keymap and context menu (#16845)

Follow-up https://github.com/zed-industries/zed/pull/16085 that fixes
the search deploy to be actually a part of the terminal-related
bindings.

Part of https://github.com/zed-industries/zed/issues/16839

Also 

* fixes few other bindings to use `shift` and avoid conflicts with the
existing key bindings.
* adds terminal inline assist to the context menu and makes both the
menu and the button to dynamically adjust to `assist.enabled` settings
change

It is still unclear to me, why certain labels for certain bindings are
wrong (it's still showing `ctrl-w` for closing the terminal tab, and
`shift-insert` instead of `ctrl-shift-v` for Paste, while Insert is near
and has a `ctrl-shift-c` binding shown) but at least the keys work now.

Release notes: 
- Improved Linux terminal keymap and context menu
This commit is contained in:
Kirill Bulatov 2024-08-26 01:01:46 +03:00 committed by GitHub
parent 28271a9a36
commit 1a2a538366
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 87 additions and 50 deletions

1
Cargo.lock generated
View File

@ -10993,6 +10993,7 @@ dependencies = [
"ui", "ui",
"util", "util",
"workspace", "workspace",
"zed_actions",
] ]
[[package]] [[package]]

View File

@ -474,8 +474,7 @@
{ {
"context": "!Terminal", "context": "!Terminal",
"bindings": { "bindings": {
"ctrl-shift-c": "collab_panel::ToggleFocus", "ctrl-shift-c": "collab_panel::ToggleFocus"
"ctrl-shift-f": "buffer_search::Deploy"
} }
}, },
{ {
@ -614,11 +613,15 @@
"ctrl-alt-space": "terminal::ShowCharacterPalette", "ctrl-alt-space": "terminal::ShowCharacterPalette",
"ctrl-shift-c": "terminal::Copy", "ctrl-shift-c": "terminal::Copy",
"ctrl-insert": "terminal::Copy", "ctrl-insert": "terminal::Copy",
// "ctrl-a": "editor::SelectAll", // conflicts with readline
"ctrl-shift-v": "terminal::Paste", "ctrl-shift-v": "terminal::Paste",
"shift-insert": "terminal::Paste", "shift-insert": "terminal::Paste",
"ctrl-enter": "assistant::InlineAssist", "ctrl-enter": "assistant::InlineAssist",
// Overrides for conflicting keybindings
"ctrl-w": ["terminal::SendKeystroke", "ctrl-w"], "ctrl-w": ["terminal::SendKeystroke", "ctrl-w"],
"ctrl-shift-a": "editor::SelectAll",
"ctrl-shift-f": "buffer_search::Deploy",
"ctrl-shift-l": "terminal::Clear",
"ctrl-shift-w": "pane::CloseActiveItem",
"ctrl-e": ["terminal::SendKeystroke", "ctrl-e"], "ctrl-e": ["terminal::SendKeystroke", "ctrl-e"],
"up": ["terminal::SendKeystroke", "up"], "up": ["terminal::SendKeystroke", "up"],
"pageup": ["terminal::SendKeystroke", "pageup"], "pageup": ["terminal::SendKeystroke", "pageup"],

View File

@ -26,7 +26,7 @@ pub use context_store::*;
use feature_flags::FeatureFlagAppExt; use feature_flags::FeatureFlagAppExt;
use fs::Fs; use fs::Fs;
use gpui::Context as _; use gpui::Context as _;
use gpui::{actions, impl_actions, AppContext, Global, SharedString, UpdateGlobal}; use gpui::{actions, AppContext, Global, SharedString, UpdateGlobal};
use indexed_docs::IndexedDocsRegistry; use indexed_docs::IndexedDocsRegistry;
pub(crate) use inline_assistant::*; pub(crate) use inline_assistant::*;
use language_model::{ use language_model::{
@ -69,13 +69,6 @@ actions!(
const DEFAULT_CONTEXT_LINES: usize = 50; const DEFAULT_CONTEXT_LINES: usize = 50;
#[derive(Clone, Default, Deserialize, PartialEq)]
pub struct InlineAssist {
prompt: Option<String>,
}
impl_actions!(assistant, [InlineAssist]);
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct MessageId(clock::Lamport); pub struct MessageId(clock::Lamport);

View File

@ -12,10 +12,10 @@ use crate::{
slash_command_picker, slash_command_picker,
terminal_inline_assistant::TerminalInlineAssistant, terminal_inline_assistant::TerminalInlineAssistant,
Assist, CacheStatus, ConfirmCommand, Context, ContextEvent, ContextId, ContextStore, Assist, CacheStatus, ConfirmCommand, Context, ContextEvent, ContextId, ContextStore,
CycleMessageRole, DeployHistory, DeployPromptLibrary, InlineAssist, InlineAssistId, CycleMessageRole, DeployHistory, DeployPromptLibrary, InlineAssistId, InlineAssistant,
InlineAssistant, InsertIntoEditor, MessageStatus, ModelSelector, PendingSlashCommand, InsertIntoEditor, MessageStatus, ModelSelector, PendingSlashCommand, PendingSlashCommandStatus,
PendingSlashCommandStatus, QuoteSelection, RemoteContextMetadata, SavedContextMetadata, Split, QuoteSelection, RemoteContextMetadata, SavedContextMetadata, Split, ToggleFocus,
ToggleFocus, ToggleModelSelector, WorkflowStepResolution, WorkflowStepView, ToggleModelSelector, WorkflowStepResolution, WorkflowStepView,
}; };
use crate::{ContextStoreEvent, ModelPickerDelegate}; use crate::{ContextStoreEvent, ModelPickerDelegate};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
@ -82,6 +82,7 @@ use workspace::{
ToolbarItemView, Workspace, ToolbarItemView, Workspace,
}; };
use workspace::{searchable::SearchableItemHandle, NewFile}; use workspace::{searchable::SearchableItemHandle, NewFile};
use zed_actions::InlineAssist;
pub fn init(cx: &mut AppContext) { pub fn init(cx: &mut AppContext) {
workspace::FollowableViewRegistry::register::<ContextEditor>(cx); workspace::FollowableViewRegistry::register::<ContextEditor>(cx);
@ -107,29 +108,12 @@ pub fn init(cx: &mut AppContext) {
cx.observe_new_views( cx.observe_new_views(
|terminal_panel: &mut TerminalPanel, cx: &mut ViewContext<TerminalPanel>| { |terminal_panel: &mut TerminalPanel, cx: &mut ViewContext<TerminalPanel>| {
let settings = AssistantSettings::get_global(cx); let settings = AssistantSettings::get_global(cx);
if !settings.enabled { terminal_panel.asssistant_enabled(settings.enabled, cx);
return;
}
terminal_panel.register_tab_bar_button(cx.new_view(|_| InlineAssistTabBarButton), cx);
}, },
) )
.detach(); .detach();
} }
struct InlineAssistTabBarButton;
impl Render for InlineAssistTabBarButton {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
IconButton::new("terminal_inline_assistant", IconName::ZedAssistant)
.icon_size(IconSize::Small)
.on_click(cx.listener(|_, _, cx| {
cx.dispatch_action(InlineAssist::default().boxed_clone());
}))
.tooltip(move |cx| Tooltip::for_action("Inline Assist", &InlineAssist::default(), cx))
}
}
pub enum AssistantPanelEvent { pub enum AssistantPanelEvent {
ContextEdited, ContextEdited,
} }

View File

@ -1,6 +1,7 @@
use crate::{ use crate::{
humanize_token_count, prompts::PromptBuilder, AssistantPanel, AssistantPanelEvent, assistant_settings::AssistantSettings, humanize_token_count, prompts::PromptBuilder,
CharOperation, LineDiff, LineOperation, ModelSelector, StreamingDiff, AssistantPanel, AssistantPanelEvent, CharOperation, LineDiff, LineOperation, ModelSelector,
StreamingDiff,
}; };
use anyhow::{anyhow, Context as _, Result}; use anyhow::{anyhow, Context as _, Result};
use client::{telemetry::Telemetry, ErrorExt}; use client::{telemetry::Telemetry, ErrorExt};
@ -35,7 +36,7 @@ use language_model::{
use multi_buffer::MultiBufferRow; use multi_buffer::MultiBufferRow;
use parking_lot::Mutex; use parking_lot::Mutex;
use rope::Rope; use rope::Rope;
use settings::Settings; use settings::{Settings, SettingsStore};
use smol::future::FutureExt; use smol::future::FutureExt;
use std::{ use std::{
cmp, cmp,
@ -47,6 +48,7 @@ use std::{
task::{self, Poll}, task::{self, Poll},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use terminal_view::terminal_panel::TerminalPanel;
use theme::ThemeSettings; use theme::ThemeSettings;
use ui::{prelude::*, CheckboxWithLabel, IconButtonShape, Popover, Tooltip}; use ui::{prelude::*, CheckboxWithLabel, IconButtonShape, Popover, Tooltip};
use util::{RangeExt, ResultExt}; use util::{RangeExt, ResultExt};
@ -131,6 +133,18 @@ impl InlineAssistant {
Self::update_global(cx, |this, cx| this.handle_workspace_event(event, cx)); Self::update_global(cx, |this, cx| this.handle_workspace_event(event, cx));
}) })
.detach(); .detach();
let workspace = workspace.clone();
cx.observe_global::<SettingsStore>(move |cx| {
let Some(terminal_panel) = workspace.read(cx).panel::<TerminalPanel>(cx) else {
return;
};
let enabled = AssistantSettings::get_global(cx).enabled;
terminal_panel.update(cx, |terminal_panel, cx| {
terminal_panel.asssistant_enabled(enabled, cx)
});
})
.detach();
} }
fn handle_workspace_event(&mut self, event: &workspace::Event, cx: &mut WindowContext) { fn handle_workspace_event(&mut self, event: &workspace::Event, cx: &mut WindowContext) {

View File

@ -1,6 +1,4 @@
use crate::{ use crate::{slash_command::SlashCommandCompletionProvider, AssistantPanel, InlineAssistant};
slash_command::SlashCommandCompletionProvider, AssistantPanel, InlineAssist, InlineAssistant,
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use collections::{HashMap, HashSet}; use collections::{HashMap, HashSet};
@ -44,6 +42,7 @@ use ui::{
use util::{ResultExt, TryFutureExt}; use util::{ResultExt, TryFutureExt};
use uuid::Uuid; use uuid::Uuid;
use workspace::Workspace; use workspace::Workspace;
use zed_actions::InlineAssist;
actions!( actions!(
prompt_library, prompt_library,

View File

@ -1,5 +1,5 @@
use assistant::assistant_settings::AssistantSettings; use assistant::assistant_settings::AssistantSettings;
use assistant::{AssistantPanel, InlineAssist}; use assistant::AssistantPanel;
use editor::actions::{ use editor::actions::{
AddSelectionAbove, AddSelectionBelow, DuplicateLineDown, GoToDiagnostic, GoToHunk, AddSelectionAbove, AddSelectionBelow, DuplicateLineDown, GoToDiagnostic, GoToHunk,
GoToPrevDiagnostic, GoToPrevHunk, MoveLineDown, MoveLineUp, SelectAll, SelectLargerSyntaxNode, GoToPrevDiagnostic, GoToPrevHunk, MoveLineDown, MoveLineUp, SelectAll, SelectLargerSyntaxNode,
@ -20,6 +20,7 @@ use ui::{
use workspace::{ use workspace::{
item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, Workspace, item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, Workspace,
}; };
use zed_actions::InlineAssist;
mod repl_menu; mod repl_menu;
mod toggle_markdown_preview; mod toggle_markdown_preview;

View File

@ -36,6 +36,7 @@ theme.workspace = true
ui.workspace = true ui.workspace = true
util.workspace = true util.workspace = true
workspace.workspace = true workspace.workspace = true
zed_actions.workspace = true
[dev-dependencies] [dev-dependencies]
client = { workspace = true, features = ["test-support"] } client = { workspace = true, features = ["test-support"] }

View File

@ -33,6 +33,7 @@ use workspace::{
}; };
use anyhow::Result; use anyhow::Result;
use zed_actions::InlineAssist;
const TERMINAL_PANEL_KEY: &str = "TerminalPanel"; const TERMINAL_PANEL_KEY: &str = "TerminalPanel";
@ -68,7 +69,8 @@ pub struct TerminalPanel {
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
deferred_tasks: HashMap<TaskId, Task<()>>, deferred_tasks: HashMap<TaskId, Task<()>>,
enabled: bool, enabled: bool,
additional_tab_bar_buttons: Vec<AnyView>, assistant_enabled: bool,
assistant_tab_bar_button: Option<AnyView>,
} }
impl TerminalPanel { impl TerminalPanel {
@ -154,23 +156,25 @@ impl TerminalPanel {
deferred_tasks: HashMap::default(), deferred_tasks: HashMap::default(),
_subscriptions: subscriptions, _subscriptions: subscriptions,
enabled, enabled,
additional_tab_bar_buttons: Vec::new(), assistant_enabled: false,
assistant_tab_bar_button: None,
}; };
this.apply_tab_bar_buttons(cx); this.apply_tab_bar_buttons(cx);
this this
} }
pub fn register_tab_bar_button( pub fn asssistant_enabled(&mut self, enabled: bool, cx: &mut ViewContext<Self>) {
&mut self, self.assistant_enabled = enabled;
button: impl Into<AnyView>, if enabled {
cx: &mut ViewContext<Self>, self.assistant_tab_bar_button = Some(cx.new_view(|_| InlineAssistTabBarButton).into());
) { } else {
self.additional_tab_bar_buttons.push(button.into()); self.assistant_tab_bar_button = None;
}
self.apply_tab_bar_buttons(cx); self.apply_tab_bar_buttons(cx);
} }
fn apply_tab_bar_buttons(&self, cx: &mut ViewContext<Self>) { fn apply_tab_bar_buttons(&self, cx: &mut ViewContext<Self>) {
let additional_buttons = self.additional_tab_bar_buttons.clone(); let assistant_tab_bar_button = self.assistant_tab_bar_button.clone();
self.pane.update(cx, |pane, cx| { self.pane.update(cx, |pane, cx| {
pane.set_render_tab_bar_buttons(cx, move |pane, cx| { pane.set_render_tab_bar_buttons(cx, move |pane, cx| {
if !pane.has_focus(cx) && !pane.context_menu_focused(cx) { if !pane.has_focus(cx) && !pane.context_menu_focused(cx) {
@ -179,7 +183,7 @@ impl TerminalPanel {
let focus_handle = pane.focus_handle(cx); let focus_handle = pane.focus_handle(cx);
let right_children = h_flex() let right_children = h_flex()
.gap_2() .gap_2()
.children(additional_buttons.clone()) .children(assistant_tab_bar_button.clone())
.child( .child(
PopoverMenu::new("terminal-tab-bar-popover-menu") PopoverMenu::new("terminal-tab-bar-popover-menu")
.trigger( .trigger(
@ -686,6 +690,10 @@ impl TerminalPanel {
fn has_no_terminals(&self, cx: &WindowContext) -> bool { fn has_no_terminals(&self, cx: &WindowContext) -> bool {
self.pane.read(cx).items_len() == 0 && self.pending_terminals_to_add == 0 self.pane.read(cx).items_len() == 0 && self.pending_terminals_to_add == 0
} }
pub fn assistant_enabled(&self) -> bool {
self.assistant_enabled
}
} }
async fn wait_for_terminals_tasks( async fn wait_for_terminals_tasks(
@ -851,6 +859,19 @@ impl Panel for TerminalPanel {
} }
} }
struct InlineAssistTabBarButton;
impl Render for InlineAssistTabBarButton {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
IconButton::new("terminal_inline_assistant", IconName::ZedAssistant)
.icon_size(IconSize::Small)
.on_click(cx.listener(|_, _, cx| {
cx.dispatch_action(InlineAssist::default().boxed_clone());
}))
.tooltip(move |cx| Tooltip::for_action("Inline Assist", &InlineAssist::default(), cx))
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SerializedTerminalPanel { struct SerializedTerminalPanel {
items: Vec<u64>, items: Vec<u64>,

View File

@ -25,6 +25,7 @@ use terminal::{
TerminalSize, TerminalSize,
}; };
use terminal_element::{is_blank, TerminalElement}; use terminal_element::{is_blank, TerminalElement};
use terminal_panel::TerminalPanel;
use ui::{h_flex, prelude::*, ContextMenu, Icon, IconName, Label, Tooltip}; use ui::{h_flex, prelude::*, ContextMenu, Icon, IconName, Label, Tooltip};
use util::{paths::PathWithPosition, ResultExt}; use util::{paths::PathWithPosition, ResultExt};
use workspace::{ use workspace::{
@ -40,6 +41,7 @@ use anyhow::Context;
use serde::Deserialize; use serde::Deserialize;
use settings::{Settings, SettingsStore}; use settings::{Settings, SettingsStore};
use smol::Timer; use smol::Timer;
use zed_actions::InlineAssist;
use std::{ use std::{
cmp, cmp,
@ -210,6 +212,13 @@ impl TerminalView {
position: gpui::Point<Pixels>, position: gpui::Point<Pixels>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
let assistant_enabled = self
.workspace
.upgrade()
.and_then(|workspace| workspace.read(cx).panel::<TerminalPanel>(cx))
.map_or(false, |terminal_panel| {
terminal_panel.read(cx).assistant_enabled()
});
let context_menu = ContextMenu::build(cx, |menu, _| { let context_menu = ContextMenu::build(cx, |menu, _| {
menu.context(self.focus_handle.clone()) menu.context(self.focus_handle.clone())
.action("New Terminal", Box::new(NewTerminal)) .action("New Terminal", Box::new(NewTerminal))
@ -218,6 +227,10 @@ impl TerminalView {
.action("Paste", Box::new(Paste)) .action("Paste", Box::new(Paste))
.action("Select All", Box::new(SelectAll)) .action("Select All", Box::new(SelectAll))
.action("Clear", Box::new(Clear)) .action("Clear", Box::new(Clear))
.when(assistant_enabled, |menu| {
menu.separator()
.action("Inline Assist", Box::new(InlineAssist::default()))
})
.separator() .separator()
.action("Close", Box::new(CloseActiveItem { save_intent: None })) .action("Close", Box::new(CloseActiveItem { save_intent: None }))
}); });

View File

@ -40,3 +40,10 @@ actions!(
ResetUiFontSize ResetUiFontSize
] ]
); );
#[derive(Clone, Default, Deserialize, PartialEq)]
pub struct InlineAssist {
pub prompt: Option<String>,
}
impl_actions!(assistant, [InlineAssist]);