2021-11-30 19:46:39 +00:00
|
|
|
mod buffer;
|
2021-12-09 15:38:46 +00:00
|
|
|
mod diagnostic_set;
|
2021-11-30 19:46:39 +00:00
|
|
|
mod highlight_map;
|
2022-01-13 02:17:19 +00:00
|
|
|
mod outline;
|
2021-11-30 19:46:39 +00:00
|
|
|
pub mod proto;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2022-03-03 21:29:25 +00:00
|
|
|
use anyhow::{anyhow, Context, Result};
|
2022-03-30 23:48:57 +00:00
|
|
|
use client::http::HttpClient;
|
2022-03-30 01:11:14 +00:00
|
|
|
use collections::HashMap;
|
2022-02-21 15:07:09 +00:00
|
|
|
use futures::{
|
|
|
|
future::{BoxFuture, Shared},
|
2022-02-21 21:54:52 +00:00
|
|
|
FutureExt, TryFutureExt,
|
2022-02-21 15:07:09 +00:00
|
|
|
};
|
2022-02-23 14:45:12 +00:00
|
|
|
use gpui::{MutableAppContext, Task};
|
2021-11-30 19:46:39 +00:00
|
|
|
use highlight_map::HighlightMap;
|
2021-11-29 17:01:51 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-02-22 18:35:20 +00:00
|
|
|
use parking_lot::{Mutex, RwLock};
|
2021-10-04 14:50:12 +00:00
|
|
|
use serde::Deserialize;
|
2022-03-03 22:16:56 +00:00
|
|
|
use serde_json::Value;
|
2022-02-21 08:46:18 +00:00
|
|
|
use std::{
|
2022-03-29 20:42:21 +00:00
|
|
|
any::Any,
|
2022-02-21 08:46:18 +00:00
|
|
|
cell::RefCell,
|
|
|
|
ops::Range,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
str,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2021-10-05 18:23:00 +00:00
|
|
|
use theme::SyntaxTheme;
|
2021-11-29 16:38:59 +00:00
|
|
|
use tree_sitter::{self, Query};
|
2022-02-22 00:44:00 +00:00
|
|
|
use util::ResultExt;
|
2022-02-05 01:35:37 +00:00
|
|
|
|
2022-02-17 00:10:36 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
use futures::channel::mpsc;
|
|
|
|
|
2022-02-05 01:35:37 +00:00
|
|
|
pub use buffer::Operation;
|
|
|
|
pub use buffer::*;
|
|
|
|
pub use diagnostic_set::DiagnosticEntry;
|
|
|
|
pub use outline::{Outline, OutlineItem};
|
2021-10-04 14:50:12 +00:00
|
|
|
pub use tree_sitter::{Parser, Tree};
|
|
|
|
|
2022-02-03 01:01:48 +00:00
|
|
|
thread_local! {
|
|
|
|
static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
|
|
|
|
}
|
|
|
|
|
2021-11-29 17:01:51 +00:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
|
|
|
|
LanguageConfig {
|
2022-02-22 18:35:20 +00:00
|
|
|
name: "Plain Text".into(),
|
2021-11-29 17:01:51 +00:00
|
|
|
path_suffixes: Default::default(),
|
|
|
|
brackets: Default::default(),
|
2022-03-14 22:17:40 +00:00
|
|
|
autoclose_before: Default::default(),
|
2021-11-29 17:01:51 +00:00
|
|
|
line_comment: None,
|
|
|
|
},
|
|
|
|
None,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-01-31 18:11:13 +00:00
|
|
|
pub trait ToLspPosition {
|
|
|
|
fn to_lsp_position(self) -> lsp::Position;
|
|
|
|
}
|
|
|
|
|
2022-03-29 23:57:18 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
|
|
|
pub struct LanguageServerName(pub Arc<str>);
|
|
|
|
|
2022-03-03 21:29:25 +00:00
|
|
|
pub trait LspAdapter: 'static + Send + Sync {
|
2022-03-29 23:57:18 +00:00
|
|
|
fn name(&self) -> LanguageServerName;
|
2022-02-22 00:11:40 +00:00
|
|
|
fn fetch_latest_server_version(
|
2022-02-21 21:54:52 +00:00
|
|
|
&self,
|
|
|
|
http: Arc<dyn HttpClient>,
|
2022-03-29 20:42:21 +00:00
|
|
|
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>>;
|
2022-02-22 00:11:40 +00:00
|
|
|
fn fetch_server_binary(
|
|
|
|
&self,
|
2022-03-29 20:42:21 +00:00
|
|
|
version: Box<dyn 'static + Send + Any>,
|
2022-02-22 00:11:40 +00:00
|
|
|
http: Arc<dyn HttpClient>,
|
2022-03-03 21:29:25 +00:00
|
|
|
container_dir: PathBuf,
|
2022-02-21 21:54:52 +00:00
|
|
|
) -> BoxFuture<'static, Result<PathBuf>>;
|
2022-03-03 21:29:25 +00:00
|
|
|
fn cached_server_binary(&self, container_dir: PathBuf) -> BoxFuture<'static, Option<PathBuf>>;
|
2022-01-25 16:49:50 +00:00
|
|
|
fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
|
2022-03-03 22:16:56 +00:00
|
|
|
|
2022-02-22 09:01:08 +00:00
|
|
|
fn label_for_completion(&self, _: &lsp::CompletionItem, _: &Language) -> Option<CodeLabel> {
|
|
|
|
None
|
|
|
|
}
|
2022-03-03 22:16:56 +00:00
|
|
|
|
2022-02-22 17:08:43 +00:00
|
|
|
fn label_for_symbol(&self, _: &str, _: lsp::SymbolKind, _: &Language) -> Option<CodeLabel> {
|
2022-02-02 17:43:55 +00:00
|
|
|
None
|
|
|
|
}
|
2022-03-03 21:29:25 +00:00
|
|
|
|
|
|
|
fn server_args(&self) -> &[&str] {
|
|
|
|
&[]
|
|
|
|
}
|
2022-03-03 22:16:56 +00:00
|
|
|
|
|
|
|
fn initialization_options(&self) -> Option<Value> {
|
|
|
|
None
|
|
|
|
}
|
2022-03-30 01:11:14 +00:00
|
|
|
|
|
|
|
fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
|
|
|
|
Default::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
|
|
|
|
None
|
|
|
|
}
|
2022-01-25 16:49:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:14:30 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
2022-02-22 09:01:08 +00:00
|
|
|
pub struct CodeLabel {
|
2022-02-03 02:14:30 +00:00
|
|
|
pub text: String,
|
|
|
|
pub runs: Vec<(Range<usize>, HighlightId)>,
|
|
|
|
pub filter_range: Range<usize>,
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
#[derive(Deserialize)]
|
2021-10-04 14:50:12 +00:00
|
|
|
pub struct LanguageConfig {
|
2022-02-22 18:35:20 +00:00
|
|
|
pub name: Arc<str>,
|
2021-10-04 14:50:12 +00:00
|
|
|
pub path_suffixes: Vec<String>,
|
2021-10-19 11:17:16 +00:00
|
|
|
pub brackets: Vec<BracketPair>,
|
2022-03-14 22:17:40 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub autoclose_before: String,
|
2021-11-23 22:13:28 +00:00
|
|
|
pub line_comment: Option<String>,
|
2021-10-26 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
impl Default for LanguageConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "".into(),
|
|
|
|
path_suffixes: Default::default(),
|
|
|
|
brackets: Default::default(),
|
2022-03-14 22:17:40 +00:00
|
|
|
autoclose_before: Default::default(),
|
2022-02-22 18:35:20 +00:00
|
|
|
line_comment: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 00:10:36 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-30 00:54:29 +00:00
|
|
|
pub struct FakeLspAdapter {
|
|
|
|
pub name: &'static str,
|
|
|
|
pub capabilities: lsp::ServerCapabilities,
|
|
|
|
pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
|
2022-03-30 01:11:14 +00:00
|
|
|
pub disk_based_diagnostics_progress_token: Option<&'static str>,
|
|
|
|
pub disk_based_diagnostics_sources: &'static [&'static str],
|
2021-10-06 14:34:57 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 11:17:16 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize)]
|
|
|
|
pub struct BracketPair {
|
2021-10-06 14:34:57 +00:00
|
|
|
pub start: String,
|
|
|
|
pub end: String,
|
2021-10-19 11:17:16 +00:00
|
|
|
pub close: bool,
|
|
|
|
pub newline: bool,
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Language {
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) config: LanguageConfig,
|
2021-11-29 16:38:59 +00:00
|
|
|
pub(crate) grammar: Option<Arc<Grammar>>,
|
2022-03-03 21:29:25 +00:00
|
|
|
pub(crate) adapter: Option<Arc<dyn LspAdapter>>,
|
2022-03-30 00:54:29 +00:00
|
|
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
fake_adapter: Option<(
|
|
|
|
mpsc::UnboundedSender<lsp::FakeLanguageServer>,
|
|
|
|
Arc<FakeLspAdapter>,
|
|
|
|
)>,
|
2021-11-29 16:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Grammar {
|
|
|
|
pub(crate) ts_language: tree_sitter::Language,
|
2021-10-06 23:09:30 +00:00
|
|
|
pub(crate) highlights_query: Query,
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) brackets_query: Query,
|
2021-10-06 23:09:30 +00:00
|
|
|
pub(crate) indents_query: Query,
|
2022-01-13 02:17:19 +00:00
|
|
|
pub(crate) outline_query: Query,
|
2021-10-05 18:23:00 +00:00
|
|
|
pub(crate) highlight_map: Mutex<HighlightMap>,
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 00:11:40 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub enum LanguageServerBinaryStatus {
|
|
|
|
CheckingForUpdate,
|
|
|
|
Downloading,
|
|
|
|
Downloaded,
|
|
|
|
Cached,
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
2021-10-04 17:59:03 +00:00
|
|
|
pub struct LanguageRegistry {
|
2022-02-22 18:35:20 +00:00
|
|
|
languages: RwLock<Vec<Arc<Language>>>,
|
2022-02-22 00:23:38 +00:00
|
|
|
language_server_download_dir: Option<Arc<Path>>,
|
2022-02-22 00:11:40 +00:00
|
|
|
lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
|
|
|
|
lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
|
2022-03-08 00:39:50 +00:00
|
|
|
login_shell_env_loaded: Shared<Task<()>>,
|
2022-03-29 23:57:18 +00:00
|
|
|
lsp_binary_paths: Mutex<
|
|
|
|
HashMap<
|
|
|
|
LanguageServerName,
|
|
|
|
Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>,
|
|
|
|
>,
|
|
|
|
>,
|
2021-10-04 17:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LanguageRegistry {
|
2022-03-08 00:39:50 +00:00
|
|
|
pub fn new(login_shell_env_loaded: Task<()>) -> Self {
|
2022-02-22 00:11:40 +00:00
|
|
|
let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
|
2022-02-21 16:25:52 +00:00
|
|
|
Self {
|
2022-02-22 00:23:38 +00:00
|
|
|
language_server_download_dir: None,
|
2022-02-21 16:25:52 +00:00
|
|
|
languages: Default::default(),
|
2022-02-22 00:11:40 +00:00
|
|
|
lsp_binary_statuses_tx,
|
|
|
|
lsp_binary_statuses_rx,
|
2022-03-08 00:39:50 +00:00
|
|
|
login_shell_env_loaded: login_shell_env_loaded.shared(),
|
2022-03-29 23:57:18 +00:00
|
|
|
lsp_binary_paths: Default::default(),
|
2022-02-21 16:25:52 +00:00
|
|
|
}
|
2021-10-04 17:59:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-08 00:39:50 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn test() -> Self {
|
|
|
|
Self::new(Task::ready(()))
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
pub fn add(&self, language: Arc<Language>) {
|
|
|
|
self.languages.write().push(language.clone());
|
2021-10-04 17:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_theme(&self, theme: &SyntaxTheme) {
|
2022-02-22 18:35:20 +00:00
|
|
|
for language in self.languages.read().iter() {
|
2021-10-04 17:59:03 +00:00
|
|
|
language.set_theme(theme);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 00:23:38 +00:00
|
|
|
pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
|
|
|
|
self.language_server_download_dir = Some(path.into());
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
pub fn get_language(&self, name: &str) -> Option<Arc<Language>> {
|
2021-10-26 19:17:51 +00:00
|
|
|
self.languages
|
2022-02-22 18:35:20 +00:00
|
|
|
.read()
|
2021-10-26 19:17:51 +00:00
|
|
|
.iter()
|
2022-02-22 18:35:20 +00:00
|
|
|
.find(|language| language.name().as_ref() == name)
|
|
|
|
.cloned()
|
2021-10-26 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
pub fn select_language(&self, path: impl AsRef<Path>) -> Option<Arc<Language>> {
|
2021-10-04 17:59:03 +00:00
|
|
|
let path = path.as_ref();
|
|
|
|
let filename = path.file_name().and_then(|name| name.to_str());
|
|
|
|
let extension = path.extension().and_then(|name| name.to_str());
|
|
|
|
let path_suffixes = [extension, filename];
|
2022-02-22 18:35:20 +00:00
|
|
|
self.languages
|
|
|
|
.read()
|
|
|
|
.iter()
|
|
|
|
.find(|language| {
|
|
|
|
language
|
|
|
|
.config
|
|
|
|
.path_suffixes
|
|
|
|
.iter()
|
|
|
|
.any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
|
|
|
|
})
|
|
|
|
.cloned()
|
2021-10-04 17:59:03 +00:00
|
|
|
}
|
2022-02-21 16:25:52 +00:00
|
|
|
|
2022-02-21 21:54:52 +00:00
|
|
|
pub fn start_language_server(
|
2022-03-29 23:57:18 +00:00
|
|
|
self: &Arc<Self>,
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id: usize,
|
2022-03-08 00:39:50 +00:00
|
|
|
language: Arc<Language>,
|
2022-02-21 21:54:52 +00:00
|
|
|
root_path: Arc<Path>,
|
|
|
|
http_client: Arc<dyn HttpClient>,
|
2022-02-23 14:45:12 +00:00
|
|
|
cx: &mut MutableAppContext,
|
2022-03-09 11:29:28 +00:00
|
|
|
) -> Option<Task<Result<lsp::LanguageServer>>> {
|
2022-02-21 21:54:52 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-30 00:54:29 +00:00
|
|
|
if language.fake_adapter.is_some() {
|
2022-03-09 01:41:52 +00:00
|
|
|
let language = language.clone();
|
|
|
|
return Some(cx.spawn(|mut cx| async move {
|
2022-03-30 00:54:29 +00:00
|
|
|
let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
|
2022-03-09 11:29:28 +00:00
|
|
|
let (server, mut fake_server) = cx.update(|cx| {
|
|
|
|
lsp::LanguageServer::fake_with_capabilities(
|
2022-03-30 00:54:29 +00:00
|
|
|
fake_adapter.capabilities.clone(),
|
2022-03-09 11:29:28 +00:00
|
|
|
cx,
|
|
|
|
)
|
|
|
|
});
|
2022-03-30 00:54:29 +00:00
|
|
|
|
|
|
|
if let Some(initializer) = &fake_adapter.initializer {
|
2022-03-09 11:29:28 +00:00
|
|
|
initializer(&mut fake_server);
|
2022-02-21 21:54:52 +00:00
|
|
|
}
|
2022-03-09 11:29:28 +00:00
|
|
|
|
2022-03-30 00:54:29 +00:00
|
|
|
let servers_tx = servers_tx.clone();
|
2022-03-09 11:29:28 +00:00
|
|
|
cx.background()
|
|
|
|
.spawn(async move {
|
|
|
|
fake_server
|
|
|
|
.receive_notification::<lsp::notification::Initialized>()
|
|
|
|
.await;
|
|
|
|
servers_tx.unbounded_send(fake_server).ok();
|
|
|
|
})
|
|
|
|
.detach();
|
|
|
|
Ok(server)
|
2022-03-09 01:41:52 +00:00
|
|
|
}));
|
2022-02-21 21:54:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 00:23:38 +00:00
|
|
|
let download_dir = self
|
|
|
|
.language_server_download_dir
|
|
|
|
.clone()
|
2022-02-22 00:44:00 +00:00
|
|
|
.ok_or_else(|| anyhow!("language server download directory has not been assigned"))
|
|
|
|
.log_err()?;
|
2022-02-22 00:23:38 +00:00
|
|
|
|
2022-03-29 23:57:18 +00:00
|
|
|
let this = self.clone();
|
2022-03-03 21:29:25 +00:00
|
|
|
let adapter = language.adapter.clone()?;
|
2022-02-21 21:54:52 +00:00
|
|
|
let background = cx.background().clone();
|
2022-03-08 00:39:50 +00:00
|
|
|
let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
|
|
|
|
let login_shell_env_loaded = self.login_shell_env_loaded.clone();
|
2022-02-21 21:54:52 +00:00
|
|
|
Some(cx.background().spawn(async move {
|
2022-03-08 00:39:50 +00:00
|
|
|
login_shell_env_loaded.await;
|
2022-03-29 23:57:18 +00:00
|
|
|
let server_binary_path = this
|
|
|
|
.lsp_binary_paths
|
2022-03-08 00:39:50 +00:00
|
|
|
.lock()
|
2022-03-29 23:57:18 +00:00
|
|
|
.entry(adapter.name())
|
|
|
|
.or_insert_with(|| {
|
2022-03-08 00:39:50 +00:00
|
|
|
get_server_binary_path(
|
|
|
|
adapter.clone(),
|
|
|
|
language.clone(),
|
|
|
|
http_client,
|
|
|
|
download_dir,
|
|
|
|
lsp_binary_statuses,
|
|
|
|
)
|
|
|
|
.map_err(Arc::new)
|
|
|
|
.boxed()
|
|
|
|
.shared()
|
|
|
|
})
|
|
|
|
.clone()
|
|
|
|
.map_err(|e| anyhow!(e));
|
|
|
|
|
2022-02-21 21:54:52 +00:00
|
|
|
let server_binary_path = server_binary_path.await?;
|
2022-03-03 21:29:25 +00:00
|
|
|
let server_args = adapter.server_args();
|
2022-03-12 01:36:27 +00:00
|
|
|
let server = lsp::LanguageServer::new(
|
2022-03-30 00:24:23 +00:00
|
|
|
server_id,
|
2022-03-03 22:16:56 +00:00
|
|
|
&server_binary_path,
|
|
|
|
server_args,
|
|
|
|
&root_path,
|
2022-03-09 11:29:28 +00:00
|
|
|
adapter.initialization_options(),
|
2022-03-03 22:16:56 +00:00
|
|
|
background,
|
2022-03-11 19:46:22 +00:00
|
|
|
)?;
|
|
|
|
Ok(server)
|
2022-02-21 21:54:52 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-02-22 00:11:40 +00:00
|
|
|
pub fn language_server_binary_statuses(
|
|
|
|
&self,
|
|
|
|
) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
|
|
|
|
self.lsp_binary_statuses_rx.clone()
|
2022-02-21 16:25:52 +00:00
|
|
|
}
|
2021-10-04 17:59:03 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 00:11:40 +00:00
|
|
|
async fn get_server_binary_path(
|
2022-03-03 21:29:25 +00:00
|
|
|
adapter: Arc<dyn LspAdapter>,
|
2022-02-22 00:11:40 +00:00
|
|
|
language: Arc<Language>,
|
|
|
|
http_client: Arc<dyn HttpClient>,
|
2022-02-22 00:23:38 +00:00
|
|
|
download_dir: Arc<Path>,
|
2022-02-22 00:11:40 +00:00
|
|
|
statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
|
|
|
|
) -> Result<PathBuf> {
|
2022-03-29 23:57:18 +00:00
|
|
|
let container_dir = download_dir.join(adapter.name().0.as_ref());
|
2022-03-03 21:29:25 +00:00
|
|
|
if !container_dir.exists() {
|
|
|
|
smol::fs::create_dir_all(&container_dir)
|
|
|
|
.await
|
|
|
|
.context("failed to create container directory")?;
|
|
|
|
}
|
|
|
|
|
2022-02-22 00:11:40 +00:00
|
|
|
let path = fetch_latest_server_binary_path(
|
2022-03-03 21:29:25 +00:00
|
|
|
adapter.clone(),
|
2022-02-22 00:11:40 +00:00
|
|
|
language.clone(),
|
|
|
|
http_client,
|
2022-03-03 21:29:25 +00:00
|
|
|
&container_dir,
|
2022-02-22 00:11:40 +00:00
|
|
|
statuses.clone(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if path.is_err() {
|
2022-03-03 21:29:25 +00:00
|
|
|
if let Some(cached_path) = adapter.cached_server_binary(container_dir).await {
|
2022-02-22 00:11:40 +00:00
|
|
|
statuses
|
|
|
|
.broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
|
|
|
|
.await?;
|
|
|
|
return Ok(cached_path);
|
|
|
|
} else {
|
|
|
|
statuses
|
|
|
|
.broadcast((language.clone(), LanguageServerBinaryStatus::Failed))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn fetch_latest_server_binary_path(
|
2022-03-03 21:29:25 +00:00
|
|
|
adapter: Arc<dyn LspAdapter>,
|
2022-02-22 00:11:40 +00:00
|
|
|
language: Arc<Language>,
|
|
|
|
http_client: Arc<dyn HttpClient>,
|
2022-03-03 21:29:25 +00:00
|
|
|
container_dir: &Path,
|
2022-02-22 00:11:40 +00:00
|
|
|
lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
|
|
|
|
) -> Result<PathBuf> {
|
|
|
|
lsp_binary_statuses_tx
|
|
|
|
.broadcast((
|
|
|
|
language.clone(),
|
|
|
|
LanguageServerBinaryStatus::CheckingForUpdate,
|
|
|
|
))
|
|
|
|
.await?;
|
2022-03-03 21:29:25 +00:00
|
|
|
let version_info = adapter
|
2022-02-22 00:11:40 +00:00
|
|
|
.fetch_latest_server_version(http_client.clone())
|
|
|
|
.await?;
|
|
|
|
lsp_binary_statuses_tx
|
|
|
|
.broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
|
|
|
|
.await?;
|
2022-03-03 21:29:25 +00:00
|
|
|
let path = adapter
|
|
|
|
.fetch_server_binary(version_info, http_client, container_dir.to_path_buf())
|
2022-02-22 00:11:40 +00:00
|
|
|
.await?;
|
|
|
|
lsp_binary_statuses_tx
|
|
|
|
.broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
|
|
|
|
.await?;
|
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
2021-10-04 14:50:12 +00:00
|
|
|
impl Language {
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
|
2021-10-05 18:23:00 +00:00
|
|
|
Self {
|
|
|
|
config,
|
2021-11-29 16:38:59 +00:00
|
|
|
grammar: ts_language.map(|ts_language| {
|
|
|
|
Arc::new(Grammar {
|
|
|
|
brackets_query: Query::new(ts_language, "").unwrap(),
|
|
|
|
highlights_query: Query::new(ts_language, "").unwrap(),
|
|
|
|
indents_query: Query::new(ts_language, "").unwrap(),
|
2022-01-13 02:17:19 +00:00
|
|
|
outline_query: Query::new(ts_language, "").unwrap(),
|
2021-11-29 16:38:59 +00:00
|
|
|
ts_language,
|
|
|
|
highlight_map: Default::default(),
|
|
|
|
})
|
|
|
|
}),
|
2022-03-03 21:29:25 +00:00
|
|
|
adapter: None,
|
2022-03-30 00:54:29 +00:00
|
|
|
|
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
fake_adapter: None,
|
2021-10-05 18:23:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 23:57:18 +00:00
|
|
|
pub fn lsp_adapter(&self) -> Option<Arc<dyn LspAdapter>> {
|
|
|
|
self.adapter.clone()
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:09:30 +00:00
|
|
|
pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.highlights_query = Query::new(grammar.ts_language, source)?;
|
2021-10-05 18:23:00 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2021-10-06 23:09:30 +00:00
|
|
|
pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.brackets_query = Query::new(grammar.ts_language, source)?;
|
2021-10-06 23:09:30 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
|
2021-11-29 16:38:59 +00:00
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.indents_query = Query::new(grammar.ts_language, source)?;
|
2021-10-05 18:23:00 +00:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2022-01-13 02:17:19 +00:00
|
|
|
pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
|
|
|
|
let grammar = self
|
|
|
|
.grammar
|
|
|
|
.as_mut()
|
|
|
|
.and_then(Arc::get_mut)
|
|
|
|
.ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
|
|
|
|
grammar.outline_query = Query::new(grammar.ts_language, source)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
2022-03-29 01:01:29 +00:00
|
|
|
pub fn with_lsp_adapter(mut self, lsp_adapter: Arc<dyn LspAdapter>) -> Self {
|
|
|
|
self.adapter = Some(lsp_adapter);
|
2022-01-25 16:49:50 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-03-30 00:54:29 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
|
|
|
pub fn set_fake_lsp_adapter(
|
|
|
|
&mut self,
|
|
|
|
fake_lsp_adapter: FakeLspAdapter,
|
|
|
|
) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
|
|
|
|
let (servers_tx, servers_rx) = mpsc::unbounded();
|
|
|
|
let adapter = Arc::new(fake_lsp_adapter);
|
|
|
|
self.fake_adapter = Some((servers_tx, adapter.clone()));
|
|
|
|
self.adapter = Some(adapter);
|
|
|
|
servers_rx
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:35:20 +00:00
|
|
|
pub fn name(&self) -> Arc<str> {
|
|
|
|
self.config.name.clone()
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
2021-10-26 19:17:51 +00:00
|
|
|
|
2021-11-23 22:13:28 +00:00
|
|
|
pub fn line_comment_prefix(&self) -> Option<&str> {
|
|
|
|
self.config.line_comment.as_deref()
|
|
|
|
}
|
|
|
|
|
2022-03-30 01:11:14 +00:00
|
|
|
pub fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
|
|
|
|
self.adapter.as_ref().map_or(&[] as &[_], |adapter| {
|
|
|
|
adapter.disk_based_diagnostic_sources()
|
|
|
|
})
|
2021-10-26 19:17:51 +00:00
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
|
2022-03-30 01:11:14 +00:00
|
|
|
pub fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
|
|
|
|
self.adapter
|
2022-01-04 16:38:45 +00:00
|
|
|
.as_ref()
|
2022-03-30 01:11:14 +00:00
|
|
|
.and_then(|adapter| adapter.disk_based_diagnostics_progress_token())
|
2022-01-04 16:38:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 16:49:50 +00:00
|
|
|
pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
|
2022-03-03 21:29:25 +00:00
|
|
|
if let Some(processor) = self.adapter.as_ref() {
|
2022-01-25 16:49:50 +00:00
|
|
|
processor.process_diagnostics(diagnostics);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:01:08 +00:00
|
|
|
pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<CodeLabel> {
|
2022-03-03 21:29:25 +00:00
|
|
|
self.adapter
|
2022-02-03 02:14:30 +00:00
|
|
|
.as_ref()?
|
|
|
|
.label_for_completion(completion, self)
|
2022-02-02 17:43:55 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:08:43 +00:00
|
|
|
pub fn label_for_symbol(&self, name: &str, kind: lsp::SymbolKind) -> Option<CodeLabel> {
|
2022-03-03 21:29:25 +00:00
|
|
|
self.adapter.as_ref()?.label_for_symbol(name, kind, self)
|
2022-02-22 09:01:08 +00:00
|
|
|
}
|
|
|
|
|
2022-02-03 02:14:30 +00:00
|
|
|
pub fn highlight_text<'a>(
|
|
|
|
&'a self,
|
|
|
|
text: &'a Rope,
|
|
|
|
range: Range<usize>,
|
|
|
|
) -> Vec<(Range<usize>, HighlightId)> {
|
2022-02-03 01:01:48 +00:00
|
|
|
let mut result = Vec::new();
|
|
|
|
if let Some(grammar) = &self.grammar {
|
|
|
|
let tree = grammar.parse_text(text, None);
|
|
|
|
let mut offset = 0;
|
2022-02-03 02:14:30 +00:00
|
|
|
for chunk in BufferChunks::new(text, range, Some(&tree), self.grammar.as_ref(), vec![])
|
|
|
|
{
|
2022-02-03 01:01:48 +00:00
|
|
|
let end_offset = offset + chunk.text.len();
|
2022-03-09 21:53:31 +00:00
|
|
|
if let Some(highlight_id) = chunk.syntax_highlight_id {
|
2022-02-03 01:01:48 +00:00
|
|
|
result.push((offset..end_offset, highlight_id));
|
|
|
|
}
|
|
|
|
offset = end_offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-10-19 11:17:16 +00:00
|
|
|
pub fn brackets(&self) -> &[BracketPair] {
|
|
|
|
&self.config.brackets
|
2021-10-06 14:34:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 22:17:40 +00:00
|
|
|
pub fn should_autoclose_before(&self, c: char) -> bool {
|
|
|
|
c.is_whitespace() || self.config.autoclose_before.contains(c)
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn set_theme(&self, theme: &SyntaxTheme) {
|
|
|
|
if let Some(grammar) = self.grammar.as_ref() {
|
|
|
|
*grammar.highlight_map.lock() =
|
|
|
|
HighlightMap::new(grammar.highlights_query.capture_names(), theme);
|
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
2022-02-03 02:14:30 +00:00
|
|
|
|
|
|
|
pub fn grammar(&self) -> Option<&Arc<Grammar>> {
|
|
|
|
self.grammar.as_ref()
|
|
|
|
}
|
2021-11-29 16:38:59 +00:00
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
impl Grammar {
|
2022-02-03 01:01:48 +00:00
|
|
|
fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
|
|
|
|
PARSER.with(|parser| {
|
|
|
|
let mut parser = parser.borrow_mut();
|
|
|
|
parser
|
|
|
|
.set_language(self.ts_language)
|
|
|
|
.expect("incompatible grammar");
|
|
|
|
let mut chunks = text.chunks_in_range(0..text.len());
|
|
|
|
parser
|
|
|
|
.parse_with(
|
|
|
|
&mut move |offset, _| {
|
|
|
|
chunks.seek(offset);
|
|
|
|
chunks.next().unwrap_or("").as_bytes()
|
|
|
|
},
|
|
|
|
old_tree.as_ref(),
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-29 16:38:59 +00:00
|
|
|
pub fn highlight_map(&self) -> HighlightMap {
|
|
|
|
self.highlight_map.lock().clone()
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
2022-02-03 02:14:30 +00:00
|
|
|
|
|
|
|
pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
|
|
|
|
let capture_id = self.highlights_query.capture_index_for_name(name)?;
|
|
|
|
Some(self.highlight_map.lock().get(capture_id))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 09:01:08 +00:00
|
|
|
impl CodeLabel {
|
|
|
|
pub fn plain(text: String, filter_text: Option<&str>) -> Self {
|
2022-02-03 02:14:30 +00:00
|
|
|
let mut result = Self {
|
|
|
|
runs: Vec::new(),
|
2022-02-22 09:01:08 +00:00
|
|
|
filter_range: 0..text.len(),
|
|
|
|
text,
|
2022-02-03 02:14:30 +00:00
|
|
|
};
|
2022-02-22 09:01:08 +00:00
|
|
|
if let Some(filter_text) = filter_text {
|
|
|
|
if let Some(ix) = result.text.find(filter_text) {
|
2022-02-03 02:14:30 +00:00
|
|
|
result.filter_range = ix..ix + filter_text.len();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
2021-10-04 14:50:12 +00:00
|
|
|
}
|
2021-10-04 17:59:03 +00:00
|
|
|
|
2021-11-03 00:41:01 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-30 00:54:29 +00:00
|
|
|
impl Default for FakeLspAdapter {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "the-fake-language-server",
|
|
|
|
capabilities: lsp::LanguageServer::full_capabilities(),
|
|
|
|
initializer: None,
|
2022-03-30 01:11:14 +00:00
|
|
|
disk_based_diagnostics_progress_token: None,
|
|
|
|
disk_based_diagnostics_sources: &[],
|
2022-03-30 00:54:29 +00:00
|
|
|
}
|
2021-11-03 00:41:01 +00:00
|
|
|
}
|
2022-03-30 00:54:29 +00:00
|
|
|
}
|
2022-02-17 01:23:16 +00:00
|
|
|
|
2022-03-30 01:11:14 +00:00
|
|
|
#[cfg(any(test, feature = "test-support"))]
|
2022-03-30 00:54:29 +00:00
|
|
|
impl LspAdapter for FakeLspAdapter {
|
|
|
|
fn name(&self) -> LanguageServerName {
|
|
|
|
LanguageServerName(self.name.into())
|
2022-02-17 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 00:54:29 +00:00
|
|
|
fn fetch_latest_server_version(
|
|
|
|
&self,
|
|
|
|
_: Arc<dyn HttpClient>,
|
|
|
|
) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
|
|
|
|
unreachable!();
|
2022-02-17 01:23:16 +00:00
|
|
|
}
|
2022-03-30 00:54:29 +00:00
|
|
|
|
|
|
|
fn fetch_server_binary(
|
|
|
|
&self,
|
|
|
|
_: Box<dyn 'static + Send + Any>,
|
|
|
|
_: Arc<dyn HttpClient>,
|
|
|
|
_: PathBuf,
|
|
|
|
) -> BoxFuture<'static, Result<PathBuf>> {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cached_server_binary(&self, _: PathBuf) -> BoxFuture<'static, Option<PathBuf>> {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
|
2022-03-30 01:11:14 +00:00
|
|
|
|
|
|
|
fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
|
|
|
|
self.disk_based_diagnostics_sources
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
|
|
|
|
self.disk_based_diagnostics_progress_token
|
|
|
|
}
|
2021-11-03 00:41:01 +00:00
|
|
|
}
|
2022-01-12 17:01:20 +00:00
|
|
|
|
2022-03-31 22:39:52 +00:00
|
|
|
pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
|
|
|
|
lsp::Position::new(point.row, point.column)
|
2022-01-31 18:11:13 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:35:37 +00:00
|
|
|
pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
|
|
|
|
PointUtf16::new(point.line, point.character)
|
|
|
|
}
|
|
|
|
|
2022-03-31 22:39:52 +00:00
|
|
|
pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
|
|
|
|
lsp::Range {
|
|
|
|
start: point_to_lsp(range.start),
|
|
|
|
end: point_to_lsp(range.end),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 17:01:20 +00:00
|
|
|
pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
|
2022-03-31 22:39:52 +00:00
|
|
|
point_from_lsp(range.start)..point_from_lsp(range.end)
|
2022-01-12 17:01:20 +00:00
|
|
|
}
|