Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge remote-tracking branch 'origin/main' into save-conversations

Antonio Scandurra6f0efec146ff
diff --cc crates/ai/src/assistant.rs
index 4ca7e7de26c,5b254fac4be..6e10916a3d3
--- a/crates/ai/src/assistant.rs
+++ b/crates/ai/src/assistant.rs
@@@ -266,200 -262,6 +266,200 @@@ impl AssistantPanel
cx.focus_self();
cx.notify();
}
+
+ fn toggle_zoom(&mut self, _: &workspace::ToggleZoom, cx: &mut ViewContext<Self>) {
+ if self.zoomed {
+ cx.emit(AssistantPanelEvent::ZoomOut)
+ } else {
+ cx.emit(AssistantPanelEvent::ZoomIn)
+ }
+ }
+
+ fn active_editor(&self) -> Option<&ViewHandle<ConversationEditor>> {
+ self.editors.get(self.active_editor_index?)
+ }
+
+ fn render_hamburger_button(style: &IconStyle) -> impl Element<Self> {
+ enum ListConversations {}
+ Svg::for_style(style.icon.clone())
+ .contained()
+ .with_style(style.container)
+ .mouse::<ListConversations>(0)
+ .with_cursor_style(CursorStyle::PointingHand)
+ .on_click(MouseButton::Left, |_, this: &mut Self, cx| {
+ this.active_editor_index = None;
+ cx.notify();
+ })
+ }
+
+ fn render_current_model(
+ &self,
+ style: &AssistantStyle,
+ cx: &mut ViewContext<Self>,
+ ) -> Option<impl Element<Self>> {
+ enum Model {}
+
+ let model = self
+ .active_editor()?
+ .read(cx)
+ .conversation
+ .read(cx)
+ .model
+ .clone();
+
+ Some(
+ MouseEventHandler::<Model, _>::new(0, cx, |state, _| {
- let style = style.model.style_for(state, false);
++ let style = style.model.style_for(state);
+ Label::new(model, style.text.clone())
+ .contained()
+ .with_style(style.container)
+ })
+ .with_cursor_style(CursorStyle::PointingHand)
+ .on_click(MouseButton::Left, |_, this, cx| {
+ if let Some(editor) = this.active_editor() {
+ editor.update(cx, |editor, cx| {
+ editor.cycle_model(cx);
+ });
+ }
+ }),
+ )
+ }
+
+ fn render_remaining_tokens(
+ &self,
+ style: &AssistantStyle,
+ cx: &mut ViewContext<Self>,
+ ) -> Option<impl Element<Self>> {
+ self.active_editor().and_then(|editor| {
+ editor
+ .read(cx)
+ .conversation
+ .read(cx)
+ .remaining_tokens()
+ .map(|remaining_tokens| {
+ let remaining_tokens_style = if remaining_tokens <= 0 {
+ &style.no_remaining_tokens
+ } else {
+ &style.remaining_tokens
+ };
+ Label::new(
+ remaining_tokens.to_string(),
+ remaining_tokens_style.text.clone(),
+ )
+ .contained()
+ .with_style(remaining_tokens_style.container)
+ })
+ })
+ }
+
+ fn render_plus_button(style: &IconStyle) -> impl Element<Self> {
+ enum AddConversation {}
+ Svg::for_style(style.icon.clone())
+ .contained()
+ .with_style(style.container)
+ .mouse::<AddConversation>(0)
+ .with_cursor_style(CursorStyle::PointingHand)
+ .on_click(MouseButton::Left, |_, this: &mut Self, cx| {
+ this.new_conversation(cx);
+ })
+ }
+
+ fn render_zoom_button(
+ &self,
+ style: &AssistantStyle,
+ cx: &mut ViewContext<Self>,
+ ) -> impl Element<Self> {
+ enum ToggleZoomButton {}
+
+ let style = if self.zoomed {
+ &style.zoom_out_button
+ } else {
+ &style.zoom_in_button
+ };
+
+ MouseEventHandler::<ToggleZoomButton, _>::new(0, cx, |_, _| {
+ Svg::for_style(style.icon.clone())
+ .contained()
+ .with_style(style.container)
+ })
+ .with_cursor_style(CursorStyle::PointingHand)
+ .on_click(MouseButton::Left, |_, this, cx| {
+ this.toggle_zoom(&ToggleZoom, cx);
+ })
+ }
+
+ fn render_saved_conversation(
+ &mut self,
+ index: usize,
+ cx: &mut ViewContext<Self>,
+ ) -> impl Element<Self> {
+ let conversation = &self.saved_conversations[index];
+ let path = conversation.path.clone();
+ MouseEventHandler::<SavedConversationMetadata, _>::new(index, cx, move |state, cx| {
+ let style = &theme::current(cx).assistant.saved_conversation;
+ Flex::row()
+ .with_child(
+ Label::new(
+ conversation.mtime.format("%F %I:%M%p").to_string(),
+ style.saved_at.text.clone(),
+ )
+ .aligned()
+ .contained()
+ .with_style(style.saved_at.container),
+ )
+ .with_child(
+ Label::new(conversation.title.clone(), style.title.text.clone())
+ .aligned()
+ .contained()
+ .with_style(style.title.container),
+ )
+ .contained()
- .with_style(*style.container.style_for(state, false))
++ .with_style(*style.container.style_for(state))
+ })
+ .with_cursor_style(CursorStyle::PointingHand)
+ .on_click(MouseButton::Left, move |_, this, cx| {
+ this.open_conversation(path.clone(), cx)
+ .detach_and_log_err(cx)
+ })
+ }
+
+ fn open_conversation(&mut self, path: PathBuf, cx: &mut ViewContext<Self>) -> Task<Result<()>> {
+ if let Some(ix) = self.editor_index_for_path(&path, cx) {
+ self.active_editor_index = Some(ix);
+ cx.notify();
+ return Task::ready(Ok(()));
+ }
+
+ let fs = self.fs.clone();
+ let conversation = Conversation::load(
+ path.clone(),
+ self.api_key.clone(),
+ self.languages.clone(),
+ self.fs.clone(),
+ cx,
+ );
+ cx.spawn(|this, mut cx| async move {
+ let conversation = conversation.await?;
+ this.update(&mut cx, |this, cx| {
+ // If, by the time we've loaded the conversation, the user has already opened
+ // the same conversation, we don't want to open it again.
+ if let Some(ix) = this.editor_index_for_path(&path, cx) {
+ this.active_editor_index = Some(ix);
+ } else {
+ let editor = cx
+ .add_view(|cx| ConversationEditor::from_conversation(conversation, fs, cx));
+ this.add_conversation(editor, cx);
+ }
+ })?;
+ Ok(())
+ })
+ }
+
+ fn editor_index_for_path(&self, path: &Path, cx: &AppContext) -> Option<usize> {
+ self.editors
+ .iter()
+ .position(|editor| editor.read(cx).conversation.read(cx).path.as_deref() == Some(path))
+ }
}
fn build_api_key_editor(cx: &mut ViewContext<AssistantPanel>) -> ViewHandle<Editor> {
diff --cc crates/gpui/src/elements/svg.rs
index a6b4fab980c,54442213229..4ef880b3e17
--- a/crates/gpui/src/elements/svg.rs
+++ b/crates/gpui/src/elements/svg.rs
@@@ -8,9 -10,6 +8,10 @@@ use crate::
},
scene, Element, LayoutContext, SceneBuilder, SizeConstraint, View, ViewContext,
};
++use schemars::JsonSchema;
+use serde_derive::Deserialize;
+use serde_json::json;
+use std::{borrow::Cow, ops::Range};
pub struct Svg {
path: Cow<'static, str>,
@@@ -115,24 -105,9 +116,24 @@@ impl<V: View> Element<V> for Svg
}
}
- #[derive(Clone, Deserialize, Default)]
-use crate::json::ToJson;
++#[derive(Clone, Deserialize, Default, JsonSchema)]
+pub struct SvgStyle {
+ pub color: Color,
+ pub asset: String,
+ pub dimensions: Dimensions,
+}
- #[derive(Clone, Deserialize, Default)]
-use super::constrain_size_preserving_aspect_ratio;
++#[derive(Clone, Deserialize, Default, JsonSchema)]
+pub struct Dimensions {
+ pub width: f32,
+ pub height: f32,
+}
+
+impl Dimensions {
+ pub fn to_vec(&self) -> Vector2F {
+ vec2f(self.width, self.height)
+ }
+}
fn from_usvg_rect(rect: usvg::Rect) -> RectF {
RectF::new(
diff --cc crates/theme/src/theme.rs
index 71473364798,75ce9b40ce0..c012e048072
--- a/crates/theme/src/theme.rs
+++ b/crates/theme/src/theme.rs
@@@ -991,15 -990,10 +990,15 @@@ pub struct TerminalStyle
pub dim_foreground: Color,
}
- #[derive(Clone, Deserialize, Default)]
+ #[derive(Clone, Deserialize, Default, JsonSchema)]
pub struct AssistantStyle {
pub container: ContainerStyle,
- pub header: ContainerStyle,
+ pub hamburger_button: IconStyle,
+ pub zoom_in_button: IconStyle,
+ pub zoom_out_button: IconStyle,
+ pub plus_button: IconStyle,
+ pub title: ContainedText,
+ pub message_header: ContainerStyle,
pub sent_at: ContainedText,
pub user_sender: Interactive<ContainedText>,
pub assistant_sender: Interactive<ContainedText>,
@@@ -1011,18 -1005,9 +1010,17 @@@
pub error_icon: Icon,
pub api_key_editor: FieldEditor,
pub api_key_prompt: ContainedText,
+ pub saved_conversation: SavedConversation,
+}
+
- #[derive(Clone, Deserialize, Default)]
++#[derive(Clone, Deserialize, Default, JsonSchema)]
+pub struct SavedConversation {
- #[serde(flatten)]
+ pub container: Interactive<ContainerStyle>,
+ pub saved_at: ContainedText,
+ pub title: ContainedText,
}
- #[derive(Clone, Deserialize, Default)]
+ #[derive(Clone, Deserialize, Default, JsonSchema)]
pub struct FeedbackStyle {
pub submit_button: Interactive<ContainedText>,
pub button_margin: f32,
diff --cc crates/theme/src/ui.rs
index 058123e53d8,5443e19d7a2..308ea6f2d7b
--- a/crates/theme/src/ui.rs
+++ b/crates/theme/src/ui.rs
@@@ -100,10 -121,10 +101,10 @@@ pub fn svg<V: View>(style: &SvgStyle) -
.with_height(style.dimensions.height)
}
- #[derive(Clone, Deserialize, Default)]
+ #[derive(Clone, Deserialize, Default, JsonSchema)]
pub struct IconStyle {
- icon: SvgStyle,
- container: ContainerStyle,
+ pub icon: SvgStyle,
+ pub container: ContainerStyle,
}
pub fn icon<V: View>(style: &IconStyle) -> Container<V> {
diff --cc styles/src/styleTree/assistant.ts
index e4e342eb015,163584cd6db..942b97a2325
--- a/styles/src/styleTree/assistant.ts
+++ b/styles/src/styleTree/assistant.ts
@@@ -14,84 -15,29 +15,105 @@@ export default function assistant(color
margin: { bottom: 6, top: 6 },
background: editor(colorScheme).background,
},
+ hamburgerButton: {
- icon: {
- color: text(layer, "sans", "default", { size: "sm" }).color,
- asset: "icons/hamburger_15.svg",
- dimensions: {
- width: 15,
- height: 15,
++ icon: {
++ color: text(layer, "sans", "default", { size: "sm" }).color,
++ asset: "icons/hamburger_15.svg",
++ dimensions: {
++ width: 15,
++ height: 15,
++ },
+ },
- },
- container: {
- margin: { left: 12 },
- }
++ container: {
++ margin: { left: 12 },
++ }
+ },
+ zoomInButton: {
- icon: {
- color: text(layer, "sans", "default", { size: "sm" }).color,
- asset: "icons/maximize_8.svg",
- dimensions: {
- width: 12,
- height: 12,
++ icon: {
++ color: text(layer, "sans", "default", { size: "sm" }).color,
++ asset: "icons/maximize_8.svg",
++ dimensions: {
++ width: 12,
++ height: 12,
++ },
+ },
- },
- container: {
- margin: { right: 12 },
- }
++ container: {
++ margin: { right: 12 },
++ }
+ },
+ zoomOutButton: {
- icon: {
- color: text(layer, "sans", "default", { size: "sm" }).color,
- asset: "icons/minimize_8.svg",
- dimensions: {
- width: 12,
- height: 12,
++ icon: {
++ color: text(layer, "sans", "default", { size: "sm" }).color,
++ asset: "icons/minimize_8.svg",
++ dimensions: {
++ width: 12,
++ height: 12,
++ },
+ },
- },
- container: {
- margin: { right: 12 },
- }
++ container: {
++ margin: { right: 12 },
++ }
+ },
+ plusButton: {
- icon: {
- color: text(layer, "sans", "default", { size: "sm" }).color,
- asset: "icons/plus_12.svg",
- dimensions: {
- width: 12,
- height: 12,
++ icon: {
++ color: text(layer, "sans", "default", { size: "sm" }).color,
++ asset: "icons/plus_12.svg",
++ dimensions: {
++ width: 12,
++ height: 12,
++ },
+ },
- },
- container: {
- margin: { right: 12 },
- }
++ container: {
++ margin: { right: 12 },
++ }
+ },
+ title: {
- margin: { left: 12 },
- ...text(layer, "sans", "default", { size: "sm" })
++ margin: { left: 12 },
++ ...text(layer, "sans", "default", { size: "sm" })
+ },
+ savedConversation: {
- background: background(layer, "on"),
- hover: {
- background: background(layer, "on", "hovered"),
- },
- savedAt: {
- margin: { left: 8 },
- ...text(layer, "sans", "default", { size: "xs" }),
- },
- title: {
- margin: { left: 8 },
- ...text(layer, "sans", "default", { size: "sm", weight: "bold" }),
- }
++ container: interactive({
++ base: {
++ background: background(layer, "on"),
++ },
++ state: {
++ hovered: {
++ background: background(layer, "on", "hovered"),
++ }
++ },
++ }),
++ savedAt: {
++ margin: { left: 8 },
++ ...text(layer, "sans", "default", { size: "xs" }),
++ },
++ title: {
++ margin: { left: 8 },
++ ...text(layer, "sans", "default", { size: "sm", weight: "bold" }),
++ }
+ },
userSender: {
- ...text(layer, "sans", "default", { size: "sm", weight: "bold" }),
+ default: {
+ ...text(layer, "sans", "default", {
+ size: "sm",
+ weight: "bold",
+ }),
+ },
},
assistantSender: {
- ...text(layer, "sans", "accent", { size: "sm", weight: "bold" }),
+ default: {
+ ...text(layer, "sans", "accent", {
+ size: "sm",
+ weight: "bold",
+ }),
+ },
},
systemSender: {
- ...text(layer, "sans", "variant", { size: "sm", weight: "bold" }),
+ default: {
+ ...text(layer, "sans", "variant", {
+ size: "sm",
+ weight: "bold",
+ }),
+ },
},
sentAt: {
margin: { top: 2, left: 8 },
@@@ -100,19 -46,26 +122,23 @@@
modelInfoContainer: {
margin: { right: 16, top: 4 },
},
- model: {
- background: background(layer, "on"),
- margin: { right: 8 },
- padding: 4,
- cornerRadius: 4,
- ...text(layer, "sans", "default", { size: "xs" }),
- hover: {
- background: background(layer, "on", "hovered"),
- border: border(layer, "on", { overlay: true }),
+ model: interactive({
+ base: {
+ background: background(layer, "on"),
- border: border(layer, "on", { overlay: true }),
++ margin: { right: 8 },
+ padding: 4,
+ cornerRadius: 4,
+ ...text(layer, "sans", "default", { size: "xs" }),
},
- },
+ state: {
+ hovered: {
+ background: background(layer, "on", "hovered"),
++ border: border(layer, "on", { overlay: true }),
+ },
+ },
+ }),
remainingTokens: {
- background: background(layer, "on"),
- border: border(layer, "on", { overlay: true }),
- padding: 4,
- margin: { left: 4 },
- cornerRadius: 4,
+ margin: { right: 12 },
...text(layer, "sans", "positive", { size: "xs" }),
},
noRemainingTokens: {