Skip to content

Commit

Permalink
Use the same InlineAssist action between both assistant and `assi…
Browse files Browse the repository at this point in the history
…stant2` (zed-industries#22126)

This PR makes it so `assistant` and `assistant2` both use the same
action for inline assist (`zed_actions::InlineAssist`).

This makes it so the keybindings to deploy the inline assist seamlessly
swap based on the feature flag without needing to rebind them.

One minor caveat: if you're using `assistant2` the action name in the
command palette will be `assistant: inline assist`.

Release Notes:

- N/A
  • Loading branch information
maxdeviant authored and helgemahrt committed Dec 18, 2024
1 parent 26d268c commit 90a7970
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
1 change: 0 additions & 1 deletion crates/assistant/src/assistant_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ pub fn init(cx: &mut AppContext) {

workspace.toggle_panel_focus::<AssistantPanel>(cx);
})
.register_action(AssistantPanel::inline_assist)
.register_action(ContextEditor::quote_selection)
.register_action(ContextEditor::insert_selection)
.register_action(ContextEditor::copy_code)
Expand Down
12 changes: 10 additions & 2 deletions crates/assistant2/src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ mod thread_history;
mod thread_store;
mod ui;

use std::any::TypeId;
use std::sync::Arc;

use assistant_settings::AssistantSettings;
use client::Client;
use command_palette_hooks::CommandPaletteFilter;
use feature_flags::{Assistant2FeatureFlag, FeatureFlagAppExt};
Expand All @@ -28,6 +28,8 @@ use settings::Settings as _;
use util::ResultExt;

pub use crate::assistant_panel::AssistantPanel;
use crate::assistant_settings::AssistantSettings;
pub use crate::inline_assistant::InlineAssistant;

actions!(
assistant2,
Expand All @@ -38,7 +40,6 @@ actions!(
ToggleModelSelector,
OpenHistory,
Chat,
ToggleInlineAssist,
CycleNextInlineAssist,
CyclePreviousInlineAssist
]
Expand Down Expand Up @@ -80,6 +81,8 @@ pub fn init(fs: Arc<dyn Fs>, client: Arc<Client>, stdout_is_a_pty: bool, cx: &mu
fn feature_gate_assistant2_actions(cx: &mut AppContext) {
const ASSISTANT1_NAMESPACE: &str = "assistant";

let inline_assist_actions = [TypeId::of::<zed_actions::InlineAssist>()];

CommandPaletteFilter::update_global(cx, |filter, _cx| {
filter.hide_namespace(NAMESPACE);
});
Expand All @@ -89,6 +92,11 @@ fn feature_gate_assistant2_actions(cx: &mut AppContext) {
CommandPaletteFilter::update_global(cx, |filter, _cx| {
filter.show_namespace(NAMESPACE);
filter.hide_namespace(ASSISTANT1_NAMESPACE);

// We're hiding all of the `assistant: ` actions, but we want to
// keep the inline assist action around so we can use the same
// one in Assistant2.
filter.show_action_types(inline_assist_actions.iter());
});
} else {
CommandPaletteFilter::update_global(cx, |filter, _cx| {
Expand Down
10 changes: 4 additions & 6 deletions crates/assistant2/src/inline_assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
prompts::PromptBuilder,
streaming_diff::{CharOperation, LineDiff, LineOperation, StreamingDiff},
terminal_inline_assistant::TerminalInlineAssistant,
CycleNextInlineAssist, CyclePreviousInlineAssist, ToggleInlineAssist,
CycleNextInlineAssist, CyclePreviousInlineAssist,
};
use crate::{AssistantPanel, ToggleContextPicker};
use anyhow::{Context as _, Result};
Expand Down Expand Up @@ -75,9 +75,7 @@ pub fn init(
cx: &mut AppContext,
) {
cx.set_global(InlineAssistant::new(fs, prompt_builder, telemetry));
cx.observe_new_views(|workspace: &mut Workspace, cx| {
workspace.register_action(InlineAssistant::toggle_inline_assist);

cx.observe_new_views(|_workspace: &mut Workspace, cx| {
let workspace = cx.view().clone();
InlineAssistant::update_global(cx, |inline_assistant, cx| {
inline_assistant.register_workspace(&workspace, cx)
Expand Down Expand Up @@ -204,9 +202,9 @@ impl InlineAssistant {
}
}

pub fn toggle_inline_assist(
pub fn inline_assist(
workspace: &mut Workspace,
_action: &ToggleInlineAssist,
_action: &zed_actions::InlineAssist,
cx: &mut ViewContext<Workspace>,
) {
let settings = AssistantSettings::get_global(cx);
Expand Down
14 changes: 13 additions & 1 deletion crates/command_palette_hooks/src/command_palette_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub fn init(cx: &mut AppContext) {
pub struct CommandPaletteFilter {
hidden_namespaces: HashSet<&'static str>,
hidden_action_types: HashSet<TypeId>,
/// Actions that have explicitly been shown. These should be shown even if
/// they are in a hidden namespace.
shown_action_types: HashSet<TypeId>,
}

#[derive(Deref, DerefMut, Default)]
Expand Down Expand Up @@ -53,6 +56,11 @@ impl CommandPaletteFilter {
let name = action.name();
let namespace = name.split("::").next().unwrap_or("malformed action name");

// If this action has specifically been shown then it should be visible.
if self.shown_action_types.contains(&action.type_id()) {
return false;
}

self.hidden_namespaces.contains(namespace)
|| self.hidden_action_types.contains(&action.type_id())
}
Expand All @@ -69,12 +77,16 @@ impl CommandPaletteFilter {

/// Hides all actions with the given types.
pub fn hide_action_types(&mut self, action_types: &[TypeId]) {
self.hidden_action_types.extend(action_types);
for action_type in action_types {
self.hidden_action_types.insert(*action_type);
self.shown_action_types.remove(action_type);
}
}

/// Shows all actions with the given types.
pub fn show_action_types<'a>(&mut self, action_types: impl Iterator<Item = &'a TypeId>) {
for action_type in action_types {
self.shown_action_types.insert(*action_type);
self.hidden_action_types.remove(action_type);
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/zed/src/zed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@ pub fn initialize_workspace(
workspace_handle.update(&mut cx, |workspace, cx| {
if let Some(assistant_panel) = assistant_panel {
workspace.add_panel(assistant_panel, cx);
workspace.register_action(assistant::AssistantPanel::inline_assist);
}

if let Some(assistant2_panel) = assistant2_panel {
workspace.add_panel(assistant2_panel, cx);
workspace.register_action(assistant2::InlineAssistant::inline_assist);
}

})
})
.detach();
Expand Down

0 comments on commit 90a7970

Please sign in to comment.