From 5d5f2df9e29dd1993cb0586c9ff0ec5af79cf368 Mon Sep 17 00:00:00 2001 From: dino Date: Thu, 30 Oct 2025 17:15:44 +0000 Subject: [PATCH] refactor(magic_palette): actually use command struct to store matches --- Cargo.lock | 1 + crates/magic_palette/Cargo.toml | 1 + crates/magic_palette/src/magic_palette.rs | 36 +++++++++++++++++------ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a4a92b7037cdd23e021c6fe9fe6133c61223b18..8cf2ff9df0e8f378ecd121bfe54e1bcd2c9f8b6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9618,6 +9618,7 @@ dependencies = [ "client", "cloud_llm_client", "collections", + "command_palette", "futures 0.3.31", "gpui", "language_model", diff --git a/crates/magic_palette/Cargo.toml b/crates/magic_palette/Cargo.toml index a5bcdbfcc4c35c07e4738002e72aacfe6aee7712..7be2701271040b23a75a559642c7d648805915c5 100644 --- a/crates/magic_palette/Cargo.toml +++ b/crates/magic_palette/Cargo.toml @@ -19,6 +19,7 @@ cloud_llm_client.workspace = true language_model.workspace = true client.workspace = true collections.workspace = true +command_palette.workspace = true gpui.workspace = true menu.workspace = true log.workspace = true diff --git a/crates/magic_palette/src/magic_palette.rs b/crates/magic_palette/src/magic_palette.rs index 51633d8b8215e8d4e77ba606c922fcf23d45bfc1..ce71472860cdac6354b28e15c08198abfa6e33de 100644 --- a/crates/magic_palette/src/magic_palette.rs +++ b/crates/magic_palette/src/magic_palette.rs @@ -1,6 +1,7 @@ use agent_settings::AgentSettings; use anyhow::Result; use cloud_llm_client::CompletionIntent; +use command_palette::humanize_action_name; use futures::StreamExt as _; use gpui::{ Action, AppContext as _, DismissEvent, Entity, EventEmitter, Focusable, IntoElement, Task, @@ -26,6 +27,7 @@ gpui::actions!(magic_palette, [Toggle]); struct MagicPalette { picker: Entity>, + matches: Vec, } impl ModalView for MagicPalette {} @@ -62,7 +64,10 @@ impl MagicPalette { let picker = Picker::uniform_list(delegate, window, cx); picker }); - Self { picker } + Self { + picker, + matches: vec![], + } } } @@ -75,6 +80,7 @@ impl Render for MagicPalette { } } +#[derive(Debug)] struct Command { name: String, action: Box, @@ -224,24 +230,36 @@ Format your response as a simple list of action names, one per line, with no add dbg!(&buffer); - // Split result by ';` and for each string, call `cx.build_action`. - let _ = cx.update(move |_window, cx| { - let mut actions: Vec> = vec![]; + // Split result by `\n` and for each string, call `cx.build_action`. + let commands = cx.update(move |_window, cx| { + let mut commands: Vec = vec![]; - for line in buffer.lines() { - dbg!(line); + for name in buffer.lines() { + dbg!(name); - let action = cx.build_action(line, None); + let action = cx.build_action(name, None); match action { - Ok(action) => actions.push(action), + Ok(action) => { + commands.push(Command { action: action, name: humanize_action_name(name) }) + }, Err(err) => { log::error!("Failed to build action: {}", err); } } } - dbg!(&actions); + commands }); + + dbg!(&commands); + if let Ok(commands) = commands { + let _ = this.update(cx, |this, cx| { + let _ = this.delegate.magic_palette.update(cx, |magic_palette, _| { + magic_palette.matches = commands; + }); + }); + } + // Ok(()) });