Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Refactor out git status into FileName component Integrate file name component into the editor's tab content
Mikayla Maki41bef2e444b9
diff --git a/Cargo.lock b/Cargo.lockindex 81e4a4e025c..e009cfd3426 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6688,6 +6688,7 @@ name = "theme"version = "0.1.0"dependencies = ["anyhow",+ "fs","gpui","indexmap","parking_lot 0.11.2",diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rsindex e971af943ab..80c1009aa4c 100644--- a/crates/editor/src/items.rs+++ b/crates/editor/src/items.rs@@ -14,7 +14,7 @@ use language::{proto::serialize_anchor as serialize_text_anchor, Bias, Buffer, OffsetRangeExt, Point,SelectionGoal,};-use project::{FormatTrigger, Item as _, Project, ProjectPath};+use project::{repository::GitFileStatus, FormatTrigger, Item as _, Project, ProjectPath};use rpc::proto::{self, update_view};use settings::Settings;use smallvec::SmallVec;@@ -27,6 +27,7 @@ use std::{path::{Path, PathBuf},};use text::Selection;+use theme::ui::FileName;use util::{ResultExt, TryFutureExt};use workspace::item::{BreadcrumbText, FollowableItemHandle};use workspace::{@@ -565,8 +566,25 @@ impl Item for Editor {style: &theme::Tab,cx: &AppContext,) -> AnyElement<T> {+ fn git_file_status(this: &Editor, cx: &AppContext) -> Option<GitFileStatus> {+ let project_entry_id = this+ .buffer()+ .read(cx)+ .as_singleton()?+ .read(cx)+ .entry_id(cx)?;+ let project = this.project.as_ref()?.read(cx);+ let path = project.path_for_entry(project_entry_id, cx)?.path;+ let worktree = project.worktree_for_entry(project_entry_id, cx)?.read(cx);+ worktree.repo_for(&path)?.status_for_path(&worktree, &path)+ }+Flex::row()- .with_child(Label::new(self.title(cx).to_string(), style.label.clone()).aligned())+ .with_child(ComponentHost::new(FileName::new(+ self.title(cx).to_string(),+ git_file_status(self, cx),+ FileName::style(style.label.clone(), &cx.global::<Settings>().theme),+ ))).with_children(detail.and_then(|detail| {let path = path_for_buffer(&self.buffer, detail, false, cx)?;let description = path.to_string_lossy();@@ -580,6 +598,7 @@ impl Item for Editor {.aligned(),)}))+ .align_children_center().into_any()}diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rsindex e2c4af143c5..27b01a8db2d 100644--- a/crates/gpui/src/elements.rs+++ b/crates/gpui/src/elements.rs@@ -578,6 +578,15 @@ pub struct ComponentHost<V: View, C: Component<V>> {view_type: PhantomData<V>,}+impl<V: View, C: Component<V>> ComponentHost<V, C> {+ pub fn new(c: C) -> Self {+ Self {+ component: c,+ view_type: PhantomData,+ }+ }+}+impl<V: View, C: Component<V>> Deref for ComponentHost<V, C> {type Target = C;diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rsindex 10668750224..bb7f97fbf8d 100644--- a/crates/project_panel/src/project_panel.rs+++ b/crates/project_panel/src/project_panel.rs@@ -6,7 +6,7 @@ use gpui::{actions,anyhow::{anyhow, Result},elements::{- AnchorCorner, ChildView, ContainerStyle, Empty, Flex, Label, MouseEventHandler,+ AnchorCorner, ChildView, ComponentHost, ContainerStyle, Empty, Flex, MouseEventHandler,ParentElement, ScrollTarget, Stack, Svg, UniformList, UniformListState,},geometry::vector::Vector2F,@@ -29,7 +29,7 @@ use std::{path::Path,sync::Arc,};-use theme::ProjectPanelEntry;+use theme::{ui::FileName, ProjectPanelEntry};use unicase::UniCase;use workspace::Workspace;@@ -1083,19 +1083,6 @@ impl ProjectPanel {let kind = details.kind;let show_editor = details.is_editing && !details.is_processing;- // Prepare colors for git statuses- let editor_theme = &cx.global::<Settings>().theme.editor;- let mut filename_text_style = style.text.clone();- filename_text_style.color = details- .git_status- .as_ref()- .map(|status| match status {- GitFileStatus::Added => editor_theme.diff.inserted,- GitFileStatus::Modified => editor_theme.diff.modified,- GitFileStatus::Conflict => editor_theme.diff.deleted,- })- .unwrap_or(style.text.color);-Flex::row().with_child(if kind == EntryKind::Dir {@@ -1123,12 +1110,16 @@ impl ProjectPanel {.flex(1.0, true).into_any()} else {- Label::new(details.filename.clone(), filename_text_style)- .contained()- .with_margin_left(style.icon_spacing)- .aligned()- .left()- .into_any()+ ComponentHost::new(FileName::new(+ details.filename.clone(),+ details.git_status,+ FileName::style(style.text.clone(), &cx.global::<Settings>().theme),+ ))+ .contained()+ .with_margin_left(style.icon_spacing)+ .aligned()+ .left()+ .into_any()}).constrained().with_height(style.height)diff --git a/crates/theme/Cargo.toml b/crates/theme/Cargo.tomlindex 67a28397e25..c7dc2938ed1 100644--- a/crates/theme/Cargo.toml+++ b/crates/theme/Cargo.toml@@ -10,6 +10,7 @@ doctest = false[dependencies]gpui = { path = "../gpui" }+fs = { path = "../fs" }anyhow.workspace = trueindexmap = "1.6.2"parking_lot.workspace = truediff --git a/crates/theme/src/ui.rs b/crates/theme/src/ui.rsindex b86bfca8c42..e4df24c89fc 100644--- a/crates/theme/src/ui.rs+++ b/crates/theme/src/ui.rs@@ -1,9 +1,10 @@use std::borrow::Cow;+use fs::repository::GitFileStatus;use gpui::{color::Color,elements::{- ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label,+ ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label, LabelStyle,MouseEventHandler, ParentElement, Stack, Svg,},fonts::TextStyle,@@ -11,11 +12,11 @@ use gpui::{platform,platform::MouseButton,scene::MouseClick,- Action, Element, EventContext, MouseState, View, ViewContext,+ Action, AnyElement, Element, EventContext, MouseState, View, ViewContext,};use serde::Deserialize;-use crate::{ContainedText, Interactive};+use crate::{ContainedText, Interactive, Theme};#[derive(Clone, Deserialize, Default)]pub struct CheckboxStyle {@@ -252,3 +253,53 @@ where.constrained().with_height(style.dimensions().y())}++pub struct FileName {+ filename: String,+ git_status: Option<GitFileStatus>,+ style: FileNameStyle,+}++pub struct FileNameStyle {+ template_style: LabelStyle,+ git_inserted: Color,+ git_modified: Color,+ git_deleted: Color,+}++impl FileName {+ pub fn new(filename: String, git_status: Option<GitFileStatus>, style: FileNameStyle) -> Self {+ FileName {+ filename,+ git_status,+ style,+ }+ }++ pub fn style<I: Into<LabelStyle>>(style: I, theme: &Theme) -> FileNameStyle {+ FileNameStyle {+ template_style: style.into(),+ git_inserted: theme.editor.diff.inserted,+ git_modified: theme.editor.diff.modified,+ git_deleted: theme.editor.diff.deleted,+ }+ }+}++impl<V: View> gpui::elements::Component<V> for FileName {+ fn render(&self, _: &mut V, _: &mut ViewContext<V>) -> AnyElement<V> {+ // Prepare colors for git statuses+ let mut filename_text_style = self.style.template_style.text.clone();+ filename_text_style.color = self+ .git_status+ .as_ref()+ .map(|status| match status {+ GitFileStatus::Added => self.style.git_inserted,+ GitFileStatus::Modified => self.style.git_modified,+ GitFileStatus::Conflict => self.style.git_deleted,+ })+ .unwrap_or(self.style.template_style.text.color);++ Label::new(self.filename.clone(), filename_text_style).into_any()+ }+}