Only generate workspace/configuration for relevant adapter

This commit is contained in:
Julia 2023-08-22 23:36:04 -04:00
parent 814896de3f
commit affb73d651
6 changed files with 95 additions and 134 deletions

View file

@ -46,7 +46,7 @@ use theme::{SyntaxTheme, Theme};
use tree_sitter::{self, Query}; use tree_sitter::{self, Query};
use unicase::UniCase; use unicase::UniCase;
use util::{http::HttpClient, paths::PathExt}; use util::{http::HttpClient, paths::PathExt};
use util::{merge_json_value_into, post_inc, ResultExt, TryFutureExt as _, UnwrapFuture}; use util::{post_inc, ResultExt, TryFutureExt as _, UnwrapFuture};
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
use futures::channel::mpsc; use futures::channel::mpsc;
@ -175,10 +175,7 @@ impl CachedLspAdapter {
self.adapter.code_action_kinds() self.adapter.code_action_kinds()
} }
pub fn workspace_configuration( pub fn workspace_configuration(&self, cx: &mut AppContext) -> BoxFuture<'static, Value> {
&self,
cx: &mut AppContext,
) -> Option<BoxFuture<'static, Value>> {
self.adapter.workspace_configuration(cx) self.adapter.workspace_configuration(cx)
} }
@ -287,8 +284,8 @@ pub trait LspAdapter: 'static + Send + Sync {
None None
} }
fn workspace_configuration(&self, _: &mut AppContext) -> Option<BoxFuture<'static, Value>> { fn workspace_configuration(&self, _: &mut AppContext) -> BoxFuture<'static, Value> {
None futures::future::ready(serde_json::json!({})).boxed()
} }
fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> { fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
@ -685,41 +682,6 @@ impl LanguageRegistry {
result result
} }
pub fn workspace_configuration(&self, cx: &mut AppContext) -> Task<serde_json::Value> {
let lsp_adapters = {
let state = self.state.read();
state
.available_languages
.iter()
.filter(|l| !l.loaded)
.flat_map(|l| l.lsp_adapters.clone())
.chain(
state
.languages
.iter()
.flat_map(|language| &language.adapters)
.map(|adapter| adapter.adapter.clone()),
)
.collect::<Vec<_>>()
};
let mut language_configs = Vec::new();
for adapter in &lsp_adapters {
if let Some(language_config) = adapter.workspace_configuration(cx) {
language_configs.push(language_config);
}
}
cx.background().spawn(async move {
let mut config = serde_json::json!({});
let language_configs = futures::future::join_all(language_configs).await;
for language_config in language_configs {
merge_json_value_into(language_config, &mut config);
}
config
})
}
pub fn add(&self, language: Arc<Language>) { pub fn add(&self, language: Arc<Language>) {
self.state.write().add(language); self.state.write().add(language);
} }

View file

