diff --git a/crates/lsp/src/lsp.rs b/crates/lsp/src/lsp.rs index e973a77f4fe211923a6aa9cb59d040bf4c606e96..d52de667d42e668f43f959ff0fb42385ba3fcde3 100644 --- a/crates/lsp/src/lsp.rs +++ b/crates/lsp/src/lsp.rs @@ -665,11 +665,11 @@ impl LanguageServer { } } - pub fn name<'a>(&self) -> &str { + pub fn name<'a>(self: &'a Arc) -> &'a str { &self.name } - pub fn capabilities<'a>(&self) -> &ServerCapabilities { + pub fn capabilities<'a>(self: &'a Arc) -> &'a ServerCapabilities { &self.capabilities } diff --git a/crates/project/src/lsp_command.rs b/crates/project/src/lsp_command.rs index 82e2c0c5a561dcfc4eda4b1dd22d1aa79937d6ea..8435de71e2f420e479035a5d21b36e850db29834 100644 --- a/crates/project/src/lsp_command.rs +++ b/crates/project/src/lsp_command.rs @@ -1,6 +1,6 @@ use crate::{ DocumentHighlight, Hover, HoverBlock, HoverBlockKind, Location, LocationLink, Project, - ProjectLanguageServer, ProjectTransaction, + ProjectTransaction, }; use anyhow::{anyhow, Result}; use async_trait::async_trait; @@ -14,7 +14,7 @@ use language::{ range_from_lsp, range_to_lsp, Anchor, Bias, Buffer, CachedLspAdapter, CharKind, CodeAction, Completion, OffsetRangeExt, PointUtf16, ToOffset, ToPointUtf16, Transaction, Unclipped, }; -use lsp::{DocumentHighlightKind, LanguageServerId, ServerCapabilities}; +use lsp::{DocumentHighlightKind, LanguageServer, LanguageServerId, ServerCapabilities}; use std::{cmp::Reverse, ops::Range, path::Path, sync::Arc}; pub fn lsp_formatting_options(tab_size: u32) -> lsp::FormattingOptions { @@ -40,7 +40,7 @@ pub(crate) trait LspCommand: 'static + Sized { &self, path: &Path, buffer: &Buffer, - language_server: &Arc, + language_server: &Arc, cx: &AppContext, ) -> ::Params; @@ -156,7 +156,7 @@ impl LspCommand for PrepareRename { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::TextDocumentPositionParams { lsp::TextDocumentPositionParams { @@ -279,7 +279,7 @@ impl LspCommand for PerformRename { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::RenameParams { lsp::RenameParams { @@ -398,7 +398,7 @@ impl LspCommand for GetDefinition { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::GotoDefinitionParams { lsp::GotoDefinitionParams { @@ -499,7 +499,7 @@ impl LspCommand for GetTypeDefinition { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::GotoTypeDefinitionParams { lsp::GotoTypeDefinitionParams { @@ -587,7 +587,7 @@ fn language_server_for_buffer( buffer: &ModelHandle, server_id: LanguageServerId, cx: &mut AsyncAppContext, -) -> Result<(Arc, Arc)> { +) -> Result<(Arc, Arc)> { project .read_with(cx, |project, cx| { project @@ -784,7 +784,7 @@ impl LspCommand for GetReferences { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::ReferenceParams { lsp::ReferenceParams { @@ -949,7 +949,7 @@ impl LspCommand for GetDocumentHighlights { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::DocumentHighlightParams { lsp::DocumentHighlightParams { @@ -1096,7 +1096,7 @@ impl LspCommand for GetHover { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::HoverParams { lsp::HoverParams { @@ -1314,7 +1314,7 @@ impl LspCommand for GetCompletions { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::CompletionParams { lsp::CompletionParams { @@ -1530,7 +1530,7 @@ impl LspCommand for GetCodeActions { &self, path: &Path, buffer: &Buffer, - language_server: &Arc, + language_server: &Arc, _: &AppContext, ) -> lsp::CodeActionParams { let relevant_diagnostics = buffer @@ -1673,7 +1673,7 @@ impl LspCommand for OnTypeFormatting { &self, path: &Path, _: &Buffer, - _: &Arc, + _: &Arc, _: &AppContext, ) -> lsp::DocumentOnTypeFormattingParams { lsp::DocumentOnTypeFormattingParams { diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index 5c22a14d4d919bb412110c3570bd43dec11245aa..2536c94225f719b9153dc42ae7ec76323438d9f6 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -279,79 +279,19 @@ pub enum Event { CollaboratorLeft(proto::PeerId), } -pub struct ProjectLanguageServer { - server: Arc, - project: WeakModelHandle, -} - -impl std::ops::Deref for ProjectLanguageServer { - type Target = LanguageServer; - - fn deref(&self) -> &Self::Target { - &self.server - } -} - -impl ProjectLanguageServer { - pub fn new(server: Arc, project: WeakModelHandle) -> Self { - ProjectLanguageServer { server, project } - } - - pub fn request( - &self, - params: T::Params, - cx: &mut AsyncAppContext, - ) -> impl Future> - where - T::Result: 'static + Send, - { - let server = self.server.clone(); - let project = self.project.clone(); - - let future = server.request::(params); - - cx.spawn(|mut cx| async move { - let result = future.await; - if result.is_ok() { - return result; - } - - let project = match project.upgrade(&cx) { - Some(project) => project, - None => return result, - }; - - project.update(&mut cx, |_, cx| { - Project::check_errored_language_server(server, cx); - }); - - result - }) - } - - pub fn notify(&self, params: T::Params) -> Result<()> { - let result = self.server.notify::(params); - if result.is_ok() { - return Ok(()); - } - - result - } -} - pub enum LanguageServerState { - Validating(Task>>), + Validating(Task>>), Starting { language: Arc, adapter: Arc, - task: Task>>, + task: Task>>, }, Running { language: Arc, adapter: Arc, - server: Arc, + server: Arc, watched_paths: HashMap, simulate_disk_based_diagnostics_completion: Option>, }, @@ -2297,13 +2237,7 @@ impl Project { fn language_servers_for_worktree( &self, worktree_id: WorktreeId, - ) -> impl Iterator< - Item = ( - &Arc, - &Arc, - &Arc, - ), - > { + ) -> impl Iterator, &Arc, &Arc)> { self.language_server_ids .iter() .filter_map(move |((language_server_worktree_id, _), id)| { @@ -2543,7 +2477,7 @@ impl Project { .await; match result { - Ok(server) => Some(Arc::new(ProjectLanguageServer::new(server, this))), + Ok(server) => Some(server), Err(err) => { log::error!("failed to start language server {:?}: {}", server_name, err); @@ -2869,10 +2803,7 @@ impl Project { adapter: adapter.clone(), language: language.clone(), watched_paths: Default::default(), - server: Arc::new(ProjectLanguageServer::new( - language_server.clone(), - cx.weak_handle(), - )), + server: language_server.clone(), simulate_disk_based_diagnostics_completion: None, }, ); @@ -3131,6 +3062,7 @@ impl Project { } fn check_errored_language_server( + &self, language_server: Arc, cx: &mut ModelContext, ) { @@ -3965,7 +3897,7 @@ impl Project { this: &ModelHandle, buffer: &ModelHandle, abs_path: &Path, - language_server: &Arc, + language_server: &Arc, tab_size: NonZeroU32, cx: &mut AsyncAppContext, ) -> Result, String)>> { @@ -3979,29 +3911,23 @@ impl Project { let result = if !matches!(formatting_provider, Some(OneOf::Left(false))) { language_server - .request::( - lsp::DocumentFormattingParams { - text_document, - options: lsp_command::lsp_formatting_options(tab_size.get()), - work_done_progress_params: Default::default(), - }, - cx, - ) + .request::(lsp::DocumentFormattingParams { + text_document, + options: lsp_command::lsp_formatting_options(tab_size.get()), + work_done_progress_params: Default::default(), + }) .await } else if !matches!(range_formatting_provider, Some(OneOf::Left(false))) { let buffer_start = lsp::Position::new(0, 0); let buffer_end = buffer.read_with(cx, |b, _| point_to_lsp(b.max_point_utf16())); language_server - .request::( - lsp::DocumentRangeFormattingParams { - text_document, - range: lsp::Range::new(buffer_start, buffer_end), - options: lsp_command::lsp_formatting_options(tab_size.get()), - work_done_progress_params: Default::default(), - }, - cx, - ) + .request::(lsp::DocumentRangeFormattingParams { + text_document, + range: lsp::Range::new(buffer_start, buffer_end), + options: lsp_command::lsp_formatting_options(tab_size.get()), + work_done_progress_params: Default::default(), + }) .await } else { Ok(None) @@ -4017,8 +3943,8 @@ impl Project { err ); - this.update(cx, |_, cx| { - Self::check_errored_language_server(language_server.server.clone(), cx); + this.update(cx, |this, cx| { + this.check_errored_language_server(language_server.clone(), cx); }); None @@ -4164,7 +4090,6 @@ impl Project { query: query.to_string(), ..Default::default() }, - &mut cx.to_async(), ) .map_ok(move |response| { let lsp_symbols = response.map(|symbol_response| match symbol_response { @@ -4398,10 +4323,7 @@ impl Project { cx.spawn(|this, mut cx| async move { let resolved_completion = match lang_server - .request::( - completion.lsp_completion, - &mut cx, - ) + .request::(completion.lsp_completion) .await { Ok(resolved_completion) => resolved_completion, @@ -4530,10 +4452,7 @@ impl Project { { *lsp_range = serde_json::to_value(&range_to_lsp(range)).unwrap(); action.lsp_action = match lang_server - .request::( - action.lsp_action, - &mut cx, - ) + .request::(action.lsp_action) .await { Ok(lsp_action) => lsp_action, @@ -4577,14 +4496,11 @@ impl Project { }); let result = lang_server - .request::( - lsp::ExecuteCommandParams { - command: command.command, - arguments: command.arguments.unwrap_or_default(), - ..Default::default() - }, - &mut cx, - ) + .request::(lsp::ExecuteCommandParams { + command: command.command, + arguments: command.arguments.unwrap_or_default(), + ..Default::default() + }) .await; if let Err(err) = result { @@ -4691,7 +4607,7 @@ impl Project { edits: Vec, push_to_history: bool, _: Arc, - language_server: Arc, + language_server: Arc, cx: &mut AsyncAppContext, ) -> Result> { let edits = this @@ -4732,7 +4648,7 @@ impl Project { edit: lsp::WorkspaceEdit, push_to_history: bool, lsp_adapter: Arc, - language_server: Arc, + language_server: Arc, cx: &mut AsyncAppContext, ) -> Result { let fs = this.read_with(cx, |this, _| this.fs.clone()); @@ -5154,15 +5070,12 @@ impl Project { .map(|(_, server)| server.clone()), ) { let lsp_params = request.to_lsp(&file.abs_path(cx), buffer, &language_server, cx); - return cx.spawn(|this, mut cx| async move { + return cx.spawn(|this, cx| async move { if !request.check_capabilities(language_server.capabilities()) { return Ok(Default::default()); } - let result = language_server - .request::(lsp_params, &mut cx) - .await; - + let result = language_server.request::(lsp_params).await; let response = match result { Ok(response) => response, @@ -7364,10 +7277,7 @@ impl Project { }) } - pub fn language_server_for_id( - &self, - id: LanguageServerId, - ) -> Option> { + pub fn language_server_for_id(&self, id: LanguageServerId) -> Option> { if let LanguageServerState::Running { server, .. } = self.language_servers.get(&id)? { Some(server.clone()) } else { @@ -7379,7 +7289,7 @@ impl Project { &self, buffer: &Buffer, cx: &AppContext, - ) -> impl Iterator, &Arc)> { + ) -> impl Iterator, &Arc)> { self.language_server_ids_for_buffer(buffer, cx) .into_iter() .filter_map(|server_id| { @@ -7399,7 +7309,7 @@ impl Project { &self, buffer: &Buffer, cx: &AppContext, - ) -> Option<(&Arc, &Arc)> { + ) -> Option<(&Arc, &Arc)> { self.language_servers_for_buffer(buffer, cx).next() } @@ -7408,7 +7318,7 @@ impl Project { buffer: &Buffer, server_id: LanguageServerId, cx: &AppContext, - ) -> Option<(&Arc, &Arc)> { + ) -> Option<(&Arc, &Arc)> { self.language_servers_for_buffer(buffer, cx) .find(|(_, s)| s.server_id() == server_id) }