diff --git a/Cargo.lock b/Cargo.lock index 8838fdb130..f02c748fbd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9471,6 +9471,27 @@ dependencies = [ "workspace", ] +[[package]] +name = "theme_selector2" +version = "0.1.0" +dependencies = [ + "editor2", + "feature_flags2", + "fs2", + "fuzzy2", + "gpui2", + "log", + "parking_lot 0.11.2", + "picker2", + "postage", + "settings2", + "smol", + "theme2", + "ui2", + "util", + "workspace2", +] + [[package]] name = "thiserror" version = "1.0.48" @@ -11054,6 +11075,31 @@ dependencies = [ "workspace", ] +[[package]] +name = "welcome2" +version = "0.1.0" +dependencies = [ + "anyhow", + "client2", + "db2", + "editor2", + "fs2", + "fuzzy2", + "gpui2", + "install_cli2", + "log", + "picker2", + "project2", + "schemars", + "serde", + "settings2", + "theme2", + "theme_selector2", + "ui2", + "util", + "workspace2", +] + [[package]] name = "which" version = "4.4.2" @@ -11508,7 +11554,7 @@ dependencies = [ [[package]] name = "zed" -version = "0.115.0" +version = "0.116.0" dependencies = [ "activity_indicator", "ai", @@ -11720,6 +11766,7 @@ dependencies = [ "terminal_view2", "text2", "theme2", + "theme_selector2", "thiserror", "tiny_http", "toml 0.5.11", @@ -11757,6 +11804,7 @@ dependencies = [ "urlencoding", "util", "uuid 1.4.1", + "welcome2", "workspace2", "zed_actions2", ] diff --git a/Cargo.toml b/Cargo.toml index 1f6a291a26..03a854b77f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -107,6 +107,7 @@ members = [ "crates/theme2", "crates/theme_importer", "crates/theme_selector", + "crates/theme_selector2", "crates/ui2", "crates/util", "crates/semantic_index", @@ -115,6 +116,7 @@ members = [ "crates/vcs_menu", "crates/workspace2", "crates/welcome", + "crates/welcome2", "crates/xtask", "crates/zed", "crates/zed2", diff --git a/crates/call2/src/call2.rs b/crates/call2/src/call2.rs index 7885ef6e3f..df7dd847cf 100644 --- a/crates/call2/src/call2.rs +++ b/crates/call2/src/call2.rs @@ -14,8 +14,8 @@ use client::{ use collections::HashSet; use futures::{channel::oneshot, future::Shared, Future, FutureExt}; use gpui::{ - AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, Subscription, Task, - View, ViewContext, VisualContext, WeakModel, WeakView, + AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, PromptLevel, + Subscription, Task, View, ViewContext, VisualContext, WeakModel, WeakView, WindowHandle, }; pub use participant::ParticipantLocation; use postage::watch; @@ -334,12 +334,55 @@ impl ActiveCall { pub fn join_channel( &mut self, channel_id: u64, + requesting_window: Option>, cx: &mut ModelContext, ) -> Task>>> { if let Some(room) = self.room().cloned() { if room.read(cx).channel_id() == Some(channel_id) { - return Task::ready(Ok(Some(room))); - } else { + return cx.spawn(|_, _| async move { + todo!(); + // let future = room.update(&mut cx, |room, cx| { + // room.most_active_project(cx).map(|(host, project)| { + // room.join_project(project, host, app_state.clone(), cx) + // }) + // }) + + // if let Some(future) = future { + // future.await?; + // } + + // Ok(Some(room)) + }); + } + + let should_prompt = room.update(cx, |room, _| { + room.channel_id().is_some() + && room.is_sharing_project() + && room.remote_participants().len() > 0 + }); + if should_prompt && requesting_window.is_some() { + return cx.spawn(|this, mut cx| async move { + let answer = requesting_window.unwrap().update(&mut cx, |_, cx| { + cx.prompt( + PromptLevel::Warning, + "Leaving this call will unshare your current project.\nDo you want to switch channels?", + &["Yes, Join Channel", "Cancel"], + ) + })?; + if answer.await? == 1 { + return Ok(None); + } + + room.update(&mut cx, |room, cx| room.clear_state(cx))?; + + this.update(&mut cx, |this, cx| { + this.join_channel(channel_id, requesting_window, cx) + })? + .await + }); + } + + if room.read(cx).channel_id().is_some() { room.update(cx, |room, cx| room.clear_state(cx)); } } diff --git a/crates/client2/src/client2.rs b/crates/client2/src/client2.rs index b31451aa87..4746c9c6e4 100644 --- a/crates/client2/src/client2.rs +++ b/crates/client2/src/client2.rs @@ -693,8 +693,8 @@ impl Client { } } - pub async fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool { - read_credentials_from_keychain(cx).await.is_some() + pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool { + read_credentials_from_keychain(cx).is_some() } #[async_recursion(?Send)] @@ -725,7 +725,7 @@ impl Client { let mut read_from_keychain = false; let mut credentials = self.state.read().credentials.clone(); if credentials.is_none() && try_keychain { - credentials = read_credentials_from_keychain(cx).await; + credentials = read_credentials_from_keychain(cx); read_from_keychain = credentials.is_some(); } if credentials.is_none() { @@ -1324,7 +1324,7 @@ impl Client { } } -async fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { +fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option { if IMPERSONATE_LOGIN.is_some() { return None; } diff --git a/crates/collab2/src/tests/channel_tests.rs b/crates/collab2/src/tests/channel_tests.rs index 8ce5d99b80..43d18ee7d1 100644 --- a/crates/collab2/src/tests/channel_tests.rs +++ b/crates/collab2/src/tests/channel_tests.rs @@ -364,7 +364,8 @@ async fn test_joining_channel_ancestor_member( let active_call_b = cx_b.read(ActiveCall::global); assert!(active_call_b - .update(cx_b, |active_call, cx| active_call.join_channel(sub_id, cx)) + .update(cx_b, |active_call, cx| active_call + .join_channel(sub_id, None, cx)) .await .is_ok()); } @@ -394,7 +395,9 @@ async fn test_channel_room( let active_call_b = cx_b.read(ActiveCall::global); active_call_a - .update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx)) + .update(cx_a, |active_call, cx| { + active_call.join_channel(zed_id, None, cx) + }) .await .unwrap(); @@ -442,7 +445,9 @@ async fn test_channel_room( }); active_call_b - .update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx)) + .update(cx_b, |active_call, cx| { + active_call.join_channel(zed_id, None, cx) + }) .await .unwrap(); @@ -559,12 +564,16 @@ async fn test_channel_room( }); active_call_a - .update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx)) + .update(cx_a, |active_call, cx| { + active_call.join_channel(zed_id, None, cx) + }) .await .unwrap(); active_call_b - .update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx)) + .update(cx_b, |active_call, cx| { + active_call.join_channel(zed_id, None, cx) + }) .await .unwrap(); @@ -608,7 +617,9 @@ async fn test_channel_jumping(executor: BackgroundExecutor, cx_a: &mut TestAppCo let active_call_a = cx_a.read(ActiveCall::global); active_call_a - .update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx)) + .update(cx_a, |active_call, cx| { + active_call.join_channel(zed_id, None, cx) + }) .await .unwrap(); @@ -627,7 +638,7 @@ async fn test_channel_jumping(executor: BackgroundExecutor, cx_a: &mut TestAppCo active_call_a .update(cx_a, |active_call, cx| { - active_call.join_channel(rust_id, cx) + active_call.join_channel(rust_id, None, cx) }) .await .unwrap(); @@ -793,7 +804,7 @@ async fn test_call_from_channel( let active_call_b = cx_b.read(ActiveCall::global); active_call_a - .update(cx_a, |call, cx| call.join_channel(channel_id, cx)) + .update(cx_a, |call, cx| call.join_channel(channel_id, None, cx)) .await .unwrap(); @@ -1286,7 +1297,7 @@ async fn test_guest_access( // Non-members should not be allowed to join assert!(active_call_b - .update(cx_b, |call, cx| call.join_channel(channel_a, cx)) + .update(cx_b, |call, cx| call.join_channel(channel_a, None, cx)) .await .is_err()); @@ -1308,7 +1319,7 @@ async fn test_guest_access( // Client B joins channel A as a guest active_call_b - .update(cx_b, |call, cx| call.join_channel(channel_a, cx)) + .update(cx_b, |call, cx| call.join_channel(channel_a, None, cx)) .await .unwrap(); @@ -1341,7 +1352,7 @@ async fn test_guest_access( assert_channels_list_shape(client_b.channel_store(), cx_b, &[]); active_call_b - .update(cx_b, |call, cx| call.join_channel(channel_b, cx)) + .update(cx_b, |call, cx| call.join_channel(channel_b, None, cx)) .await .unwrap(); @@ -1372,7 +1383,7 @@ async fn test_invite_access( // should not be allowed to join assert!(active_call_b - .update(cx_b, |call, cx| call.join_channel(channel_b_id, cx)) + .update(cx_b, |call, cx| call.join_channel(channel_b_id, None, cx)) .await .is_err()); @@ -1390,7 +1401,7 @@ async fn test_invite_access( .unwrap(); active_call_b - .update(cx_b, |call, cx| call.join_channel(channel_b_id, cx)) + .update(cx_b, |call, cx| call.join_channel(channel_b_id, None, cx)) .await .unwrap(); diff --git a/crates/collab2/src/tests/integration_tests.rs b/crates/collab2/src/tests/integration_tests.rs index f2a39f3511..e579c384e3 100644 --- a/crates/collab2/src/tests/integration_tests.rs +++ b/crates/collab2/src/tests/integration_tests.rs @@ -510,9 +510,10 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously( // Simultaneously join channel 1 and then channel 2 active_call_a - .update(cx_a, |call, cx| call.join_channel(channel_1, cx)) + .update(cx_a, |call, cx| call.join_channel(channel_1, None, cx)) .detach(); - let join_channel_2 = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, cx)); + let join_channel_2 = + active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, None, cx)); join_channel_2.await.unwrap(); @@ -538,7 +539,8 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously( call.invite(client_c.user_id().unwrap(), None, cx) }); - let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx)); + let join_channel = + active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, None, cx)); b_invite.await.unwrap(); c_invite.await.unwrap(); @@ -567,7 +569,8 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously( .unwrap(); // Simultaneously join channel 1 and call user B and user C from client A. - let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx)); + let join_channel = + active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, None, cx)); let b_invite = active_call_a.update(cx_a, |call, cx| { call.invite(client_b.user_id().unwrap(), None, cx) diff --git a/crates/collab_ui2/src/collab_panel.rs b/crates/collab_ui2/src/collab_panel.rs index 7ef2d47c81..64580f0efc 100644 --- a/crates/collab_ui2/src/collab_panel.rs +++ b/crates/collab_ui2/src/collab_panel.rs @@ -17,6 +17,7 @@ mod contact_finder; // Client, Contact, User, UserStore, // }; use contact_finder::ContactFinder; +use menu::Confirm; use rpc::proto; // use context_menu::{ContextMenu, ContextMenuItem}; // use db::kvp::KEY_VALUE_STORE; @@ -90,10 +91,10 @@ use rpc::proto; // channel_id: ChannelId, // } -// #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -// pub struct OpenChannelNotes { -// pub channel_id: ChannelId, -// } +#[derive(Action, PartialEq, Debug, Clone, Serialize, Deserialize)] +pub struct OpenChannelNotes { + pub channel_id: ChannelId, +} // #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] // pub struct JoinChannelCall { @@ -160,26 +161,26 @@ const COLLABORATION_PANEL_KEY: &'static str = "CollaborationPanel"; use std::{iter::once, mem, sync::Arc}; use call::ActiveCall; -use channel::{Channel, ChannelId, ChannelStore}; +use channel::{Channel, ChannelEvent, ChannelId, ChannelStore}; use client::{Client, Contact, User, UserStore}; use db::kvp::KEY_VALUE_STORE; use editor::Editor; -use feature_flags::{ChannelsAlpha, FeatureFlagAppExt}; +use feature_flags::{ChannelsAlpha, FeatureFlagAppExt, FeatureFlagViewExt}; use fuzzy::{match_strings, StringMatchCandidate}; use gpui::{ - actions, div, img, prelude::*, serde_json, AppContext, AsyncWindowContext, Div, EventEmitter, - FocusHandle, Focusable, FocusableView, InteractiveElement, IntoElement, Model, ParentElement, - Render, RenderOnce, SharedString, Styled, Subscription, View, ViewContext, VisualContext, - WeakView, + actions, div, img, prelude::*, serde_json, Action, AppContext, AsyncWindowContext, Div, + EventEmitter, FocusHandle, Focusable, FocusableView, InteractiveElement, IntoElement, Model, + ParentElement, PromptLevel, Render, RenderOnce, SharedString, Styled, Subscription, Task, View, + ViewContext, VisualContext, WeakView, }; use project::Fs; use serde_derive::{Deserialize, Serialize}; -use settings::Settings; +use settings::{Settings, SettingsStore}; use ui::{ - h_stack, v_stack, Avatar, Button, Color, Icon, IconButton, Label, List, ListHeader, ListItem, - Toggle, Tooltip, + h_stack, v_stack, Avatar, Button, Color, Icon, IconButton, IconElement, Label, List, + ListHeader, ListItem, Toggle, Tooltip, }; -use util::{maybe, ResultExt}; +use util::{maybe, ResultExt, TryFutureExt}; use workspace::{ dock::{DockPosition, Panel, PanelEvent}, notifications::NotifyResultExt, @@ -293,10 +294,10 @@ pub enum ChannelEditingState { } impl ChannelEditingState { - fn pending_name(&self) -> Option<&str> { + fn pending_name(&self) -> Option { match self { - ChannelEditingState::Create { pending_name, .. } => pending_name.as_deref(), - ChannelEditingState::Rename { pending_name, .. } => pending_name.as_deref(), + ChannelEditingState::Create { pending_name, .. } => pending_name.clone(), + ChannelEditingState::Rename { pending_name, .. } => pending_name.clone(), } } } @@ -306,10 +307,10 @@ pub struct CollabPanel { fs: Arc, focus_handle: FocusHandle, // channel_clipboard: Option, - // pending_serialization: Task>, + pending_serialization: Task>, // context_menu: ViewHandle, filter_editor: View, - // channel_name_editor: ViewHandle, + channel_name_editor: View, channel_editing_state: Option, entries: Vec, selection: Option, @@ -322,17 +323,17 @@ pub struct CollabPanel { subscriptions: Vec, collapsed_sections: Vec
, collapsed_channels: Vec, - // drag_target_channel: ChannelDragTarget, + drag_target_channel: ChannelDragTarget, workspace: WeakView, // context_menu_on_selected: bool, } -// #[derive(PartialEq, Eq)] -// enum ChannelDragTarget { -// None, -// Root, -// Channel(ChannelId), -// } +#[derive(PartialEq, Eq)] +enum ChannelDragTarget { + None, + Root, + Channel(ChannelId), +} #[derive(Serialize, Deserialize)] struct SerializedCollabPanel { @@ -438,28 +439,21 @@ impl CollabPanel { // }) // .detach(); - // let channel_name_editor = cx.add_view(|cx| { - // Editor::single_line( - // Some(Arc::new(|theme| { - // theme.collab_panel.user_query_editor.clone() - // })), - // cx, - // ) - // }); + let channel_name_editor = cx.build_view(|cx| Editor::single_line(cx)); - // cx.subscribe(&channel_name_editor, |this, _, event, cx| { - // if let editor::Event::Blurred = event { - // if let Some(state) = &this.channel_editing_state { - // if state.pending_name().is_some() { - // return; - // } - // } - // this.take_editing_state(cx); - // this.update_entries(false, cx); - // cx.notify(); - // } - // }) - // .detach(); + cx.subscribe(&channel_name_editor, |this: &mut Self, _, event, cx| { + if let editor::EditorEvent::Blurred = event { + if let Some(state) = &this.channel_editing_state { + if state.pending_name().is_some() { + return; + } + } + this.take_editing_state(cx); + this.update_entries(false, cx); + cx.notify(); + } + }) + .detach(); // let list_state = // ListState::::new(0, Orientation::Top, 1000., move |this, ix, cx| { @@ -597,9 +591,9 @@ impl CollabPanel { focus_handle: cx.focus_handle(), // channel_clipboard: None, fs: workspace.app_state().fs.clone(), - // pending_serialization: Task::ready(None), + pending_serialization: Task::ready(None), // context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)), - // channel_name_editor, + channel_name_editor, filter_editor, entries: Vec::default(), channel_editing_state: None, @@ -614,59 +608,58 @@ impl CollabPanel { workspace: workspace.weak_handle(), client: workspace.app_state().client.clone(), // context_menu_on_selected: true, - // drag_target_channel: ChannelDragTarget::None, + drag_target_channel: ChannelDragTarget::None, // list_state, }; this.update_entries(false, cx); - // // Update the dock position when the setting changes. - // let mut old_dock_position = this.position(cx); - // this.subscriptions - // .push( - // cx.observe_global::(move |this: &mut Self, cx| { - // let new_dock_position = this.position(cx); - // if new_dock_position != old_dock_position { - // old_dock_position = new_dock_position; - // cx.emit(Event::DockPositionChanged); - // } - // cx.notify(); - // }), - // ); + // Update the dock position when the setting changes. + let mut old_dock_position = this.position(cx); + this.subscriptions.push(cx.observe_global::( + move |this: &mut Self, cx| { + let new_dock_position = this.position(cx); + if new_dock_position != old_dock_position { + old_dock_position = new_dock_position; + cx.emit(PanelEvent::ChangePosition); + } + cx.notify(); + }, + )); - // let active_call = ActiveCall::global(cx); + let active_call = ActiveCall::global(cx); this.subscriptions .push(cx.observe(&this.user_store, |this, _, cx| { this.update_entries(true, cx) })); - // this.subscriptions - // .push(cx.observe(&this.channel_store, |this, _, cx| { - // this.update_entries(true, cx) - // })); - // this.subscriptions - // .push(cx.observe(&active_call, |this, _, cx| this.update_entries(true, cx))); - // this.subscriptions - // .push(cx.observe_flag::(move |_, this, cx| { - // this.update_entries(true, cx) - // })); - // this.subscriptions.push(cx.subscribe( - // &this.channel_store, - // |this, _channel_store, e, cx| match e { - // ChannelEvent::ChannelCreated(channel_id) - // | ChannelEvent::ChannelRenamed(channel_id) => { - // if this.take_editing_state(cx) { - // this.update_entries(false, cx); - // this.selection = this.entries.iter().position(|entry| { - // if let ListEntry::Channel { channel, .. } = entry { - // channel.id == *channel_id - // } else { - // false - // } - // }); - // } - // } - // }, - // )); + this.subscriptions + .push(cx.observe(&this.channel_store, |this, _, cx| { + this.update_entries(true, cx) + })); + this.subscriptions + .push(cx.observe(&active_call, |this, _, cx| this.update_entries(true, cx))); + this.subscriptions + .push(cx.observe_flag::(move |_, this, cx| { + this.update_entries(true, cx) + })); + this.subscriptions.push(cx.subscribe( + &this.channel_store, + |this, _channel_store, e, cx| match e { + ChannelEvent::ChannelCreated(channel_id) + | ChannelEvent::ChannelRenamed(channel_id) => { + if this.take_editing_state(cx) { + this.update_entries(false, cx); + this.selection = this.entries.iter().position(|entry| { + if let ListEntry::Channel { channel, .. } = entry { + channel.id == *channel_id + } else { + false + } + }); + } + } + }, + )); this }) @@ -696,10 +689,9 @@ impl CollabPanel { if let Some(serialized_panel) = serialized_panel { panel.update(cx, |panel, cx| { panel.width = serialized_panel.width; - //todo!(collapsed_channels) - // panel.collapsed_channels = serialized_panel - // .collapsed_channels - // .unwrap_or_else(|| Vec::new()); + panel.collapsed_channels = serialized_panel + .collapsed_channels + .unwrap_or_else(|| Vec::new()); cx.notify(); }); } @@ -707,25 +699,25 @@ impl CollabPanel { }) } - // fn serialize(&mut self, cx: &mut ViewContext) { - // let width = self.width; - // let collapsed_channels = self.collapsed_channels.clone(); - // self.pending_serialization = cx.background().spawn( - // async move { - // KEY_VALUE_STORE - // .write_kvp( - // COLLABORATION_PANEL_KEY.into(), - // serde_json::to_string(&SerializedCollabPanel { - // width, - // collapsed_channels: Some(collapsed_channels), - // })?, - // ) - // .await?; - // anyhow::Ok(()) - // } - // .log_err(), - // ); - // } + fn serialize(&mut self, cx: &mut ViewContext) { + let width = self.width; + let collapsed_channels = self.collapsed_channels.clone(); + self.pending_serialization = cx.background_executor().spawn( + async move { + KEY_VALUE_STORE + .write_kvp( + COLLABORATION_PANEL_KEY.into(), + serde_json::to_string(&SerializedCollabPanel { + width, + collapsed_channels: Some(collapsed_channels), + })?, + ) + .await?; + anyhow::Ok(()) + } + .log_err(), + ); + } fn update_entries(&mut self, select_same_item: bool, cx: &mut ViewContext) { let channel_store = self.channel_store.read(cx); @@ -1456,16 +1448,16 @@ impl CollabPanel { // .into_any() // } - // fn take_editing_state(&mut self, cx: &mut ViewContext) -> bool { - // if let Some(_) = self.channel_editing_state.take() { - // self.channel_name_editor.update(cx, |editor, cx| { - // editor.set_text("", cx); - // }); - // true - // } else { - // false - // } - // } + fn take_editing_state(&mut self, cx: &mut ViewContext) -> bool { + if let Some(_) = self.channel_editing_state.take() { + self.channel_name_editor.update(cx, |editor, cx| { + editor.set_text("", cx); + }); + true + } else { + false + } + } // fn render_contact_placeholder( // &self, @@ -1501,67 +1493,6 @@ impl CollabPanel { // .into_any() // } - // fn render_channel_editor( - // &self, - // theme: &theme::Theme, - // depth: usize, - // cx: &AppContext, - // ) -> AnyElement { - // Flex::row() - // .with_child( - // Empty::new() - // .constrained() - // .with_width(theme.collab_panel.disclosure.button_space()), - // ) - // .with_child( - // Svg::new("icons/hash.svg") - // .with_color(theme.collab_panel.channel_hash.color) - // .constrained() - // .with_width(theme.collab_panel.channel_hash.width) - // .aligned() - // .left(), - // ) - // .with_child( - // if let Some(pending_name) = self - // .channel_editing_state - // .as_ref() - // .and_then(|state| state.pending_name()) - // { - // Label::new( - // pending_name.to_string(), - // theme.collab_panel.contact_username.text.clone(), - // ) - // .contained() - // .with_style(theme.collab_panel.contact_username.container) - // .aligned() - // .left() - // .flex(1., true) - // .into_any() - // } else { - // ChildView::new(&self.channel_name_editor, cx) - // .aligned() - // .left() - // .contained() - // .with_style(theme.collab_panel.channel_editor) - // .flex(1.0, true) - // .into_any() - // }, - // ) - // .align_children_center() - // .constrained() - // .with_height(theme.collab_panel.row_height) - // .contained() - // .with_style(ContainerStyle { - // background_color: Some(theme.editor.background), - // ..*theme.collab_panel.contact_row.default_style() - // }) - // .with_padding_left( - // theme.collab_panel.contact_row.default_style().padding.left - // + theme.collab_panel.channel_indent * depth as f32, - // ) - // .into_any() - // } - // fn render_channel_notes( // &self, // channel_id: ChannelId, @@ -1754,109 +1685,6 @@ impl CollabPanel { // .into_any() // } - // fn render_contact_request( - // user: Arc, - // user_store: ModelHandle, - // theme: &theme::CollabPanel, - // is_incoming: bool, - // is_selected: bool, - // cx: &mut ViewContext, - // ) -> AnyElement { - // enum Decline {} - // enum Accept {} - // enum Cancel {} - - // let mut row = Flex::row() - // .with_children(user.avatar.clone().map(|avatar| { - // Image::from_data(avatar) - // .with_style(theme.contact_avatar) - // .aligned() - // .left() - // })) - // .with_child( - // Label::new( - // user.github_login.clone(), - // theme.contact_username.text.clone(), - // ) - // .contained() - // .with_style(theme.contact_username.container) - // .aligned() - // .left() - // .flex(1., true), - // ); - - // let user_id = user.id; - // let github_login = user.github_login.clone(); - // let is_contact_request_pending = user_store.read(cx).is_contact_request_pending(&user); - // let button_spacing = theme.contact_button_spacing; - - // if is_incoming { - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/x.svg").aligned() - // }) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.respond_to_contact_request(user_id, false, cx); - // }) - // .contained() - // .with_margin_right(button_spacing), - // ); - - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/check.svg") - // .aligned() - // .flex_float() - // }) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.respond_to_contact_request(user_id, true, cx); - // }), - // ); - // } else { - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/x.svg") - // .aligned() - // .flex_float() - // }) - // .with_padding(Padding::uniform(2.)) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.remove_contact(user_id, &github_login, cx); - // }) - // .flex_float(), - // ); - // } - - // row.constrained() - // .with_height(theme.row_height) - // .contained() - // .with_style( - // *theme - // .contact_row - // .in_state(is_selected) - // .style_for(&mut Default::default()), - // ) - // .into_any() - // } - // fn has_subchannels(&self, ix: usize) -> bool { // self.entries.get(ix).map_or(false, |entry| { // if let ListEntry::Channel { has_children, .. } = entry { @@ -2054,148 +1882,148 @@ impl CollabPanel { // cx.notify(); // } - // fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { - // if self.confirm_channel_edit(cx) { - // return; - // } + fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { + if self.confirm_channel_edit(cx) { + return; + } - // if let Some(selection) = self.selection { - // if let Some(entry) = self.entries.get(selection) { - // match entry { - // ListEntry::Header(section) => match section { - // Section::ActiveCall => Self::leave_call(cx), - // Section::Channels => self.new_root_channel(cx), - // Section::Contacts => self.toggle_contact_finder(cx), - // Section::ContactRequests - // | Section::Online - // | Section::Offline - // | Section::ChannelInvites => { - // self.toggle_section_expanded(*section, cx); - // } - // }, - // ListEntry::Contact { contact, calling } => { - // if contact.online && !contact.busy && !calling { - // self.call(contact.user.id, Some(self.project.clone()), cx); - // } - // } - // ListEntry::ParticipantProject { - // project_id, - // host_user_id, - // .. - // } => { - // if let Some(workspace) = self.workspace.upgrade(cx) { - // let app_state = workspace.read(cx).app_state().clone(); - // workspace::join_remote_project( - // *project_id, - // *host_user_id, - // app_state, - // cx, - // ) - // .detach_and_log_err(cx); - // } - // } - // ListEntry::ParticipantScreen { peer_id, .. } => { - // let Some(peer_id) = peer_id else { - // return; - // }; - // if let Some(workspace) = self.workspace.upgrade(cx) { - // workspace.update(cx, |workspace, cx| { - // workspace.open_shared_screen(*peer_id, cx) - // }); - // } - // } - // ListEntry::Channel { channel, .. } => { - // let is_active = maybe!({ - // let call_channel = ActiveCall::global(cx) - // .read(cx) - // .room()? - // .read(cx) - // .channel_id()?; + // if let Some(selection) = self.selection { + // if let Some(entry) = self.entries.get(selection) { + // match entry { + // ListEntry::Header(section) => match section { + // Section::ActiveCall => Self::leave_call(cx), + // Section::Channels => self.new_root_channel(cx), + // Section::Contacts => self.toggle_contact_finder(cx), + // Section::ContactRequests + // | Section::Online + // | Section::Offline + // | Section::ChannelInvites => { + // self.toggle_section_expanded(*section, cx); + // } + // }, + // ListEntry::Contact { contact, calling } => { + // if contact.online && !contact.busy && !calling { + // self.call(contact.user.id, Some(self.project.clone()), cx); + // } + // } + // ListEntry::ParticipantProject { + // project_id, + // host_user_id, + // .. + // } => { + // if let Some(workspace) = self.workspace.upgrade(cx) { + // let app_state = workspace.read(cx).app_state().clone(); + // workspace::join_remote_project( + // *project_id, + // *host_user_id, + // app_state, + // cx, + // ) + // .detach_and_log_err(cx); + // } + // } + // ListEntry::ParticipantScreen { peer_id, .. } => { + // let Some(peer_id) = peer_id else { + // return; + // }; + // if let Some(workspace) = self.workspace.upgrade(cx) { + // workspace.update(cx, |workspace, cx| { + // workspace.open_shared_screen(*peer_id, cx) + // }); + // } + // } + // ListEntry::Channel { channel, .. } => { + // let is_active = maybe!({ + // let call_channel = ActiveCall::global(cx) + // .read(cx) + // .room()? + // .read(cx) + // .channel_id()?; - // Some(call_channel == channel.id) - // }) - // .unwrap_or(false); - // if is_active { - // self.open_channel_notes( - // &OpenChannelNotes { - // channel_id: channel.id, - // }, - // cx, - // ) - // } else { - // self.join_channel(channel.id, cx) - // } - // } - // ListEntry::ContactPlaceholder => self.toggle_contact_finder(cx), - // _ => {} - // } - // } - // } - // } + // Some(call_channel == channel.id) + // }) + // .unwrap_or(false); + // if is_active { + // self.open_channel_notes( + // &OpenChannelNotes { + // channel_id: channel.id, + // }, + // cx, + // ) + // } else { + // self.join_channel(channel.id, cx) + // } + // } + // ListEntry::ContactPlaceholder => self.toggle_contact_finder(cx), + // _ => {} + // } + // } + // } + } - // fn insert_space(&mut self, _: &InsertSpace, cx: &mut ViewContext) { - // if self.channel_editing_state.is_some() { - // self.channel_name_editor.update(cx, |editor, cx| { - // editor.insert(" ", cx); - // }); - // } - // } + fn insert_space(&mut self, _: &InsertSpace, cx: &mut ViewContext) { + if self.channel_editing_state.is_some() { + self.channel_name_editor.update(cx, |editor, cx| { + editor.insert(" ", cx); + }); + } + } - // fn confirm_channel_edit(&mut self, cx: &mut ViewContext) -> bool { - // if let Some(editing_state) = &mut self.channel_editing_state { - // match editing_state { - // ChannelEditingState::Create { - // location, - // pending_name, - // .. - // } => { - // if pending_name.is_some() { - // return false; - // } - // let channel_name = self.channel_name_editor.read(cx).text(cx); + fn confirm_channel_edit(&mut self, cx: &mut ViewContext) -> bool { + if let Some(editing_state) = &mut self.channel_editing_state { + match editing_state { + ChannelEditingState::Create { + location, + pending_name, + .. + } => { + if pending_name.is_some() { + return false; + } + let channel_name = self.channel_name_editor.read(cx).text(cx); - // *pending_name = Some(channel_name.clone()); + *pending_name = Some(channel_name.clone()); - // self.channel_store - // .update(cx, |channel_store, cx| { - // channel_store.create_channel(&channel_name, *location, cx) - // }) - // .detach(); - // cx.notify(); - // } - // ChannelEditingState::Rename { - // location, - // pending_name, - // } => { - // if pending_name.is_some() { - // return false; - // } - // let channel_name = self.channel_name_editor.read(cx).text(cx); - // *pending_name = Some(channel_name.clone()); + self.channel_store + .update(cx, |channel_store, cx| { + channel_store.create_channel(&channel_name, *location, cx) + }) + .detach(); + cx.notify(); + } + ChannelEditingState::Rename { + location, + pending_name, + } => { + if pending_name.is_some() { + return false; + } + let channel_name = self.channel_name_editor.read(cx).text(cx); + *pending_name = Some(channel_name.clone()); - // self.channel_store - // .update(cx, |channel_store, cx| { - // channel_store.rename(*location, &channel_name, cx) - // }) - // .detach(); - // cx.notify(); - // } - // } - // cx.focus_self(); - // true - // } else { - // false - // } - // } + self.channel_store + .update(cx, |channel_store, cx| { + channel_store.rename(*location, &channel_name, cx) + }) + .detach(); + cx.notify(); + } + } + cx.focus_self(); + true + } else { + false + } + } - // fn toggle_section_expanded(&mut self, section: Section, cx: &mut ViewContext) { - // if let Some(ix) = self.collapsed_sections.iter().position(|s| *s == section) { - // self.collapsed_sections.remove(ix); - // } else { - // self.collapsed_sections.push(section); - // } - // self.update_entries(false, cx); - // } + fn toggle_section_expanded(&mut self, section: Section, cx: &mut ViewContext) { + if let Some(ix) = self.collapsed_sections.iter().position(|s| *s == section) { + self.collapsed_sections.remove(ix); + } else { + self.collapsed_sections.push(section); + } + self.update_entries(false, cx); + } // fn collapse_selected_channel( // &mut self, @@ -2233,20 +2061,20 @@ impl CollabPanel { // self.toggle_channel_collapsed(action.location, cx); // } - // fn toggle_channel_collapsed<'a>(&mut self, channel_id: ChannelId, cx: &mut ViewContext) { - // match self.collapsed_channels.binary_search(&channel_id) { - // Ok(ix) => { - // self.collapsed_channels.remove(ix); - // } - // Err(ix) => { - // self.collapsed_channels.insert(ix, channel_id); - // } - // }; - // self.serialize(cx); - // self.update_entries(true, cx); - // cx.notify(); - // cx.focus_self(); - // } + fn toggle_channel_collapsed<'a>(&mut self, channel_id: ChannelId, cx: &mut ViewContext) { + match self.collapsed_channels.binary_search(&channel_id) { + Ok(ix) => { + self.collapsed_channels.remove(ix); + } + Err(ix) => { + self.collapsed_channels.insert(ix, channel_id); + } + }; + self.serialize(cx); + self.update_entries(true, cx); + cx.notify(); + cx.focus_self(); + } fn is_channel_collapsed(&self, channel_id: ChannelId) -> bool { self.collapsed_channels.binary_search(&channel_id).is_ok() @@ -2270,23 +2098,23 @@ impl CollabPanel { } } - // fn new_root_channel(&mut self, cx: &mut ViewContext) { - // self.channel_editing_state = Some(ChannelEditingState::Create { - // location: None, - // pending_name: None, - // }); - // self.update_entries(false, cx); - // self.select_channel_editor(); - // cx.focus(self.channel_name_editor.as_any()); - // cx.notify(); - // } + fn new_root_channel(&mut self, cx: &mut ViewContext) { + self.channel_editing_state = Some(ChannelEditingState::Create { + location: None, + pending_name: None, + }); + self.update_entries(false, cx); + self.select_channel_editor(); + cx.focus_view(&self.channel_name_editor); + cx.notify(); + } - // fn select_channel_editor(&mut self) { - // self.selection = self.entries.iter().position(|entry| match entry { - // ListEntry::ChannelEditor { .. } => true, - // _ => false, - // }); - // } + fn select_channel_editor(&mut self) { + self.selection = self.entries.iter().position(|entry| match entry { + ListEntry::ChannelEditor { .. } => true, + _ => false, + }); + } // fn new_subchannel(&mut self, action: &NewChannel, cx: &mut ViewContext) { // self.collapsed_channels @@ -2346,11 +2174,12 @@ impl CollabPanel { // } // } - // fn open_channel_notes(&mut self, action: &OpenChannelNotes, cx: &mut ViewContext) { - // if let Some(workspace) = self.workspace.upgrade(cx) { - // ChannelView::open(action.channel_id, workspace, cx).detach(); - // } - // } + fn open_channel_notes(&mut self, action: &OpenChannelNotes, cx: &mut ViewContext) { + if let Some(workspace) = self.workspace.upgrade() { + todo!(); + // ChannelView::open(action.channel_id, workspace, cx).detach(); + } + } // fn show_inline_context_menu(&mut self, _: &menu::ShowContextMenu, cx: &mut ViewContext) { // let Some(channel) = self.selected_channel() else { @@ -2439,44 +2268,38 @@ impl CollabPanel { // // Should move to the filter editor if clicking on it // // Should move selection to the channel editor if activating it - // fn remove_contact(&mut self, user_id: u64, github_login: &str, cx: &mut ViewContext) { - // let user_store = self.user_store.clone(); - // let prompt_message = format!( - // "Are you sure you want to remove \"{}\" from your contacts?", - // github_login - // ); - // let mut answer = cx.prompt(PromptLevel::Warning, &prompt_message, &["Remove", "Cancel"]); - // let window = cx.window(); - // cx.spawn(|_, mut cx| async move { - // if answer.next().await == Some(0) { - // if let Err(e) = user_store - // .update(&mut cx, |store, cx| store.remove_contact(user_id, cx)) - // .await - // { - // window.prompt( - // PromptLevel::Info, - // &format!("Failed to remove contact: {}", e), - // &["Ok"], - // &mut cx, - // ); - // } - // } - // }) - // .detach(); - // } + fn remove_contact(&mut self, user_id: u64, github_login: &str, cx: &mut ViewContext) { + let user_store = self.user_store.clone(); + let prompt_message = format!( + "Are you sure you want to remove \"{}\" from your contacts?", + github_login + ); + let mut answer = cx.prompt(PromptLevel::Warning, &prompt_message, &["Remove", "Cancel"]); + let window = cx.window(); + cx.spawn(|_, mut cx| async move { + if answer.await? == 0 { + user_store + .update(&mut cx, |store, cx| store.remove_contact(user_id, cx))? + .await + .notify_async_err(&mut cx); + } + anyhow::Ok(()) + }) + .detach_and_log_err(cx); + } - // fn respond_to_contact_request( - // &mut self, - // user_id: u64, - // accept: bool, - // cx: &mut ViewContext, - // ) { - // self.user_store - // .update(cx, |store, cx| { - // store.respond_to_contact_request(user_id, accept, cx) - // }) - // .detach(); - // } + fn respond_to_contact_request( + &mut self, + user_id: u64, + accept: bool, + cx: &mut ViewContext, + ) { + self.user_store + .update(cx, |store, cx| { + store.respond_to_contact_request(user_id, accept, cx) + }) + .detach_and_log_err(cx); + } // fn respond_to_channel_invite( // &mut self, @@ -2504,21 +2327,22 @@ impl CollabPanel { // .detach_and_log_err(cx); // } - // fn join_channel(&self, channel_id: u64, cx: &mut ViewContext) { - // let Some(workspace) = self.workspace.upgrade(cx) else { - // return; - // }; - // let Some(handle) = cx.window().downcast::() else { - // return; - // }; - // workspace::join_channel( - // channel_id, - // workspace.read(cx).app_state().clone(), - // Some(handle), - // cx, - // ) - // .detach_and_log_err(cx) - // } + fn join_channel(&self, channel_id: u64, cx: &mut ViewContext) { + let Some(handle) = cx.window_handle().downcast::() else { + return; + }; + let active_call = ActiveCall::global(cx); + cx.spawn(|_, mut cx| async move { + active_call + .update(&mut cx, |active_call, cx| { + active_call.join_channel(channel_id, Some(handle), cx) + }) + .log_err()? + .await + .notify_async_err(&mut cx) + }) + .detach() + } // fn join_channel_chat(&mut self, action: &JoinChannelChat, cx: &mut ViewContext) { // let channel_id = action.channel_id; @@ -2590,7 +2414,9 @@ impl CollabPanel { } => self .render_channel(&*channel, depth, has_children, is_selected, cx) .into_any_element(), - ListEntry::ChannelEditor { depth } => todo!(), + ListEntry::ChannelEditor { depth } => { + self.render_channel_editor(depth, cx).into_any_element() + } } })) } @@ -2689,10 +2515,7 @@ impl CollabPanel { Some( IconButton::new("add-channel", Icon::Plus) - .on_click(cx.listener(|this, _, cx| { - todo!() - // this.new_root_channel(cx) - })) + .on_click(cx.listener(|this, _, cx| this.new_root_channel(cx))) .tooltip(|cx| Tooltip::text("Create a channel", cx)), ) } @@ -2707,18 +2530,16 @@ impl CollabPanel { | Section::Offline => true, }; - let mut header = ListHeader::new(text); - if let Some(button) = button { - header = header.right_button(button) - } - // todo!() is selected - if can_collapse { - // todo!() on click to toggle - header = header.toggle(ui::Toggle::Toggled(is_collapsed)); - } - - header + ListHeader::new(text) + .when_some(button, |el, button| el.right_button(button)) + .selected(is_selected) + .when(can_collapse, |el| { + el.toggle(ui::Toggle::Toggled(is_collapsed)).on_toggle( + cx.listener(move |this, _, cx| this.toggle_section_expanded(section, cx)), + ) + }) } + fn render_contact( &mut self, contact: &Contact, @@ -2742,10 +2563,32 @@ impl CollabPanel { }) .log_err(); })) - .child(Label::new(github_login.clone())); + .child( + h_stack() + .w_full() + .justify_between() + .child(Label::new(github_login.clone())) + .child( + div() + .id("remove_contact") + .invisible() + .group_hover("", |style| style.visible()) + .child( + IconButton::new("remove_contact", Icon::Close) + .color(Color::Muted) + .tooltip(|cx| Tooltip::text("Remove Contact", cx)) + .on_click(cx.listener(move |this, _, cx| { + this.remove_contact(user_id, &github_login, cx); + })), + ), + ), + ); + if let Some(avatar) = contact.user.avatar.clone() { - //item = item.left_avatar(avatar); + item = item.left_avatar(avatar); } + + div().group("").child(item) // let event_handler = // MouseEventHandler::new::(contact.user.id as usize, cx, |state, cx| { // Flex::row() @@ -2774,40 +2617,7 @@ impl CollabPanel { // ) // .with_children(status_badge) // })) - // .with_child( - // Label::new( - // contact.user.github_login.clone(), - // collab_theme.contact_username.text.clone(), - // ) - // .contained() - // .with_style(collab_theme.contact_username.container) - // .aligned() - // .left() - // .flex(1., true), - // ) - // .with_children(if state.hovered() { - // Some( - // MouseEventHandler::new::( - // contact.user.id as usize, - // cx, - // |mouse_state, _| { - // let button_style = - // collab_theme.contact_button.style_for(mouse_state); - // render_icon_button(button_style, "icons/x.svg") - // .aligned() - // .flex_float() - // }, - // ) - // .with_padding(Padding::uniform(2.)) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.remove_contact(user_id, &github_login, cx); - // }) - // .flex_float(), - // ) - // } else { - // None - // }) + // .with_children(if calling { // Some( // Label::new("Calling", collab_theme.calling_indicator.text.clone()) @@ -2865,8 +2675,6 @@ impl CollabPanel { // ) // .into_any() // }; - - item } fn render_contact_request( @@ -2877,104 +2685,48 @@ impl CollabPanel { cx: &mut ViewContext, ) -> impl IntoElement { let github_login = SharedString::from(user.github_login.clone()); + let user_id = user.id; + let is_contact_request_pending = self.user_store.read(cx).is_contact_request_pending(&user); + let color = if is_contact_request_pending { + Color::Muted + } else { + Color::Default + }; - let mut item = ListItem::new(github_login.clone()) - .child(Label::new(github_login.clone())) - .on_click(cx.listener(|this, _, cx| { - todo!(); - })); - if let Some(avatar) = user.avatar.clone() { - item = item.left_avatar(avatar); - } - // .with_children(user.avatar.clone().map(|avatar| { - // Image::from_data(avatar) - // .with_style(theme.contact_avatar) - // .aligned() - // .left() - // })) - // .with_child( - // Label::new( - // user.github_login.clone(), - // theme.contact_username.text.clone(), - // ) - // .contained() - // .with_style(theme.contact_username.container) - // .aligned() - // .left() - // .flex(1., true), - // ); + let controls = if is_incoming { + vec![ + IconButton::new("remove_contact", Icon::Close) + .on_click(cx.listener(move |this, _, cx| { + this.respond_to_contact_request(user_id, false, cx); + })) + .color(color) + .tooltip(|cx| Tooltip::text("Decline invite", cx)), + IconButton::new("remove_contact", Icon::Check) + .on_click(cx.listener(move |this, _, cx| { + this.respond_to_contact_request(user_id, true, cx); + })) + .color(color) + .tooltip(|cx| Tooltip::text("Accept invite", cx)), + ] + } else { + let github_login = github_login.clone(); + vec![IconButton::new("remove_contact", Icon::Close) + .on_click(cx.listener(move |this, _, cx| { + this.remove_contact(user_id, &github_login, cx); + })) + .color(color) + .tooltip(|cx| Tooltip::text("Cancel invite", cx))] + }; - // let user_id = user.id; - // let github_login = user.github_login.clone(); - // let is_contact_request_pending = user_store.read(cx).is_contact_request_pending(&user); - // let button_spacing = theme.contact_button_spacing; - - // if is_incoming { - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/x.svg").aligned() - // }) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.respond_to_contact_request(user_id, false, cx); - // }) - // .contained() - // .with_margin_right(button_spacing), - // ); - - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/check.svg") - // .aligned() - // .flex_float() - // }) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.respond_to_contact_request(user_id, true, cx); - // }), - // ); - // } else { - // row.add_child( - // MouseEventHandler::new::(user.id as usize, cx, |mouse_state, _| { - // let button_style = if is_contact_request_pending { - // &theme.disabled_button - // } else { - // theme.contact_button.style_for(mouse_state) - // }; - // render_icon_button(button_style, "icons/x.svg") - // .aligned() - // .flex_float() - // }) - // .with_padding(Padding::uniform(2.)) - // .with_cursor_style(CursorStyle::PointingHand) - // .on_click(MouseButton::Left, move |_, this, cx| { - // this.remove_contact(user_id, &github_login, cx); - // }) - // .flex_float(), - // ); - // } - - // row.constrained() - // .with_height(theme.row_height) - // .contained() - // .with_style( - // *theme - // .contact_row - // .in_state(is_selected) - // .style_for(&mut Default::default()), - // ) - // .into_any() - item + ListItem::new(github_login.clone()) + .child( + h_stack() + .w_full() + .justify_between() + .child(Label::new(github_login.clone())) + .child(h_stack().children(controls)), + ) + .when_some(user.avatar.clone(), |el, avatar| el.left_avatar(avatar)) } fn render_contact_placeholder( @@ -2983,34 +2735,10 @@ impl CollabPanel { cx: &mut ViewContext, ) -> impl IntoElement { ListItem::new("contact-placeholder") + .child(IconElement::new(Icon::Plus)) .child(Label::new("Add a Contact")) - .on_click(cx.listener(|this, _, cx| todo!())) - // enum AddContacts {} - // MouseEventHandler::new::(0, cx, |state, _| { - // let style = theme.list_empty_state.style_for(is_selected, state); - // Flex::row() - // .with_child( - // Svg::new("icons/plus.svg") - // .with_color(theme.list_empty_icon.color) - // .constrained() - // .with_width(theme.list_empty_icon.width) - // .aligned() - // .left(), - // ) - // .with_child( - // Label::new("Add a contact", style.text.clone()) - // .contained() - // .with_style(theme.list_empty_label_container), - // ) - // .align_children_center() - // .contained() - // .with_style(style.container) - // .into_any() - // }) - // .on_click(MouseButton::Left, |_, this, cx| { - // this.toggle_contact_finder(cx); - // }) - // .into_any() + .selected(is_selected) + .on_click(cx.listener(|this, _, cx| this.toggle_contact_finder(cx))) } fn render_channel( @@ -3023,6 +2751,15 @@ impl CollabPanel { ) -> impl IntoElement { let channel_id = channel.id; + let is_active = maybe!({ + let call_channel = ActiveCall::global(cx) + .read(cx) + .room()? + .read(cx) + .channel_id()?; + Some(call_channel == channel_id) + }) + .unwrap_or(false); let is_public = self .channel_store .read(cx) @@ -3034,17 +2771,7 @@ impl CollabPanel { .then(|| !self.collapsed_channels.binary_search(&channel.id).is_ok()) .unwrap_or(false); - let is_active = maybe!({ - let call_channel = ActiveCall::global(cx) - .read(cx) - .room()? - .read(cx) - .channel_id()?; - Some(call_channel == channel_id) - }) - .unwrap_or(false); - - let has_messages_notification = channel.unseen_message_id.is_some() || true; + let has_messages_notification = channel.unseen_message_id.is_some(); let has_notes_notification = channel.unseen_note_version.is_some(); const FACEPILE_LIMIT: usize = 3; @@ -3052,6 +2779,7 @@ impl CollabPanel { let face_pile = if !participants.is_empty() { let extra_count = participants.len().saturating_sub(FACEPILE_LIMIT); + let user = &participants[0]; let result = FacePile { faces: participants @@ -3059,6 +2787,7 @@ impl CollabPanel { .filter_map(|user| Some(Avatar::data(user.avatar.clone()?).into_any_element())) .take(FACEPILE_LIMIT) .chain(if extra_count > 0 { + // todo!() @nate - this label looks wrong. Some(Label::new(format!("+{}", extra_count)).into_any_element()) } else { None @@ -3081,7 +2810,7 @@ impl CollabPanel { .w_full() .justify_between() .child( - div() + h_stack() .id(channel_id as usize) .child(Label::new(channel.name.clone())) .children(face_pile.map(|face_pile| face_pile.render(cx))) @@ -3092,11 +2821,10 @@ impl CollabPanel { .child( div() .id("channel_chat") - .bg(gpui::blue()) .when(!has_messages_notification, |el| el.invisible()) .group_hover("", |style| style.visible()) .child( - IconButton::new("test_chat", Icon::MessageBubbles) + IconButton::new("channel_chat", Icon::MessageBubbles) .color(if has_messages_notification { Color::Default } else { @@ -3111,20 +2839,16 @@ impl CollabPanel { .when(!has_notes_notification, |el| el.invisible()) .group_hover("", |style| style.visible()) .child( - div().child("Notes").id("test_notes").tooltip(|cx| { - Tooltip::text("Open channel notes", cx) - }), - ), // .child( - // IconButton::new("channel_notes", Icon::File) - // .color(if has_notes_notification { - // Color::Default - // } else { - // Color::Muted - // }) - // .tooltip(|cx| { - // Tooltip::text("Open channel notes", cx) - // }), - // ), + IconButton::new("channel_notes", Icon::File) + .color(if has_notes_notification { + Color::Default + } else { + Color::Muted + }) + .tooltip(|cx| { + Tooltip::text("Open channel notes", cx) + }), + ), ), ), ) @@ -3133,7 +2857,18 @@ impl CollabPanel { } else { Toggle::NotToggleable }) - .on_click(cx.listener(|this, _, cx| todo!())) + .on_toggle( + cx.listener(move |this, _, cx| this.toggle_channel_collapsed(channel_id, cx)), + ) + .on_click(cx.listener(move |this, _, cx| { + if this.drag_target_channel == ChannelDragTarget::None { + if is_active { + this.open_channel_notes(&OpenChannelNotes { channel_id }, cx) + } else { + this.join_channel(channel_id, cx) + } + } + })) .on_secondary_mouse_down(cx.listener(|this, _, cx| { todo!() // open context menu })), @@ -3445,6 +3180,32 @@ impl CollabPanel { // .with_cursor_style(CursorStyle::PointingHand) // .into_any() } + + fn render_channel_editor( + &mut self, + depth: usize, + cx: &mut ViewContext, + ) -> impl IntoElement { + let item = ListItem::new("channel-editor") + .inset(false) + .indent_level(depth) + .left_icon(Icon::Hash); + + if let Some(pending_name) = self + .channel_editing_state + .as_ref() + .and_then(|state| state.pending_name()) + { + item.child(Label::new(pending_name)) + } else { + item.child( + div() + .w_full() + .py_1() // todo!() @nate this is a px off at the default font size. + .child(self.channel_name_editor.clone()), + ) + } + } } // fn render_tree_branch( @@ -3499,6 +3260,8 @@ impl Render for CollabPanel { div() .key_context("CollabPanel") .track_focus(&self.focus_handle) + .on_action(cx.listener(Self::confirm)) + .on_action(cx.listener(Self::insert_space)) .map(|el| { if self.user_store.read(cx).current_user().is_none() { el.child(self.render_signed_out(cx)) diff --git a/crates/collab_ui2/src/collab_panel/contact_finder.rs b/crates/collab_ui2/src/collab_panel/contact_finder.rs index 3701b070d9..fee04ec40c 100644 --- a/crates/collab_ui2/src/collab_panel/contact_finder.rs +++ b/crates/collab_ui2/src/collab_panel/contact_finder.rs @@ -181,7 +181,6 @@ impl PickerDelegate for ContactFinderDelegate { ContactRequestStatus::RequestSent => Some("icons/x.svg"), ContactRequestStatus::RequestAccepted => None, }; - dbg!(icon_path); Some( div() .flex_1() diff --git a/crates/collab_ui2/src/collab_titlebar_item.rs b/crates/collab_ui2/src/collab_titlebar_item.rs index 2307ba2fcb..ac72176d67 100644 --- a/crates/collab_ui2/src/collab_titlebar_item.rs +++ b/crates/collab_ui2/src/collab_titlebar_item.rs @@ -37,7 +37,10 @@ use gpui::{ }; use project::Project; use theme::ActiveTheme; -use ui::{h_stack, Avatar, Button, ButtonVariant, Color, IconButton, KeyBinding, Tooltip}; +use ui::{ + h_stack, Avatar, Button, ButtonCommon, ButtonLike, ButtonVariant, Clickable, Color, IconButton, + IconElement, IconSize, KeyBinding, Tooltip, +}; use util::ResultExt; use workspace::{notifications::NotifyResultExt, Workspace}; @@ -298,6 +301,27 @@ impl Render for CollabTitlebarItem { }) .detach(); })) + // Temporary, will be removed when the last part of button2 is merged + .child( + div().border().border_color(gpui::blue()).child( + ButtonLike::new("test-button") + .children([ + Avatar::uri( + "https://avatars.githubusercontent.com/u/1714999?v=4", + ) + .into_element() + .into_any(), + IconElement::new(ui::Icon::ChevronDown) + .size(IconSize::Small) + .into_element() + .into_any(), + ]) + .on_click(move |event, _cx| { + dbg!(format!("clicked: {:?}", event.down.position)); + }) + .tooltip(|cx| Tooltip::text("Test tooltip", cx)), + ), + ) } }) } diff --git a/crates/command_palette2/src/command_palette.rs b/crates/command_palette2/src/command_palette.rs index cf39b4f29b..f9b58b1d56 100644 --- a/crates/command_palette2/src/command_palette.rs +++ b/crates/command_palette2/src/command_palette.rs @@ -1,3 +1,8 @@ +use std::{ + cmp::{self, Reverse}, + sync::Arc, +}; + use collections::{CommandPaletteFilter, HashMap}; use fuzzy::{StringMatch, StringMatchCandidate}; use gpui::{ @@ -5,10 +10,7 @@ use gpui::{ Keystroke, ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView, }; use picker::{Picker, PickerDelegate}; -use std::{ - cmp::{self, Reverse}, - sync::Arc, -}; + use ui::{h_stack, v_stack, HighlightedLabel, KeyBinding, ListItem}; use util::{ channel::{parse_zed_link, ReleaseChannel, RELEASE_CHANNEL}, diff --git a/crates/feature_flags2/src/feature_flags2.rs b/crates/feature_flags2/src/feature_flags2.rs index 23167796ec..065d06f96d 100644 --- a/crates/feature_flags2/src/feature_flags2.rs +++ b/crates/feature_flags2/src/feature_flags2.rs @@ -30,11 +30,11 @@ pub trait FeatureFlagViewExt { impl FeatureFlagViewExt for ViewContext<'_, V> where - V: 'static + Send + Sync, + V: 'static, { fn observe_flag(&mut self, callback: F) -> Subscription where - F: Fn(bool, &mut V, &mut ViewContext) + Send + Sync + 'static, + F: Fn(bool, &mut V, &mut ViewContext) + 'static, { self.observe_global::(move |v, cx| { let feature_flags = cx.global::(); diff --git a/crates/gpui2/src/action.rs b/crates/gpui2/src/action.rs index 958eaabdb8..03ef2d2281 100644 --- a/crates/gpui2/src/action.rs +++ b/crates/gpui2/src/action.rs @@ -162,6 +162,7 @@ macro_rules! actions { ( $name:ident ) => { #[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, gpui::serde_derive::Deserialize, gpui::Action)] + #[serde(crate = "gpui::serde")] pub struct $name; }; diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index b18ffb8ca6..3c8f678b89 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -111,7 +111,7 @@ pub struct Component { pub struct CompositeElementState { rendered_element: Option<::Element>, - rendered_element_state: <::Element as Element>::State, + rendered_element_state: Option<<::Element as Element>::State>, } impl Component { @@ -131,20 +131,40 @@ impl Element for Component { cx: &mut WindowContext, ) -> (LayoutId, Self::State) { let mut element = self.component.take().unwrap().render(cx).into_element(); - let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx); - let state = CompositeElementState { - rendered_element: Some(element), - rendered_element_state: state, - }; - (layout_id, state) + if let Some(element_id) = element.element_id() { + let layout_id = + cx.with_element_state(element_id, |state, cx| element.layout(state, cx)); + let state = CompositeElementState { + rendered_element: Some(element), + rendered_element_state: None, + }; + (layout_id, state) + } else { + let (layout_id, state) = + element.layout(state.and_then(|s| s.rendered_element_state), cx); + let state = CompositeElementState { + rendered_element: Some(element), + rendered_element_state: Some(state), + }; + (layout_id, state) + } } fn paint(self, bounds: Bounds, state: &mut Self::State, cx: &mut WindowContext) { - state - .rendered_element - .take() - .unwrap() - .paint(bounds, &mut state.rendered_element_state, cx); + let element = state.rendered_element.take().unwrap(); + if let Some(element_id) = element.element_id() { + cx.with_element_state(element_id, |element_state, cx| { + let mut element_state = element_state.unwrap(); + element.paint(bounds, &mut element_state, cx); + ((), element_state) + }); + } else { + element.paint( + bounds, + &mut state.rendered_element_state.as_mut().unwrap(), + cx, + ); + } } } diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs index 8e61f247bd..2d5a46f3d9 100644 --- a/crates/gpui2/src/elements/uniform_list.rs +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -173,7 +173,7 @@ impl Element for UniformList { let item_size = element_state.item_size; let content_size = Size { width: padded_bounds.size.width, - height: item_size.height * self.item_count, + height: item_size.height * self.item_count + padding.top + padding.bottom, }; let shared_scroll_offset = element_state @@ -221,9 +221,7 @@ impl Element for UniformList { let items = (self.render_items)(visible_range.clone(), cx); cx.with_z_index(1, |cx| { - let content_mask = ContentMask { - bounds: padded_bounds, - }; + let content_mask = ContentMask { bounds }; cx.with_content_mask(Some(content_mask), |cx| { for (item, ix) in items.into_iter().zip(visible_range) { let item_origin = padded_bounds.origin diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index f8326e9df8..507c46d067 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -1939,23 +1939,6 @@ pub trait BorrowWindow: BorrowMut + BorrowMut { }) } - /// Like `with_element_state`, but for situations where the element_id is optional. If the - /// id is `None`, no state will be retrieved or stored. - fn with_optional_element_state( - &mut self, - element_id: Option, - f: impl FnOnce(Option, &mut Self) -> (R, S), - ) -> R - where - S: 'static, - { - if let Some(element_id) = element_id { - self.with_element_state(element_id, f) - } else { - f(None, self).0 - } - } - /// Obtain the current content mask. fn content_mask(&self) -> ContentMask { self.window() diff --git a/crates/picker2/src/picker2.rs b/crates/picker2/src/picker2.rs index 8ab85a97ec..44056dabd1 100644 --- a/crates/picker2/src/picker2.rs +++ b/crates/picker2/src/picker2.rs @@ -1,7 +1,8 @@ use editor::Editor; use gpui::{ - div, prelude::*, uniform_list, AppContext, Div, FocusHandle, FocusableView, MouseButton, - MouseDownEvent, Render, Task, UniformListScrollHandle, View, ViewContext, WindowContext, + div, prelude::*, uniform_list, AnyElement, AppContext, Div, FocusHandle, FocusableView, + MouseButton, MouseDownEvent, Render, Task, UniformListScrollHandle, View, ViewContext, + WindowContext, }; use std::{cmp, sync::Arc}; use ui::{prelude::*, v_stack, Color, Divider, Label}; @@ -16,7 +17,6 @@ pub struct Picker { pub trait PickerDelegate: Sized + 'static { type ListItem: IntoElement; - fn match_count(&self) -> usize; fn selected_index(&self) -> usize; fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext>); @@ -205,7 +205,6 @@ impl Render for Picker { .when(self.delegate.match_count() > 0, |el| { el.child( v_stack() - .p_1() .grow() .child( uniform_list( @@ -239,7 +238,8 @@ impl Render for Picker { } }, ) - .track_scroll(self.scroll_handle.clone()), + .track_scroll(self.scroll_handle.clone()) + .p_1() ) .max_h_72() .overflow_hidden(), @@ -256,3 +256,22 @@ impl Render for Picker { }) } } + +pub fn simple_picker_match( + selected: bool, + cx: &mut WindowContext, + children: impl FnOnce(&mut WindowContext) -> AnyElement, +) -> AnyElement { + let colors = cx.theme().colors(); + + div() + .px_1() + .text_color(colors.text) + .text_ui() + .bg(colors.ghost_element_background) + .rounded_md() + .when(selected, |this| this.bg(colors.ghost_element_selected)) + .hover(|this| this.bg(colors.ghost_element_hover)) + .child((children)(cx)) + .into_any() +} diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rs index cb9d32d0b0..0886d68747 100644 --- a/crates/prettier/src/prettier.rs +++ b/crates/prettier/src/prettier.rs @@ -13,12 +13,14 @@ use node_runtime::NodeRuntime; use serde::{Deserialize, Serialize}; use util::paths::{PathMatcher, DEFAULT_PRETTIER_DIR}; +#[derive(Clone)] pub enum Prettier { Real(RealPrettier), #[cfg(any(test, feature = "test-support"))] Test(TestPrettier), } +#[derive(Clone)] pub struct RealPrettier { default: bool, prettier_dir: PathBuf, @@ -26,11 +28,13 @@ pub struct RealPrettier { } #[cfg(any(test, feature = "test-support"))] +#[derive(Clone)] pub struct TestPrettier { prettier_dir: PathBuf, default: bool, } +pub const FAIL_THRESHOLD: usize = 4; pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js"; pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); const PRETTIER_PACKAGE_NAME: &str = "prettier"; diff --git a/crates/prettier/src/prettier_server.js b/crates/prettier/src/prettier_server.js index 191431da0b..bf62e538dd 100644 --- a/crates/prettier/src/prettier_server.js +++ b/crates/prettier/src/prettier_server.js @@ -153,7 +153,10 @@ async function handleMessage(message, prettier) { const { method, id, params } = message; if (method === undefined) { throw new Error(`Message method is undefined: ${JSON.stringify(message)}`); + } else if (method == "initialized") { + return; } + if (id === undefined) { throw new Error(`Message id is undefined: ${JSON.stringify(message)}`); } diff --git a/crates/prettier2/src/prettier2.rs b/crates/prettier2/src/prettier2.rs index a01144ced3..61bcf9c9b3 100644 --- a/crates/prettier2/src/prettier2.rs +++ b/crates/prettier2/src/prettier2.rs @@ -13,12 +13,14 @@ use std::{ }; use util::paths::{PathMatcher, DEFAULT_PRETTIER_DIR}; +#[derive(Clone)] pub enum Prettier { Real(RealPrettier), #[cfg(any(test, feature = "test-support"))] Test(TestPrettier), } +#[derive(Clone)] pub struct RealPrettier { default: bool, prettier_dir: PathBuf, @@ -26,11 +28,13 @@ pub struct RealPrettier { } #[cfg(any(test, feature = "test-support"))] +#[derive(Clone)] pub struct TestPrettier { prettier_dir: PathBuf, default: bool, } +pub const FAIL_THRESHOLD: usize = 4; pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js"; pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); const PRETTIER_PACKAGE_NAME: &str = "prettier"; diff --git a/crates/prettier2/src/prettier_server.js b/crates/prettier2/src/prettier_server.js index 191431da0b..bf62e538dd 100644 --- a/crates/prettier2/src/prettier_server.js +++ b/crates/prettier2/src/prettier_server.js @@ -153,7 +153,10 @@ async function handleMessage(message, prettier) { const { method, id, params } = message; if (method === undefined) { throw new Error(`Message method is undefined: ${JSON.stringify(message)}`); + } else if (method == "initialized") { + return; } + if (id === undefined) { throw new Error(`Message id is undefined: ${JSON.stringify(message)}`); } diff --git a/crates/project/src/prettier_support.rs b/crates/project/src/prettier_support.rs new file mode 100644 index 0000000000..c438f294b6 --- /dev/null +++ b/crates/project/src/prettier_support.rs @@ -0,0 +1,758 @@ +use std::{ + ops::ControlFlow, + path::{Path, PathBuf}, + sync::Arc, +}; + +use anyhow::Context; +use collections::HashSet; +use fs::Fs; +use futures::{ + future::{self, Shared}, + FutureExt, +}; +use gpui::{AsyncAppContext, ModelContext, ModelHandle, Task}; +use language::{ + language_settings::{Formatter, LanguageSettings}, + Buffer, Language, LanguageServerName, LocalFile, +}; +use lsp::LanguageServerId; +use node_runtime::NodeRuntime; +use prettier::Prettier; +use util::{paths::DEFAULT_PRETTIER_DIR, ResultExt, TryFutureExt}; + +use crate::{ + Event, File, FormatOperation, PathChange, Project, ProjectEntryId, Worktree, WorktreeId, +}; + +pub fn prettier_plugins_for_language( + language: &Language, + language_settings: &LanguageSettings, +) -> Option> { + match &language_settings.formatter { + Formatter::Prettier { .. } | Formatter::Auto => {} + Formatter::LanguageServer | Formatter::External { .. } => return None, + }; + let mut prettier_plugins = None; + if language.prettier_parser_name().is_some() { + prettier_plugins + .get_or_insert_with(|| HashSet::default()) + .extend( + language + .lsp_adapters() + .iter() + .flat_map(|adapter| adapter.prettier_plugins()), + ) + } + + prettier_plugins +} + +pub(super) async fn format_with_prettier( + project: &ModelHandle, + buffer: &ModelHandle, + cx: &mut AsyncAppContext, +) -> Option { + if let Some((prettier_path, prettier_task)) = project + .update(cx, |project, cx| { + project.prettier_instance_for_buffer(buffer, cx) + }) + .await + { + match prettier_task.await { + Ok(prettier) => { + let buffer_path = buffer.update(cx, |buffer, cx| { + File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) + }); + match prettier.format(buffer, buffer_path, cx).await { + Ok(new_diff) => return Some(FormatOperation::Prettier(new_diff)), + Err(e) => { + log::error!( + "Prettier instance from {prettier_path:?} failed to format a buffer: {e:#}" + ); + } + } + } + Err(e) => project.update(cx, |project, _| { + let instance_to_update = match prettier_path { + Some(prettier_path) => { + log::error!( + "Prettier instance from path {prettier_path:?} failed to spawn: {e:#}" + ); + project.prettier_instances.get_mut(&prettier_path) + } + None => { + log::error!("Default prettier instance failed to spawn: {e:#}"); + match &mut project.default_prettier.prettier { + PrettierInstallation::NotInstalled { .. } => None, + PrettierInstallation::Installed(instance) => Some(instance), + } + } + }; + + if let Some(instance) = instance_to_update { + instance.attempt += 1; + instance.prettier = None; + } + }), + } + } + + None +} + +pub struct DefaultPrettier { + prettier: PrettierInstallation, + installed_plugins: HashSet<&'static str>, +} + +pub enum PrettierInstallation { + NotInstalled { + attempts: usize, + installation_task: Option>>>>, + not_installed_plugins: HashSet<&'static str>, + }, + Installed(PrettierInstance), +} + +pub type PrettierTask = Shared, Arc>>>; + +#[derive(Clone)] +pub struct PrettierInstance { + attempt: usize, + prettier: Option, +} + +impl Default for DefaultPrettier { + fn default() -> Self { + Self { + prettier: PrettierInstallation::NotInstalled { + attempts: 0, + installation_task: None, + not_installed_plugins: HashSet::default(), + }, + installed_plugins: HashSet::default(), + } + } +} + +impl DefaultPrettier { + pub fn instance(&self) -> Option<&PrettierInstance> { + if let PrettierInstallation::Installed(instance) = &self.prettier { + Some(instance) + } else { + None + } + } + + pub fn prettier_task( + &mut self, + node: &Arc, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, + ) -> Option>> { + match &mut self.prettier { + PrettierInstallation::NotInstalled { .. } => { + Some(start_default_prettier(Arc::clone(node), worktree_id, cx)) + } + PrettierInstallation::Installed(existing_instance) => { + existing_instance.prettier_task(node, None, worktree_id, cx) + } + } + } +} + +impl PrettierInstance { + pub fn prettier_task( + &mut self, + node: &Arc, + prettier_dir: Option<&Path>, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, + ) -> Option>> { + if self.attempt > prettier::FAIL_THRESHOLD { + match prettier_dir { + Some(prettier_dir) => log::warn!( + "Prettier from path {prettier_dir:?} exceeded launch threshold, not starting" + ), + None => log::warn!("Default prettier exceeded launch threshold, not starting"), + } + return None; + } + Some(match &self.prettier { + Some(prettier_task) => Task::ready(Ok(prettier_task.clone())), + None => match prettier_dir { + Some(prettier_dir) => { + let new_task = start_prettier( + Arc::clone(node), + prettier_dir.to_path_buf(), + worktree_id, + cx, + ); + self.attempt += 1; + self.prettier = Some(new_task.clone()); + Task::ready(Ok(new_task)) + } + None => { + self.attempt += 1; + let node = Arc::clone(node); + cx.spawn(|project, mut cx| async move { + project + .update(&mut cx, |_, cx| { + start_default_prettier(node, worktree_id, cx) + }) + .await + }) + } + }, + }) + } +} + +fn start_default_prettier( + node: Arc, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, +) -> Task> { + cx.spawn(|project, mut cx| async move { + loop { + let installation_task = project.update(&mut cx, |project, _| { + match &project.default_prettier.prettier { + PrettierInstallation::NotInstalled { + installation_task, .. + } => ControlFlow::Continue(installation_task.clone()), + PrettierInstallation::Installed(default_prettier) => { + ControlFlow::Break(default_prettier.clone()) + } + } + }); + match installation_task { + ControlFlow::Continue(None) => { + anyhow::bail!("Default prettier is not installed and cannot be started") + } + ControlFlow::Continue(Some(installation_task)) => { + log::info!("Waiting for default prettier to install"); + if let Err(e) = installation_task.await { + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { + installation_task, + attempts, + .. + } = &mut project.default_prettier.prettier + { + *installation_task = None; + *attempts += 1; + } + }); + anyhow::bail!( + "Cannot start default prettier due to its installation failure: {e:#}" + ); + } + let new_default_prettier = project.update(&mut cx, |project, cx| { + let new_default_prettier = + start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: Some(new_default_prettier.clone()), + }); + new_default_prettier + }); + return Ok(new_default_prettier); + } + ControlFlow::Break(instance) => match instance.prettier { + Some(instance) => return Ok(instance), + None => { + let new_default_prettier = project.update(&mut cx, |project, cx| { + let new_default_prettier = + start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: instance.attempt + 1, + prettier: Some(new_default_prettier.clone()), + }); + new_default_prettier + }); + return Ok(new_default_prettier); + } + }, + } + } + }) +} + +fn start_prettier( + node: Arc, + prettier_dir: PathBuf, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, +) -> PrettierTask { + cx.spawn(|project, mut cx| async move { + log::info!("Starting prettier at path {prettier_dir:?}"); + let new_server_id = project.update(&mut cx, |project, _| { + project.languages.next_language_server_id() + }); + + let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone()) + .await + .context("default prettier spawn") + .map(Arc::new) + .map_err(Arc::new)?; + register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx); + Ok(new_prettier) + }) + .shared() +} + +fn register_new_prettier( + project: &ModelHandle, + prettier: &Prettier, + worktree_id: Option, + new_server_id: LanguageServerId, + cx: &mut AsyncAppContext, +) { + let prettier_dir = prettier.prettier_dir(); + let is_default = prettier.is_default(); + if is_default { + log::info!("Started default prettier in {prettier_dir:?}"); + } else { + log::info!("Started prettier in {prettier_dir:?}"); + } + if let Some(prettier_server) = prettier.server() { + project.update(cx, |project, cx| { + let name = if is_default { + LanguageServerName(Arc::from("prettier (default)")) + } else { + let worktree_path = worktree_id + .and_then(|id| project.worktree_for_id(id, cx)) + .map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path())); + let name = match worktree_path { + Some(worktree_path) => { + if prettier_dir == worktree_path.as_ref() { + let name = prettier_dir + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or_default(); + format!("prettier ({name})") + } else { + let dir_to_display = prettier_dir + .strip_prefix(worktree_path.as_ref()) + .ok() + .unwrap_or(prettier_dir); + format!("prettier ({})", dir_to_display.display()) + } + } + None => format!("prettier ({})", prettier_dir.display()), + }; + LanguageServerName(Arc::from(name)) + }; + project + .supplementary_language_servers + .insert(new_server_id, (name, Arc::clone(prettier_server))); + cx.emit(Event::LanguageServerAdded(new_server_id)); + }); + } +} + +async fn install_prettier_packages( + plugins_to_install: HashSet<&'static str>, + node: Arc, +) -> anyhow::Result<()> { + let packages_to_versions = + future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map( + |package_name| async { + let returned_package_name = package_name.to_string(); + let latest_version = node + .npm_package_latest_version(package_name) + .await + .with_context(|| { + format!("fetching latest npm version for package {returned_package_name}") + })?; + anyhow::Ok((returned_package_name, latest_version)) + }, + )) + .await + .context("fetching latest npm versions")?; + + log::info!("Fetching default prettier and plugins: {packages_to_versions:?}"); + let borrowed_packages = packages_to_versions + .iter() + .map(|(package, version)| (package.as_str(), version.as_str())) + .collect::>(); + node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages) + .await + .context("fetching formatter packages")?; + anyhow::Ok(()) +} + +async fn save_prettier_server_file(fs: &dyn Fs) -> Result<(), anyhow::Error> { + let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE); + fs.save( + &prettier_wrapper_path, + &text::Rope::from(prettier::PRETTIER_SERVER_JS), + text::LineEnding::Unix, + ) + .await + .with_context(|| { + format!( + "writing {} file at {prettier_wrapper_path:?}", + prettier::PRETTIER_SERVER_FILE + ) + })?; + Ok(()) +} + +impl Project { + pub fn update_prettier_settings( + &self, + worktree: &ModelHandle, + changes: &[(Arc, ProjectEntryId, PathChange)], + cx: &mut ModelContext<'_, Project>, + ) { + let prettier_config_files = Prettier::CONFIG_FILE_NAMES + .iter() + .map(Path::new) + .collect::>(); + + let prettier_config_file_changed = changes + .iter() + .filter(|(_, _, change)| !matches!(change, PathChange::Loaded)) + .filter(|(path, _, _)| { + !path + .components() + .any(|component| component.as_os_str().to_string_lossy() == "node_modules") + }) + .find(|(path, _, _)| prettier_config_files.contains(path.as_ref())); + let current_worktree_id = worktree.read(cx).id(); + if let Some((config_path, _, _)) = prettier_config_file_changed { + log::info!( + "Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}" + ); + let prettiers_to_reload = + self.prettiers_per_worktree + .get(¤t_worktree_id) + .iter() + .flat_map(|prettier_paths| prettier_paths.iter()) + .flatten() + .filter_map(|prettier_path| { + Some(( + current_worktree_id, + Some(prettier_path.clone()), + self.prettier_instances.get(prettier_path)?.clone(), + )) + }) + .chain(self.default_prettier.instance().map(|default_prettier| { + (current_worktree_id, None, default_prettier.clone()) + })) + .collect::>(); + + cx.background() + .spawn(async move { + let _: Vec<()> = future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_instance)| { + async move { + if let Some(instance) = prettier_instance.prettier { + match instance.await { + Ok(prettier) => { + prettier.clear_cache().log_err().await; + }, + Err(e) => { + match prettier_path { + Some(prettier_path) => log::error!( + "Failed to clear prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update: {e:#}" + ), + None => log::error!( + "Failed to clear default prettier cache for worktree {worktree_id:?} on prettier settings update: {e:#}" + ), + } + }, + } + } + } + })) + .await; + }) + .detach(); + } + } + + fn prettier_instance_for_buffer( + &mut self, + buffer: &ModelHandle, + cx: &mut ModelContext, + ) -> Task, PrettierTask)>> { + let buffer = buffer.read(cx); + let buffer_file = buffer.file(); + let Some(buffer_language) = buffer.language() else { + return Task::ready(None); + }; + if buffer_language.prettier_parser_name().is_none() { + return Task::ready(None); + } + + if self.is_local() { + let Some(node) = self.node.as_ref().map(Arc::clone) else { + return Task::ready(None); + }; + match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx))) + { + Some((worktree_id, buffer_path)) => { + let fs = Arc::clone(&self.fs); + let installed_prettiers = self.prettier_instances.keys().cloned().collect(); + return cx.spawn(|project, mut cx| async move { + match cx + .background() + .spawn(async move { + Prettier::locate_prettier_installation( + fs.as_ref(), + &installed_prettiers, + &buffer_path, + ) + .await + }) + .await + { + Ok(ControlFlow::Break(())) => { + return None; + } + Ok(ControlFlow::Continue(None)) => { + let default_instance = project.update(&mut cx, |project, cx| { + project + .prettiers_per_worktree + .entry(worktree_id) + .or_default() + .insert(None); + project.default_prettier.prettier_task( + &node, + Some(worktree_id), + cx, + ) + }); + Some((None, default_instance?.log_err().await?)) + } + Ok(ControlFlow::Continue(Some(prettier_dir))) => { + project.update(&mut cx, |project, _| { + project + .prettiers_per_worktree + .entry(worktree_id) + .or_default() + .insert(Some(prettier_dir.clone())) + }); + if let Some(prettier_task) = + project.update(&mut cx, |project, cx| { + project.prettier_instances.get_mut(&prettier_dir).map( + |existing_instance| { + existing_instance.prettier_task( + &node, + Some(&prettier_dir), + Some(worktree_id), + cx, + ) + }, + ) + }) + { + log::debug!( + "Found already started prettier in {prettier_dir:?}" + ); + return Some(( + Some(prettier_dir), + prettier_task?.await.log_err()?, + )); + } + + log::info!("Found prettier in {prettier_dir:?}, starting."); + let new_prettier_task = project.update(&mut cx, |project, cx| { + let new_prettier_task = start_prettier( + node, + prettier_dir.clone(), + Some(worktree_id), + cx, + ); + project.prettier_instances.insert( + prettier_dir.clone(), + PrettierInstance { + attempt: 0, + prettier: Some(new_prettier_task.clone()), + }, + ); + new_prettier_task + }); + Some((Some(prettier_dir), new_prettier_task)) + } + Err(e) => { + log::error!("Failed to determine prettier path for buffer: {e:#}"); + return None; + } + } + }); + } + None => { + let new_task = self.default_prettier.prettier_task(&node, None, cx); + return cx + .spawn(|_, _| async move { Some((None, new_task?.log_err().await?)) }); + } + } + } else { + return Task::ready(None); + } + } + + #[cfg(any(test, feature = "test-support"))] + pub fn install_default_prettier( + &mut self, + _worktree: Option, + plugins: HashSet<&'static str>, + _cx: &mut ModelContext, + ) { + // suppress unused code warnings + let _ = install_prettier_packages; + let _ = save_prettier_server_file; + + self.default_prettier.installed_plugins.extend(plugins); + self.default_prettier.prettier = PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: None, + }); + } + + #[cfg(not(any(test, feature = "test-support")))] + pub fn install_default_prettier( + &mut self, + worktree: Option, + mut new_plugins: HashSet<&'static str>, + cx: &mut ModelContext, + ) { + let Some(node) = self.node.as_ref().cloned() else { + return; + }; + log::info!("Initializing default prettier with plugins {new_plugins:?}"); + let fs = Arc::clone(&self.fs); + let locate_prettier_installation = match worktree.and_then(|worktree_id| { + self.worktree_for_id(worktree_id, cx) + .map(|worktree| worktree.read(cx).abs_path()) + }) { + Some(locate_from) => { + let installed_prettiers = self.prettier_instances.keys().cloned().collect(); + cx.background().spawn(async move { + Prettier::locate_prettier_installation( + fs.as_ref(), + &installed_prettiers, + locate_from.as_ref(), + ) + .await + }) + } + None => Task::ready(Ok(ControlFlow::Continue(None))), + }; + new_plugins.retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin)); + let mut installation_attempt = 0; + let previous_installation_task = match &mut self.default_prettier.prettier { + PrettierInstallation::NotInstalled { + installation_task, + attempts, + not_installed_plugins, + } => { + installation_attempt = *attempts; + if installation_attempt > prettier::FAIL_THRESHOLD { + *installation_task = None; + log::warn!( + "Default prettier installation had failed {installation_attempt} times, not attempting again", + ); + return; + } + new_plugins.extend(not_installed_plugins.iter()); + installation_task.clone() + } + PrettierInstallation::Installed { .. } => { + if new_plugins.is_empty() { + return; + } + None + } + }; + + let plugins_to_install = new_plugins.clone(); + let fs = Arc::clone(&self.fs); + let new_installation_task = cx + .spawn(|project, mut cx| async move { + match locate_prettier_installation + .await + .context("locate prettier installation") + .map_err(Arc::new)? + { + ControlFlow::Break(()) => return Ok(()), + ControlFlow::Continue(prettier_path) => { + if prettier_path.is_some() { + new_plugins.clear(); + } + let mut needs_install = false; + if let Some(previous_installation_task) = previous_installation_task { + if let Err(e) = previous_installation_task.await { + log::error!("Failed to install default prettier: {e:#}"); + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { attempts, not_installed_plugins, .. } = &mut project.default_prettier.prettier { + *attempts += 1; + new_plugins.extend(not_installed_plugins.iter()); + installation_attempt = *attempts; + needs_install = true; + }; + }); + } + }; + if installation_attempt > prettier::FAIL_THRESHOLD { + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { installation_task, .. } = &mut project.default_prettier.prettier { + *installation_task = None; + }; + }); + log::warn!( + "Default prettier installation had failed {installation_attempt} times, not attempting again", + ); + return Ok(()); + } + project.update(&mut cx, |project, _| { + new_plugins.retain(|plugin| { + !project.default_prettier.installed_plugins.contains(plugin) + }); + if let PrettierInstallation::NotInstalled { not_installed_plugins, .. } = &mut project.default_prettier.prettier { + not_installed_plugins.retain(|plugin| { + !project.default_prettier.installed_plugins.contains(plugin) + }); + not_installed_plugins.extend(new_plugins.iter()); + } + needs_install |= !new_plugins.is_empty(); + }); + if needs_install { + let installed_plugins = new_plugins.clone(); + cx.background() + .spawn(async move { + save_prettier_server_file(fs.as_ref()).await?; + install_prettier_packages(new_plugins, node).await + }) + .await + .context("prettier & plugins install") + .map_err(Arc::new)?; + log::info!("Initialized prettier with plugins: {installed_plugins:?}"); + project.update(&mut cx, |project, _| { + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: None, + }); + project.default_prettier + .installed_plugins + .extend(installed_plugins); + }); + } + } + } + Ok(()) + }) + .shared(); + self.default_prettier.prettier = PrettierInstallation::NotInstalled { + attempts: installation_attempt, + installation_task: Some(new_installation_task), + not_installed_plugins: plugins_to_install, + }; + } +} diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index cf3fa547f6..a2ad82585e 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -1,5 +1,6 @@ mod ignore; mod lsp_command; +mod prettier_support; pub mod project_settings; pub mod search; pub mod terminals; @@ -20,7 +21,7 @@ use futures::{ mpsc::{self, UnboundedReceiver}, oneshot, }, - future::{self, try_join_all, Shared}, + future::{try_join_all, Shared}, stream::FuturesUnordered, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, }; @@ -31,9 +32,7 @@ use gpui::{ }; use itertools::Itertools; use language::{ - language_settings::{ - language_settings, FormatOnSave, Formatter, InlayHintKind, LanguageSettings, - }, + language_settings::{language_settings, FormatOnSave, Formatter, InlayHintKind}, point_to_lsp, proto::{ deserialize_anchor, deserialize_fingerprint, deserialize_line_ending, deserialize_version, @@ -54,7 +53,7 @@ use lsp_command::*; use node_runtime::NodeRuntime; use parking_lot::Mutex; use postage::watch; -use prettier::Prettier; +use prettier_support::{DefaultPrettier, PrettierInstance}; use project_settings::{LspSettings, ProjectSettings}; use rand::prelude::*; use search::SearchQuery; @@ -72,7 +71,7 @@ use std::{ hash::Hash, mem, num::NonZeroU32, - ops::{ControlFlow, Range}, + ops::Range, path::{self, Component, Path, PathBuf}, process::Stdio, str, @@ -85,11 +84,8 @@ use std::{ use terminals::Terminals; use text::Anchor; use util::{ - debug_panic, defer, - http::HttpClient, - merge_json_value_into, - paths::{DEFAULT_PRETTIER_DIR, LOCAL_SETTINGS_RELATIVE_PATH}, - post_inc, ResultExt, TryFutureExt as _, + debug_panic, defer, http::HttpClient, merge_json_value_into, + paths::LOCAL_SETTINGS_RELATIVE_PATH, post_inc, ResultExt, TryFutureExt as _, }; pub use fs::*; @@ -168,16 +164,9 @@ pub struct Project { copilot_log_subscription: Option, current_lsp_settings: HashMap, LspSettings>, node: Option>, - default_prettier: Option, + default_prettier: DefaultPrettier, prettiers_per_worktree: HashMap>>, - prettier_instances: HashMap, Arc>>>>, -} - -struct DefaultPrettier { - instance: Option, Arc>>>>, - installation_process: Option>>>>, - #[cfg(not(any(test, feature = "test-support")))] - installed_plugins: HashSet<&'static str>, + prettier_instances: HashMap, } struct DelayedDebounced { @@ -542,6 +531,14 @@ struct ProjectLspAdapterDelegate { http_client: Arc, } +// Currently, formatting operations are represented differently depending on +// whether they come from a language server or an external command. +enum FormatOperation { + Lsp(Vec<(Range, String)>), + External(Diff), + Prettier(Diff), +} + impl FormatTrigger { fn from_proto(value: i32) -> FormatTrigger { match value { @@ -690,7 +687,7 @@ impl Project { copilot_log_subscription: None, current_lsp_settings: settings::get::(cx).lsp.clone(), node: Some(node), - default_prettier: None, + default_prettier: DefaultPrettier::default(), prettiers_per_worktree: HashMap::default(), prettier_instances: HashMap::default(), } @@ -791,7 +788,7 @@ impl Project { copilot_log_subscription: None, current_lsp_settings: settings::get::(cx).lsp.clone(), node: None, - default_prettier: None, + default_prettier: DefaultPrettier::default(), prettiers_per_worktree: HashMap::default(), prettier_instances: HashMap::default(), }; @@ -928,8 +925,19 @@ impl Project { .detach(); } + let mut prettier_plugins_by_worktree = HashMap::default(); for (worktree, language, settings) in language_formatters_to_check { - self.install_default_formatters(worktree, &language, &settings, cx); + if let Some(plugins) = + prettier_support::prettier_plugins_for_language(&language, &settings) + { + prettier_plugins_by_worktree + .entry(worktree) + .or_insert_with(|| HashSet::default()) + .extend(plugins); + } + } + for (worktree, prettier_plugins) in prettier_plugins_by_worktree { + self.install_default_prettier(worktree, prettier_plugins, cx); } // Start all the newly-enabled language servers. @@ -2685,8 +2693,11 @@ impl Project { let settings = language_settings(Some(&new_language), buffer_file.as_ref(), cx).clone(); let buffer_file = File::from_dyn(buffer_file.as_ref()); let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx)); - - self.install_default_formatters(worktree, &new_language, &settings, cx); + if let Some(prettier_plugins) = + prettier_support::prettier_plugins_for_language(&new_language, &settings) + { + self.install_default_prettier(worktree, prettier_plugins, cx); + }; if let Some(file) = buffer_file { let worktree = file.worktree.clone(); if let Some(tree) = worktree.read(cx).as_local() { @@ -4073,8 +4084,6 @@ impl Project { let remove_trailing_whitespace = settings.remove_trailing_whitespace_on_save; let ensure_final_newline = settings.ensure_final_newline_on_save; - let format_on_save = settings.format_on_save.clone(); - let formatter = settings.formatter.clone(); let tab_size = settings.tab_size; // First, format buffer's whitespace according to the settings. @@ -4099,18 +4108,10 @@ impl Project { buffer.end_transaction(cx) }); - // Currently, formatting operations are represented differently depending on - // whether they come from a language server or an external command. - enum FormatOperation { - Lsp(Vec<(Range, String)>), - External(Diff), - Prettier(Diff), - } - // Apply language-specific formatting using either a language server // or external command. let mut format_operation = None; - match (formatter, format_on_save) { + match (&settings.formatter, &settings.format_on_save) { (_, FormatOnSave::Off) if trigger == FormatTrigger::Save => {} (Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off) @@ -4155,46 +4156,11 @@ impl Project { } } (Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => { - if let Some((prettier_path, prettier_task)) = project - .update(&mut cx, |project, cx| { - project.prettier_instance_for_buffer(buffer, cx) - }).await { - match prettier_task.await - { - Ok(prettier) => { - let buffer_path = buffer.update(&mut cx, |buffer, cx| { - File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) - }); - format_operation = Some(FormatOperation::Prettier( - prettier - .format(buffer, buffer_path, &cx) - .await - .context("formatting via prettier")?, - )); - } - Err(e) => { - project.update(&mut cx, |project, _| { - match &prettier_path { - Some(prettier_path) => { - project.prettier_instances.remove(prettier_path); - }, - None => { - if let Some(default_prettier) = project.default_prettier.as_mut() { - default_prettier.instance = None; - } - }, - } - }); - match &prettier_path { - Some(prettier_path) => { - log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}"); - }, - None => { - log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}"); - }, - } - } - } + if let Some(new_operation) = + prettier_support::format_with_prettier(&project, buffer, &mut cx) + .await + { + format_operation = Some(new_operation); } else if let Some((language_server, buffer_abs_path)) = language_server.as_ref().zip(buffer_abs_path.as_ref()) { @@ -4212,48 +4178,13 @@ impl Project { )); } } - (Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => { - if let Some((prettier_path, prettier_task)) = project - .update(&mut cx, |project, cx| { - project.prettier_instance_for_buffer(buffer, cx) - }).await { - match prettier_task.await - { - Ok(prettier) => { - let buffer_path = buffer.update(&mut cx, |buffer, cx| { - File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) - }); - format_operation = Some(FormatOperation::Prettier( - prettier - .format(buffer, buffer_path, &cx) - .await - .context("formatting via prettier")?, - )); - } - Err(e) => { - project.update(&mut cx, |project, _| { - match &prettier_path { - Some(prettier_path) => { - project.prettier_instances.remove(prettier_path); - }, - None => { - if let Some(default_prettier) = project.default_prettier.as_mut() { - default_prettier.instance = None; - } - }, - } - }); - match &prettier_path { - Some(prettier_path) => { - log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}"); - }, - None => { - log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}"); - }, - } - } - } - } + (Formatter::Prettier, FormatOnSave::On | FormatOnSave::Off) => { + if let Some(new_operation) = + prettier_support::format_with_prettier(&project, buffer, &mut cx) + .await + { + format_operation = Some(new_operation); + } } }; @@ -6566,85 +6497,6 @@ impl Project { .detach(); } - fn update_prettier_settings( - &self, - worktree: &ModelHandle, - changes: &[(Arc, ProjectEntryId, PathChange)], - cx: &mut ModelContext<'_, Project>, - ) { - let prettier_config_files = Prettier::CONFIG_FILE_NAMES - .iter() - .map(Path::new) - .collect::>(); - - let prettier_config_file_changed = changes - .iter() - .filter(|(_, _, change)| !matches!(change, PathChange::Loaded)) - .filter(|(path, _, _)| { - !path - .components() - .any(|component| component.as_os_str().to_string_lossy() == "node_modules") - }) - .find(|(path, _, _)| prettier_config_files.contains(path.as_ref())); - let current_worktree_id = worktree.read(cx).id(); - if let Some((config_path, _, _)) = prettier_config_file_changed { - log::info!( - "Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}" - ); - let prettiers_to_reload = self - .prettiers_per_worktree - .get(¤t_worktree_id) - .iter() - .flat_map(|prettier_paths| prettier_paths.iter()) - .flatten() - .filter_map(|prettier_path| { - Some(( - current_worktree_id, - Some(prettier_path.clone()), - self.prettier_instances.get(prettier_path)?.clone(), - )) - }) - .chain(self.default_prettier.iter().filter_map(|default_prettier| { - Some(( - current_worktree_id, - None, - default_prettier.instance.clone()?, - )) - })) - .collect::>(); - - cx.background() - .spawn(async move { - for task_result in future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_task)| { - async move { - prettier_task.await? - .clear_cache() - .await - .with_context(|| { - match prettier_path { - Some(prettier_path) => format!( - "clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update" - ), - None => format!( - "clearing default prettier cache for worktree {worktree_id:?} on prettier settings update" - ), - } - - }) - .map_err(Arc::new) - } - })) - .await - { - if let Err(e) = task_result { - log::error!("Failed to clear cache for prettier: {e:#}"); - } - } - }) - .detach(); - } - } - pub fn set_active_path(&mut self, entry: Option, cx: &mut ModelContext) { let new_active_entry = entry.and_then(|project_path| { let worktree = self.worktree_for_id(project_path.worktree_id, cx)?; @@ -8536,446 +8388,6 @@ impl Project { Vec::new() } } - - fn prettier_instance_for_buffer( - &mut self, - buffer: &ModelHandle, - cx: &mut ModelContext, - ) -> Task< - Option<( - Option, - Shared, Arc>>>, - )>, - > { - let buffer = buffer.read(cx); - let buffer_file = buffer.file(); - let Some(buffer_language) = buffer.language() else { - return Task::ready(None); - }; - if buffer_language.prettier_parser_name().is_none() { - return Task::ready(None); - } - - if self.is_local() { - let Some(node) = self.node.as_ref().map(Arc::clone) else { - return Task::ready(None); - }; - match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx))) - { - Some((worktree_id, buffer_path)) => { - let fs = Arc::clone(&self.fs); - let installed_prettiers = self.prettier_instances.keys().cloned().collect(); - return cx.spawn(|project, mut cx| async move { - match cx - .background() - .spawn(async move { - Prettier::locate_prettier_installation( - fs.as_ref(), - &installed_prettiers, - &buffer_path, - ) - .await - }) - .await - { - Ok(ControlFlow::Break(())) => { - return None; - } - Ok(ControlFlow::Continue(None)) => { - let started_default_prettier = - project.update(&mut cx, |project, _| { - project - .prettiers_per_worktree - .entry(worktree_id) - .or_default() - .insert(None); - project.default_prettier.as_ref().and_then( - |default_prettier| default_prettier.instance.clone(), - ) - }); - match started_default_prettier { - Some(old_task) => return Some((None, old_task)), - None => { - let new_default_prettier = project - .update(&mut cx, |_, cx| { - start_default_prettier(node, Some(worktree_id), cx) - }) - .await; - return Some((None, new_default_prettier)); - } - } - } - Ok(ControlFlow::Continue(Some(prettier_dir))) => { - project.update(&mut cx, |project, _| { - project - .prettiers_per_worktree - .entry(worktree_id) - .or_default() - .insert(Some(prettier_dir.clone())) - }); - if let Some(existing_prettier) = - project.update(&mut cx, |project, _| { - project.prettier_instances.get(&prettier_dir).cloned() - }) - { - log::debug!( - "Found already started prettier in {prettier_dir:?}" - ); - return Some((Some(prettier_dir), existing_prettier)); - } - - log::info!("Found prettier in {prettier_dir:?}, starting."); - let new_prettier_task = project.update(&mut cx, |project, cx| { - let new_prettier_task = start_prettier( - node, - prettier_dir.clone(), - Some(worktree_id), - cx, - ); - project - .prettier_instances - .insert(prettier_dir.clone(), new_prettier_task.clone()); - new_prettier_task - }); - Some((Some(prettier_dir), new_prettier_task)) - } - Err(e) => { - return Some(( - None, - Task::ready(Err(Arc::new( - e.context("determining prettier path"), - ))) - .shared(), - )); - } - } - }); - } - None => { - let started_default_prettier = self - .default_prettier - .as_ref() - .and_then(|default_prettier| default_prettier.instance.clone()); - match started_default_prettier { - Some(old_task) => return Task::ready(Some((None, old_task))), - None => { - let new_task = start_default_prettier(node, None, cx); - return cx.spawn(|_, _| async move { Some((None, new_task.await)) }); - } - } - } - } - } else if self.remote_id().is_some() { - return Task::ready(None); - } else { - Task::ready(Some(( - None, - Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(), - ))) - } - } - - #[cfg(any(test, feature = "test-support"))] - fn install_default_formatters( - &mut self, - _worktree: Option, - _new_language: &Language, - _language_settings: &LanguageSettings, - _cx: &mut ModelContext, - ) { - } - - #[cfg(not(any(test, feature = "test-support")))] - fn install_default_formatters( - &mut self, - worktree: Option, - new_language: &Language, - language_settings: &LanguageSettings, - cx: &mut ModelContext, - ) { - match &language_settings.formatter { - Formatter::Prettier { .. } | Formatter::Auto => {} - Formatter::LanguageServer | Formatter::External { .. } => return, - }; - let Some(node) = self.node.as_ref().cloned() else { - return; - }; - - let mut prettier_plugins = None; - if new_language.prettier_parser_name().is_some() { - prettier_plugins - .get_or_insert_with(|| HashSet::<&'static str>::default()) - .extend( - new_language - .lsp_adapters() - .iter() - .flat_map(|adapter| adapter.prettier_plugins()), - ) - } - let Some(prettier_plugins) = prettier_plugins else { - return; - }; - - let fs = Arc::clone(&self.fs); - let locate_prettier_installation = match worktree.and_then(|worktree_id| { - self.worktree_for_id(worktree_id, cx) - .map(|worktree| worktree.read(cx).abs_path()) - }) { - Some(locate_from) => { - let installed_prettiers = self.prettier_instances.keys().cloned().collect(); - cx.background().spawn(async move { - Prettier::locate_prettier_installation( - fs.as_ref(), - &installed_prettiers, - locate_from.as_ref(), - ) - .await - }) - } - None => Task::ready(Ok(ControlFlow::Break(()))), - }; - let mut plugins_to_install = prettier_plugins; - let previous_installation_process = - if let Some(default_prettier) = &mut self.default_prettier { - plugins_to_install - .retain(|plugin| !default_prettier.installed_plugins.contains(plugin)); - if plugins_to_install.is_empty() { - return; - } - default_prettier.installation_process.clone() - } else { - None - }; - let fs = Arc::clone(&self.fs); - let default_prettier = self - .default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: None, - installed_plugins: HashSet::default(), - }); - default_prettier.installation_process = Some( - cx.spawn(|this, mut cx| async move { - match locate_prettier_installation - .await - .context("locate prettier installation") - .map_err(Arc::new)? - { - ControlFlow::Break(()) => return Ok(()), - ControlFlow::Continue(Some(_non_default_prettier)) => return Ok(()), - ControlFlow::Continue(None) => { - let mut needs_install = match previous_installation_process { - Some(previous_installation_process) => { - previous_installation_process.await.is_err() - } - None => true, - }; - this.update(&mut cx, |this, _| { - if let Some(default_prettier) = &mut this.default_prettier { - plugins_to_install.retain(|plugin| { - !default_prettier.installed_plugins.contains(plugin) - }); - needs_install |= !plugins_to_install.is_empty(); - } - }); - if needs_install { - let installed_plugins = plugins_to_install.clone(); - cx.background() - .spawn(async move { - install_default_prettier(plugins_to_install, node, fs).await - }) - .await - .context("prettier & plugins install") - .map_err(Arc::new)?; - this.update(&mut cx, |this, _| { - let default_prettier = - this.default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: Some( - Task::ready(Ok(())).shared(), - ), - installed_plugins: HashSet::default(), - }); - default_prettier.instance = None; - default_prettier.installed_plugins.extend(installed_plugins); - }); - } - } - } - Ok(()) - }) - .shared(), - ); - } -} - -fn start_default_prettier( - node: Arc, - worktree_id: Option, - cx: &mut ModelContext<'_, Project>, -) -> Task, Arc>>>> { - cx.spawn(|project, mut cx| async move { - loop { - let default_prettier_installing = project.update(&mut cx, |project, _| { - project - .default_prettier - .as_ref() - .and_then(|default_prettier| default_prettier.installation_process.clone()) - }); - match default_prettier_installing { - Some(installation_task) => { - if installation_task.await.is_ok() { - break; - } - } - None => break, - } - } - - project.update(&mut cx, |project, cx| { - match project - .default_prettier - .as_mut() - .and_then(|default_prettier| default_prettier.instance.as_mut()) - { - Some(default_prettier) => default_prettier.clone(), - None => { - let new_default_prettier = - start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); - project - .default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: None, - #[cfg(not(any(test, feature = "test-support")))] - installed_plugins: HashSet::default(), - }) - .instance = Some(new_default_prettier.clone()); - new_default_prettier - } - } - }) - }) -} - -fn start_prettier( - node: Arc, - prettier_dir: PathBuf, - worktree_id: Option, - cx: &mut ModelContext<'_, Project>, -) -> Shared, Arc>>> { - cx.spawn(|project, mut cx| async move { - let new_server_id = project.update(&mut cx, |project, _| { - project.languages.next_language_server_id() - }); - let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone()) - .await - .context("default prettier spawn") - .map(Arc::new) - .map_err(Arc::new)?; - register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx); - Ok(new_prettier) - }) - .shared() -} - -fn register_new_prettier( - project: &ModelHandle, - prettier: &Prettier, - worktree_id: Option, - new_server_id: LanguageServerId, - cx: &mut AsyncAppContext, -) { - let prettier_dir = prettier.prettier_dir(); - let is_default = prettier.is_default(); - if is_default { - log::info!("Started default prettier in {prettier_dir:?}"); - } else { - log::info!("Started prettier in {prettier_dir:?}"); - } - if let Some(prettier_server) = prettier.server() { - project.update(cx, |project, cx| { - let name = if is_default { - LanguageServerName(Arc::from("prettier (default)")) - } else { - let worktree_path = worktree_id - .and_then(|id| project.worktree_for_id(id, cx)) - .map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path())); - let name = match worktree_path { - Some(worktree_path) => { - if prettier_dir == worktree_path.as_ref() { - let name = prettier_dir - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or_default(); - format!("prettier ({name})") - } else { - let dir_to_display = prettier_dir - .strip_prefix(worktree_path.as_ref()) - .ok() - .unwrap_or(prettier_dir); - format!("prettier ({})", dir_to_display.display()) - } - } - None => format!("prettier ({})", prettier_dir.display()), - }; - LanguageServerName(Arc::from(name)) - }; - project - .supplementary_language_servers - .insert(new_server_id, (name, Arc::clone(prettier_server))); - cx.emit(Event::LanguageServerAdded(new_server_id)); - }); - } -} - -#[cfg(not(any(test, feature = "test-support")))] -async fn install_default_prettier( - plugins_to_install: HashSet<&'static str>, - node: Arc, - fs: Arc, -) -> anyhow::Result<()> { - let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE); - // method creates parent directory if it doesn't exist - fs.save( - &prettier_wrapper_path, - &text::Rope::from(prettier::PRETTIER_SERVER_JS), - text::LineEnding::Unix, - ) - .await - .with_context(|| { - format!( - "writing {} file at {prettier_wrapper_path:?}", - prettier::PRETTIER_SERVER_FILE - ) - })?; - - let packages_to_versions = - future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map( - |package_name| async { - let returned_package_name = package_name.to_string(); - let latest_version = node - .npm_package_latest_version(package_name) - .await - .with_context(|| { - format!("fetching latest npm version for package {returned_package_name}") - })?; - anyhow::Ok((returned_package_name, latest_version)) - }, - )) - .await - .context("fetching latest npm versions")?; - - log::info!("Fetching default prettier and plugins: {packages_to_versions:?}"); - let borrowed_packages = packages_to_versions - .iter() - .map(|(package, version)| (package.as_str(), version.as_str())) - .collect::>(); - node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages) - .await - .context("fetching formatter packages")?; - anyhow::Ok(()) } fn subscribe_for_copilot_events( diff --git a/crates/project2/src/prettier_support.rs b/crates/project2/src/prettier_support.rs new file mode 100644 index 0000000000..c176c79a91 --- /dev/null +++ b/crates/project2/src/prettier_support.rs @@ -0,0 +1,772 @@ +use std::{ + ops::ControlFlow, + path::{Path, PathBuf}, + sync::Arc, +}; + +use anyhow::Context; +use collections::HashSet; +use fs::Fs; +use futures::{ + future::{self, Shared}, + FutureExt, +}; +use gpui::{AsyncAppContext, Model, ModelContext, Task, WeakModel}; +use language::{ + language_settings::{Formatter, LanguageSettings}, + Buffer, Language, LanguageServerName, LocalFile, +}; +use lsp::LanguageServerId; +use node_runtime::NodeRuntime; +use prettier::Prettier; +use util::{paths::DEFAULT_PRETTIER_DIR, ResultExt, TryFutureExt}; + +use crate::{ + Event, File, FormatOperation, PathChange, Project, ProjectEntryId, Worktree, WorktreeId, +}; + +pub fn prettier_plugins_for_language( + language: &Language, + language_settings: &LanguageSettings, +) -> Option> { + match &language_settings.formatter { + Formatter::Prettier { .. } | Formatter::Auto => {} + Formatter::LanguageServer | Formatter::External { .. } => return None, + }; + let mut prettier_plugins = None; + if language.prettier_parser_name().is_some() { + prettier_plugins + .get_or_insert_with(|| HashSet::default()) + .extend( + language + .lsp_adapters() + .iter() + .flat_map(|adapter| adapter.prettier_plugins()), + ) + } + + prettier_plugins +} + +pub(super) async fn format_with_prettier( + project: &WeakModel, + buffer: &Model, + cx: &mut AsyncAppContext, +) -> Option { + if let Some((prettier_path, prettier_task)) = project + .update(cx, |project, cx| { + project.prettier_instance_for_buffer(buffer, cx) + }) + .ok()? + .await + { + match prettier_task.await { + Ok(prettier) => { + let buffer_path = buffer + .update(cx, |buffer, cx| { + File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) + }) + .ok()?; + match prettier.format(buffer, buffer_path, cx).await { + Ok(new_diff) => return Some(FormatOperation::Prettier(new_diff)), + Err(e) => { + log::error!( + "Prettier instance from {prettier_path:?} failed to format a buffer: {e:#}" + ); + } + } + } + Err(e) => project + .update(cx, |project, _| { + let instance_to_update = match prettier_path { + Some(prettier_path) => { + log::error!( + "Prettier instance from path {prettier_path:?} failed to spawn: {e:#}" + ); + project.prettier_instances.get_mut(&prettier_path) + } + None => { + log::error!("Default prettier instance failed to spawn: {e:#}"); + match &mut project.default_prettier.prettier { + PrettierInstallation::NotInstalled { .. } => None, + PrettierInstallation::Installed(instance) => Some(instance), + } + } + }; + + if let Some(instance) = instance_to_update { + instance.attempt += 1; + instance.prettier = None; + } + }) + .ok()?, + } + } + + None +} + +pub struct DefaultPrettier { + prettier: PrettierInstallation, + installed_plugins: HashSet<&'static str>, +} + +pub enum PrettierInstallation { + NotInstalled { + attempts: usize, + installation_task: Option>>>>, + not_installed_plugins: HashSet<&'static str>, + }, + Installed(PrettierInstance), +} + +pub type PrettierTask = Shared, Arc>>>; + +#[derive(Clone)] +pub struct PrettierInstance { + attempt: usize, + prettier: Option, +} + +impl Default for DefaultPrettier { + fn default() -> Self { + Self { + prettier: PrettierInstallation::NotInstalled { + attempts: 0, + installation_task: None, + not_installed_plugins: HashSet::default(), + }, + installed_plugins: HashSet::default(), + } + } +} + +impl DefaultPrettier { + pub fn instance(&self) -> Option<&PrettierInstance> { + if let PrettierInstallation::Installed(instance) = &self.prettier { + Some(instance) + } else { + None + } + } + + pub fn prettier_task( + &mut self, + node: &Arc, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, + ) -> Option>> { + match &mut self.prettier { + PrettierInstallation::NotInstalled { .. } => { + Some(start_default_prettier(Arc::clone(node), worktree_id, cx)) + } + PrettierInstallation::Installed(existing_instance) => { + existing_instance.prettier_task(node, None, worktree_id, cx) + } + } + } +} + +impl PrettierInstance { + pub fn prettier_task( + &mut self, + node: &Arc, + prettier_dir: Option<&Path>, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, + ) -> Option>> { + if self.attempt > prettier::FAIL_THRESHOLD { + match prettier_dir { + Some(prettier_dir) => log::warn!( + "Prettier from path {prettier_dir:?} exceeded launch threshold, not starting" + ), + None => log::warn!("Default prettier exceeded launch threshold, not starting"), + } + return None; + } + Some(match &self.prettier { + Some(prettier_task) => Task::ready(Ok(prettier_task.clone())), + None => match prettier_dir { + Some(prettier_dir) => { + let new_task = start_prettier( + Arc::clone(node), + prettier_dir.to_path_buf(), + worktree_id, + cx, + ); + self.attempt += 1; + self.prettier = Some(new_task.clone()); + Task::ready(Ok(new_task)) + } + None => { + self.attempt += 1; + let node = Arc::clone(node); + cx.spawn(|project, mut cx| async move { + project + .update(&mut cx, |_, cx| { + start_default_prettier(node, worktree_id, cx) + })? + .await + }) + } + }, + }) + } +} + +fn start_default_prettier( + node: Arc, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, +) -> Task> { + cx.spawn(|project, mut cx| async move { + loop { + let installation_task = project.update(&mut cx, |project, _| { + match &project.default_prettier.prettier { + PrettierInstallation::NotInstalled { + installation_task, .. + } => ControlFlow::Continue(installation_task.clone()), + PrettierInstallation::Installed(default_prettier) => { + ControlFlow::Break(default_prettier.clone()) + } + } + })?; + match installation_task { + ControlFlow::Continue(None) => { + anyhow::bail!("Default prettier is not installed and cannot be started") + } + ControlFlow::Continue(Some(installation_task)) => { + log::info!("Waiting for default prettier to install"); + if let Err(e) = installation_task.await { + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { + installation_task, + attempts, + .. + } = &mut project.default_prettier.prettier + { + *installation_task = None; + *attempts += 1; + } + })?; + anyhow::bail!( + "Cannot start default prettier due to its installation failure: {e:#}" + ); + } + let new_default_prettier = project.update(&mut cx, |project, cx| { + let new_default_prettier = + start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: Some(new_default_prettier.clone()), + }); + new_default_prettier + })?; + return Ok(new_default_prettier); + } + ControlFlow::Break(instance) => match instance.prettier { + Some(instance) => return Ok(instance), + None => { + let new_default_prettier = project.update(&mut cx, |project, cx| { + let new_default_prettier = + start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: instance.attempt + 1, + prettier: Some(new_default_prettier.clone()), + }); + new_default_prettier + })?; + return Ok(new_default_prettier); + } + }, + } + } + }) +} + +fn start_prettier( + node: Arc, + prettier_dir: PathBuf, + worktree_id: Option, + cx: &mut ModelContext<'_, Project>, +) -> PrettierTask { + cx.spawn(|project, mut cx| async move { + log::info!("Starting prettier at path {prettier_dir:?}"); + let new_server_id = project.update(&mut cx, |project, _| { + project.languages.next_language_server_id() + })?; + + let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone()) + .await + .context("default prettier spawn") + .map(Arc::new) + .map_err(Arc::new)?; + register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx); + Ok(new_prettier) + }) + .shared() +} + +fn register_new_prettier( + project: &WeakModel, + prettier: &Prettier, + worktree_id: Option, + new_server_id: LanguageServerId, + cx: &mut AsyncAppContext, +) { + let prettier_dir = prettier.prettier_dir(); + let is_default = prettier.is_default(); + if is_default { + log::info!("Started default prettier in {prettier_dir:?}"); + } else { + log::info!("Started prettier in {prettier_dir:?}"); + } + if let Some(prettier_server) = prettier.server() { + project + .update(cx, |project, cx| { + let name = if is_default { + LanguageServerName(Arc::from("prettier (default)")) + } else { + let worktree_path = worktree_id + .and_then(|id| project.worktree_for_id(id, cx)) + .map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path())); + let name = match worktree_path { + Some(worktree_path) => { + if prettier_dir == worktree_path.as_ref() { + let name = prettier_dir + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or_default(); + format!("prettier ({name})") + } else { + let dir_to_display = prettier_dir + .strip_prefix(worktree_path.as_ref()) + .ok() + .unwrap_or(prettier_dir); + format!("prettier ({})", dir_to_display.display()) + } + } + None => format!("prettier ({})", prettier_dir.display()), + }; + LanguageServerName(Arc::from(name)) + }; + project + .supplementary_language_servers + .insert(new_server_id, (name, Arc::clone(prettier_server))); + cx.emit(Event::LanguageServerAdded(new_server_id)); + }) + .ok(); + } +} + +async fn install_prettier_packages( + plugins_to_install: HashSet<&'static str>, + node: Arc, +) -> anyhow::Result<()> { + let packages_to_versions = + future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map( + |package_name| async { + let returned_package_name = package_name.to_string(); + let latest_version = node + .npm_package_latest_version(package_name) + .await + .with_context(|| { + format!("fetching latest npm version for package {returned_package_name}") + })?; + anyhow::Ok((returned_package_name, latest_version)) + }, + )) + .await + .context("fetching latest npm versions")?; + + log::info!("Fetching default prettier and plugins: {packages_to_versions:?}"); + let borrowed_packages = packages_to_versions + .iter() + .map(|(package, version)| (package.as_str(), version.as_str())) + .collect::>(); + node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages) + .await + .context("fetching formatter packages")?; + anyhow::Ok(()) +} + +async fn save_prettier_server_file(fs: &dyn Fs) -> Result<(), anyhow::Error> { + let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE); + fs.save( + &prettier_wrapper_path, + &text::Rope::from(prettier::PRETTIER_SERVER_JS), + text::LineEnding::Unix, + ) + .await + .with_context(|| { + format!( + "writing {} file at {prettier_wrapper_path:?}", + prettier::PRETTIER_SERVER_FILE + ) + })?; + Ok(()) +} + +impl Project { + pub fn update_prettier_settings( + &self, + worktree: &Model, + changes: &[(Arc, ProjectEntryId, PathChange)], + cx: &mut ModelContext<'_, Project>, + ) { + let prettier_config_files = Prettier::CONFIG_FILE_NAMES + .iter() + .map(Path::new) + .collect::>(); + + let prettier_config_file_changed = changes + .iter() + .filter(|(_, _, change)| !matches!(change, PathChange::Loaded)) + .filter(|(path, _, _)| { + !path + .components() + .any(|component| component.as_os_str().to_string_lossy() == "node_modules") + }) + .find(|(path, _, _)| prettier_config_files.contains(path.as_ref())); + let current_worktree_id = worktree.read(cx).id(); + if let Some((config_path, _, _)) = prettier_config_file_changed { + log::info!( + "Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}" + ); + let prettiers_to_reload = + self.prettiers_per_worktree + .get(¤t_worktree_id) + .iter() + .flat_map(|prettier_paths| prettier_paths.iter()) + .flatten() + .filter_map(|prettier_path| { + Some(( + current_worktree_id, + Some(prettier_path.clone()), + self.prettier_instances.get(prettier_path)?.clone(), + )) + }) + .chain(self.default_prettier.instance().map(|default_prettier| { + (current_worktree_id, None, default_prettier.clone()) + })) + .collect::>(); + + cx.background_executor() + .spawn(async move { + let _: Vec<()> = future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_instance)| { + async move { + if let Some(instance) = prettier_instance.prettier { + match instance.await { + Ok(prettier) => { + prettier.clear_cache().log_err().await; + }, + Err(e) => { + match prettier_path { + Some(prettier_path) => log::error!( + "Failed to clear prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update: {e:#}" + ), + None => log::error!( + "Failed to clear default prettier cache for worktree {worktree_id:?} on prettier settings update: {e:#}" + ), + } + }, + } + } + } + })) + .await; + }) + .detach(); + } + } + + fn prettier_instance_for_buffer( + &mut self, + buffer: &Model, + cx: &mut ModelContext, + ) -> Task, PrettierTask)>> { + let buffer = buffer.read(cx); + let buffer_file = buffer.file(); + let Some(buffer_language) = buffer.language() else { + return Task::ready(None); + }; + if buffer_language.prettier_parser_name().is_none() { + return Task::ready(None); + } + + if self.is_local() { + let Some(node) = self.node.as_ref().map(Arc::clone) else { + return Task::ready(None); + }; + match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx))) + { + Some((worktree_id, buffer_path)) => { + let fs = Arc::clone(&self.fs); + let installed_prettiers = self.prettier_instances.keys().cloned().collect(); + return cx.spawn(|project, mut cx| async move { + match cx + .background_executor() + .spawn(async move { + Prettier::locate_prettier_installation( + fs.as_ref(), + &installed_prettiers, + &buffer_path, + ) + .await + }) + .await + { + Ok(ControlFlow::Break(())) => { + return None; + } + Ok(ControlFlow::Continue(None)) => { + let default_instance = project + .update(&mut cx, |project, cx| { + project + .prettiers_per_worktree + .entry(worktree_id) + .or_default() + .insert(None); + project.default_prettier.prettier_task( + &node, + Some(worktree_id), + cx, + ) + }) + .ok()?; + Some((None, default_instance?.log_err().await?)) + } + Ok(ControlFlow::Continue(Some(prettier_dir))) => { + project + .update(&mut cx, |project, _| { + project + .prettiers_per_worktree + .entry(worktree_id) + .or_default() + .insert(Some(prettier_dir.clone())) + }) + .ok()?; + if let Some(prettier_task) = project + .update(&mut cx, |project, cx| { + project.prettier_instances.get_mut(&prettier_dir).map( + |existing_instance| { + existing_instance.prettier_task( + &node, + Some(&prettier_dir), + Some(worktree_id), + cx, + ) + }, + ) + }) + .ok()? + { + log::debug!( + "Found already started prettier in {prettier_dir:?}" + ); + return Some(( + Some(prettier_dir), + prettier_task?.await.log_err()?, + )); + } + + log::info!("Found prettier in {prettier_dir:?}, starting."); + let new_prettier_task = project + .update(&mut cx, |project, cx| { + let new_prettier_task = start_prettier( + node, + prettier_dir.clone(), + Some(worktree_id), + cx, + ); + project.prettier_instances.insert( + prettier_dir.clone(), + PrettierInstance { + attempt: 0, + prettier: Some(new_prettier_task.clone()), + }, + ); + new_prettier_task + }) + .ok()?; + Some((Some(prettier_dir), new_prettier_task)) + } + Err(e) => { + log::error!("Failed to determine prettier path for buffer: {e:#}"); + return None; + } + } + }); + } + None => { + let new_task = self.default_prettier.prettier_task(&node, None, cx); + return cx + .spawn(|_, _| async move { Some((None, new_task?.log_err().await?)) }); + } + } + } else { + return Task::ready(None); + } + } + + #[cfg(any(test, feature = "test-support"))] + pub fn install_default_prettier( + &mut self, + _worktree: Option, + plugins: HashSet<&'static str>, + _cx: &mut ModelContext, + ) { + // suppress unused code warnings + let _ = install_prettier_packages; + let _ = save_prettier_server_file; + + self.default_prettier.installed_plugins.extend(plugins); + self.default_prettier.prettier = PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: None, + }); + } + + #[cfg(not(any(test, feature = "test-support")))] + pub fn install_default_prettier( + &mut self, + worktree: Option, + mut new_plugins: HashSet<&'static str>, + cx: &mut ModelContext, + ) { + let Some(node) = self.node.as_ref().cloned() else { + return; + }; + log::info!("Initializing default prettier with plugins {new_plugins:?}"); + let fs = Arc::clone(&self.fs); + let locate_prettier_installation = match worktree.and_then(|worktree_id| { + self.worktree_for_id(worktree_id, cx) + .map(|worktree| worktree.read(cx).abs_path()) + }) { + Some(locate_from) => { + let installed_prettiers = self.prettier_instances.keys().cloned().collect(); + cx.background_executor().spawn(async move { + Prettier::locate_prettier_installation( + fs.as_ref(), + &installed_prettiers, + locate_from.as_ref(), + ) + .await + }) + } + None => Task::ready(Ok(ControlFlow::Continue(None))), + }; + new_plugins.retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin)); + let mut installation_attempt = 0; + let previous_installation_task = match &mut self.default_prettier.prettier { + PrettierInstallation::NotInstalled { + installation_task, + attempts, + not_installed_plugins, + } => { + installation_attempt = *attempts; + if installation_attempt > prettier::FAIL_THRESHOLD { + *installation_task = None; + log::warn!( + "Default prettier installation had failed {installation_attempt} times, not attempting again", + ); + return; + } + new_plugins.extend(not_installed_plugins.iter()); + installation_task.clone() + } + PrettierInstallation::Installed { .. } => { + if new_plugins.is_empty() { + return; + } + None + } + }; + + let plugins_to_install = new_plugins.clone(); + let fs = Arc::clone(&self.fs); + let new_installation_task = cx + .spawn(|project, mut cx| async move { + match locate_prettier_installation + .await + .context("locate prettier installation") + .map_err(Arc::new)? + { + ControlFlow::Break(()) => return Ok(()), + ControlFlow::Continue(prettier_path) => { + if prettier_path.is_some() { + new_plugins.clear(); + } + let mut needs_install = false; + if let Some(previous_installation_task) = previous_installation_task { + if let Err(e) = previous_installation_task.await { + log::error!("Failed to install default prettier: {e:#}"); + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { attempts, not_installed_plugins, .. } = &mut project.default_prettier.prettier { + *attempts += 1; + new_plugins.extend(not_installed_plugins.iter()); + installation_attempt = *attempts; + needs_install = true; + }; + })?; + } + }; + if installation_attempt > prettier::FAIL_THRESHOLD { + project.update(&mut cx, |project, _| { + if let PrettierInstallation::NotInstalled { installation_task, .. } = &mut project.default_prettier.prettier { + *installation_task = None; + }; + })?; + log::warn!( + "Default prettier installation had failed {installation_attempt} times, not attempting again", + ); + return Ok(()); + } + project.update(&mut cx, |project, _| { + new_plugins.retain(|plugin| { + !project.default_prettier.installed_plugins.contains(plugin) + }); + if let PrettierInstallation::NotInstalled { not_installed_plugins, .. } = &mut project.default_prettier.prettier { + not_installed_plugins.retain(|plugin| { + !project.default_prettier.installed_plugins.contains(plugin) + }); + not_installed_plugins.extend(new_plugins.iter()); + } + needs_install |= !new_plugins.is_empty(); + })?; + if needs_install { + let installed_plugins = new_plugins.clone(); + cx.background_executor() + .spawn(async move { + save_prettier_server_file(fs.as_ref()).await?; + install_prettier_packages(new_plugins, node).await + }) + .await + .context("prettier & plugins install") + .map_err(Arc::new)?; + log::info!("Initialized prettier with plugins: {installed_plugins:?}"); + project.update(&mut cx, |project, _| { + project.default_prettier.prettier = + PrettierInstallation::Installed(PrettierInstance { + attempt: 0, + prettier: None, + }); + project.default_prettier + .installed_plugins + .extend(installed_plugins); + })?; + } + } + } + Ok(()) + }) + .shared(); + self.default_prettier.prettier = PrettierInstallation::NotInstalled { + attempts: installation_attempt, + installation_task: Some(new_installation_task), + not_installed_plugins: plugins_to_install, + }; + } +} diff --git a/crates/project2/src/project2.rs b/crates/project2/src/project2.rs index 856c280ac0..d2cc4fe406 100644 --- a/crates/project2/src/project2.rs +++ b/crates/project2/src/project2.rs @@ -1,5 +1,6 @@ mod ignore; mod lsp_command; +mod prettier_support; pub mod project_settings; pub mod search; pub mod terminals; @@ -20,7 +21,7 @@ use futures::{ mpsc::{self, UnboundedReceiver}, oneshot, }, - future::{self, try_join_all, Shared}, + future::{try_join_all, Shared}, stream::FuturesUnordered, AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt, }; @@ -31,9 +32,7 @@ use gpui::{ }; use itertools::Itertools; use language::{ - language_settings::{ - language_settings, FormatOnSave, Formatter, InlayHintKind, LanguageSettings, - }, + language_settings::{language_settings, FormatOnSave, Formatter, InlayHintKind}, point_to_lsp, proto::{ deserialize_anchor, deserialize_fingerprint, deserialize_line_ending, deserialize_version, @@ -54,7 +53,7 @@ use lsp_command::*; use node_runtime::NodeRuntime; use parking_lot::Mutex; use postage::watch; -use prettier::Prettier; +use prettier_support::{DefaultPrettier, PrettierInstance}; use project_settings::{LspSettings, ProjectSettings}; use rand::prelude::*; use search::SearchQuery; @@ -70,7 +69,7 @@ use std::{ hash::Hash, mem, num::NonZeroU32, - ops::{ControlFlow, Range}, + ops::Range, path::{self, Component, Path, PathBuf}, process::Stdio, str, @@ -83,11 +82,8 @@ use std::{ use terminals::Terminals; use text::Anchor; use util::{ - debug_panic, defer, - http::HttpClient, - merge_json_value_into, - paths::{DEFAULT_PRETTIER_DIR, LOCAL_SETTINGS_RELATIVE_PATH}, - post_inc, ResultExt, TryFutureExt as _, + debug_panic, defer, http::HttpClient, merge_json_value_into, + paths::LOCAL_SETTINGS_RELATIVE_PATH, post_inc, ResultExt, TryFutureExt as _, }; pub use fs::*; @@ -166,16 +162,9 @@ pub struct Project { copilot_log_subscription: Option, current_lsp_settings: HashMap, LspSettings>, node: Option>, - default_prettier: Option, + default_prettier: DefaultPrettier, prettiers_per_worktree: HashMap>>, - prettier_instances: HashMap, Arc>>>>, -} - -struct DefaultPrettier { - instance: Option, Arc>>>>, - installation_process: Option>>>>, - #[cfg(not(any(test, feature = "test-support")))] - installed_plugins: HashSet<&'static str>, + prettier_instances: HashMap, } struct DelayedDebounced { @@ -540,6 +529,14 @@ struct ProjectLspAdapterDelegate { http_client: Arc, } +// Currently, formatting operations are represented differently depending on +// whether they come from a language server or an external command. +enum FormatOperation { + Lsp(Vec<(Range, String)>), + External(Diff), + Prettier(Diff), +} + impl FormatTrigger { fn from_proto(value: i32) -> FormatTrigger { match value { @@ -689,7 +686,7 @@ impl Project { copilot_log_subscription: None, current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(), node: Some(node), - default_prettier: None, + default_prettier: DefaultPrettier::default(), prettiers_per_worktree: HashMap::default(), prettier_instances: HashMap::default(), } @@ -792,7 +789,7 @@ impl Project { copilot_log_subscription: None, current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(), node: None, - default_prettier: None, + default_prettier: DefaultPrettier::default(), prettiers_per_worktree: HashMap::default(), prettier_instances: HashMap::default(), }; @@ -965,8 +962,19 @@ impl Project { .detach(); } + let mut prettier_plugins_by_worktree = HashMap::default(); for (worktree, language, settings) in language_formatters_to_check { - self.install_default_formatters(worktree, &language, &settings, cx); + if let Some(plugins) = + prettier_support::prettier_plugins_for_language(&language, &settings) + { + prettier_plugins_by_worktree + .entry(worktree) + .or_insert_with(|| HashSet::default()) + .extend(plugins); + } + } + for (worktree, prettier_plugins) in prettier_plugins_by_worktree { + self.install_default_prettier(worktree, prettier_plugins, cx); } // Start all the newly-enabled language servers. @@ -2722,8 +2730,11 @@ impl Project { let settings = language_settings(Some(&new_language), buffer_file.as_ref(), cx).clone(); let buffer_file = File::from_dyn(buffer_file.as_ref()); let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx)); - - self.install_default_formatters(worktree, &new_language, &settings, cx); + if let Some(prettier_plugins) = + prettier_support::prettier_plugins_for_language(&new_language, &settings) + { + self.install_default_prettier(worktree, prettier_plugins, cx); + }; if let Some(file) = buffer_file { let worktree = file.worktree.clone(); if let Some(tree) = worktree.read(cx).as_local() { @@ -4126,7 +4137,8 @@ impl Project { this.buffers_being_formatted .remove(&buffer.read(cx).remote_id()); } - }).ok(); + }) + .ok(); } }); @@ -4138,8 +4150,6 @@ impl Project { let remove_trailing_whitespace = settings.remove_trailing_whitespace_on_save; let ensure_final_newline = settings.ensure_final_newline_on_save; - let format_on_save = settings.format_on_save.clone(); - let formatter = settings.formatter.clone(); let tab_size = settings.tab_size; // First, format buffer's whitespace according to the settings. @@ -4164,18 +4174,10 @@ impl Project { buffer.end_transaction(cx) })?; - // Currently, formatting operations are represented differently depending on - // whether they come from a language server or an external command. - enum FormatOperation { - Lsp(Vec<(Range, String)>), - External(Diff), - Prettier(Diff), - } - // Apply language-specific formatting using either a language server // or external command. let mut format_operation = None; - match (formatter, format_on_save) { + match (&settings.formatter, &settings.format_on_save) { (_, FormatOnSave::Off) if trigger == FormatTrigger::Save => {} (Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off) @@ -4220,46 +4222,11 @@ impl Project { } } (Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => { - if let Some((prettier_path, prettier_task)) = project - .update(&mut cx, |project, cx| { - project.prettier_instance_for_buffer(buffer, cx) - })?.await { - match prettier_task.await - { - Ok(prettier) => { - let buffer_path = buffer.update(&mut cx, |buffer, cx| { - File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) - })?; - format_operation = Some(FormatOperation::Prettier( - prettier - .format(buffer, buffer_path, &mut cx) - .await - .context("formatting via prettier")?, - )); - } - Err(e) => { - project.update(&mut cx, |project, _| { - match &prettier_path { - Some(prettier_path) => { - project.prettier_instances.remove(prettier_path); - }, - None => { - if let Some(default_prettier) = project.default_prettier.as_mut() { - default_prettier.instance = None; - } - }, - } - })?; - match &prettier_path { - Some(prettier_path) => { - log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}"); - }, - None => { - log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}"); - }, - } - } - } + if let Some(new_operation) = + prettier_support::format_with_prettier(&project, buffer, &mut cx) + .await + { + format_operation = Some(new_operation); } else if let Some((language_server, buffer_abs_path)) = language_server.as_ref().zip(buffer_abs_path.as_ref()) { @@ -4277,48 +4244,13 @@ impl Project { )); } } - (Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => { - if let Some((prettier_path, prettier_task)) = project - .update(&mut cx, |project, cx| { - project.prettier_instance_for_buffer(buffer, cx) - })?.await { - match prettier_task.await - { - Ok(prettier) => { - let buffer_path = buffer.update(&mut cx, |buffer, cx| { - File::from_dyn(buffer.file()).map(|file| file.abs_path(cx)) - })?; - format_operation = Some(FormatOperation::Prettier( - prettier - .format(buffer, buffer_path, &mut cx) - .await - .context("formatting via prettier")?, - )); - } - Err(e) => { - project.update(&mut cx, |project, _| { - match &prettier_path { - Some(prettier_path) => { - project.prettier_instances.remove(prettier_path); - }, - None => { - if let Some(default_prettier) = project.default_prettier.as_mut() { - default_prettier.instance = None; - } - }, - } - })?; - match &prettier_path { - Some(prettier_path) => { - log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}"); - }, - None => { - log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}"); - }, - } - } - } - } + (Formatter::Prettier, FormatOnSave::On | FormatOnSave::Off) => { + if let Some(new_operation) = + prettier_support::format_with_prettier(&project, buffer, &mut cx) + .await + { + format_operation = Some(new_operation); + } } }; @@ -6638,84 +6570,6 @@ impl Project { .detach(); } - fn update_prettier_settings( - &self, - worktree: &Model, - changes: &[(Arc, ProjectEntryId, PathChange)], - cx: &mut ModelContext<'_, Project>, - ) { - let prettier_config_files = Prettier::CONFIG_FILE_NAMES - .iter() - .map(Path::new) - .collect::>(); - - let prettier_config_file_changed = changes - .iter() - .filter(|(_, _, change)| !matches!(change, PathChange::Loaded)) - .filter(|(path, _, _)| { - !path - .components() - .any(|component| component.as_os_str().to_string_lossy() == "node_modules") - }) - .find(|(path, _, _)| prettier_config_files.contains(path.as_ref())); - let current_worktree_id = worktree.read(cx).id(); - if let Some((config_path, _, _)) = prettier_config_file_changed { - log::info!( - "Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}" - ); - let prettiers_to_reload = self - .prettiers_per_worktree - .get(¤t_worktree_id) - .iter() - .flat_map(|prettier_paths| prettier_paths.iter()) - .flatten() - .filter_map(|prettier_path| { - Some(( - current_worktree_id, - Some(prettier_path.clone()), - self.prettier_instances.get(prettier_path)?.clone(), - )) - }) - .chain(self.default_prettier.iter().filter_map(|default_prettier| { - Some(( - current_worktree_id, - None, - default_prettier.instance.clone()?, - )) - })) - .collect::>(); - - cx.background_executor() - .spawn(async move { - for task_result in future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_task)| { - async move { - prettier_task.await? - .clear_cache() - .await - .with_context(|| { - match prettier_path { - Some(prettier_path) => format!( - "clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update" - ), - None => format!( - "clearing default prettier cache for worktree {worktree_id:?} on prettier settings update" - ), - } - }) - .map_err(Arc::new) - } - })) - .await - { - if let Err(e) = task_result { - log::error!("Failed to clear cache for prettier: {e:#}"); - } - } - }) - .detach(); - } - } - pub fn set_active_path(&mut self, entry: Option, cx: &mut ModelContext) { let new_active_entry = entry.and_then(|project_path| { let worktree = self.worktree_for_id(project_path.worktree_id, cx)?; @@ -8579,486 +8433,6 @@ impl Project { Vec::new() } } - - fn prettier_instance_for_buffer( - &mut self, - buffer: &Model, - cx: &mut ModelContext, - ) -> Task< - Option<( - Option, - Shared, Arc>>>, - )>, - > { - let buffer = buffer.read(cx); - let buffer_file = buffer.file(); - let Some(buffer_language) = buffer.language() else { - return Task::ready(None); - }; - if buffer_language.prettier_parser_name().is_none() { - return Task::ready(None); - } - - if self.is_local() { - let Some(node) = self.node.as_ref().map(Arc::clone) else { - return Task::ready(None); - }; - match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx))) - { - Some((worktree_id, buffer_path)) => { - let fs = Arc::clone(&self.fs); - let installed_prettiers = self.prettier_instances.keys().cloned().collect(); - return cx.spawn(|project, mut cx| async move { - match cx - .background_executor() - .spawn(async move { - Prettier::locate_prettier_installation( - fs.as_ref(), - &installed_prettiers, - &buffer_path, - ) - .await - }) - .await - { - Ok(ControlFlow::Break(())) => { - return None; - } - Ok(ControlFlow::Continue(None)) => { - match project.update(&mut cx, |project, _| { - project - .prettiers_per_worktree - .entry(worktree_id) - .or_default() - .insert(None); - project.default_prettier.as_ref().and_then( - |default_prettier| default_prettier.instance.clone(), - ) - }) { - Ok(Some(old_task)) => Some((None, old_task)), - Ok(None) => { - match project.update(&mut cx, |_, cx| { - start_default_prettier(node, Some(worktree_id), cx) - }) { - Ok(new_default_prettier) => { - return Some((None, new_default_prettier.await)) - } - Err(e) => { - Some(( - None, - Task::ready(Err(Arc::new(e.context("project is gone during default prettier startup")))) - .shared(), - )) - } - } - } - Err(e) => Some((None, Task::ready(Err(Arc::new(e.context("project is gone during default prettier checks")))) - .shared())), - } - } - Ok(ControlFlow::Continue(Some(prettier_dir))) => { - match project.update(&mut cx, |project, _| { - project - .prettiers_per_worktree - .entry(worktree_id) - .or_default() - .insert(Some(prettier_dir.clone())); - project.prettier_instances.get(&prettier_dir).cloned() - }) { - Ok(Some(existing_prettier)) => { - log::debug!( - "Found already started prettier in {prettier_dir:?}" - ); - return Some((Some(prettier_dir), existing_prettier)); - } - Err(e) => { - return Some(( - Some(prettier_dir), - Task::ready(Err(Arc::new(e.context("project is gone during custom prettier checks")))) - .shared(), - )) - } - _ => {}, - } - - log::info!("Found prettier in {prettier_dir:?}, starting."); - let new_prettier_task = - match project.update(&mut cx, |project, cx| { - let new_prettier_task = start_prettier( - node, - prettier_dir.clone(), - Some(worktree_id), - cx, - ); - project.prettier_instances.insert( - prettier_dir.clone(), - new_prettier_task.clone(), - ); - new_prettier_task - }) { - Ok(task) => task, - Err(e) => return Some(( - Some(prettier_dir), - Task::ready(Err(Arc::new(e.context("project is gone during custom prettier startup")))) - .shared() - )), - }; - Some((Some(prettier_dir), new_prettier_task)) - } - Err(e) => { - return Some(( - None, - Task::ready(Err(Arc::new( - e.context("determining prettier path"), - ))) - .shared(), - )); - } - } - }); - } - None => { - let started_default_prettier = self - .default_prettier - .as_ref() - .and_then(|default_prettier| default_prettier.instance.clone()); - match started_default_prettier { - Some(old_task) => return Task::ready(Some((None, old_task))), - None => { - let new_task = start_default_prettier(node, None, cx); - return cx.spawn(|_, _| async move { Some((None, new_task.await)) }); - } - } - } - } - } else if self.remote_id().is_some() { - return Task::ready(None); - } else { - Task::ready(Some(( - None, - Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(), - ))) - } - } - - #[cfg(any(test, feature = "test-support"))] - fn install_default_formatters( - &mut self, - _: Option, - _: &Language, - _: &LanguageSettings, - _: &mut ModelContext, - ) { - } - - #[cfg(not(any(test, feature = "test-support")))] - fn install_default_formatters( - &mut self, - worktree: Option, - new_language: &Language, - language_settings: &LanguageSettings, - cx: &mut ModelContext, - ) { - match &language_settings.formatter { - Formatter::Prettier { .. } | Formatter::Auto => {} - Formatter::LanguageServer | Formatter::External { .. } => return, - }; - let Some(node) = self.node.as_ref().cloned() else { - return; - }; - - let mut prettier_plugins = None; - if new_language.prettier_parser_name().is_some() { - prettier_plugins - .get_or_insert_with(|| HashSet::<&'static str>::default()) - .extend( - new_language - .lsp_adapters() - .iter() - .flat_map(|adapter| adapter.prettier_plugins()), - ) - } - let Some(prettier_plugins) = prettier_plugins else { - return; - }; - - let fs = Arc::clone(&self.fs); - let locate_prettier_installation = match worktree.and_then(|worktree_id| { - self.worktree_for_id(worktree_id, cx) - .map(|worktree| worktree.read(cx).abs_path()) - }) { - Some(locate_from) => { - let installed_prettiers = self.prettier_instances.keys().cloned().collect(); - cx.background_executor().spawn(async move { - Prettier::locate_prettier_installation( - fs.as_ref(), - &installed_prettiers, - locate_from.as_ref(), - ) - .await - }) - } - None => Task::ready(Ok(ControlFlow::Break(()))), - }; - let mut plugins_to_install = prettier_plugins; - let previous_installation_process = - if let Some(default_prettier) = &mut self.default_prettier { - plugins_to_install - .retain(|plugin| !default_prettier.installed_plugins.contains(plugin)); - if plugins_to_install.is_empty() { - return; - } - default_prettier.installation_process.clone() - } else { - None - }; - - let fs = Arc::clone(&self.fs); - let default_prettier = self - .default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: None, - installed_plugins: HashSet::default(), - }); - default_prettier.installation_process = Some( - cx.spawn(|this, mut cx| async move { - match locate_prettier_installation - .await - .context("locate prettier installation") - .map_err(Arc::new)? - { - ControlFlow::Break(()) => return Ok(()), - ControlFlow::Continue(Some(_non_default_prettier)) => return Ok(()), - ControlFlow::Continue(None) => { - let mut needs_install = match previous_installation_process { - Some(previous_installation_process) => { - previous_installation_process.await.is_err() - } - None => true, - }; - this.update(&mut cx, |this, _| { - if let Some(default_prettier) = &mut this.default_prettier { - plugins_to_install.retain(|plugin| { - !default_prettier.installed_plugins.contains(plugin) - }); - needs_install |= !plugins_to_install.is_empty(); - } - })?; - if needs_install { - let installed_plugins = plugins_to_install.clone(); - cx.background_executor() - .spawn(async move { - install_default_prettier(plugins_to_install, node, fs).await - }) - .await - .context("prettier & plugins install") - .map_err(Arc::new)?; - this.update(&mut cx, |this, _| { - let default_prettier = - this.default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: Some( - Task::ready(Ok(())).shared(), - ), - installed_plugins: HashSet::default(), - }); - default_prettier.instance = None; - default_prettier.installed_plugins.extend(installed_plugins); - })?; - } - } - } - Ok(()) - }) - .shared(), - ); - } -} - -fn start_default_prettier( - node: Arc, - worktree_id: Option, - cx: &mut ModelContext<'_, Project>, -) -> Task, Arc>>>> { - cx.spawn(|project, mut cx| async move { - loop { - let default_prettier_installing = match project.update(&mut cx, |project, _| { - project - .default_prettier - .as_ref() - .and_then(|default_prettier| default_prettier.installation_process.clone()) - }) { - Ok(installation) => installation, - Err(e) => { - return Task::ready(Err(Arc::new( - e.context("project is gone during default prettier installation"), - ))) - .shared() - } - }; - match default_prettier_installing { - Some(installation_task) => { - if installation_task.await.is_ok() { - break; - } - } - None => break, - } - } - - match project.update(&mut cx, |project, cx| { - match project - .default_prettier - .as_mut() - .and_then(|default_prettier| default_prettier.instance.as_mut()) - { - Some(default_prettier) => default_prettier.clone(), - None => { - let new_default_prettier = - start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx); - project - .default_prettier - .get_or_insert_with(|| DefaultPrettier { - instance: None, - installation_process: None, - #[cfg(not(any(test, feature = "test-support")))] - installed_plugins: HashSet::default(), - }) - .instance = Some(new_default_prettier.clone()); - new_default_prettier - } - } - }) { - Ok(task) => task, - Err(e) => Task::ready(Err(Arc::new( - e.context("project is gone during default prettier startup"), - ))) - .shared(), - } - }) -} - -fn start_prettier( - node: Arc, - prettier_dir: PathBuf, - worktree_id: Option, - cx: &mut ModelContext<'_, Project>, -) -> Shared, Arc>>> { - cx.spawn(|project, mut cx| async move { - let new_server_id = project.update(&mut cx, |project, _| { - project.languages.next_language_server_id() - })?; - let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone()) - .await - .context("default prettier spawn") - .map(Arc::new) - .map_err(Arc::new)?; - register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx); - Ok(new_prettier) - }) - .shared() -} - -fn register_new_prettier( - project: &WeakModel, - prettier: &Prettier, - worktree_id: Option, - new_server_id: LanguageServerId, - cx: &mut AsyncAppContext, -) { - let prettier_dir = prettier.prettier_dir(); - let is_default = prettier.is_default(); - if is_default { - log::info!("Started default prettier in {prettier_dir:?}"); - } else { - log::info!("Started prettier in {prettier_dir:?}"); - } - if let Some(prettier_server) = prettier.server() { - project - .update(cx, |project, cx| { - let name = if is_default { - LanguageServerName(Arc::from("prettier (default)")) - } else { - let worktree_path = worktree_id - .and_then(|id| project.worktree_for_id(id, cx)) - .map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path())); - let name = match worktree_path { - Some(worktree_path) => { - if prettier_dir == worktree_path.as_ref() { - let name = prettier_dir - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or_default(); - format!("prettier ({name})") - } else { - let dir_to_display = prettier_dir - .strip_prefix(worktree_path.as_ref()) - .ok() - .unwrap_or(prettier_dir); - format!("prettier ({})", dir_to_display.display()) - } - } - None => format!("prettier ({})", prettier_dir.display()), - }; - LanguageServerName(Arc::from(name)) - }; - project - .supplementary_language_servers - .insert(new_server_id, (name, Arc::clone(prettier_server))); - cx.emit(Event::LanguageServerAdded(new_server_id)); - }) - .ok(); - } -} - -#[cfg(not(any(test, feature = "test-support")))] -async fn install_default_prettier( - plugins_to_install: HashSet<&'static str>, - node: Arc, - fs: Arc, -) -> anyhow::Result<()> { - let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE); - // method creates parent directory if it doesn't exist - fs.save( - &prettier_wrapper_path, - &text::Rope::from(prettier::PRETTIER_SERVER_JS), - text::LineEnding::Unix, - ) - .await - .with_context(|| { - format!( - "writing {} file at {prettier_wrapper_path:?}", - prettier::PRETTIER_SERVER_FILE - ) - })?; - - let packages_to_versions = - future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map( - |package_name| async { - let returned_package_name = package_name.to_string(); - let latest_version = node - .npm_package_latest_version(package_name) - .await - .with_context(|| { - format!("fetching latest npm version for package {returned_package_name}") - })?; - anyhow::Ok((returned_package_name, latest_version)) - }, - )) - .await - .context("fetching latest npm versions")?; - - log::info!("Fetching default prettier and plugins: {packages_to_versions:?}"); - let borrowed_packages = packages_to_versions - .iter() - .map(|(package, version)| (package.as_str(), version.as_str())) - .collect::>(); - node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages) - .await - .context("fetching formatter packages")?; - anyhow::Ok(()) } fn subscribe_for_copilot_events( diff --git a/crates/project_panel2/src/file_associations.rs b/crates/project_panel2/src/file_associations.rs index 9e9a865f3e..82aebe7913 100644 --- a/crates/project_panel2/src/file_associations.rs +++ b/crates/project_panel2/src/file_associations.rs @@ -41,56 +41,47 @@ impl FileAssociations { }) } - pub fn get_icon(path: &Path, cx: &AppContext) -> Arc { + pub fn get_icon(path: &Path, cx: &AppContext) -> Option> { + let this = cx.has_global::().then(|| cx.global::())?; + + // FIXME: Associate a type with the languages and have the file's langauge + // override these associations maybe!({ - let this = cx.has_global::().then(|| cx.global::())?; + let suffix = path.icon_suffix()?; - // FIXME: Associate a type with the languages and have the file's langauge - // override these associations - maybe!({ - let suffix = path.icon_suffix()?; - - this.suffixes - .get(suffix) - .and_then(|type_str| this.types.get(type_str)) - .map(|type_config| type_config.icon.clone()) - }) - .or_else(|| this.types.get("default").map(|config| config.icon.clone())) - }) - .unwrap_or_else(|| Arc::from("".to_string())) - } - - pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Arc { - maybe!({ - let this = cx.has_global::().then(|| cx.global::())?; - - let key = if expanded { - EXPANDED_DIRECTORY_TYPE - } else { - COLLAPSED_DIRECTORY_TYPE - }; - - this.types - .get(key) + this.suffixes + .get(suffix) + .and_then(|type_str| this.types.get(type_str)) .map(|type_config| type_config.icon.clone()) }) - .unwrap_or_else(|| Arc::from("".to_string())) + .or_else(|| this.types.get("default").map(|config| config.icon.clone())) } - pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Arc { - maybe!({ - let this = cx.has_global::().then(|| cx.global::())?; + pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option> { + let this = cx.has_global::().then(|| cx.global::())?; - let key = if expanded { - EXPANDED_CHEVRON_TYPE - } else { - COLLAPSED_CHEVRON_TYPE - }; + let key = if expanded { + EXPANDED_DIRECTORY_TYPE + } else { + COLLAPSED_DIRECTORY_TYPE + }; - this.types - .get(key) - .map(|type_config| type_config.icon.clone()) - }) - .unwrap_or_else(|| Arc::from("".to_string())) + this.types + .get(key) + .map(|type_config| type_config.icon.clone()) + } + + pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Option> { + let this = cx.has_global::().then(|| cx.global::())?; + + let key = if expanded { + EXPANDED_CHEVRON_TYPE + } else { + COLLAPSED_CHEVRON_TYPE + }; + + this.types + .get(key) + .map(|type_config| type_config.icon.clone()) } } diff --git a/crates/project_panel2/src/project_panel.rs b/crates/project_panel2/src/project_panel.rs index 594c2d8e03..e4846af76c 100644 --- a/crates/project_panel2/src/project_panel.rs +++ b/crates/project_panel2/src/project_panel.rs @@ -1283,16 +1283,16 @@ impl ProjectPanel { let icon = match entry.kind { EntryKind::File(_) => { if show_file_icons { - Some(FileAssociations::get_icon(&entry.path, cx)) + FileAssociations::get_icon(&entry.path, cx) } else { None } } _ => { if show_folder_icons { - Some(FileAssociations::get_folder_icon(is_expanded, cx)) + FileAssociations::get_folder_icon(is_expanded, cx) } else { - Some(FileAssociations::get_chevron_icon(is_expanded, cx)) + FileAssociations::get_chevron_icon(is_expanded, cx) } } }; diff --git a/crates/search2/src/search_bar.rs b/crates/search2/src/search_bar.rs index 1a7456f41c..f5a9a8c8f7 100644 --- a/crates/search2/src/search_bar.rs +++ b/crates/search2/src/search_bar.rs @@ -1,4 +1,4 @@ -use gpui::{IntoElement, MouseDownEvent, WindowContext}; +use gpui::{ClickEvent, IntoElement, WindowContext}; use ui::{Button, ButtonVariant, IconButton}; use crate::mode::SearchMode; @@ -6,7 +6,7 @@ use crate::mode::SearchMode; pub(super) fn render_nav_button( icon: ui::Icon, _active: bool, - on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static, ) -> impl IntoElement { // let tooltip_style = cx.theme().tooltip.clone(); // let cursor_style = if active { @@ -21,7 +21,7 @@ pub(super) fn render_nav_button( pub(crate) fn render_search_mode_button( mode: SearchMode, is_active: bool, - on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, + on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static, ) -> Button { let button_variant = if is_active { ButtonVariant::Filled diff --git a/crates/storybook2/src/stories/focus.rs b/crates/storybook2/src/stories/focus.rs index 77aa057b09..7b375b10e3 100644 --- a/crates/storybook2/src/stories/focus.rs +++ b/crates/storybook2/src/stories/focus.rs @@ -2,7 +2,7 @@ use gpui::{ actions, div, prelude::*, Div, FocusHandle, Focusable, KeyBinding, Render, Stateful, View, WindowContext, }; -use theme2::ActiveTheme; +use ui::prelude::*; actions!(ActionA, ActionB, ActionC); diff --git a/crates/storybook2/src/stories/picker.rs b/crates/storybook2/src/stories/picker.rs index 75eb0d88e7..75aa7aed05 100644 --- a/crates/storybook2/src/stories/picker.rs +++ b/crates/storybook2/src/stories/picker.rs @@ -4,7 +4,7 @@ use gpui::{ }; use picker::{Picker, PickerDelegate}; use std::sync::Arc; -use theme2::ActiveTheme; +use ui::prelude::*; use ui::{Label, ListItem}; pub struct PickerStory { diff --git a/crates/storybook2/src/stories/scroll.rs b/crates/storybook2/src/stories/scroll.rs index 297e65d411..300aae1144 100644 --- a/crates/storybook2/src/stories/scroll.rs +++ b/crates/storybook2/src/stories/scroll.rs @@ -1,5 +1,5 @@ use gpui::{div, prelude::*, px, Div, Render, SharedString, Stateful, Styled, View, WindowContext}; -use theme2::ActiveTheme; +use ui::prelude::*; use ui::Tooltip; pub struct ScrollStory; diff --git a/crates/storybook2/src/story_selector.rs b/crates/storybook2/src/story_selector.rs index 9945b2e7ef..1c0890e4be 100644 --- a/crates/storybook2/src/story_selector.rs +++ b/crates/storybook2/src/story_selector.rs @@ -19,7 +19,6 @@ pub enum ComponentStory { Focus, Icon, IconButton, - Input, Keybinding, Label, ListItem, @@ -39,7 +38,6 @@ impl ComponentStory { Self::Focus => FocusStory::view(cx).into(), Self::Icon => cx.build_view(|_| ui::IconStory).into(), Self::IconButton => cx.build_view(|_| ui::IconButtonStory).into(), - Self::Input => cx.build_view(|_| ui::InputStory).into(), Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(), Self::Label => cx.build_view(|_| ui::LabelStory).into(), Self::ListItem => cx.build_view(|_| ui::ListItemStory).into(), diff --git a/crates/theme2/src/registry.rs b/crates/theme2/src/registry.rs index 919dd1b109..b50eb831dd 100644 --- a/crates/theme2/src/registry.rs +++ b/crates/theme2/src/registry.rs @@ -86,6 +86,10 @@ impl ThemeRegistry { })); } + pub fn clear(&mut self) { + self.themes.clear(); + } + pub fn list_names(&self, _staff: bool) -> impl Iterator + '_ { self.themes.keys().cloned() } diff --git a/crates/theme_selector2/Cargo.toml b/crates/theme_selector2/Cargo.toml new file mode 100644 index 0000000000..853a53af68 --- /dev/null +++ b/crates/theme_selector2/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "theme_selector2" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +path = "src/theme_selector.rs" +doctest = false + +[dependencies] +editor = { package = "editor2", path = "../editor2" } +fuzzy = { package = "fuzzy2", path = "../fuzzy2" } +fs = { package = "fs2", path = "../fs2" } +gpui = { package = "gpui2", path = "../gpui2" } +ui = { package = "ui2", path = "../ui2" } +picker = { package = "picker2", path = "../picker2" } +theme = { package = "theme2", path = "../theme2" } +settings = { package = "settings2", path = "../settings2" } +feature_flags = { package = "feature_flags2", path = "../feature_flags2" } +workspace = { package = "workspace2", path = "../workspace2" } +util = { path = "../util" } +log.workspace = true +parking_lot.workspace = true +postage.workspace = true +smol.workspace = true + +[dev-dependencies] +editor = { package = "editor2", path = "../editor2", features = ["test-support"] } diff --git a/crates/theme_selector2/src/theme_selector.rs b/crates/theme_selector2/src/theme_selector.rs new file mode 100644 index 0000000000..7b0a0c3d3a --- /dev/null +++ b/crates/theme_selector2/src/theme_selector.rs @@ -0,0 +1,276 @@ +use feature_flags::FeatureFlagAppExt; +use fs::Fs; +use fuzzy::{match_strings, StringMatch, StringMatchCandidate}; +use gpui::{ + actions, AppContext, DismissEvent, EventEmitter, FocusableView, ParentElement, Render, + SharedString, View, ViewContext, VisualContext, WeakView, +}; +use picker::{Picker, PickerDelegate}; +use settings::{update_settings_file, SettingsStore}; +use std::sync::Arc; +use theme::{ActiveTheme, Theme, ThemeRegistry, ThemeSettings}; +use ui::ListItem; +use util::ResultExt; +use workspace::{ui::HighlightedLabel, Workspace}; + +actions!(Toggle, Reload); + +pub fn init(cx: &mut AppContext) { + cx.observe_new_views( + |workspace: &mut Workspace, _cx: &mut ViewContext| { + workspace.register_action(toggle); + }, + ) + .detach(); +} + +pub fn toggle(workspace: &mut Workspace, _: &Toggle, cx: &mut ViewContext) { + let fs = workspace.app_state().fs.clone(); + workspace.toggle_modal(cx, |cx| { + ThemeSelector::new( + ThemeSelectorDelegate::new(cx.view().downgrade(), fs, cx), + cx, + ) + }); +} + +#[cfg(debug_assertions)] +pub fn reload(cx: &mut AppContext) { + let current_theme_name = cx.theme().name.clone(); + let current_theme = cx.update_global(|registry: &mut ThemeRegistry, _cx| { + registry.clear(); + registry.get(¤t_theme_name) + }); + match current_theme { + Ok(theme) => { + ThemeSelectorDelegate::set_theme(theme, cx); + log::info!("reloaded theme {}", current_theme_name); + } + Err(error) => { + log::error!("failed to load theme {}: {:?}", current_theme_name, error) + } + } +} + +pub struct ThemeSelector { + picker: View>, +} + +impl EventEmitter for ThemeSelector {} + +impl FocusableView for ThemeSelector { + fn focus_handle(&self, cx: &AppContext) -> gpui::FocusHandle { + self.picker.focus_handle(cx) + } +} + +impl Render for ThemeSelector { + type Element = View>; + + fn render(&mut self, _cx: &mut ViewContext) -> Self::Element { + self.picker.clone() + } +} + +impl ThemeSelector { + pub fn new(delegate: ThemeSelectorDelegate, cx: &mut ViewContext) -> Self { + let picker = cx.build_view(|cx| Picker::new(delegate, cx)); + Self { picker } + } +} + +pub struct ThemeSelectorDelegate { + fs: Arc, + theme_names: Vec, + matches: Vec, + original_theme: Arc, + selection_completed: bool, + selected_index: usize, + view: WeakView, +} + +impl ThemeSelectorDelegate { + fn new( + weak_view: WeakView, + fs: Arc, + cx: &mut ViewContext, + ) -> Self { + let original_theme = cx.theme().clone(); + + let staff_mode = cx.is_staff(); + let registry = cx.global::>(); + let theme_names = registry.list(staff_mode).collect::>(); + //todo!(theme sorting) + // theme_names.sort_unstable_by(|a, b| a.is_light.cmp(&b.is_light).then(a.name.cmp(&b.name))); + let matches = theme_names + .iter() + .map(|meta| StringMatch { + candidate_id: 0, + score: 0.0, + positions: Default::default(), + string: meta.to_string(), + }) + .collect(); + let mut this = Self { + fs, + theme_names, + matches, + original_theme: original_theme.clone(), + selected_index: 0, + selection_completed: false, + view: weak_view, + }; + this.select_if_matching(&original_theme.name); + this + } + + fn show_selected_theme(&mut self, cx: &mut ViewContext>) { + if let Some(mat) = self.matches.get(self.selected_index) { + let registry = cx.global::>(); + match registry.get(&mat.string) { + Ok(theme) => { + Self::set_theme(theme, cx); + } + Err(error) => { + log::error!("error loading theme {}: {}", mat.string, error) + } + } + } + } + + fn select_if_matching(&mut self, theme_name: &str) { + self.selected_index = self + .matches + .iter() + .position(|mat| mat.string == theme_name) + .unwrap_or(self.selected_index); + } + + fn set_theme(theme: Arc, cx: &mut AppContext) { + cx.update_global(|store: &mut SettingsStore, cx| { + let mut theme_settings = store.get::(None).clone(); + theme_settings.active_theme = theme; + store.override_global(theme_settings); + cx.refresh(); + }); + } +} + +impl PickerDelegate for ThemeSelectorDelegate { + type ListItem = ui::ListItem; + + fn placeholder_text(&self) -> Arc { + "Select Theme...".into() + } + + fn match_count(&self) -> usize { + self.matches.len() + } + + fn confirm(&mut self, _: bool, cx: &mut ViewContext>) { + self.selection_completed = true; + + let theme_name = cx.theme().name.clone(); + update_settings_file::(self.fs.clone(), cx, move |settings| { + settings.theme = Some(theme_name.to_string()); + }); + + self.view + .update(cx, |_, cx| { + cx.emit(DismissEvent); + }) + .ok(); + } + + fn dismissed(&mut self, cx: &mut ViewContext>) { + if !self.selection_completed { + Self::set_theme(self.original_theme.clone(), cx); + self.selection_completed = true; + } + } + + fn selected_index(&self) -> usize { + self.selected_index + } + + fn set_selected_index( + &mut self, + ix: usize, + cx: &mut ViewContext>, + ) { + self.selected_index = ix; + self.show_selected_theme(cx); + } + + fn update_matches( + &mut self, + query: String, + cx: &mut ViewContext>, + ) -> gpui::Task<()> { + let background = cx.background_executor().clone(); + let candidates = self + .theme_names + .iter() + .enumerate() + .map(|(id, meta)| StringMatchCandidate { + id, + char_bag: meta.as_ref().into(), + string: meta.to_string(), + }) + .collect::>(); + + cx.spawn(|this, mut cx| async move { + let matches = if query.is_empty() { + candidates + .into_iter() + .enumerate() + .map(|(index, candidate)| StringMatch { + candidate_id: index, + string: candidate.string, + positions: Vec::new(), + score: 0.0, + }) + .collect() + } else { + match_strings( + &candidates, + &query, + false, + 100, + &Default::default(), + background, + ) + .await + }; + + this.update(&mut cx, |this, cx| { + this.delegate.matches = matches; + this.delegate.selected_index = this + .delegate + .selected_index + .min(this.delegate.matches.len().saturating_sub(1)); + this.delegate.show_selected_theme(cx); + }) + .log_err(); + }) + } + + fn render_match( + &self, + ix: usize, + selected: bool, + _cx: &mut ViewContext>, + ) -> Option { + let theme_match = &self.matches[ix]; + + Some( + ListItem::new(ix) + .inset(true) + .selected(selected) + .child(HighlightedLabel::new( + theme_match.string.clone(), + theme_match.positions.clone(), + )), + ) + } +} diff --git a/crates/ui2/src/clickable.rs b/crates/ui2/src/clickable.rs new file mode 100644 index 0000000000..b25f6b0e70 --- /dev/null +++ b/crates/ui2/src/clickable.rs @@ -0,0 +1,5 @@ +use gpui::{ClickEvent, WindowContext}; + +pub trait Clickable { + fn on_click(self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self; +} diff --git a/crates/ui2/src/components.rs b/crates/ui2/src/components.rs index c467576f4a..9dc061e31f 100644 --- a/crates/ui2/src/components.rs +++ b/crates/ui2/src/components.rs @@ -1,12 +1,12 @@ mod avatar; mod button; +mod button2; mod checkbox; mod context_menu; mod disclosure; mod divider; mod icon; mod icon_button; -mod input; mod keybinding; mod label; mod list; @@ -21,13 +21,13 @@ mod stories; pub use avatar::*; pub use button::*; +pub use button2::*; pub use checkbox::*; pub use context_menu::*; pub use disclosure::*; pub use divider::*; pub use icon::*; pub use icon_button::*; -pub use input::*; pub use keybinding::*; pub use label::*; pub use list::*; diff --git a/crates/ui2/src/components/button.rs b/crates/ui2/src/components/button.rs index 02902a4b64..fbe5b951fa 100644 --- a/crates/ui2/src/components/button.rs +++ b/crates/ui2/src/components/button.rs @@ -1,9 +1,7 @@ -use std::rc::Rc; - use gpui::{ - DefiniteLength, Div, Hsla, IntoElement, MouseButton, MouseDownEvent, - StatefulInteractiveElement, WindowContext, + ClickEvent, DefiniteLength, Div, Hsla, IntoElement, StatefulInteractiveElement, WindowContext, }; +use std::rc::Rc; use crate::prelude::*; use crate::{h_stack, Color, Icon, IconButton, IconElement, Label, LineHeightStyle}; @@ -67,7 +65,7 @@ impl ButtonVariant { #[derive(IntoElement)] pub struct Button { disabled: bool, - click_handler: Option>, + click_handler: Option>, icon: Option, icon_position: Option, label: SharedString, @@ -118,7 +116,7 @@ impl RenderOnce for Button { } if let Some(click_handler) = self.click_handler.clone() { - button = button.on_mouse_down(MouseButton::Left, move |event, cx| { + button = button.on_click(move |event, cx| { click_handler(event, cx); }); } @@ -168,10 +166,7 @@ impl Button { self } - pub fn on_click( - mut self, - handler: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static, - ) -> Self { + pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self { self.click_handler = Some(Rc::new(handler)); self } diff --git a/crates/ui2/src/components/button2.rs b/crates/ui2/src/components/button2.rs new file mode 100644 index 0000000000..e9192afea0 --- /dev/null +++ b/crates/ui2/src/components/button2.rs @@ -0,0 +1,413 @@ +use gpui::{ + rems, AnyElement, AnyView, ClickEvent, Div, Hsla, IntoElement, Rems, Stateful, + StatefulInteractiveElement, WindowContext, +}; +use smallvec::SmallVec; + +use crate::{h_stack, prelude::*}; + +// 🚧 Heavily WIP 🚧 + +// #[derive(Default, PartialEq, Clone, Copy)] +// pub enum ButtonType2 { +// #[default] +// DefaultButton, +// IconButton, +// ButtonLike, +// SplitButton, +// ToggleButton, +// } + +#[derive(Default, PartialEq, Clone, Copy)] +pub enum IconPosition2 { + #[default] + Before, + After, +} + +#[derive(Default, PartialEq, Clone, Copy)] +pub enum ButtonStyle2 { + #[default] + Filled, + // Tinted, + Subtle, + Transparent, +} + +#[derive(Debug, Clone, Copy)] +pub struct ButtonStyle { + pub background: Hsla, + pub border_color: Hsla, + pub label_color: Hsla, + pub icon_color: Hsla, +} + +impl ButtonStyle2 { + pub fn enabled(self, cx: &mut WindowContext) -> ButtonStyle { + match self { + ButtonStyle2::Filled => ButtonStyle { + background: cx.theme().colors().element_background, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Subtle => ButtonStyle { + background: cx.theme().colors().ghost_element_background, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Transparent => ButtonStyle { + background: gpui::transparent_black(), + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + } + } + + pub fn hovered(self, cx: &mut WindowContext) -> ButtonStyle { + match self { + ButtonStyle2::Filled => ButtonStyle { + background: cx.theme().colors().element_hover, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Subtle => ButtonStyle { + background: cx.theme().colors().ghost_element_hover, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Transparent => ButtonStyle { + background: gpui::transparent_black(), + border_color: gpui::transparent_black(), + // TODO: These are not great + label_color: Color::Muted.color(cx), + // TODO: These are not great + icon_color: Color::Muted.color(cx), + }, + } + } + + pub fn active(self, cx: &mut WindowContext) -> ButtonStyle { + match self { + ButtonStyle2::Filled => ButtonStyle { + background: cx.theme().colors().element_active, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Subtle => ButtonStyle { + background: cx.theme().colors().ghost_element_active, + border_color: gpui::transparent_black(), + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Transparent => ButtonStyle { + background: gpui::transparent_black(), + border_color: gpui::transparent_black(), + // TODO: These are not great + label_color: Color::Muted.color(cx), + // TODO: These are not great + icon_color: Color::Muted.color(cx), + }, + } + } + + pub fn focused(self, cx: &mut WindowContext) -> ButtonStyle { + match self { + ButtonStyle2::Filled => ButtonStyle { + background: cx.theme().colors().element_background, + border_color: cx.theme().colors().border_focused, + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Subtle => ButtonStyle { + background: cx.theme().colors().ghost_element_background, + border_color: cx.theme().colors().border_focused, + label_color: Color::Default.color(cx), + icon_color: Color::Default.color(cx), + }, + ButtonStyle2::Transparent => ButtonStyle { + background: gpui::transparent_black(), + border_color: cx.theme().colors().border_focused, + label_color: Color::Accent.color(cx), + icon_color: Color::Accent.color(cx), + }, + } + } + + pub fn disabled(self, cx: &mut WindowContext) -> ButtonStyle { + match self { + ButtonStyle2::Filled => ButtonStyle { + background: cx.theme().colors().element_disabled, + border_color: cx.theme().colors().border_disabled, + label_color: Color::Disabled.color(cx), + icon_color: Color::Disabled.color(cx), + }, + ButtonStyle2::Subtle => ButtonStyle { + background: cx.theme().colors().ghost_element_disabled, + border_color: cx.theme().colors().border_disabled, + label_color: Color::Disabled.color(cx), + icon_color: Color::Disabled.color(cx), + }, + ButtonStyle2::Transparent => ButtonStyle { + background: gpui::transparent_black(), + border_color: gpui::transparent_black(), + label_color: Color::Disabled.color(cx), + icon_color: Color::Disabled.color(cx), + }, + } + } +} + +#[derive(Default, PartialEq, Clone, Copy)] +pub enum ButtonSize2 { + #[default] + Default, + Compact, + None, +} + +impl ButtonSize2 { + fn height(self) -> Rems { + match self { + ButtonSize2::Default => rems(22. / 16.), + ButtonSize2::Compact => rems(18. / 16.), + ButtonSize2::None => rems(16. / 16.), + } + } +} + +// pub struct Button { +// id: ElementId, +// icon: Option, +// icon_color: Option, +// icon_position: Option, +// label: Option