@ -603,7 +603,7 @@ impl Project {
cx.observe_global::<SettingsStore, _>(Self::on_settings_changed) cx.observe_global::<SettingsStore, _>(Self::on_settings_changed)
], ],
_maintain_buffer_languages: Self::maintain_buffer_languages(languages.clone(), cx), _maintain_buffer_languages: Self::maintain_buffer_languages(languages.clone(), cx),
_maintain_workspace_config: Self::maintain_workspace_config(languages.clone(), cx), _maintain_workspace_config: Self::maintain_workspace_config(cx),
active_entry: None, active_entry: None,
languages, languages,
client, client,
@ -673,7 +673,7 @@ impl Project {
collaborators: Default::default(), collaborators: Default::default(),
join_project_response_message_id: response.message_id, join_project_response_message_id: response.message_id,
_maintain_buffer_languages: Self::maintain_buffer_languages(languages.clone(), cx), _maintain_buffer_languages: Self::maintain_buffer_languages(languages.clone(), cx),
_maintain_workspace_config: Self::maintain_workspace_config(languages.clone(), cx), _maintain_workspace_config: Self::maintain_workspace_config(cx),
languages, languages,
user_store: user_store.clone(), user_store: user_store.clone(),
fs, fs,
@ -2441,35 +2441,42 @@ impl Project {
}) })
} }
fn maintain_workspace_config( fn maintain_workspace_config(cx: &mut ModelContext<Project>) -> Task<()> {
languages: Arc<LanguageRegistry>,
cx: &mut ModelContext<Project>,
) -> Task<()> {
let (mut settings_changed_tx, mut settings_changed_rx) = watch::channel(); let (mut settings_changed_tx, mut settings_changed_rx) = watch::channel();
let _ = postage::stream::Stream::try_recv(&mut settings_changed_rx); let _ = postage::stream::Stream::try_recv(&mut settings_changed_rx);
let settings_observation = cx.observe_global::<SettingsStore, _>(move |_, _| { let settings_observation = cx.observe_global::<SettingsStore, _>(move |_, _| {
*settings_changed_tx.borrow_mut() = (); *settings_changed_tx.borrow_mut() = ();
}); });
cx.spawn_weak(|this, mut cx| async move { cx.spawn_weak(|this, mut cx| async move {
while let Some(_) = settings_changed_rx.next().await { while let Some(_) = settings_changed_rx.next().await {
let workspace_config = cx.update(|cx| languages.workspace_configuration(cx)).await; let Some(this) = this.upgrade(&cx) else {
if let Some(this) = this.upgrade(&cx) {
this.read_with(&cx, |this, _| {
for server_state in this.language_servers.values() {
if let LanguageServerState::Running { server, .. } = server_state {
server
.notify::<lsp::notification::DidChangeConfiguration>(
lsp::DidChangeConfigurationParams {
settings: workspace_config.clone(),
},
)
.ok();
}
}
})
} else {
break; break;
};
let servers: Vec<_> = this.read_with(&cx, |this, _| {
this.language_servers
.values()
.filter_map(|state| match state {
LanguageServerState::Starting(_) => None,
LanguageServerState::Running {
adapter, server, ..
} => Some((adapter.clone(), server.clone())),
})
.collect()
});
for (adapter, server) in servers {
let workspace_config =
cx.update(|cx| adapter.workspace_configuration(cx)).await;
server
.notify::<lsp::notification::DidChangeConfiguration>(
lsp::DidChangeConfigurationParams {
settings: workspace_config.clone(),
},
)
.ok();
} }
} }
@ -2584,7 +2591,6 @@ impl Project {
let state = LanguageServerState::Starting({ let state = LanguageServerState::Starting({
let adapter = adapter.clone(); let adapter = adapter.clone();
let server_name = adapter.name.0.clone(); let server_name = adapter.name.0.clone();
let languages = self.languages.clone();
let language = language.clone(); let language = language.clone();
let key = key.clone(); let key = key.clone();
@ -2594,7 +2600,6 @@ impl Project {
initialization_options, initialization_options,
pending_server, pending_server,
adapter.clone(), adapter.clone(),
languages,
language.clone(), language.clone(),
server_id, server_id,
key, key,
@ -2698,7 +2703,6 @@ impl Project {
initialization_options: Option<serde_json::Value>, initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer, pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>, adapter: Arc<CachedLspAdapter>,
languages: Arc<LanguageRegistry>,
language: Arc<Language>, language: Arc<Language>,
server_id: LanguageServerId, server_id: LanguageServerId,
key: (WorktreeId, LanguageServerName), key: (WorktreeId, LanguageServerName),
@ -2709,7 +2713,6 @@ impl Project {
initialization_options, initialization_options,
pending_server, pending_server,
adapter.clone(), adapter.clone(),
languages,
server_id, server_id,
cx, cx,
); );
@ -2742,11 +2745,10 @@ impl Project {
initialization_options: Option<serde_json::Value>, initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer, pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>, adapter: Arc<CachedLspAdapter>,
languages: Arc<LanguageRegistry>,
server_id: LanguageServerId, server_id: LanguageServerId,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) -> Result<Option<Arc<LanguageServer>>> { ) -> Result<Option<Arc<LanguageServer>>> {
let workspace_config = cx.update(|cx| languages.workspace_configuration(cx)).await; let workspace_config = cx.update(|cx| adapter.workspace_configuration(cx)).await;
let language_server = match pending_server.task.await? { let language_server = match pending_server.task.await? {
Some(server) => server, Some(server) => server,
None => return Ok(None), None => return Ok(None),
@ -2788,12 +2790,12 @@ impl Project {
language_server language_server
.on_request::<lsp::request::WorkspaceConfiguration, _, _>({ .on_request::<lsp::request::WorkspaceConfiguration, _, _>({
let languages = languages.clone(); let adapter = adapter.clone();
move |params, mut cx| { move |params, mut cx| {
let languages = languages.clone(); let adapter = adapter.clone();
async move { async move {
let workspace_config = let workspace_config =
cx.update(|cx| languages.workspace_configuration(cx)).await; cx.update(|cx| adapter.workspace_configuration(cx)).await;
Ok(params Ok(params
.items .items
.into_iter() .into_iter()

View file

@ -102,7 +102,7 @@ impl LspAdapter for JsonLspAdapter {
fn workspace_configuration( fn workspace_configuration(
&self, &self,
cx: &mut AppContext, cx: &mut AppContext,
) -> Option<BoxFuture<'static, serde_json::Value>> { ) -> BoxFuture<'static, serde_json::Value> {
let action_names = cx.all_action_names().collect::<Vec<_>>(); let action_names = cx.all_action_names().collect::<Vec<_>>();
let staff_mode = cx.default_global::<StaffMode>().0; let staff_mode = cx.default_global::<StaffMode>().0;
let language_names = &self.languages.language_names(); let language_names = &self.languages.language_names();
@ -113,29 +113,28 @@ impl LspAdapter for JsonLspAdapter {
}, },
cx, cx,
); );
Some(
future::ready(serde_json::json!({ future::ready(serde_json::json!({
"json": { "json": {
"format": { "format": {
"enable": true, "enable": true,
},
"schemas": [
{
"fileMatch": [
schema_file_match(&paths::SETTINGS),
&*paths::LOCAL_SETTINGS_RELATIVE_PATH,
],
"schema": settings_schema,
}, },
"schemas": [ {
{ "fileMatch": [schema_file_match(&paths::KEYMAP)],
"fileMatch": [ "schema": KeymapFile::generate_json_schema(&action_names),
schema_file_match(&paths::SETTINGS), }
&*paths::LOCAL_SETTINGS_RELATIVE_PATH, ]
], }
"schema": settings_schema, }))
}, .boxed()
{
"fileMatch": [schema_file_match(&paths::KEYMAP)],
"schema": KeymapFile::generate_json_schema(&action_names),
}
]
}
}))
.boxed(),
)
} }
async fn language_ids(&self) -> HashMap<String, String> { async fn language_ids(&self) -> HashMap<String, String> {

View file

@ -103,23 +103,24 @@ impl LspAdapter for TailwindLspAdapter {
})) }))
} }
fn workspace_configuration(&self, _: &mut AppContext) -> Option<BoxFuture<'static, Value>> { fn workspace_configuration(&self, _: &mut AppContext) -> BoxFuture<'static, Value> {
Some( future::ready(json!({
future::ready(json!({ "tailwindCSS": {
"tailwindCSS": { "emmetCompletions": true,
"emmetCompletions": true, }
} }))
})) .boxed()
.boxed(),
)
} }
async fn language_ids(&self) -> HashMap<String, String> { async fn language_ids(&self) -> HashMap<String, String> {
HashMap::from([ HashMap::from_iter(
("HTML".to_string(), "html".to_string()), [
("CSS".to_string(), "css".to_string()), ("HTML".to_string(), "html".to_string()),
("JavaScript".to_string(), "javascript".to_string()), ("CSS".to_string(), "css".to_string()),
]) ("JavaScript".to_string(), "javascript".to_string()),
]
.into_iter(),
)
} }
} }

View file

@ -202,18 +202,16 @@ impl EsLintLspAdapter {
#[async_trait] #[async_trait]
impl LspAdapter for EsLintLspAdapter { impl LspAdapter for EsLintLspAdapter {
fn workspace_configuration(&self, _: &mut AppContext) -> Option<BoxFuture<'static, Value>> { fn workspace_configuration(&self, _: &mut AppContext) -> BoxFuture<'static, Value> {
Some( future::ready(json!({
future::ready(json!({ "": {
"": { "validate": "on",
"validate": "on", "rulesCustomizations": [],
"rulesCustomizations": [], "run": "onType",
"run": "onType", "nodePath": null,
"nodePath": null, }
} }))
})) .boxed()
.boxed(),
)
} }
async fn name(&self) -> LanguageServerName { async fn name(&self) -> LanguageServerName {

View file

@ -86,21 +86,20 @@ impl LspAdapter for YamlLspAdapter {
) -> Option<LanguageServerBinary> { ) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &self.node).await get_cached_server_binary(container_dir, &self.node).await
} }
fn workspace_configuration(&self, cx: &mut AppContext) -> Option<BoxFuture<'static, Value>> { fn workspace_configuration(&self, cx: &mut AppContext) -> BoxFuture<'static, Value> {
let tab_size = all_language_settings(None, cx) let tab_size = all_language_settings(None, cx)
.language(Some("YAML")) .language(Some("YAML"))
.tab_size; .tab_size;
Some(
future::ready(serde_json::json!({ future::ready(serde_json::json!({
"yaml": { "yaml": {
"keyOrdering": false "keyOrdering": false
}, },
"[yaml]": { "[yaml]": {
"editor.tabSize": tab_size, "editor.tabSize": tab_size,
} }
})) }))
.boxed(), .boxed()
)
} }
} }