Move lsp configuration into language crate

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-10-26 12:17:51 -07:00
parent de8218314c
commit 7d5425e142
8 changed files with 79 additions and 47 deletions

View file

@ -1,5 +1,6 @@
use crate::HighlightMap;
use anyhow::Result;
use gpui::AppContext;
use parking_lot::Mutex;
use serde::Deserialize;
use std::{path::Path, str, sync::Arc};
@ -12,6 +13,13 @@ pub struct LanguageConfig {
pub name: String,
pub path_suffixes: Vec<String>,
pub brackets: Vec<BracketPair>,
pub language_server: Option<LanguageServerConfig>,
}
#[derive(Deserialize)]
pub struct LanguageServerConfig {
pub binary: String,
pub disk_based_diagnostic_sources: Vec<String>,
}
#[derive(Clone, Debug, Deserialize)]
@ -51,6 +59,12 @@ impl LanguageRegistry {
}
}
pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
self.languages
.iter()
.find(|language| language.name() == name)
}
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
let path = path.as_ref();
let filename = path.file_name().and_then(|name| name.to_str());
@ -97,6 +111,32 @@ impl Language {
self.config.name.as_str()
}
pub fn start_server(
&self,
root_path: &Path,
cx: &AppContext,
) -> Result<Option<Arc<lsp::LanguageServer>>> {
if let Some(config) = &self.config.language_server {
const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
let binary_path = if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
cx.platform()
.path_for_resource(Some(&config.binary), None)?
} else {
Path::new(&config.binary).to_path_buf()
};
lsp::LanguageServer::new(&binary_path, root_path, cx.background()).map(Some)
} else {
Ok(None)
}
}
pub fn disk_based_diagnostic_sources(&self) -> &[String] {
self.config
.language_server
.as_ref()
.map_or(&[], |config| &config.disk_based_diagnostic_sources)
}
pub fn brackets(&self) -> &[BracketPair] {
&self.config.brackets
}