From ffa2d90dc381b61f114217ee9590fa3024f9f7c0 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 8 May 2024 18:21:43 +0300 Subject: [PATCH] Return prettier entry back to LSP logs (#11563) Fixes prettier entries disappeared after https://github.com/zed-industries/zed/pull/10788 Release Notes: - N/A --- crates/language_tools/src/lsp_log.rs | 96 +++++++++++++++------------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/crates/language_tools/src/lsp_log.rs b/crates/language_tools/src/lsp_log.rs index 28a27aac60..ad08d5529d 100644 --- a/crates/language_tools/src/lsp_log.rs +++ b/crates/language_tools/src/lsp_log.rs @@ -35,14 +35,27 @@ struct ProjectState { } struct LanguageServerState { - name: LanguageServerName, + kind: LanguageServerKind, log_messages: VecDeque, rpc_state: Option, - project: Option>, _io_logs_subscription: Option, _lsp_logs_subscription: Option, } +enum LanguageServerKind { + Local { project: WeakModel }, + Global { name: LanguageServerName }, +} + +impl LanguageServerKind { + fn project(&self) -> Option<&WeakModel> { + match self { + Self::Local { project } => Some(project), + Self::Global { .. } => None, + } + } +} + struct LanguageServerRpcState { rpc_messages: VecDeque, last_message_kind: Option, @@ -138,8 +151,9 @@ impl LogStore { }, )); this.add_language_server( - None, - LanguageServerName(Arc::from("copilot")), + LanguageServerKind::Global { + name: LanguageServerName(Arc::from("copilot")), + }, server.clone(), cx, ); @@ -180,18 +194,16 @@ impl LogStore { cx.observe_release(project, move |this, _, _| { this.projects.remove(&weak_project); this.language_servers - .retain(|_, state| state.project.as_ref() != Some(&weak_project)); + .retain(|_, state| state.kind.project() != Some(&weak_project)); }), cx.subscribe(project, |this, project, event, cx| match event { project::Event::LanguageServerAdded(id) => { let read_project = project.read(cx); - if let Some((server, adapter)) = read_project - .language_server_for_id(*id) - .zip(read_project.language_server_adapter_for_id(*id)) - { + if let Some(server) = read_project.language_server_for_id(*id) { this.add_language_server( - Some(&project.downgrade()), - adapter.name.clone(), + LanguageServerKind::Local { + project: project.downgrade(), + }, server, cx, ); @@ -219,8 +231,7 @@ impl LogStore { fn add_language_server( &mut self, - project: Option<&WeakModel>, - name: LanguageServerName, + kind: LanguageServerKind, server: Arc, cx: &mut ModelContext, ) -> Option<&mut LanguageServerState> { @@ -230,9 +241,8 @@ impl LogStore { .or_insert_with(|| { cx.notify(); LanguageServerState { - name, + kind, rpc_state: None, - project: project.cloned(), log_messages: VecDeque::with_capacity(MAX_STORED_LOG_ENTRIES), _io_logs_subscription: None, _lsp_logs_subscription: None, @@ -300,23 +310,20 @@ impl LogStore { fn server_ids_for_project<'a>( &'a self, - project: &'a WeakModel, + lookup_project: &'a WeakModel, ) -> impl Iterator + 'a { - [].into_iter() - .chain(self.language_servers.iter().filter_map(|(id, state)| { - if state.project.as_ref() == Some(project) { - return Some(*id); - } else { - None + self.language_servers + .iter() + .filter_map(move |(id, state)| match &state.kind { + LanguageServerKind::Local { project } => { + if project == lookup_project { + Some(*id) + } else { + None + } } - })) - .chain(self.language_servers.iter().filter_map(|(id, state)| { - if state.project.is_none() { - return Some(*id); - } else { - None - } - })) + LanguageServerKind::Global { .. } => Some(*id), + }) } fn enable_rpc_trace_for_language_server( @@ -408,7 +415,7 @@ impl LspLogView { .read(cx) .language_servers .iter() - .find(|(_, server)| server.project == Some(project.downgrade())) + .find(|(_, server)| server.kind.project() == Some(&project.downgrade())) .map(|(id, _)| *id); let weak_project = project.downgrade(); @@ -562,21 +569,18 @@ impl LspLogView { log_store .language_servers .iter() - .filter_map(|(server_id, state)| { - if state.project.is_none() { - Some(LogMenuItem { - server_id: *server_id, - server_name: state.name.clone(), - worktree_root_name: "supplementary".to_string(), - rpc_trace_enabled: state.rpc_state.is_some(), - rpc_trace_selected: self.is_showing_rpc_trace - && self.current_server_id == Some(*server_id), - logs_selected: !self.is_showing_rpc_trace - && self.current_server_id == Some(*server_id), - }) - } else { - None - } + .filter_map(|(server_id, state)| match &state.kind { + LanguageServerKind::Global { name } => Some(LogMenuItem { + server_id: *server_id, + server_name: name.clone(), + worktree_root_name: "supplementary".to_string(), + rpc_trace_enabled: state.rpc_state.is_some(), + rpc_trace_selected: self.is_showing_rpc_trace + && self.current_server_id == Some(*server_id), + logs_selected: !self.is_showing_rpc_trace + && self.current_server_id == Some(*server_id), + }), + _ => None, }), ) .collect::>();