mod buffer; mod diagnostic_set; mod highlight_map; pub mod language_settings; pub mod markdown; mod outline; pub mod proto; mod syntax_map; #[cfg(test)] mod buffer_tests; use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; use collections::{HashMap, HashSet}; use futures::{ channel::{mpsc, oneshot}, future::{BoxFuture, Shared}, FutureExt, TryFutureExt as _, }; use gpui::{executor::Background, AppContext, AsyncAppContext, Task}; pub use highlight_map::HighlightMap; use lazy_static::lazy_static; use lsp::{CodeActionKind, LanguageServerBinary}; use parking_lot::{Mutex, RwLock}; use postage::watch; use regex::Regex; use serde::{de, Deserialize, Deserializer}; use serde_json::Value; use std::{ any::Any, borrow::Cow, cell::RefCell, fmt::Debug, hash::Hash, mem, ops::{Not, Range}, path::{Path, PathBuf}, str, sync::{ atomic::{AtomicU64, AtomicUsize, Ordering::SeqCst}, Arc, }, }; use syntax_map::SyntaxSnapshot; use theme::{SyntaxTheme, Theme}; use tree_sitter::{self, Query}; use unicase::UniCase; use util::{http::HttpClient, paths::PathExt}; use util::{post_inc, ResultExt, TryFutureExt as _, UnwrapFuture}; pub use buffer::Operation; pub use buffer::*; pub use diagnostic_set::DiagnosticEntry; pub use lsp::LanguageServerId; pub use outline::{Outline, OutlineItem}; pub use syntax_map::{OwnedSyntaxLayerInfo, SyntaxLayerInfo}; pub use text::LineEnding; pub use tree_sitter::{Parser, Tree}; pub fn init(cx: &mut AppContext) { language_settings::init(cx); } #[derive(Clone, Default)] struct LspBinaryStatusSender { txs: Arc, LanguageServerBinaryStatus)>>>>, } impl LspBinaryStatusSender { fn subscribe(&self) -> mpsc::UnboundedReceiver<(Arc, LanguageServerBinaryStatus)> { let (tx, rx) = mpsc::unbounded(); self.txs.lock().push(tx); rx } fn send(&self, language: Arc, status: LanguageServerBinaryStatus) { let mut txs = self.txs.lock(); txs.retain(|tx| { tx.unbounded_send((language.clone(), status.clone())) .is_ok() }); } } thread_local! { static PARSER: RefCell = RefCell::new(Parser::new()); } lazy_static! { pub static ref NEXT_GRAMMAR_ID: AtomicUsize = Default::default(); pub static ref PLAIN_TEXT: Arc = Arc::new(Language::new( LanguageConfig { name: "Plain Text".into(), ..Default::default() }, None, )); } pub trait ToLspPosition { fn to_lsp_position(self) -> lsp::Position; } #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct LanguageServerName(pub Arc); /// Represents a Language Server, with certain cached sync properties. /// Uses [`LspAdapter`] under the hood, but calls all 'static' methods /// once at startup, and caches the results. pub struct CachedLspAdapter { pub name: LanguageServerName, pub short_name: &'static str, pub disk_based_diagnostic_sources: Vec, pub disk_based_diagnostics_progress_token: Option, pub language_ids: HashMap, pub adapter: Arc, pub reinstall_attempt_count: AtomicU64, } impl CachedLspAdapter { pub async fn new(adapter: Arc) -> Arc { let name = adapter.name().await; let short_name = adapter.short_name(); let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await; let disk_based_diagnostics_progress_token = adapter.disk_based_diagnostics_progress_token().await; let language_ids = adapter.language_ids().await; Arc::new(CachedLspAdapter { name, short_name, disk_based_diagnostic_sources, disk_based_diagnostics_progress_token, language_ids, adapter, reinstall_attempt_count: AtomicU64::new(0), }) } pub async fn fetch_latest_server_version( &self, delegate: &dyn LspAdapterDelegate, ) -> Result> { self.adapter.fetch_latest_server_version(delegate).await } pub fn will_fetch_server( &self, delegate: &Arc, cx: &mut AsyncAppContext, ) -> Option>> { self.adapter.will_fetch_server(delegate, cx) } pub fn will_start_server( &self, delegate: &Arc, cx: &mut AsyncAppContext, ) -> Option>> { self.adapter.will_start_server(delegate, cx) } pub async fn fetch_server_binary( &self, version: Box, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, ) -> Result { self.adapter .fetch_server_binary(version, container_dir, delegate) .await } pub async fn cached_server_binary( &self, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, ) -> Option { self.adapter .cached_server_binary(container_dir, delegate) .await } pub fn can_be_reinstalled(&self) -> bool { self.adapter.can_be_reinstalled() } pub async fn installation_test_binary( &self, container_dir: PathBuf, ) -> Option { self.adapter.installation_test_binary(container_dir).await } pub fn code_action_kinds(&self) -> Option> { self.adapter.code_action_kinds() } pub fn workspace_configuration( &self, workspace_root: &Path, cx: &mut AppContext, ) -> BoxFuture<'static, Value> { self.adapter.workspace_configuration(workspace_root, cx) } pub fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) { self.adapter.process_diagnostics(params) } pub async fn process_completion(&self, completion_item: &mut lsp::CompletionItem) { self.adapter.process_completion(completion_item).await } pub async fn label_for_completion( &self, completion_item: &lsp::CompletionItem, language: &Arc, ) -> Option { self.adapter .label_for_completion(completion_item, language) .await } pub async fn label_for_symbol( &self, name: &str, kind: lsp::SymbolKind, language: &Arc, ) -> Option { self.adapter.label_for_symbol(name, kind, language).await } pub fn prettier_plugins(&self) -> &[&'static str] { self.adapter.prettier_plugins() } } pub trait LspAdapterDelegate: Send + Sync { fn show_notification(&self, message: &str, cx: &mut AppContext); fn http_client(&self) -> Arc; } #[async_trait] pub trait LspAdapter: 'static + Send + Sync { async fn name(&self) -> LanguageServerName; fn short_name(&self) -> &'static str; async fn fetch_latest_server_version( &self, delegate: &dyn LspAdapterDelegate, ) -> Result>; fn will_fetch_server( &self, _: &Arc, _: &mut AsyncAppContext, ) -> Option>> { None } fn will_start_server( &self, _: &Arc, _: &mut AsyncAppContext, ) -> Option>> { None } async fn fetch_server_binary( &self, version: Box, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, ) -> Result; async fn cached_server_binary( &self, container_dir: PathBuf, delegate: &dyn LspAdapterDelegate, ) -> Option; fn can_be_reinstalled(&self) -> bool { true } async fn installation_test_binary( &self, container_dir: PathBuf, ) -> Option; fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} async fn process_completion(&self, _: &mut lsp::CompletionItem) {} async fn label_for_completion( &self, _: &lsp::CompletionItem, _: &Arc, ) -> Option { None } async fn label_for_symbol( &self, _: &str, _: lsp::SymbolKind, _: &Arc, ) -> Option { None } async fn initialization_options(&self) -> Option { None } fn workspace_configuration(&self, _: &Path, _: &mut AppContext) -> BoxFuture<'static, Value> { futures::future::ready(serde_json::json!({})).boxed() } fn code_action_kinds(&self) -> Option> { Some(vec![ CodeActionKind::EMPTY, CodeActionKind::QUICKFIX, CodeActionKind::REFACTOR, CodeActionKind::REFACTOR_EXTRACT, CodeActionKind::SOURCE, ]) } async fn disk_based_diagnostic_sources(&self) -> Vec { Default::default() } async fn disk_based_diagnostics_progress_token(&self) -> Option { None } async fn language_ids(&self) -> HashMap { Default::default() } fn prettier_plugins(&self) -> &[&'static str] { &[] } } #[derive(Clone, Debug, PartialEq, Eq)] pub struct CodeLabel { pub text: String, pub runs: Vec<(Range, HighlightId)>, pub filter_range: Range, } #[derive(Clone, Deserialize)] pub struct LanguageConfig { pub name: Arc, pub path_suffixes: Vec, pub brackets: BracketPairConfig, #[serde(default, deserialize_with = "deserialize_regex")] pub first_line_pattern: Option, #[serde(default = "auto_indent_using_last_non_empty_line_default")] pub auto_indent_using_last_non_empty_line: bool, #[serde(default, deserialize_with = "deserialize_regex")] pub increase_indent_pattern: Option, #[serde(default, deserialize_with = "deserialize_regex")] pub decrease_indent_pattern: Option, #[serde(default)] pub autoclose_before: String, #[serde(default)] pub line_comment: Option>, #[serde(default)] pub collapsed_placeholder: String, #[serde(default)] pub block_comment: Option<(Arc, Arc)>, #[serde(default)] pub scope_opt_in_language_servers: Vec, #[serde(default)] pub overrides: HashMap, #[serde(default)] pub word_characters: HashSet, #[serde(default)] pub prettier_parser_name: Option, } #[derive(Debug, Default)] pub struct LanguageQueries { pub highlights: Option>, pub brackets: Option>, pub indents: Option>, pub outline: Option>, pub embedding: Option>, pub injections: Option>, pub overrides: Option>, } #[derive(Clone, Debug)] pub struct LanguageScope { language: Arc, override_id: Option, } #[derive(Clone, Deserialize, Default, Debug)] pub struct LanguageConfigOverride { #[serde(default)] pub line_comment: Override>, #[serde(default)] pub block_comment: Override<(Arc, Arc)>, #[serde(skip_deserializing)] pub disabled_bracket_ixs: Vec, #[serde(default)] pub word_characters: Override>, #[serde(default)] pub opt_into_language_servers: Vec, } #[derive(Clone, Deserialize, Debug)] #[serde(untagged)] pub enum Override { Remove { remove: bool }, Set(T), } impl Default for Override { fn default() -> Self { Override::Remove { remove: false } } } impl Override { fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> { match this { Some(Self::Set(value)) => Some(value), Some(Self::Remove { remove: true }) => None, Some(Self::Remove { remove: false }) | None => original, } } } impl Default for LanguageConfig { fn default() -> Self { Self { name: "".into(), path_suffixes: Default::default(), brackets: Default::default(), auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(), first_line_pattern: Default::default(), increase_indent_pattern: Default::default(), decrease_indent_pattern: Default::default(), autoclose_before: Default::default(), line_comment: Default::default(), block_comment: Default::default(), scope_opt_in_language_servers: Default::default(), overrides: Default::default(), collapsed_placeholder: Default::default(), word_characters: Default::default(), prettier_parser_name: None, } } } fn auto_indent_using_last_non_empty_line_default() -> bool { true } fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result, D::Error> { let source = Option::::deserialize(d)?; if let Some(source) = source { Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?)) } else { Ok(None) } } #[cfg(any(test, feature = "test-support"))] pub struct FakeLspAdapter { pub name: &'static str, pub initialization_options: Option, pub capabilities: lsp::ServerCapabilities, pub initializer: Option>, pub disk_based_diagnostics_progress_token: Option, pub disk_based_diagnostics_sources: Vec, pub prettier_plugins: Vec<&'static str>, } #[derive(Clone, Debug, Default)] pub struct BracketPairConfig { pub pairs: Vec, pub disabled_scopes_by_bracket_ix: Vec>, } impl<'de> Deserialize<'de> for BracketPairConfig { fn deserialize(deserializer: D) -> std::result::Result where D: Deserializer<'de>, { #[derive(Deserialize)] pub struct Entry { #[serde(flatten)] pub bracket_pair: BracketPair, #[serde(default)] pub not_in: Vec, } let result = Vec::::deserialize(deserializer)?; let mut brackets = Vec::with_capacity(result.len()); let mut disabled_scopes_by_bracket_ix = Vec::with_capacity(result.len()); for entry in result { brackets.push(entry.bracket_pair); disabled_scopes_by_bracket_ix.push(entry.not_in); } Ok(BracketPairConfig { pairs: brackets, disabled_scopes_by_bracket_ix, }) } } #[derive(Clone, Debug, Default, Deserialize, PartialEq)] pub struct BracketPair { pub start: String, pub end: String, pub close: bool, pub newline: bool, } pub struct Language { pub(crate) config: LanguageConfig, pub(crate) grammar: Option>, pub(crate) adapters: Vec>, #[cfg(any(test, feature = "test-support"))] fake_adapter: Option<( mpsc::UnboundedSender, Arc, )>, } pub struct Grammar { id: usize, pub ts_language: tree_sitter::Language, pub(crate) error_query: Query, pub(crate) highlights_query: Option, pub(crate) brackets_config: Option, pub(crate) indents_config: Option, pub outline_config: Option, pub embedding_config: Option, pub(crate) injection_config: Option, pub(crate) override_config: Option, pub(crate) highlight_map: Mutex, } struct IndentConfig { query: Query, indent_capture_ix: u32, start_capture_ix: Option, end_capture_ix: Option, outdent_capture_ix: Option, } pub struct OutlineConfig { pub query: Query, pub item_capture_ix: u32, pub name_capture_ix: u32, pub context_capture_ix: Option, pub extra_context_capture_ix: Option, } #[derive(Debug)] pub struct EmbeddingConfig { pub query: Query, pub item_capture_ix: u32, pub name_capture_ix: Option, pub context_capture_ix: Option, pub collapse_capture_ix: Option, pub keep_capture_ix: Option, } struct InjectionConfig { query: Query, content_capture_ix: u32, language_capture_ix: Option, patterns: Vec, } struct OverrideConfig { query: Query, values: HashMap, } #[derive(Default, Clone)] struct InjectionPatternConfig { language: Option>, combined: bool, } struct BracketConfig { query: Query, open_capture_ix: u32, close_capture_ix: u32, } #[derive(Clone)] pub enum LanguageServerBinaryStatus { CheckingForUpdate, Downloading, Downloaded, Cached, Failed { error: String }, } type AvailableLanguageId = usize; #[derive(Clone)] struct AvailableLanguage { id: AvailableLanguageId, path: &'static str, config: LanguageConfig, grammar: tree_sitter::Language, lsp_adapters: Vec>, get_queries: fn(&str) -> LanguageQueries, loaded: bool, } pub struct LanguageRegistry { state: RwLock, language_server_download_dir: Option>, login_shell_env_loaded: Shared>, #[allow(clippy::type_complexity)] lsp_binary_paths: Mutex< HashMap>>>>, >, executor: Option>, lsp_binary_status_tx: LspBinaryStatusSender, } struct LanguageRegistryState { next_language_server_id: usize, languages: Vec>, available_languages: Vec, next_available_language_id: AvailableLanguageId, loading_languages: HashMap>>>>, subscription: (watch::Sender<()>, watch::Receiver<()>), theme: Option>, version: usize, reload_count: usize, } pub struct PendingLanguageServer { pub server_id: LanguageServerId, pub task: Task>, pub container_dir: Option>, } impl LanguageRegistry { pub fn new(login_shell_env_loaded: Task<()>) -> Self { Self { state: RwLock::new(LanguageRegistryState { next_language_server_id: 0, languages: vec![PLAIN_TEXT.clone()], available_languages: Default::default(), next_available_language_id: 0, loading_languages: Default::default(), subscription: watch::channel(), theme: Default::default(), version: 0, reload_count: 0, }), language_server_download_dir: None, login_shell_env_loaded: login_shell_env_loaded.shared(), lsp_binary_paths: Default::default(), executor: None, lsp_binary_status_tx: Default::default(), } } #[cfg(any(test, feature = "test-support"))] pub fn test() -> Self { Self::new(Task::ready(())) } pub fn set_executor(&mut self, executor: Arc) { self.executor = Some(executor); } /// Clear out all of the loaded languages and reload them from scratch. /// /// This is useful in development, when queries have changed. #[cfg(debug_assertions)] pub fn reload(&self) { self.state.write().reload(); } pub fn register( &self, path: &'static str, config: LanguageConfig, grammar: tree_sitter::Language, lsp_adapters: Vec>, get_queries: fn(&str) -> LanguageQueries, ) { let state = &mut *self.state.write(); state.available_languages.push(AvailableLanguage { id: post_inc(&mut state.next_available_language_id), path, config, grammar, lsp_adapters, get_queries, loaded: false, }); } pub fn language_names(&self) -> Vec { let state = self.state.read(); let mut result = state .available_languages .iter() .filter_map(|l| l.loaded.not().then_some(l.config.name.to_string())) .chain(state.languages.iter().map(|l| l.config.name.to_string())) .collect::>(); result.sort_unstable_by_key(|language_name| language_name.to_lowercase()); result } pub fn add(&self, language: Arc) { self.state.write().add(language); } pub fn subscribe(&self) -> watch::Receiver<()> { self.state.read().subscription.1.clone() } /// The number of times that the registry has been changed, /// by adding languages or reloading. pub fn version(&self) -> usize { self.state.read().version } /// The number of times that the registry has been reloaded. pub fn reload_count(&self) -> usize { self.state.read().reload_count } pub fn set_theme(&self, theme: Arc) { let mut state = self.state.write(); state.theme = Some(theme.clone()); for language in &state.languages { language.set_theme(&theme.editor.syntax); } } pub fn set_language_server_download_dir(&mut self, path: impl Into>) { self.language_server_download_dir = Some(path.into()); } pub fn language_for_name( self: &Arc, name: &str, ) -> UnwrapFuture>>> { let name = UniCase::new(name); self.get_or_load_language(|config| UniCase::new(config.name.as_ref()) == name) } pub fn language_for_name_or_extension( self: &Arc, string: &str, ) -> UnwrapFuture>>> { let string = UniCase::new(string); self.get_or_load_language(|config| { UniCase::new(config.name.as_ref()) == string || config .path_suffixes .iter() .any(|suffix| UniCase::new(suffix) == string) }) } pub fn language_for_file( self: &Arc, path: impl AsRef, content: Option<&Rope>, ) -> UnwrapFuture>>> { let path = path.as_ref(); let filename = path.file_name().and_then(|name| name.to_str()); let extension = path.extension_or_hidden_file_name(); let path_suffixes = [extension, filename]; self.get_or_load_language(|config| { let path_matches = config .path_suffixes .iter() .any(|suffix| path_suffixes.contains(&Some(suffix.as_str()))); let content_matches = content.zip(config.first_line_pattern.as_ref()).map_or( false, |(content, pattern)| { let end = content.clip_point(Point::new(0, 256), Bias::Left); let end = content.point_to_offset(end); let text = content.chunks_in_range(0..end).collect::(); pattern.is_match(&text) }, ); path_matches || content_matches }) } fn get_or_load_language( self: &Arc, callback: impl Fn(&LanguageConfig) -> bool, ) -> UnwrapFuture>>> { let (tx, rx) = oneshot::channel(); let mut state = self.state.write(); if let Some(language) = state .languages .iter() .find(|language| callback(&language.config)) { let _ = tx.send(Ok(language.clone())); } else if let Some(executor) = self.executor.clone() { if let Some(language) = state .available_languages .iter() .find(|l| !l.loaded && callback(&l.config)) .cloned() { let txs = state .loading_languages .entry(language.id) .or_insert_with(|| { let this = self.clone(); executor .spawn(async move { let id = language.id; let queries = (language.get_queries)(&language.path); let language = Language::new(language.config, Some(language.grammar)) .with_lsp_adapters(language.lsp_adapters) .await; let name = language.name(); match language.with_queries(queries) { Ok(language) => { let language = Arc::new(language); let mut state = this.state.write(); state.add(language.clone()); state.mark_language_loaded(id); if let Some(mut txs) = state.loading_languages.remove(&id) { for tx in txs.drain(..) { let _ = tx.send(Ok(language.clone())); } } } Err(e) => { log::error!("failed to load language {name}:\n{:?}", e); let mut state = this.state.write(); state.mark_language_loaded(id); if let Some(mut txs) = state.loading_languages.remove(&id) { for tx in txs.drain(..) { let _ = tx.send(Err(anyhow!( "failed to load language {}: {}", name, e ))); } } } }; }) .detach(); Vec::new() }); txs.push(tx); } else { let _ = tx.send(Err(anyhow!("language not found"))); } } else { let _ = tx.send(Err(anyhow!("executor does not exist"))); } rx.unwrap() } pub fn to_vec(&self) -> Vec> { self.state.read().languages.iter().cloned().collect() } pub fn create_pending_language_server( self: &Arc, stderr_capture: Arc>>, language: Arc, adapter: Arc, root_path: Arc, delegate: Arc, cx: &mut AppContext, ) -> Option { let server_id = self.state.write().next_language_server_id(); log::info!( "starting language server {:?}, path: {root_path:?}, id: {server_id}", adapter.name.0 ); #[cfg(any(test, feature = "test-support"))] if language.fake_adapter.is_some() { let task = cx.spawn(|cx| async move { let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap(); let (server, mut fake_server) = lsp::LanguageServer::fake( fake_adapter.name.to_string(), fake_adapter.capabilities.clone(), cx.clone(), ); if let Some(initializer) = &fake_adapter.initializer { initializer(&mut fake_server); } let servers_tx = servers_tx.clone(); cx.background() .spawn(async move { if fake_server .try_receive_notification::() .await .is_some() { servers_tx.unbounded_send(fake_server).ok(); } }) .detach(); Ok(server) }); return Some(PendingLanguageServer { server_id, task, container_dir: None, }); } let download_dir = self .language_server_download_dir .clone() .ok_or_else(|| anyhow!("language server download directory has not been assigned before starting server")) .log_err()?; let this = self.clone(); let language = language.clone(); let container_dir: Arc = Arc::from(download_dir.join(adapter.name.0.as_ref())); let root_path = root_path.clone(); let adapter = adapter.clone(); let login_shell_env_loaded = self.login_shell_env_loaded.clone(); let lsp_binary_statuses = self.lsp_binary_status_tx.clone(); let task = { let container_dir = container_dir.clone(); cx.spawn(|mut cx| async move { login_shell_env_loaded.await; let mut lock = this.lsp_binary_paths.lock(); let entry = lock .entry(adapter.name.clone()) .or_insert_with(|| { cx.spawn(|cx| { get_binary( adapter.clone(), language.clone(), delegate.clone(), container_dir, lsp_binary_statuses, cx, ) .map_err(Arc::new) }) .shared() }) .clone(); drop(lock); let binary = match entry.clone().await { Ok(binary) => binary, Err(err) => anyhow::bail!("{err}"), }; if let Some(task) = adapter.will_start_server(&delegate, &mut cx) { task.await?; } lsp::LanguageServer::new( stderr_capture, server_id, binary, &root_path, adapter.code_action_kinds(), cx, ) }) }; Some(PendingLanguageServer { server_id, task, container_dir: Some(container_dir), }) } pub fn language_server_binary_statuses( &self, ) -> mpsc::UnboundedReceiver<(Arc, LanguageServerBinaryStatus)> { self.lsp_binary_status_tx.subscribe() } pub fn delete_server_container( &self, adapter: Arc, cx: &mut AppContext, ) -> Task<()> { log::info!("deleting server container"); let mut lock = self.lsp_binary_paths.lock(); lock.remove(&adapter.name); let download_dir = self .language_server_download_dir .clone() .expect("language server download directory has not been assigned before deleting server container"); cx.spawn(|_| async move { let container_dir = download_dir.join(adapter.name.0.as_ref()); smol::fs::remove_dir_all(container_dir) .await .context("server container removal") .log_err(); }) } pub fn next_language_server_id(&self) -> LanguageServerId { self.state.write().next_language_server_id() } } impl LanguageRegistryState { fn next_language_server_id(&mut self) -> LanguageServerId { LanguageServerId(post_inc(&mut self.next_language_server_id)) } fn add(&mut self, language: Arc) { if let Some(theme) = self.theme.as_ref() { language.set_theme(&theme.editor.syntax); } self.languages.push(language); self.version += 1; *self.subscription.0.borrow_mut() = (); } #[cfg(debug_assertions)] fn reload(&mut self) { self.languages.clear(); self.version += 1; self.reload_count += 1; for language in &mut self.available_languages { language.loaded = false; } *self.subscription.0.borrow_mut() = (); } /// Mark the given language a having been loaded, so that the /// language registry won't try to load it again. fn mark_language_loaded(&mut self, id: AvailableLanguageId) { for language in &mut self.available_languages { if language.id == id { language.loaded = true; break; } } } } #[cfg(any(test, feature = "test-support"))] impl Default for LanguageRegistry { fn default() -> Self { Self::test() } } async fn get_binary( adapter: Arc, language: Arc, delegate: Arc, container_dir: Arc, statuses: LspBinaryStatusSender, mut cx: AsyncAppContext, ) -> Result { if !container_dir.exists() { smol::fs::create_dir_all(&container_dir) .await .context("failed to create container directory")?; } if let Some(task) = adapter.will_fetch_server(&delegate, &mut cx) { task.await?; } let binary = fetch_latest_binary( adapter.clone(), language.clone(), delegate.as_ref(), &container_dir, statuses.clone(), ) .await; if let Err(error) = binary.as_ref() { if let Some(binary) = adapter .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref()) .await { statuses.send(language.clone(), LanguageServerBinaryStatus::Cached); return Ok(binary); } else { statuses.send( language.clone(), LanguageServerBinaryStatus::Failed { error: format!("{:?}", error), }, ); } } binary } async fn fetch_latest_binary( adapter: Arc, language: Arc, delegate: &dyn LspAdapterDelegate, container_dir: &Path, lsp_binary_statuses_tx: LspBinaryStatusSender, ) -> Result { let container_dir: Arc = container_dir.into(); lsp_binary_statuses_tx.send( language.clone(), LanguageServerBinaryStatus::CheckingForUpdate, ); let version_info = adapter.fetch_latest_server_version(delegate).await?; lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloading); let binary = adapter .fetch_server_binary(version_info, container_dir.to_path_buf(), delegate) .await?; lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloaded); Ok(binary) } impl Language { pub fn new(config: LanguageConfig, ts_language: Option) -> Self { Self { config, grammar: ts_language.map(|ts_language| { Arc::new(Grammar { id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst), highlights_query: None, brackets_config: None, outline_config: None, embedding_config: None, indents_config: None, injection_config: None, override_config: None, error_query: Query::new(ts_language, "(ERROR) @error").unwrap(), ts_language, highlight_map: Default::default(), }) }), adapters: Vec::new(), #[cfg(any(test, feature = "test-support"))] fake_adapter: None, } } pub fn lsp_adapters(&self) -> &[Arc] { &self.adapters } pub fn id(&self) -> Option { self.grammar.as_ref().map(|g| g.id) } pub fn with_queries(mut self, queries: LanguageQueries) -> Result { if let Some(query) = queries.highlights { self = self .with_highlights_query(query.as_ref()) .context("Error loading highlights query")?; } if let Some(query) = queries.brackets { self = self .with_brackets_query(query.as_ref()) .context("Error loading brackets query")?; } if let Some(query) = queries.indents { self = self .with_indents_query(query.as_ref()) .context("Error loading indents query")?; } if let Some(query) = queries.outline { self = self .with_outline_query(query.as_ref()) .context("Error loading outline query")?; } if let Some(query) = queries.embedding { self = self .with_embedding_query(query.as_ref()) .context("Error loading embedding query")?; } if let Some(query) = queries.injections { self = self .with_injection_query(query.as_ref()) .context("Error loading injection query")?; } if let Some(query) = queries.overrides { self = self .with_override_query(query.as_ref()) .context("Error loading override query")?; } Ok(self) } pub fn with_highlights_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); grammar.highlights_query = Some(Query::new(grammar.ts_language, source)?); Ok(self) } pub fn with_outline_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); let query = Query::new(grammar.ts_language, source)?; let mut item_capture_ix = None; let mut name_capture_ix = None; let mut context_capture_ix = None; let mut extra_context_capture_ix = None; get_capture_indices( &query, &mut [ ("item", &mut item_capture_ix), ("name", &mut name_capture_ix), ("context", &mut context_capture_ix), ("context.extra", &mut extra_context_capture_ix), ], ); if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) { grammar.outline_config = Some(OutlineConfig { query, item_capture_ix, name_capture_ix, context_capture_ix, extra_context_capture_ix, }); } Ok(self) } pub fn with_embedding_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); let query = Query::new(grammar.ts_language, source)?; let mut item_capture_ix = None; let mut name_capture_ix = None; let mut context_capture_ix = None; let mut collapse_capture_ix = None; let mut keep_capture_ix = None; get_capture_indices( &query, &mut [ ("item", &mut item_capture_ix), ("name", &mut name_capture_ix), ("context", &mut context_capture_ix), ("keep", &mut keep_capture_ix), ("collapse", &mut collapse_capture_ix), ], ); if let Some(item_capture_ix) = item_capture_ix { grammar.embedding_config = Some(EmbeddingConfig { query, item_capture_ix, name_capture_ix, context_capture_ix, collapse_capture_ix, keep_capture_ix, }); } Ok(self) } pub fn with_brackets_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); let query = Query::new(grammar.ts_language, source)?; let mut open_capture_ix = None; let mut close_capture_ix = None; get_capture_indices( &query, &mut [ ("open", &mut open_capture_ix), ("close", &mut close_capture_ix), ], ); if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) { grammar.brackets_config = Some(BracketConfig { query, open_capture_ix, close_capture_ix, }); } Ok(self) } pub fn with_indents_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); let query = Query::new(grammar.ts_language, source)?; let mut indent_capture_ix = None; let mut start_capture_ix = None; let mut end_capture_ix = None; let mut outdent_capture_ix = None; get_capture_indices( &query, &mut [ ("indent", &mut indent_capture_ix), ("start", &mut start_capture_ix), ("end", &mut end_capture_ix), ("outdent", &mut outdent_capture_ix), ], ); if let Some(indent_capture_ix) = indent_capture_ix { grammar.indents_config = Some(IndentConfig { query, indent_capture_ix, start_capture_ix, end_capture_ix, outdent_capture_ix, }); } Ok(self) } pub fn with_injection_query(mut self, source: &str) -> Result { let grammar = self.grammar_mut(); let query = Query::new(grammar.ts_language, source)?; let mut language_capture_ix = None; let mut content_capture_ix = None; get_capture_indices( &query, &mut [ ("language", &mut language_capture_ix), ("content", &mut content_capture_ix), ], ); let patterns = (0..query.pattern_count()) .map(|ix| { let mut config = InjectionPatternConfig::default(); for setting in query.property_settings(ix) { match setting.key.as_ref() { "language" => { config.language = setting.value.clone(); } "combined" => { config.combined = true; } _ => {} } } config }) .collect(); if let Some(content_capture_ix) = content_capture_ix { grammar.injection_config = Some(InjectionConfig { query, language_capture_ix, content_capture_ix, patterns, }); } Ok(self) } pub fn with_override_query(mut self, source: &str) -> anyhow::Result { let query = Query::new(self.grammar_mut().ts_language, source)?; let mut override_configs_by_id = HashMap::default(); for (ix, name) in query.capture_names().iter().copied().enumerate() { if !name.starts_with('_') { let value = self.config.overrides.remove(name).unwrap_or_default(); for server_name in &value.opt_into_language_servers { if !self .config .scope_opt_in_language_servers .contains(server_name) { util::debug_panic!("Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server"); } } override_configs_by_id.insert(ix as u32, (name.into(), value)); } } if !self.config.overrides.is_empty() { let keys = self.config.overrides.keys().collect::>(); Err(anyhow!( "language {:?} has overrides in config not in query: {keys:?}", self.config.name ))?; } for disabled_scope_name in self .config .brackets .disabled_scopes_by_bracket_ix .iter() .flatten() { if !override_configs_by_id .values() .any(|(scope_name, _)| scope_name == disabled_scope_name) { Err(anyhow!( "language {:?} has overrides in config not in query: {disabled_scope_name:?}", self.config.name ))?; } } for (name, override_config) in override_configs_by_id.values_mut() { override_config.disabled_bracket_ixs = self .config .brackets .disabled_scopes_by_bracket_ix .iter() .enumerate() .filter_map(|(ix, disabled_scope_names)| { if disabled_scope_names.contains(name) { Some(ix as u16) } else { None } }) .collect(); } self.config.brackets.disabled_scopes_by_bracket_ix.clear(); self.grammar_mut().override_config = Some(OverrideConfig { query, values: override_configs_by_id, }); Ok(self) } fn grammar_mut(&mut self) -> &mut Grammar { Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap() } pub async fn with_lsp_adapters(mut self, lsp_adapters: Vec>) -> Self { for adapter in lsp_adapters { self.adapters.push(CachedLspAdapter::new(adapter).await); } self } #[cfg(any(test, feature = "test-support"))] pub async fn set_fake_lsp_adapter( &mut self, fake_lsp_adapter: Arc, ) -> mpsc::UnboundedReceiver { let (servers_tx, servers_rx) = mpsc::unbounded(); self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone())); let adapter = CachedLspAdapter::new(Arc::new(fake_lsp_adapter)).await; self.adapters = vec![adapter]; servers_rx } pub fn name(&self) -> Arc { self.config.name.clone() } pub async fn disk_based_diagnostic_sources(&self) -> &[String] { match self.adapters.first().as_ref() { Some(adapter) => &adapter.disk_based_diagnostic_sources, None => &[], } } pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> { for adapter in &self.adapters { let token = adapter.disk_based_diagnostics_progress_token.as_deref(); if token.is_some() { return token; } } None } pub async fn process_completion(self: &Arc, completion: &mut lsp::CompletionItem) { for adapter in &self.adapters { adapter.process_completion(completion).await; } } pub async fn label_for_completion( self: &Arc, completion: &lsp::CompletionItem, ) -> Option { self.adapters .first() .as_ref()? .label_for_completion(completion, self) .await } pub async fn label_for_symbol( self: &Arc, name: &str, kind: lsp::SymbolKind, ) -> Option { self.adapters .first() .as_ref()? .label_for_symbol(name, kind, self) .await } pub fn highlight_text<'a>( self: &'a Arc, text: &'a Rope, range: Range, ) -> Vec<(Range, HighlightId)> { let mut result = Vec::new(); if let Some(grammar) = &self.grammar { let tree = grammar.parse_text(text, None); let captures = SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| { grammar.highlights_query.as_ref() }); let highlight_maps = vec![grammar.highlight_map()]; let mut offset = 0; for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) { let end_offset = offset + chunk.text.len(); if let Some(highlight_id) = chunk.syntax_highlight_id { if !highlight_id.is_default() { result.push((offset..end_offset, highlight_id)); } } offset = end_offset; } } result } pub fn path_suffixes(&self) -> &[String] { &self.config.path_suffixes } pub fn should_autoclose_before(&self, c: char) -> bool { c.is_whitespace() || self.config.autoclose_before.contains(c) } pub fn set_theme(&self, theme: &SyntaxTheme) { if let Some(grammar) = self.grammar.as_ref() { if let Some(highlights_query) = &grammar.highlights_query { *grammar.highlight_map.lock() = HighlightMap::new(highlights_query.capture_names(), theme); } } } pub fn grammar(&self) -> Option<&Arc> { self.grammar.as_ref() } pub fn default_scope(self: &Arc) -> LanguageScope { LanguageScope { language: self.clone(), override_id: None, } } pub fn prettier_parser_name(&self) -> Option<&str> { self.config.prettier_parser_name.as_deref() } } impl LanguageScope { pub fn collapsed_placeholder(&self) -> &str { self.language.config.collapsed_placeholder.as_ref() } pub fn line_comment_prefix(&self) -> Option<&Arc> { Override::as_option( self.config_override().map(|o| &o.line_comment), self.language.config.line_comment.as_ref(), ) } pub fn block_comment_delimiters(&self) -> Option<(&Arc, &Arc)> { Override::as_option( self.config_override().map(|o| &o.block_comment), self.language.config.block_comment.as_ref(), ) .map(|e| (&e.0, &e.1)) } pub fn word_characters(&self) -> Option<&HashSet> { Override::as_option( self.config_override().map(|o| &o.word_characters), Some(&self.language.config.word_characters), ) } pub fn brackets(&self) -> impl Iterator { let mut disabled_ids = self .config_override() .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice()); self.language .config .brackets .pairs .iter() .enumerate() .map(move |(ix, bracket)| { let mut is_enabled = true; if let Some(next_disabled_ix) = disabled_ids.first() { if ix == *next_disabled_ix as usize { disabled_ids = &disabled_ids[1..]; is_enabled = false; } } (bracket, is_enabled) }) } pub fn should_autoclose_before(&self, c: char) -> bool { c.is_whitespace() || self.language.config.autoclose_before.contains(c) } pub fn language_allowed(&self, name: &LanguageServerName) -> bool { let config = &self.language.config; let opt_in_servers = &config.scope_opt_in_language_servers; if opt_in_servers.iter().any(|o| *o == *name.0) { if let Some(over) = self.config_override() { over.opt_into_language_servers.iter().any(|o| *o == *name.0) } else { false } } else { true } } fn config_override(&self) -> Option<&LanguageConfigOverride> { let id = self.override_id?; let grammar = self.language.grammar.as_ref()?; let override_config = grammar.override_config.as_ref()?; override_config.values.get(&id).map(|e| &e.1) } } impl Hash for Language { fn hash(&self, state: &mut H) { self.id().hash(state) } } impl PartialEq for Language { fn eq(&self, other: &Self) -> bool { self.id().eq(&other.id()) } } impl Eq for Language {} impl Debug for Language { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Language") .field("name", &self.config.name) .finish() } } impl Grammar { pub fn id(&self) -> usize { self.id } fn parse_text(&self, text: &Rope, old_tree: Option) -> 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() }) } pub fn highlight_map(&self) -> HighlightMap { self.highlight_map.lock().clone() } pub fn highlight_id_for_name(&self, name: &str) -> Option { let capture_id = self .highlights_query .as_ref()? .capture_index_for_name(name)?; Some(self.highlight_map.lock().get(capture_id)) } } impl CodeLabel { pub fn plain(text: String, filter_text: Option<&str>) -> Self { let mut result = Self { runs: Vec::new(), filter_range: 0..text.len(), text, }; if let Some(filter_text) = filter_text { if let Some(ix) = result.text.find(filter_text) { result.filter_range = ix..ix + filter_text.len(); } } result } } #[cfg(any(test, feature = "test-support"))] impl Default for FakeLspAdapter { fn default() -> Self { Self { name: "the-fake-language-server", capabilities: lsp::LanguageServer::full_capabilities(), initializer: None, disk_based_diagnostics_progress_token: None, initialization_options: None, disk_based_diagnostics_sources: Vec::new(), prettier_plugins: Vec::new(), } } } #[cfg(any(test, feature = "test-support"))] #[async_trait] impl LspAdapter for Arc { async fn name(&self) -> LanguageServerName { LanguageServerName(self.name.into()) } fn short_name(&self) -> &'static str { "FakeLspAdapter" } async fn fetch_latest_server_version( &self, _: &dyn LspAdapterDelegate, ) -> Result> { unreachable!(); } async fn fetch_server_binary( &self, _: Box, _: PathBuf, _: &dyn LspAdapterDelegate, ) -> Result { unreachable!(); } async fn cached_server_binary( &self, _: PathBuf, _: &dyn LspAdapterDelegate, ) -> Option { unreachable!(); } async fn installation_test_binary(&self, _: PathBuf) -> Option { unreachable!(); } fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {} async fn disk_based_diagnostic_sources(&self) -> Vec { self.disk_based_diagnostics_sources.clone() } async fn disk_based_diagnostics_progress_token(&self) -> Option { self.disk_based_diagnostics_progress_token.clone() } async fn initialization_options(&self) -> Option { self.initialization_options.clone() } fn prettier_plugins(&self) -> &[&'static str] { &self.prettier_plugins } } fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option)]) { for (ix, name) in query.capture_names().iter().enumerate() { for (capture_name, index) in captures.iter_mut() { if capture_name == name { **index = Some(ix as u32); break; } } } } pub fn point_to_lsp(point: PointUtf16) -> lsp::Position { lsp::Position::new(point.row, point.column) } pub fn point_from_lsp(point: lsp::Position) -> Unclipped { Unclipped(PointUtf16::new(point.line, point.character)) } pub fn range_to_lsp(range: Range) -> lsp::Range { lsp::Range { start: point_to_lsp(range.start), end: point_to_lsp(range.end), } } pub fn range_from_lsp(range: lsp::Range) -> Range> { let mut start = point_from_lsp(range.start); let mut end = point_from_lsp(range.end); if start > end { mem::swap(&mut start, &mut end); } start..end } #[cfg(test)] mod tests { use super::*; use gpui::TestAppContext; #[gpui::test(iterations = 10)] async fn test_first_line_pattern(cx: &mut TestAppContext) { let mut languages = LanguageRegistry::test(); languages.set_executor(cx.background()); let languages = Arc::new(languages); languages.register( "/javascript", LanguageConfig { name: "JavaScript".into(), path_suffixes: vec!["js".into()], first_line_pattern: Some(Regex::new(r"\bnode\b").unwrap()), ..Default::default() }, tree_sitter_typescript::language_tsx(), vec![], |_| Default::default(), ); languages .language_for_file("the/script", None) .await .unwrap_err(); languages .language_for_file("the/script", Some(&"nothing".into())) .await .unwrap_err(); assert_eq!( languages .language_for_file("the/script", Some(&"#!/bin/env node".into())) .await .unwrap() .name() .as_ref(), "JavaScript" ); } #[gpui::test(iterations = 10)] async fn test_language_loading(cx: &mut TestAppContext) { let mut languages = LanguageRegistry::test(); languages.set_executor(cx.background()); let languages = Arc::new(languages); languages.register( "/JSON", LanguageConfig { name: "JSON".into(), path_suffixes: vec!["json".into()], ..Default::default() }, tree_sitter_json::language(), vec![], |_| Default::default(), ); languages.register( "/rust", LanguageConfig { name: "Rust".into(), path_suffixes: vec!["rs".into()], ..Default::default() }, tree_sitter_rust::language(), vec![], |_| Default::default(), ); assert_eq!( languages.language_names(), &[ "JSON".to_string(), "Plain Text".to_string(), "Rust".to_string(), ] ); let rust1 = languages.language_for_name("Rust"); let rust2 = languages.language_for_name("Rust"); // Ensure language is still listed even if it's being loaded. assert_eq!( languages.language_names(), &[ "JSON".to_string(), "Plain Text".to_string(), "Rust".to_string(), ] ); let (rust1, rust2) = futures::join!(rust1, rust2); assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap())); // Ensure language is still listed even after loading it. assert_eq!( languages.language_names(), &[ "JSON".to_string(), "Plain Text".to_string(), "Rust".to_string(), ] ); // Loading an unknown language returns an error. assert!(languages.language_for_name("Unknown").await.is_err()); } }