Start computing workspace configuration more dynamically

This commit is contained in:
Antonio Scandurra
2023-03-16 10:46:55 +01:00
parent ed9927b495
commit 60d3fb48e2
8 changed files with 174 additions and 92 deletions
+6 -2
View File
@@ -2,6 +2,7 @@ use anyhow::Context;
pub use language::*;
use rust_embed::RustEmbed;
use std::{borrow::Cow, str, sync::Arc};
use theme::ThemeRegistry;
mod c;
mod elixir;
@@ -31,7 +32,7 @@ mod yaml;
#[exclude = "*.rs"]
struct LanguageDir;
pub fn init(languages: Arc<LanguageRegistry>) {
pub fn init(languages: Arc<LanguageRegistry>, themes: Arc<ThemeRegistry>) {
for (name, grammar, lsp_adapter) in [
(
"c",
@@ -61,7 +62,10 @@ pub fn init(languages: Arc<LanguageRegistry>) {
(
"json",
tree_sitter_json::language(),
Some(Box::new(json::JsonLspAdapter)),
Some(Box::new(json::JsonLspAdapter::new(
languages.clone(),
themes.clone(),
))),
),
(
"markdown",
+61 -5
View File
@@ -4,14 +4,32 @@ use async_compression::futures::bufread::GzipDecoder;
use async_trait::async_trait;
use client::http::HttpClient;
use collections::HashMap;
use futures::{io::BufReader, StreamExt};
use language::{LanguageServerName, LspAdapter};
use futures::{future::BoxFuture, io::BufReader, FutureExt, StreamExt};
use gpui::MutableAppContext;
use language::{LanguageRegistry, LanguageServerName, LspAdapter};
use serde_json::json;
use settings::{keymap_file_json_schema, settings_file_json_schema};
use smol::fs::{self, File};
use std::{any::Any, env::consts, path::PathBuf, sync::Arc};
use util::ResultExt;
use std::{
any::Any,
env::consts,
future,
path::{Path, PathBuf},
sync::Arc,
};
use theme::ThemeRegistry;
use util::{paths, ResultExt, StaffMode};
pub struct JsonLspAdapter;
pub struct JsonLspAdapter {
languages: Arc<LanguageRegistry>,
themes: Arc<ThemeRegistry>,
}
impl JsonLspAdapter {
pub fn new(languages: Arc<LanguageRegistry>, themes: Arc<ThemeRegistry>) -> Self {
Self { languages, themes }
}
}
#[async_trait]
impl LspAdapter for JsonLspAdapter {
@@ -102,7 +120,45 @@ impl LspAdapter for JsonLspAdapter {
}))
}
fn workspace_configuration(
&self,
cx: &mut MutableAppContext,
) -> Option<BoxFuture<'static, serde_json::Value>> {
let action_names = cx.all_action_names().collect::<Vec<_>>();
let theme_names = self
.themes
.list(**cx.default_global::<StaffMode>())
.map(|meta| meta.name)
.collect();
let language_names = self.languages.language_names();
Some(
future::ready(serde_json::json!({
"json": {
"format": {
"enable": true,
},
"schemas": [
{
"fileMatch": [schema_file_match(&paths::SETTINGS)],
"schema": settings_file_json_schema(theme_names, &language_names),
},
{
"fileMatch": [schema_file_match(&paths::KEYMAP)],
"schema": keymap_file_json_schema(&action_names),
}
]
}
}))
.boxed(),
)
}
async fn language_ids(&self) -> HashMap<String, String> {
[("JSON".into(), "jsonc".into())].into_iter().collect()
}
}
fn schema_file_match(path: &Path) -> &Path {
path.strip_prefix(path.parent().unwrap().parent().unwrap())
.unwrap()
}
+1 -1
View File
@@ -139,7 +139,7 @@ fn main() {
languages.set_executor(cx.background().clone());
languages.set_language_server_download_dir(paths::LANGUAGES_DIR.clone());
let languages = Arc::new(languages);
languages::init(languages.clone());
languages::init(languages.clone(), themes.clone());
let user_store = cx.add_model(|cx| UserStore::new(client.clone(), http.clone(), cx));
cx.set_global(client.clone());
+4 -36
View File
@@ -29,10 +29,10 @@ use project_panel::ProjectPanel;
use search::{BufferSearchBar, ProjectSearchBar};
use serde::Deserialize;
use serde_json::to_string_pretty;
use settings::{keymap_file_json_schema, settings_file_json_schema, Settings};
use settings::Settings;
use std::{borrow::Cow, env, path::Path, str, sync::Arc};
use terminal_view::terminal_button::{self, TerminalButton};
use util::{channel::ReleaseChannel, paths, ResultExt, StaffMode};
use util::{channel::ReleaseChannel, paths, ResultExt};
use uuid::Uuid;
pub use workspace;
use workspace::{sidebar::SidebarSide, AppState, Restart, Workspace};
@@ -296,34 +296,6 @@ pub fn initialize_workspace(
cx.emit(workspace::Event::PaneAdded(workspace.active_pane().clone()));
cx.emit(workspace::Event::PaneAdded(workspace.dock_pane().clone()));
let theme_names = app_state
.themes
.list(**cx.default_global::<StaffMode>())
.map(|meta| meta.name)
.collect();
let language_names = app_state.languages.language_names();
workspace.project().update(cx, |project, cx| {
let action_names = cx.all_action_names().collect::<Vec<_>>();
project.set_language_server_settings(serde_json::json!({
"json": {
"format": {
"enable": true,
},
"schemas": [
{
"fileMatch": [schema_file_match(&paths::SETTINGS)],
"schema": settings_file_json_schema(theme_names, &language_names),
},
{
"fileMatch": [schema_file_match(&paths::KEYMAP)],
"schema": keymap_file_json_schema(&action_names),
}
]
}
}));
});
let collab_titlebar_item =
cx.add_view(|cx| CollabTitlebarItem::new(&workspace_handle, &app_state.user_store, cx));
workspace.set_titlebar_item(collab_titlebar_item, cx);
@@ -676,11 +648,6 @@ fn open_bundled_file(
.detach();
}
fn schema_file_match(path: &Path) -> &Path {
path.strip_prefix(path.parent().unwrap().parent().unwrap())
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
@@ -1882,7 +1849,8 @@ mod tests {
let mut languages = LanguageRegistry::new(Task::ready(()));
languages.set_executor(cx.background().clone());
let languages = Arc::new(languages);
languages::init(languages.clone());
let themes = ThemeRegistry::new((), cx.font_cache().clone());
languages::init(languages.clone(), themes);
for name in languages.language_names() {
languages.language_for_name(&name);
}