Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
assistant2: Show file icons for context entries (#22928)
Agus Zubiagaec4c6744d694
diff --git a/Cargo.lock b/Cargo.lockindex 3308304a850..3383c87bafb 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -469,6 +469,7 @@ dependencies = ["db","editor","feature_flags",+ "file_icons","fs","futures 0.3.31","fuzzy",diff --git a/crates/assistant2/Cargo.toml b/crates/assistant2/Cargo.tomlindex 8b52a946a7d..85220bc56f7 100644--- a/crates/assistant2/Cargo.toml+++ b/crates/assistant2/Cargo.toml@@ -18,15 +18,16 @@ anyhow.workspace = trueassets.workspace = trueassistant_tool.workspace = trueasync-watch.workspace = true+chrono.workspace = trueclient.workspace = trueclock.workspace = true-chrono.workspace = truecollections.workspace = truecommand_palette_hooks.workspace = truecontext_server.workspace = truedb.workspace = trueeditor.workspace = truefeature_flags.workspace = true+file_icons.workspace = truefs.workspace = truefutures.workspace = truefuzzy.workspace = true@@ -47,8 +48,8 @@ multi_buffer.workspace = trueollama = { workspace = true, features = ["schemars"] }open_ai = { workspace = true, features = ["schemars"] }ordered-float.workspace = true-paths.workspace = trueparking_lot.workspace = true+paths.workspace = truepicker.workspace = trueproject.workspace = trueproto.workspace = true@@ -61,9 +62,9 @@ settings.workspace = truesimilar.workspace = truesmol.workspace = truetelemetry_events.workspace = true+terminal.workspace = trueterminal_view.workspace = truetext.workspace = true-terminal.workspace = truetheme.workspace = truetime.workspace = truetime_format.workspace = truediff --git a/crates/assistant2/src/context.rs b/crates/assistant2/src/context.rsindex 3ef3e649bdd..cb33e6a9c10 100644--- a/crates/assistant2/src/context.rs+++ b/crates/assistant2/src/context.rs@@ -2,11 +2,13 @@ use std::path::Path;use std::rc::Rc;use std::sync::Arc;+use file_icons::FileIcons;use gpui::{AppContext, Model, SharedString};use language::Buffer;use language_model::{LanguageModelRequestMessage, MessageContent};use serde::{Deserialize, Serialize};use text::BufferId;+use ui::IconName;use util::post_inc;use crate::thread::Thread;@@ -27,6 +29,7 @@ pub struct ContextSnapshot {pub name: SharedString,pub parent: Option<SharedString>,pub tooltip: Option<SharedString>,+ pub icon_path: Option<SharedString>,pub kind: ContextKind,/// Concatenating these strings yields text to send to the model. Not refreshed by `snapshot`.pub text: Box<[SharedString]>,@@ -40,6 +43,17 @@ pub enum ContextKind {Thread,}+impl ContextKind {+ pub fn icon(&self) -> IconName {+ match self {+ ContextKind::File => IconName::File,+ ContextKind::Directory => IconName::Folder,+ ContextKind::FetchedUrl => IconName::Globe,+ ContextKind::Thread => IconName::MessageCircle,+ }+ }+}+#[derive(Debug)]pub enum Context {File(FileContext),@@ -138,11 +152,14 @@ impl FileContext {.and_then(|p| p.file_name()).map(|p| p.to_string_lossy().into_owned().into());+ let icon_path = FileIcons::get_icon(&path, cx);+Some(ContextSnapshot {id: self.id,name,parent,tooltip: Some(full_path),+ icon_path,kind: ContextKind::File,text: Box::new([self.buffer.text.clone()]),})@@ -162,6 +179,7 @@ impl FetchedUrlContext {name: self.url.clone(),parent: None,tooltip: None,+ icon_path: None,kind: ContextKind::FetchedUrl,text: Box::new([self.text.clone()]),}@@ -176,6 +194,7 @@ impl ThreadContext {name: thread.summary().unwrap_or("New thread".into()),parent: None,tooltip: None,+ icon_path: None,kind: ContextKind::Thread,text: Box::new([self.text.clone()]),}diff --git a/crates/assistant2/src/context_picker/file_context_picker.rs b/crates/assistant2/src/context_picker/file_context_picker.rsindex 8db3b46e0d6..eac9f2ff74e 100644--- a/crates/assistant2/src/context_picker/file_context_picker.rs+++ b/crates/assistant2/src/context_picker/file_context_picker.rs@@ -2,6 +2,7 @@ use std::path::Path;use std::sync::atomic::AtomicBool;use std::sync::Arc;+use file_icons::FileIcons;use fuzzy::PathMatch;use gpui::{AppContext, DismissEvent, FocusHandle, FocusableView, Task, View, WeakModel, WeakView};use picker::{Picker, PickerDelegate};@@ -281,6 +282,10 @@ impl PickerDelegate for FileContextPickerDelegate {.will_include_file_path(&path_match.path, cx)});+ let file_icon = FileIcons::get_icon(&path_match.path.clone(), cx)+ .map(Icon::from_path)+ .unwrap_or_else(|| Icon::new(IconName::File));+Some(ListItem::new(ix).inset(true)@@ -288,6 +293,7 @@ impl PickerDelegate for FileContextPickerDelegate {.child(h_flex().gap_2()+ .child(file_icon.size(IconSize::Small)).child(Label::new(file_name)).children(directory.map(|directory| {Label::new(directory)diff --git a/crates/assistant2/src/context_store.rs b/crates/assistant2/src/context_store.rsindex 61e1ac280c6..49518a12965 100644--- a/crates/assistant2/src/context_store.rs+++ b/crates/assistant2/src/context_store.rs@@ -273,6 +273,7 @@ impl ContextStore {name,parent,tooltip: Some(full_path),+ icon_path: None,kind: ContextKind::Directory,text,},diff --git a/crates/assistant2/src/context_strip.rs b/crates/assistant2/src/context_strip.rsindex b2dd67846b3..42dc1244c7c 100644--- a/crates/assistant2/src/context_strip.rs+++ b/crates/assistant2/src/context_strip.rs@@ -3,6 +3,7 @@ use std::rc::Rc;use anyhow::Result;use collections::HashSet;use editor::Editor;+use file_icons::FileIcons;use gpui::{DismissEvent, EventEmitter, FocusHandle, Model, ModelContext, Subscription, Task, View,WeakModel, WeakView,@@ -95,9 +96,12 @@ impl ContextStrip {None => path.to_string_lossy().into_owned().into(),};+ let icon_path = FileIcons::get_icon(path, cx);+Some(SuggestedContext::File {name,buffer: active_buffer_model.downgrade(),+ icon_path,})}@@ -228,6 +232,7 @@ impl Render for ContextStrip {.when_some(suggested_context, |el, suggested| {el.child(ContextPill::new_suggested(suggested.name().clone(),+ suggested.icon_path(),suggested.kind(),{let context_store = self.context_store.clone();@@ -304,6 +309,7 @@ pub enum SuggestContextKind {pub enum SuggestedContext {File {name: SharedString,+ icon_path: Option<SharedString>,buffer: WeakModel<Buffer>,},Thread {@@ -320,13 +326,24 @@ impl SuggestedContext {}}+ pub fn icon_path(&self) -> Option<SharedString> {+ match self {+ Self::File { icon_path, .. } => icon_path.clone(),+ Self::Thread { .. } => None,+ }+ }+pub fn accept(&self,context_store: &mut ContextStore,cx: &mut ModelContext<ContextStore>,) -> Task<Result<()>> {match self {- Self::File { buffer, name: _ } => {+ Self::File {+ buffer,+ icon_path: _,+ name: _,+ } => {if let Some(buffer) = buffer.upgrade() {return context_store.add_file_from_buffer(buffer, cx);};diff --git a/crates/assistant2/src/ui/context_pill.rs b/crates/assistant2/src/ui/context_pill.rsindex e70169ed5f7..6e5de117288 100644--- a/crates/assistant2/src/ui/context_pill.rs+++ b/crates/assistant2/src/ui/context_pill.rs@@ -14,6 +14,7 @@ pub enum ContextPill {},Suggested {name: SharedString,+ icon_path: Option<SharedString>,kind: ContextKind,on_add: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,},@@ -34,10 +35,16 @@ impl ContextPill {pub fn new_suggested(name: SharedString,+ icon_path: Option<SharedString>,kind: ContextKind,on_add: Rc<dyn Fn(&ClickEvent, &mut WindowContext)>,) -> Self {- Self::Suggested { name, kind, on_add }+ Self::Suggested {+ name,+ icon_path,+ kind,+ on_add,+ }}pub fn id(&self) -> ElementId {@@ -49,23 +56,27 @@ impl ContextPill {}}- pub fn kind(&self) -> ContextKind {+ pub fn icon(&self) -> Icon {match self {- Self::Added { context, .. } => context.kind,- Self::Suggested { kind, .. } => *kind,+ Self::Added { context, .. } => match &context.icon_path {+ Some(icon_path) => Icon::from_path(icon_path),+ None => Icon::new(context.kind.icon()),+ },+ Self::Suggested {+ icon_path: Some(icon_path),+ ..+ } => Icon::from_path(icon_path),+ Self::Suggested {+ kind,+ icon_path: None,+ ..+ } => Icon::new(kind.icon()),}}}impl RenderOnce for ContextPill {fn render(self, cx: &mut WindowContext) -> impl IntoElement {- let icon = match &self.kind() {- ContextKind::File => IconName::File,- ContextKind::Directory => IconName::Folder,- ContextKind::FetchedUrl => IconName::Globe,- ContextKind::Thread => IconName::MessageCircle,- };-let color = cx.theme().colors();let base_pill = h_flex()@@ -75,7 +86,7 @@ impl RenderOnce for ContextPill {.border_1().rounded_md().gap_1()- .child(Icon::new(icon).size(IconSize::XSmall).color(Color::Muted));+ .child(self.icon().size(IconSize::XSmall).color(Color::Muted));match &self {ContextPill::Added {@@ -118,7 +129,12 @@ impl RenderOnce for ContextPill {}),)}),- ContextPill::Suggested { name, kind, on_add } => base_pill+ ContextPill::Suggested {+ name,+ icon_path: _,+ kind,+ on_add,+ } => base_pill.cursor_pointer().pr_1().border_color(color.border_variant.opacity(0.5))