1#![warn(missing_docs)]
2//! The `language` crate provides a large chunk of Zed's language-related
3//! features (the other big contributors being project and lsp crates that revolve around LSP features).
4//! Namely, this crate:
5//! - Provides [`Language`], [`Grammar`] and [`LanguageRegistry`] types that
6//! use Tree-sitter to provide syntax highlighting to the editor; note though that `language` doesn't perform the highlighting by itself. It only maps ranges in a buffer to colors. Treesitter is also used for buffer outlines (lists of symbols in a buffer)
7//! - Exposes [`LanguageConfig`] that describes how constructs (like brackets or line comments) should be handled by the editor for a source file of a particular language.
8//!
9//! Notably we do *not* assign a single language to a single file; in real world a single file can consist of multiple programming languages - HTML is a good example of that - and `language` crate tends to reflect that status quo in it's API.
10mod buffer;
11mod diagnostic_set;
12mod highlight_map;
13pub mod language_settings;
14mod outline;
15pub mod proto;
16mod syntax_map;
17
18#[cfg(test)]
19mod buffer_tests;
20pub mod markdown;
21
22use anyhow::{anyhow, Context, Result};
23use async_trait::async_trait;
24use collections::{HashMap, HashSet};
25use futures::{
26 channel::{mpsc, oneshot},
27 future::{BoxFuture, Shared},
28 FutureExt, TryFutureExt as _,
29};
30use gpui::{AppContext, AsyncAppContext, BackgroundExecutor, Task};
31pub use highlight_map::HighlightMap;
32use lazy_static::lazy_static;
33use lsp::{CodeActionKind, LanguageServerBinary};
34use parking_lot::{Mutex, RwLock};
35use postage::watch;
36use regex::Regex;
37use serde::{de, Deserialize, Deserializer};
38use serde_json::Value;
39use std::{
40 any::Any,
41 borrow::Cow,
42 cell::RefCell,
43 fmt::Debug,
44 hash::Hash,
45 mem,
46 ops::{Not, Range},
47 path::{Path, PathBuf},
48 str,
49 sync::{
50 atomic::{AtomicU64, AtomicUsize, Ordering::SeqCst},
51 Arc,
52 },
53};
54use syntax_map::SyntaxSnapshot;
55use theme::{SyntaxTheme, Theme};
56use tree_sitter::{self, Query};
57use unicase::UniCase;
58use util::{http::HttpClient, paths::PathExt};
59use util::{post_inc, ResultExt, TryFutureExt as _, UnwrapFuture};
60
61pub use buffer::Operation;
62pub use buffer::*;
63pub use diagnostic_set::DiagnosticEntry;
64pub use lsp::LanguageServerId;
65pub use outline::{Outline, OutlineItem};
66pub use syntax_map::{OwnedSyntaxLayer, SyntaxLayer};
67pub use text::LineEnding;
68pub use tree_sitter::{Parser, Tree};
69
70/// Initializes the `language` crate.
71///
72/// This should be called before making use of items from the create.
73pub fn init(cx: &mut AppContext) {
74 language_settings::init(cx);
75}
76
77#[derive(Clone, Default)]
78struct LspBinaryStatusSender {
79 txs: Arc<Mutex<Vec<mpsc::UnboundedSender<(Arc<Language>, LanguageServerBinaryStatus)>>>>,
80}
81
82impl LspBinaryStatusSender {
83 fn subscribe(&self) -> mpsc::UnboundedReceiver<(Arc<Language>, LanguageServerBinaryStatus)> {
84 let (tx, rx) = mpsc::unbounded();
85 self.txs.lock().push(tx);
86 rx
87 }
88
89 fn send(&self, language: Arc<Language>, status: LanguageServerBinaryStatus) {
90 let mut txs = self.txs.lock();
91 txs.retain(|tx| {
92 tx.unbounded_send((language.clone(), status.clone()))
93 .is_ok()
94 });
95 }
96}
97
98thread_local! {
99 static PARSER: RefCell<Parser> = {
100 RefCell::new(Parser::new())
101 };
102}
103
104lazy_static! {
105 pub(crate) static ref NEXT_GRAMMAR_ID: AtomicUsize = Default::default();
106 /// A shared grammar for plain text, exposed for reuse by downstream crates.
107 #[doc(hidden)]
108 pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
109 LanguageConfig {
110 name: "Plain Text".into(),
111 ..Default::default()
112 },
113 None,
114 ));
115}
116
117/// Types that represent a position in a buffer, and can be converted into
118/// an LSP position, to send to a language server.
119pub trait ToLspPosition {
120 /// Converts the value into an LSP position.
121 fn to_lsp_position(self) -> lsp::Position;
122}
123
124/// A name of a language server.
125#[derive(Clone, Debug, PartialEq, Eq, Hash)]
126pub struct LanguageServerName(pub Arc<str>);
127
128/// Represents a Language Server, with certain cached sync properties.
129/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
130/// once at startup, and caches the results.
131pub struct CachedLspAdapter {
132 pub name: LanguageServerName,
133 pub short_name: &'static str,
134 pub disk_based_diagnostic_sources: Vec<String>,
135 pub disk_based_diagnostics_progress_token: Option<String>,
136 pub language_ids: HashMap<String, String>,
137 pub adapter: Arc<dyn LspAdapter>,
138 pub reinstall_attempt_count: AtomicU64,
139}
140
141impl CachedLspAdapter {
142 pub async fn new(adapter: Arc<dyn LspAdapter>) -> Arc<Self> {
143 let name = adapter.name().await;
144 let short_name = adapter.short_name();
145 let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await;
146 let disk_based_diagnostics_progress_token =
147 adapter.disk_based_diagnostics_progress_token().await;
148 let language_ids = adapter.language_ids().await;
149
150 Arc::new(CachedLspAdapter {
151 name,
152 short_name,
153 disk_based_diagnostic_sources,
154 disk_based_diagnostics_progress_token,
155 language_ids,
156 adapter,
157 reinstall_attempt_count: AtomicU64::new(0),
158 })
159 }
160
161 pub async fn fetch_latest_server_version(
162 &self,
163 delegate: &dyn LspAdapterDelegate,
164 ) -> Result<Box<dyn 'static + Send + Any>> {
165 self.adapter.fetch_latest_server_version(delegate).await
166 }
167
168 pub fn will_fetch_server(
169 &self,
170 delegate: &Arc<dyn LspAdapterDelegate>,
171 cx: &mut AsyncAppContext,
172 ) -> Option<Task<Result<()>>> {
173 self.adapter.will_fetch_server(delegate, cx)
174 }
175
176 pub fn will_start_server(
177 &self,
178 delegate: &Arc<dyn LspAdapterDelegate>,
179 cx: &mut AsyncAppContext,
180 ) -> Option<Task<Result<()>>> {
181 self.adapter.will_start_server(delegate, cx)
182 }
183
184 pub async fn fetch_server_binary(
185 &self,
186 version: Box<dyn 'static + Send + Any>,
187 container_dir: PathBuf,
188 delegate: &dyn LspAdapterDelegate,
189 ) -> Result<LanguageServerBinary> {
190 self.adapter
191 .fetch_server_binary(version, container_dir, delegate)
192 .await
193 }
194
195 pub async fn cached_server_binary(
196 &self,
197 container_dir: PathBuf,
198 delegate: &dyn LspAdapterDelegate,
199 ) -> Option<LanguageServerBinary> {
200 self.adapter
201 .cached_server_binary(container_dir, delegate)
202 .await
203 }
204
205 pub fn can_be_reinstalled(&self) -> bool {
206 self.adapter.can_be_reinstalled()
207 }
208
209 pub async fn installation_test_binary(
210 &self,
211 container_dir: PathBuf,
212 ) -> Option<LanguageServerBinary> {
213 self.adapter.installation_test_binary(container_dir).await
214 }
215
216 pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
217 self.adapter.code_action_kinds()
218 }
219
220 pub fn workspace_configuration(
221 &self,
222 workspace_root: &Path,
223 cx: &mut AppContext,
224 ) -> BoxFuture<'static, Value> {
225 self.adapter.workspace_configuration(workspace_root, cx)
226 }
227
228 pub fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
229 self.adapter.process_diagnostics(params)
230 }
231
232 pub async fn process_completion(&self, completion_item: &mut lsp::CompletionItem) {
233 self.adapter.process_completion(completion_item).await
234 }
235
236 pub async fn label_for_completion(
237 &self,
238 completion_item: &lsp::CompletionItem,
239 language: &Arc<Language>,
240 ) -> Option<CodeLabel> {
241 self.adapter
242 .label_for_completion(completion_item, language)
243 .await
244 }
245
246 pub async fn label_for_symbol(
247 &self,
248 name: &str,
249 kind: lsp::SymbolKind,
250 language: &Arc<Language>,
251 ) -> Option<CodeLabel> {
252 self.adapter.label_for_symbol(name, kind, language).await
253 }
254
255 pub fn prettier_plugins(&self) -> &[&'static str] {
256 self.adapter.prettier_plugins()
257 }
258}
259
260/// [`LspAdapterDelegate`] allows [`LspAdapter]` implementations to interface with the application
261// e.g. to display a notification or fetch data from the web.
262pub trait LspAdapterDelegate: Send + Sync {
263 fn show_notification(&self, message: &str, cx: &mut AppContext);
264 fn http_client(&self) -> Arc<dyn HttpClient>;
265}
266
267#[async_trait]
268pub trait LspAdapter: 'static + Send + Sync {
269 async fn name(&self) -> LanguageServerName;
270
271 fn short_name(&self) -> &'static str;
272
273 async fn fetch_latest_server_version(
274 &self,
275 delegate: &dyn LspAdapterDelegate,
276 ) -> Result<Box<dyn 'static + Send + Any>>;
277
278 fn will_fetch_server(
279 &self,
280 _: &Arc<dyn LspAdapterDelegate>,
281 _: &mut AsyncAppContext,
282 ) -> Option<Task<Result<()>>> {
283 None
284 }
285
286 fn will_start_server(
287 &self,
288 _: &Arc<dyn LspAdapterDelegate>,
289 _: &mut AsyncAppContext,
290 ) -> Option<Task<Result<()>>> {
291 None
292 }
293
294 async fn fetch_server_binary(
295 &self,
296 version: Box<dyn 'static + Send + Any>,
297 container_dir: PathBuf,
298 delegate: &dyn LspAdapterDelegate,
299 ) -> Result<LanguageServerBinary>;
300
301 async fn cached_server_binary(
302 &self,
303 container_dir: PathBuf,
304 delegate: &dyn LspAdapterDelegate,
305 ) -> Option<LanguageServerBinary>;
306
307 /// Returns true if a language server can be reinstalled.
308 /// If language server initialization fails, a reinstallation will be attempted unless the value returned from this method is false.
309 /// Implementations that rely on software already installed on user's system
310 /// should have [`can_be_reinstalled`] return false.
311 fn can_be_reinstalled(&self) -> bool {
312 true
313 }
314
315 async fn installation_test_binary(
316 &self,
317 container_dir: PathBuf,
318 ) -> Option<LanguageServerBinary>;
319
320 fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
321
322 /// A callback called for each [`lsp_types::CompletionItem`] obtained from LSP server.
323 /// Some LspAdapter implementations might want to modify the obtained item to
324 /// change how it's displayed.
325 async fn process_completion(&self, _: &mut lsp::CompletionItem) {}
326
327 async fn label_for_completion(
328 &self,
329 _: &lsp::CompletionItem,
330 _: &Arc<Language>,
331 ) -> Option<CodeLabel> {
332 None
333 }
334
335 async fn label_for_symbol(
336 &self,
337 _: &str,
338 _: lsp::SymbolKind,
339 _: &Arc<Language>,
340 ) -> Option<CodeLabel> {
341 None
342 }
343
344 /// Returns initialization options that are going to be sent to a LSP server as a part of [`lsp_types::InitializeParams`]
345 async fn initialization_options(&self) -> Option<Value> {
346 None
347 }
348
349 fn workspace_configuration(&self, _: &Path, _: &mut AppContext) -> BoxFuture<'static, Value> {
350 futures::future::ready(serde_json::json!({})).boxed()
351 }
352
353 /// Returns a list of code actions supported by a given LspAdapter
354 fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
355 Some(vec![
356 CodeActionKind::EMPTY,
357 CodeActionKind::QUICKFIX,
358 CodeActionKind::REFACTOR,
359 CodeActionKind::REFACTOR_EXTRACT,
360 CodeActionKind::SOURCE,
361 ])
362 }
363
364 async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
365 Default::default()
366 }
367
368 async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
369 None
370 }
371
372 async fn language_ids(&self) -> HashMap<String, String> {
373 Default::default()
374 }
375
376 fn prettier_plugins(&self) -> &[&'static str] {
377 &[]
378 }
379}
380
381#[derive(Clone, Debug, PartialEq, Eq)]
382pub struct CodeLabel {
383 pub text: String,
384 pub runs: Vec<(Range<usize>, HighlightId)>,
385 pub filter_range: Range<usize>,
386}
387
388#[derive(Clone, Deserialize)]
389pub struct LanguageConfig {
390 /// Human-readable name of the language.
391 pub name: Arc<str>,
392 // The name of the grammar in a WASM bundle.
393 pub grammar_name: Option<Arc<str>>,
394 /// Given a list of `LanguageConfig`'s, the language of a file can be determined based on the path extension matching any of the `path_suffixes`.
395 pub path_suffixes: Vec<String>,
396 /// List of bracket types in a language.
397 pub brackets: BracketPairConfig,
398 /// A regex pattern that determines whether the language should be assigned to a file or not.
399 #[serde(default, deserialize_with = "deserialize_regex")]
400 pub first_line_pattern: Option<Regex>,
401 /// If set to true, auto indentation uses last non empty line to determine
402 /// the indentation level for a new line.
403 #[serde(default = "auto_indent_using_last_non_empty_line_default")]
404 pub auto_indent_using_last_non_empty_line: bool,
405 /// A regex that is used to determine whether the indentation level should be
406 /// increased in the following line.
407 #[serde(default, deserialize_with = "deserialize_regex")]
408 pub increase_indent_pattern: Option<Regex>,
409 /// A regex that is used to determine whether the indentation level should be
410 /// decreased in the following line.
411 #[serde(default, deserialize_with = "deserialize_regex")]
412 pub decrease_indent_pattern: Option<Regex>,
413 /// A list of characters that trigger the automatic insertion of a closing
414 /// bracket when they immediately precede the point where an opening
415 /// bracket is inserted.
416 #[serde(default)]
417 pub autoclose_before: String,
418 /// A placeholder used internally by Semantic Index.
419 #[serde(default)]
420 pub collapsed_placeholder: String,
421 /// A line comment string that is inserted in e.g. `toggle comments` action.
422 #[serde(default)]
423 pub line_comment: Option<Arc<str>>,
424 /// Starting and closing characters of a block comment.
425 #[serde(default)]
426 pub block_comment: Option<(Arc<str>, Arc<str>)>,
427 /// A list of language servers that are allowed to run on subranges of a given language.
428 #[serde(default)]
429 pub scope_opt_in_language_servers: Vec<String>,
430 #[serde(default)]
431 pub overrides: HashMap<String, LanguageConfigOverride>,
432 /// A list of characters that Zed should treat as word characters for the
433 /// purpose of features that operate on word boundaries, like 'move to next word end'
434 /// or a whole-word search in buffer search.
435 #[serde(default)]
436 pub word_characters: HashSet<char>,
437 /// The name of a Prettier parser that should be used for this language.
438 #[serde(default)]
439 pub prettier_parser_name: Option<String>,
440}
441
442/// Tree-sitter language queries for a given language.
443#[derive(Debug, Default)]
444pub struct LanguageQueries {
445 pub highlights: Option<Cow<'static, str>>,
446 pub brackets: Option<Cow<'static, str>>,
447 pub indents: Option<Cow<'static, str>>,
448 pub outline: Option<Cow<'static, str>>,
449 pub embedding: Option<Cow<'static, str>>,
450 pub injections: Option<Cow<'static, str>>,
451 pub overrides: Option<Cow<'static, str>>,
452}
453
454/// Represents a language for the given range. Some languages (e.g. HTML)
455/// interleave several languages together, thus a single buffer might actually contain
456/// several nested scopes.
457#[derive(Clone, Debug)]
458pub struct LanguageScope {
459 language: Arc<Language>,
460 override_id: Option<u32>,
461}
462
463#[derive(Clone, Deserialize, Default, Debug)]
464pub struct LanguageConfigOverride {
465 #[serde(default)]
466 pub line_comment: Override<Arc<str>>,
467 #[serde(default)]
468 pub block_comment: Override<(Arc<str>, Arc<str>)>,
469 #[serde(skip_deserializing)]
470 pub disabled_bracket_ixs: Vec<u16>,
471 #[serde(default)]
472 pub word_characters: Override<HashSet<char>>,
473 #[serde(default)]
474 pub opt_into_language_servers: Vec<String>,
475}
476
477#[derive(Clone, Deserialize, Debug)]
478#[serde(untagged)]
479pub enum Override<T> {
480 Remove { remove: bool },
481 Set(T),
482}
483
484impl<T> Default for Override<T> {
485 fn default() -> Self {
486 Override::Remove { remove: false }
487 }
488}
489
490impl<T> Override<T> {
491 fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> {
492 match this {
493 Some(Self::Set(value)) => Some(value),
494 Some(Self::Remove { remove: true }) => None,
495 Some(Self::Remove { remove: false }) | None => original,
496 }
497 }
498}
499
500impl Default for LanguageConfig {
501 fn default() -> Self {
502 Self {
503 name: "".into(),
504 grammar_name: None,
505 path_suffixes: Default::default(),
506 brackets: Default::default(),
507 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
508 first_line_pattern: Default::default(),
509 increase_indent_pattern: Default::default(),
510 decrease_indent_pattern: Default::default(),
511 autoclose_before: Default::default(),
512 line_comment: Default::default(),
513 block_comment: Default::default(),
514 scope_opt_in_language_servers: Default::default(),
515 overrides: Default::default(),
516 word_characters: Default::default(),
517 prettier_parser_name: None,
518 collapsed_placeholder: Default::default(),
519 }
520 }
521}
522
523fn auto_indent_using_last_non_empty_line_default() -> bool {
524 true
525}
526
527fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
528 let source = Option::<String>::deserialize(d)?;
529 if let Some(source) = source {
530 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
531 } else {
532 Ok(None)
533 }
534}
535
536#[doc(hidden)]
537#[cfg(any(test, feature = "test-support"))]
538pub struct FakeLspAdapter {
539 pub name: &'static str,
540 pub initialization_options: Option<Value>,
541 pub capabilities: lsp::ServerCapabilities,
542 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
543 pub disk_based_diagnostics_progress_token: Option<String>,
544 pub disk_based_diagnostics_sources: Vec<String>,
545 pub prettier_plugins: Vec<&'static str>,
546}
547
548/// Configuration of handling bracket pairs for a given language.
549///
550/// This struct includes settings for defining which pairs of characters are considered brackets and
551/// also specifies any language-specific scopes where these pairs should be ignored for bracket matching purposes.
552#[derive(Clone, Debug, Default)]
553pub struct BracketPairConfig {
554 /// A list of character pairs that should be treated as brackets in the context of a given language.
555 pub pairs: Vec<BracketPair>,
556 /// A list of tree-sitter scopes for which a given bracket should not be active.
557 /// N-th entry in `[Self::disabled_scopes_by_bracket_ix]` contains a list of disabled scopes for an n-th entry in `[Self::pairs]`
558 pub disabled_scopes_by_bracket_ix: Vec<Vec<String>>,
559}
560
561impl<'de> Deserialize<'de> for BracketPairConfig {
562 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
563 where
564 D: Deserializer<'de>,
565 {
566 #[derive(Deserialize)]
567 pub struct Entry {
568 #[serde(flatten)]
569 pub bracket_pair: BracketPair,
570 #[serde(default)]
571 pub not_in: Vec<String>,
572 }
573
574 let result = Vec::<Entry>::deserialize(deserializer)?;
575 let mut brackets = Vec::with_capacity(result.len());
576 let mut disabled_scopes_by_bracket_ix = Vec::with_capacity(result.len());
577 for entry in result {
578 brackets.push(entry.bracket_pair);
579 disabled_scopes_by_bracket_ix.push(entry.not_in);
580 }
581
582 Ok(BracketPairConfig {
583 pairs: brackets,
584 disabled_scopes_by_bracket_ix,
585 })
586 }
587}
588
589/// Describes a single bracket pair and how an editor should react to e.g. inserting
590/// an opening bracket or to a newline character insertion inbetween `start` and `end` characters.
591#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
592pub struct BracketPair {
593 /// Starting substring for a bracket.
594 pub start: String,
595 /// Ending substring for a bracket.
596 pub end: String,
597 /// True if `end` should be automatically inserted right after `start` characters.
598 pub close: bool,
599 /// True if an extra newline should be inserted while the cursor is in the middle
600 /// of that bracket pair.
601 pub newline: bool,
602}
603
604pub struct Language {
605 pub(crate) config: LanguageConfig,
606 pub(crate) grammar: Option<Arc<Grammar>>,
607 pub(crate) adapters: Vec<Arc<CachedLspAdapter>>,
608
609 #[cfg(any(test, feature = "test-support"))]
610 fake_adapter: Option<(
611 mpsc::UnboundedSender<lsp::FakeLanguageServer>,
612 Arc<FakeLspAdapter>,
613 )>,
614}
615
616pub struct Grammar {
617 id: usize,
618 pub ts_language: tree_sitter::Language,
619 pub(crate) error_query: Query,
620 pub(crate) highlights_query: Option<Query>,
621 pub(crate) brackets_config: Option<BracketConfig>,
622 pub(crate) indents_config: Option<IndentConfig>,
623 pub outline_config: Option<OutlineConfig>,
624 pub embedding_config: Option<EmbeddingConfig>,
625 pub(crate) injection_config: Option<InjectionConfig>,
626 pub(crate) override_config: Option<OverrideConfig>,
627 pub(crate) highlight_map: Mutex<HighlightMap>,
628}
629
630struct IndentConfig {
631 query: Query,
632 indent_capture_ix: u32,
633 start_capture_ix: Option<u32>,
634 end_capture_ix: Option<u32>,
635 outdent_capture_ix: Option<u32>,
636}
637
638pub struct OutlineConfig {
639 pub query: Query,
640 pub item_capture_ix: u32,
641 pub name_capture_ix: u32,
642 pub context_capture_ix: Option<u32>,
643 pub extra_context_capture_ix: Option<u32>,
644}
645
646#[derive(Debug)]
647pub struct EmbeddingConfig {
648 pub query: Query,
649 pub item_capture_ix: u32,
650 pub name_capture_ix: Option<u32>,
651 pub context_capture_ix: Option<u32>,
652 pub collapse_capture_ix: Option<u32>,
653 pub keep_capture_ix: Option<u32>,
654}
655
656struct InjectionConfig {
657 query: Query,
658 content_capture_ix: u32,
659 language_capture_ix: Option<u32>,
660 patterns: Vec<InjectionPatternConfig>,
661}
662
663struct OverrideConfig {
664 query: Query,
665 values: HashMap<u32, (String, LanguageConfigOverride)>,
666}
667
668#[derive(Default, Clone)]
669struct InjectionPatternConfig {
670 language: Option<Box<str>>,
671 combined: bool,
672}
673
674struct BracketConfig {
675 query: Query,
676 open_capture_ix: u32,
677 close_capture_ix: u32,
678}
679
680#[derive(Clone)]
681pub enum LanguageServerBinaryStatus {
682 CheckingForUpdate,
683 Downloading,
684 Downloaded,
685 Cached,
686 Failed { error: String },
687}
688
689type AvailableLanguageId = usize;
690
691#[derive(Clone)]
692struct AvailableLanguage {
693 id: AvailableLanguageId,
694 config: LanguageConfig,
695 grammar: AvailableGrammar,
696 lsp_adapters: Vec<Arc<dyn LspAdapter>>,
697 loaded: bool,
698}
699
700#[derive(Clone)]
701enum AvailableGrammar {
702 Native {
703 grammar: tree_sitter::Language,
704 asset_dir: &'static str,
705 get_queries: fn(&str) -> LanguageQueries,
706 },
707 Wasm {
708 _grammar_name: Arc<str>,
709 _path: Arc<Path>,
710 },
711}
712
713pub struct LanguageRegistry {
714 state: RwLock<LanguageRegistryState>,
715 language_server_download_dir: Option<Arc<Path>>,
716 login_shell_env_loaded: Shared<Task<()>>,
717 #[allow(clippy::type_complexity)]
718 lsp_binary_paths: Mutex<
719 HashMap<LanguageServerName, Shared<Task<Result<LanguageServerBinary, Arc<anyhow::Error>>>>>,
720 >,
721 executor: Option<BackgroundExecutor>,
722 lsp_binary_status_tx: LspBinaryStatusSender,
723}
724
725struct LanguageRegistryState {
726 next_language_server_id: usize,
727 languages: Vec<Arc<Language>>,
728 available_languages: Vec<AvailableLanguage>,
729 next_available_language_id: AvailableLanguageId,
730 loading_languages: HashMap<AvailableLanguageId, Vec<oneshot::Sender<Result<Arc<Language>>>>>,
731 subscription: (watch::Sender<()>, watch::Receiver<()>),
732 theme: Option<Arc<Theme>>,
733 version: usize,
734 reload_count: usize,
735}
736
737pub struct PendingLanguageServer {
738 pub server_id: LanguageServerId,
739 pub task: Task<Result<lsp::LanguageServer>>,
740 pub container_dir: Option<Arc<Path>>,
741}
742
743impl LanguageRegistry {
744 pub fn new(login_shell_env_loaded: Task<()>) -> Self {
745 Self {
746 state: RwLock::new(LanguageRegistryState {
747 next_language_server_id: 0,
748 languages: vec![PLAIN_TEXT.clone()],
749 available_languages: Default::default(),
750 next_available_language_id: 0,
751 loading_languages: Default::default(),
752 subscription: watch::channel(),
753 theme: Default::default(),
754 version: 0,
755 reload_count: 0,
756 }),
757 language_server_download_dir: None,
758 login_shell_env_loaded: login_shell_env_loaded.shared(),
759 lsp_binary_paths: Default::default(),
760 executor: None,
761 lsp_binary_status_tx: Default::default(),
762 }
763 }
764
765 #[cfg(any(test, feature = "test-support"))]
766 pub fn test() -> Self {
767 Self::new(Task::ready(()))
768 }
769
770 pub fn set_executor(&mut self, executor: BackgroundExecutor) {
771 self.executor = Some(executor);
772 }
773
774 /// Clear out all of the loaded languages and reload them from scratch.
775 ///
776 /// This is useful in development, when queries have changed.
777 #[cfg(debug_assertions)]
778 pub fn reload(&self) {
779 self.state.write().reload();
780 }
781
782 pub fn register(
783 &self,
784 asset_dir: &'static str,
785 config: LanguageConfig,
786 grammar: tree_sitter::Language,
787 lsp_adapters: Vec<Arc<dyn LspAdapter>>,
788 get_queries: fn(&str) -> LanguageQueries,
789 ) {
790 let state = &mut *self.state.write();
791 state.available_languages.push(AvailableLanguage {
792 id: post_inc(&mut state.next_available_language_id),
793 config,
794 grammar: AvailableGrammar::Native {
795 grammar,
796 get_queries,
797 asset_dir,
798 },
799 lsp_adapters,
800 loaded: false,
801 });
802 }
803
804 pub fn register_wasm(&self, path: Arc<Path>, grammar_name: Arc<str>, config: LanguageConfig) {
805 let state = &mut *self.state.write();
806 state.available_languages.push(AvailableLanguage {
807 id: post_inc(&mut state.next_available_language_id),
808 config,
809 grammar: AvailableGrammar::Wasm {
810 _grammar_name: grammar_name,
811 _path: path,
812 },
813 lsp_adapters: Vec::new(),
814 loaded: false,
815 });
816 }
817
818 pub fn language_names(&self) -> Vec<String> {
819 let state = self.state.read();
820 let mut result = state
821 .available_languages
822 .iter()
823 .filter_map(|l| l.loaded.not().then_some(l.config.name.to_string()))
824 .chain(state.languages.iter().map(|l| l.config.name.to_string()))
825 .collect::<Vec<_>>();
826 result.sort_unstable_by_key(|language_name| language_name.to_lowercase());
827 result
828 }
829
830 pub fn add(&self, language: Arc<Language>) {
831 self.state.write().add(language);
832 }
833
834 pub fn subscribe(&self) -> watch::Receiver<()> {
835 self.state.read().subscription.1.clone()
836 }
837
838 /// The number of times that the registry has been changed,
839 /// by adding languages or reloading.
840 pub fn version(&self) -> usize {
841 self.state.read().version
842 }
843
844 /// The number of times that the registry has been reloaded.
845 pub fn reload_count(&self) -> usize {
846 self.state.read().reload_count
847 }
848
849 pub fn set_theme(&self, theme: Arc<Theme>) {
850 let mut state = self.state.write();
851 state.theme = Some(theme.clone());
852 for language in &state.languages {
853 language.set_theme(&theme.syntax());
854 }
855 }
856
857 pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
858 self.language_server_download_dir = Some(path.into());
859 }
860
861 pub fn language_for_name(
862 self: &Arc<Self>,
863 name: &str,
864 ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
865 let name = UniCase::new(name);
866 self.get_or_load_language(|config| UniCase::new(config.name.as_ref()) == name)
867 }
868
869 pub fn language_for_name_or_extension(
870 self: &Arc<Self>,
871 string: &str,
872 ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
873 let string = UniCase::new(string);
874 self.get_or_load_language(|config| {
875 UniCase::new(config.name.as_ref()) == string
876 || config
877 .path_suffixes
878 .iter()
879 .any(|suffix| UniCase::new(suffix) == string)
880 })
881 }
882
883 pub fn language_for_file(
884 self: &Arc<Self>,
885 path: impl AsRef<Path>,
886 content: Option<&Rope>,
887 ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
888 let path = path.as_ref();
889 let filename = path.file_name().and_then(|name| name.to_str());
890 let extension = path.extension_or_hidden_file_name();
891 let path_suffixes = [extension, filename];
892 self.get_or_load_language(|config| {
893 let path_matches = config
894 .path_suffixes
895 .iter()
896 .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
897 let content_matches = content.zip(config.first_line_pattern.as_ref()).map_or(
898 false,
899 |(content, pattern)| {
900 let end = content.clip_point(Point::new(0, 256), Bias::Left);
901 let end = content.point_to_offset(end);
902 let text = content.chunks_in_range(0..end).collect::<String>();
903 pattern.is_match(&text)
904 },
905 );
906 path_matches || content_matches
907 })
908 }
909
910 fn get_or_load_language(
911 self: &Arc<Self>,
912 callback: impl Fn(&LanguageConfig) -> bool,
913 ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
914 let (tx, rx) = oneshot::channel();
915
916 let mut state = self.state.write();
917 if let Some(language) = state
918 .languages
919 .iter()
920 .find(|language| callback(&language.config))
921 {
922 let _ = tx.send(Ok(language.clone()));
923 } else if let Some(executor) = self.executor.clone() {
924 if let Some(language) = state
925 .available_languages
926 .iter()
927 .find(|l| !l.loaded && callback(&l.config))
928 .cloned()
929 {
930 let txs = state
931 .loading_languages
932 .entry(language.id)
933 .or_insert_with(|| {
934 let this = self.clone();
935 executor
936 .spawn(async move {
937 let id = language.id;
938 let name = language.config.name.clone();
939 let language = async {
940 let (grammar, queries) = match language.grammar {
941 AvailableGrammar::Native {
942 grammar,
943 asset_dir,
944 get_queries,
945 } => (grammar, (get_queries)(asset_dir)),
946 AvailableGrammar::Wasm { .. } => {
947 Err(anyhow!("not supported"))?
948 }
949 };
950 Language::new(language.config, Some(grammar))
951 .with_lsp_adapters(language.lsp_adapters)
952 .await
953 .with_queries(queries)
954 }
955 .await;
956
957 match language {
958 Ok(language) => {
959 let language = Arc::new(language);
960 let mut state = this.state.write();
961
962 state.add(language.clone());
963 state.mark_language_loaded(id);
964 if let Some(mut txs) = state.loading_languages.remove(&id) {
965 for tx in txs.drain(..) {
966 let _ = tx.send(Ok(language.clone()));
967 }
968 }
969 }
970 Err(e) => {
971 log::error!("failed to load language {name}:\n{:?}", e);
972 let mut state = this.state.write();
973 state.mark_language_loaded(id);
974 if let Some(mut txs) = state.loading_languages.remove(&id) {
975 for tx in txs.drain(..) {
976 let _ = tx.send(Err(anyhow!(
977 "failed to load language {}: {}",
978 name,
979 e
980 )));
981 }
982 }
983 }
984 };
985 })
986 .detach();
987
988 Vec::new()
989 });
990 txs.push(tx);
991 } else {
992 let _ = tx.send(Err(anyhow!("language not found")));
993 }
994 } else {
995 let _ = tx.send(Err(anyhow!("executor does not exist")));
996 }
997
998 rx.unwrap()
999 }
1000
1001 pub fn to_vec(&self) -> Vec<Arc<Language>> {
1002 self.state.read().languages.iter().cloned().collect()
1003 }
1004
1005 pub fn create_pending_language_server(
1006 self: &Arc<Self>,
1007 stderr_capture: Arc<Mutex<Option<String>>>,
1008 language: Arc<Language>,
1009 adapter: Arc<CachedLspAdapter>,
1010 root_path: Arc<Path>,
1011 delegate: Arc<dyn LspAdapterDelegate>,
1012 cx: &mut AppContext,
1013 ) -> Option<PendingLanguageServer> {
1014 let server_id = self.state.write().next_language_server_id();
1015 log::info!(
1016 "starting language server {:?}, path: {root_path:?}, id: {server_id}",
1017 adapter.name.0
1018 );
1019
1020 #[cfg(any(test, feature = "test-support"))]
1021 if language.fake_adapter.is_some() {
1022 let task = cx.spawn(|cx| async move {
1023 let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
1024 let (server, mut fake_server) = lsp::FakeLanguageServer::new(
1025 fake_adapter.name.to_string(),
1026 fake_adapter.capabilities.clone(),
1027 cx.clone(),
1028 );
1029
1030 if let Some(initializer) = &fake_adapter.initializer {
1031 initializer(&mut fake_server);
1032 }
1033
1034 let servers_tx = servers_tx.clone();
1035 cx.background_executor()
1036 .spawn(async move {
1037 if fake_server
1038 .try_receive_notification::<lsp::notification::Initialized>()
1039 .await
1040 .is_some()
1041 {
1042 servers_tx.unbounded_send(fake_server).ok();
1043 }
1044 })
1045 .detach();
1046
1047 Ok(server)
1048 });
1049
1050 return Some(PendingLanguageServer {
1051 server_id,
1052 task,
1053 container_dir: None,
1054 });
1055 }
1056
1057 let download_dir = self
1058 .language_server_download_dir
1059 .clone()
1060 .ok_or_else(|| anyhow!("language server download directory has not been assigned before starting server"))
1061 .log_err()?;
1062 let this = self.clone();
1063 let language = language.clone();
1064 let container_dir: Arc<Path> = Arc::from(download_dir.join(adapter.name.0.as_ref()));
1065 let root_path = root_path.clone();
1066 let adapter = adapter.clone();
1067 let login_shell_env_loaded = self.login_shell_env_loaded.clone();
1068 let lsp_binary_statuses = self.lsp_binary_status_tx.clone();
1069
1070 let task = {
1071 let container_dir = container_dir.clone();
1072 cx.spawn(move |mut cx| async move {
1073 login_shell_env_loaded.await;
1074
1075 let entry = this
1076 .lsp_binary_paths
1077 .lock()
1078 .entry(adapter.name.clone())
1079 .or_insert_with(|| {
1080 let adapter = adapter.clone();
1081 let language = language.clone();
1082 let delegate = delegate.clone();
1083 cx.spawn(|cx| {
1084 get_binary(
1085 adapter,
1086 language,
1087 delegate,
1088 container_dir,
1089 lsp_binary_statuses,
1090 cx,
1091 )
1092 .map_err(Arc::new)
1093 })
1094 .shared()
1095 })
1096 .clone();
1097
1098 let binary = match entry.await {
1099 Ok(binary) => binary,
1100 Err(err) => anyhow::bail!("{err}"),
1101 };
1102
1103 if let Some(task) = adapter.will_start_server(&delegate, &mut cx) {
1104 task.await?;
1105 }
1106
1107 lsp::LanguageServer::new(
1108 stderr_capture,
1109 server_id,
1110 binary,
1111 &root_path,
1112 adapter.code_action_kinds(),
1113 cx,
1114 )
1115 })
1116 };
1117
1118 Some(PendingLanguageServer {
1119 server_id,
1120 task,
1121 container_dir: Some(container_dir),
1122 })
1123 }
1124
1125 pub fn language_server_binary_statuses(
1126 &self,
1127 ) -> mpsc::UnboundedReceiver<(Arc<Language>, LanguageServerBinaryStatus)> {
1128 self.lsp_binary_status_tx.subscribe()
1129 }
1130
1131 pub fn delete_server_container(
1132 &self,
1133 adapter: Arc<CachedLspAdapter>,
1134 cx: &mut AppContext,
1135 ) -> Task<()> {
1136 log::info!("deleting server container");
1137
1138 let mut lock = self.lsp_binary_paths.lock();
1139 lock.remove(&adapter.name);
1140
1141 let download_dir = self
1142 .language_server_download_dir
1143 .clone()
1144 .expect("language server download directory has not been assigned before deleting server container");
1145
1146 cx.spawn(|_| async move {
1147 let container_dir = download_dir.join(adapter.name.0.as_ref());
1148 smol::fs::remove_dir_all(container_dir)
1149 .await
1150 .context("server container removal")
1151 .log_err();
1152 })
1153 }
1154
1155 pub fn next_language_server_id(&self) -> LanguageServerId {
1156 self.state.write().next_language_server_id()
1157 }
1158}
1159
1160impl LanguageRegistryState {
1161 fn next_language_server_id(&mut self) -> LanguageServerId {
1162 LanguageServerId(post_inc(&mut self.next_language_server_id))
1163 }
1164
1165 fn add(&mut self, language: Arc<Language>) {
1166 if let Some(theme) = self.theme.as_ref() {
1167 language.set_theme(&theme.syntax());
1168 }
1169 self.languages.push(language);
1170 self.version += 1;
1171 *self.subscription.0.borrow_mut() = ();
1172 }
1173
1174 #[cfg(debug_assertions)]
1175 fn reload(&mut self) {
1176 self.languages.clear();
1177 self.version += 1;
1178 self.reload_count += 1;
1179 for language in &mut self.available_languages {
1180 language.loaded = false;
1181 }
1182 *self.subscription.0.borrow_mut() = ();
1183 }
1184
1185 /// Mark the given language a having been loaded, so that the
1186 /// language registry won't try to load it again.
1187 fn mark_language_loaded(&mut self, id: AvailableLanguageId) {
1188 for language in &mut self.available_languages {
1189 if language.id == id {
1190 language.loaded = true;
1191 break;
1192 }
1193 }
1194 }
1195}
1196
1197#[cfg(any(test, feature = "test-support"))]
1198impl Default for LanguageRegistry {
1199 fn default() -> Self {
1200 Self::test()
1201 }
1202}
1203
1204async fn get_binary(
1205 adapter: Arc<CachedLspAdapter>,
1206 language: Arc<Language>,
1207 delegate: Arc<dyn LspAdapterDelegate>,
1208 container_dir: Arc<Path>,
1209 statuses: LspBinaryStatusSender,
1210 mut cx: AsyncAppContext,
1211) -> Result<LanguageServerBinary> {
1212 if !container_dir.exists() {
1213 smol::fs::create_dir_all(&container_dir)
1214 .await
1215 .context("failed to create container directory")?;
1216 }
1217
1218 if let Some(task) = adapter.will_fetch_server(&delegate, &mut cx) {
1219 task.await?;
1220 }
1221
1222 let binary = fetch_latest_binary(
1223 adapter.clone(),
1224 language.clone(),
1225 delegate.as_ref(),
1226 &container_dir,
1227 statuses.clone(),
1228 )
1229 .await;
1230
1231 if let Err(error) = binary.as_ref() {
1232 if let Some(binary) = adapter
1233 .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref())
1234 .await
1235 {
1236 statuses.send(language.clone(), LanguageServerBinaryStatus::Cached);
1237 return Ok(binary);
1238 } else {
1239 statuses.send(
1240 language.clone(),
1241 LanguageServerBinaryStatus::Failed {
1242 error: format!("{:?}", error),
1243 },
1244 );
1245 }
1246 }
1247
1248 binary
1249}
1250
1251async fn fetch_latest_binary(
1252 adapter: Arc<CachedLspAdapter>,
1253 language: Arc<Language>,
1254 delegate: &dyn LspAdapterDelegate,
1255 container_dir: &Path,
1256 lsp_binary_statuses_tx: LspBinaryStatusSender,
1257) -> Result<LanguageServerBinary> {
1258 let container_dir: Arc<Path> = container_dir.into();
1259 lsp_binary_statuses_tx.send(
1260 language.clone(),
1261 LanguageServerBinaryStatus::CheckingForUpdate,
1262 );
1263
1264 let version_info = adapter.fetch_latest_server_version(delegate).await?;
1265 lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloading);
1266
1267 let binary = adapter
1268 .fetch_server_binary(version_info, container_dir.to_path_buf(), delegate)
1269 .await?;
1270 lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloaded);
1271
1272 Ok(binary)
1273}
1274
1275impl Language {
1276 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
1277 Self {
1278 config,
1279 grammar: ts_language.map(|ts_language| {
1280 Arc::new(Grammar {
1281 id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst),
1282 highlights_query: None,
1283 brackets_config: None,
1284 outline_config: None,
1285 embedding_config: None,
1286 indents_config: None,
1287 injection_config: None,
1288 override_config: None,
1289 error_query: Query::new(&ts_language, "(ERROR) @error").unwrap(),
1290 ts_language,
1291 highlight_map: Default::default(),
1292 })
1293 }),
1294 adapters: Vec::new(),
1295
1296 #[cfg(any(test, feature = "test-support"))]
1297 fake_adapter: None,
1298 }
1299 }
1300
1301 pub fn lsp_adapters(&self) -> &[Arc<CachedLspAdapter>] {
1302 &self.adapters
1303 }
1304
1305 pub fn id(&self) -> Option<usize> {
1306 self.grammar.as_ref().map(|g| g.id)
1307 }
1308
1309 pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
1310 if let Some(query) = queries.highlights {
1311 self = self
1312 .with_highlights_query(query.as_ref())
1313 .context("Error loading highlights query")?;
1314 }
1315 if let Some(query) = queries.brackets {
1316 self = self
1317 .with_brackets_query(query.as_ref())
1318 .context("Error loading brackets query")?;
1319 }
1320 if let Some(query) = queries.indents {
1321 self = self
1322 .with_indents_query(query.as_ref())
1323 .context("Error loading indents query")?;
1324 }
1325 if let Some(query) = queries.outline {
1326 self = self
1327 .with_outline_query(query.as_ref())
1328 .context("Error loading outline query")?;
1329 }
1330 if let Some(query) = queries.embedding {
1331 self = self
1332 .with_embedding_query(query.as_ref())
1333 .context("Error loading embedding query")?;
1334 }
1335 if let Some(query) = queries.injections {
1336 self = self
1337 .with_injection_query(query.as_ref())
1338 .context("Error loading injection query")?;
1339 }
1340 if let Some(query) = queries.overrides {
1341 self = self
1342 .with_override_query(query.as_ref())
1343 .context("Error loading override query")?;
1344 }
1345 Ok(self)
1346 }
1347
1348 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
1349 let grammar = self.grammar_mut();
1350 grammar.highlights_query = Some(Query::new(&grammar.ts_language, source)?);
1351 Ok(self)
1352 }
1353
1354 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
1355 let grammar = self.grammar_mut();
1356 let query = Query::new(&grammar.ts_language, source)?;
1357 let mut item_capture_ix = None;
1358 let mut name_capture_ix = None;
1359 let mut context_capture_ix = None;
1360 let mut extra_context_capture_ix = None;
1361 get_capture_indices(
1362 &query,
1363 &mut [
1364 ("item", &mut item_capture_ix),
1365 ("name", &mut name_capture_ix),
1366 ("context", &mut context_capture_ix),
1367 ("context.extra", &mut extra_context_capture_ix),
1368 ],
1369 );
1370 if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
1371 grammar.outline_config = Some(OutlineConfig {
1372 query,
1373 item_capture_ix,
1374 name_capture_ix,
1375 context_capture_ix,
1376 extra_context_capture_ix,
1377 });
1378 }
1379 Ok(self)
1380 }
1381
1382 pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
1383 let grammar = self.grammar_mut();
1384 let query = Query::new(&grammar.ts_language, source)?;
1385 let mut item_capture_ix = None;
1386 let mut name_capture_ix = None;
1387 let mut context_capture_ix = None;
1388 let mut collapse_capture_ix = None;
1389 let mut keep_capture_ix = None;
1390 get_capture_indices(
1391 &query,
1392 &mut [
1393 ("item", &mut item_capture_ix),
1394 ("name", &mut name_capture_ix),
1395 ("context", &mut context_capture_ix),
1396 ("keep", &mut keep_capture_ix),
1397 ("collapse", &mut collapse_capture_ix),
1398 ],
1399 );
1400 if let Some(item_capture_ix) = item_capture_ix {
1401 grammar.embedding_config = Some(EmbeddingConfig {
1402 query,
1403 item_capture_ix,
1404 name_capture_ix,
1405 context_capture_ix,
1406 collapse_capture_ix,
1407 keep_capture_ix,
1408 });
1409 }
1410 Ok(self)
1411 }
1412
1413 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
1414 let grammar = self.grammar_mut();
1415 let query = Query::new(&grammar.ts_language, source)?;
1416 let mut open_capture_ix = None;
1417 let mut close_capture_ix = None;
1418 get_capture_indices(
1419 &query,
1420 &mut [
1421 ("open", &mut open_capture_ix),
1422 ("close", &mut close_capture_ix),
1423 ],
1424 );
1425 if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
1426 grammar.brackets_config = Some(BracketConfig {
1427 query,
1428 open_capture_ix,
1429 close_capture_ix,
1430 });
1431 }
1432 Ok(self)
1433 }
1434
1435 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
1436 let grammar = self.grammar_mut();
1437 let query = Query::new(&grammar.ts_language, source)?;
1438 let mut indent_capture_ix = None;
1439 let mut start_capture_ix = None;
1440 let mut end_capture_ix = None;
1441 let mut outdent_capture_ix = None;
1442 get_capture_indices(
1443 &query,
1444 &mut [
1445 ("indent", &mut indent_capture_ix),
1446 ("start", &mut start_capture_ix),
1447 ("end", &mut end_capture_ix),
1448 ("outdent", &mut outdent_capture_ix),
1449 ],
1450 );
1451 if let Some(indent_capture_ix) = indent_capture_ix {
1452 grammar.indents_config = Some(IndentConfig {
1453 query,
1454 indent_capture_ix,
1455 start_capture_ix,
1456 end_capture_ix,
1457 outdent_capture_ix,
1458 });
1459 }
1460 Ok(self)
1461 }
1462
1463 pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
1464 let grammar = self.grammar_mut();
1465 let query = Query::new(&grammar.ts_language, source)?;
1466 let mut language_capture_ix = None;
1467 let mut content_capture_ix = None;
1468 get_capture_indices(
1469 &query,
1470 &mut [
1471 ("language", &mut language_capture_ix),
1472 ("content", &mut content_capture_ix),
1473 ],
1474 );
1475 let patterns = (0..query.pattern_count())
1476 .map(|ix| {
1477 let mut config = InjectionPatternConfig::default();
1478 for setting in query.property_settings(ix) {
1479 match setting.key.as_ref() {
1480 "language" => {
1481 config.language = setting.value.clone();
1482 }
1483 "combined" => {
1484 config.combined = true;
1485 }
1486 _ => {}
1487 }
1488 }
1489 config
1490 })
1491 .collect();
1492 if let Some(content_capture_ix) = content_capture_ix {
1493 grammar.injection_config = Some(InjectionConfig {
1494 query,
1495 language_capture_ix,
1496 content_capture_ix,
1497 patterns,
1498 });
1499 }
1500 Ok(self)
1501 }
1502
1503 pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
1504 let query = Query::new(&self.grammar_mut().ts_language, source)?;
1505
1506 let mut override_configs_by_id = HashMap::default();
1507 for (ix, name) in query.capture_names().iter().enumerate() {
1508 if !name.starts_with('_') {
1509 let value = self.config.overrides.remove(*name).unwrap_or_default();
1510 for server_name in &value.opt_into_language_servers {
1511 if !self
1512 .config
1513 .scope_opt_in_language_servers
1514 .contains(server_name)
1515 {
1516 util::debug_panic!("Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server");
1517 }
1518 }
1519
1520 override_configs_by_id.insert(ix as u32, (name.to_string(), value));
1521 }
1522 }
1523
1524 if !self.config.overrides.is_empty() {
1525 let keys = self.config.overrides.keys().collect::<Vec<_>>();
1526 Err(anyhow!(
1527 "language {:?} has overrides in config not in query: {keys:?}",
1528 self.config.name
1529 ))?;
1530 }
1531
1532 for disabled_scope_name in self
1533 .config
1534 .brackets
1535 .disabled_scopes_by_bracket_ix
1536 .iter()
1537 .flatten()
1538 {
1539 if !override_configs_by_id
1540 .values()
1541 .any(|(scope_name, _)| scope_name == disabled_scope_name)
1542 {
1543 Err(anyhow!(
1544 "language {:?} has overrides in config not in query: {disabled_scope_name:?}",
1545 self.config.name
1546 ))?;
1547 }
1548 }
1549
1550 for (name, override_config) in override_configs_by_id.values_mut() {
1551 override_config.disabled_bracket_ixs = self
1552 .config
1553 .brackets
1554 .disabled_scopes_by_bracket_ix
1555 .iter()
1556 .enumerate()
1557 .filter_map(|(ix, disabled_scope_names)| {
1558 if disabled_scope_names.contains(name) {
1559 Some(ix as u16)
1560 } else {
1561 None
1562 }
1563 })
1564 .collect();
1565 }
1566
1567 self.config.brackets.disabled_scopes_by_bracket_ix.clear();
1568 self.grammar_mut().override_config = Some(OverrideConfig {
1569 query,
1570 values: override_configs_by_id,
1571 });
1572 Ok(self)
1573 }
1574
1575 fn grammar_mut(&mut self) -> &mut Grammar {
1576 Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
1577 }
1578
1579 pub async fn with_lsp_adapters(mut self, lsp_adapters: Vec<Arc<dyn LspAdapter>>) -> Self {
1580 for adapter in lsp_adapters {
1581 self.adapters.push(CachedLspAdapter::new(adapter).await);
1582 }
1583 self
1584 }
1585
1586 #[cfg(any(test, feature = "test-support"))]
1587 pub async fn set_fake_lsp_adapter(
1588 &mut self,
1589 fake_lsp_adapter: Arc<FakeLspAdapter>,
1590 ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
1591 let (servers_tx, servers_rx) = mpsc::unbounded();
1592 self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
1593 let adapter = CachedLspAdapter::new(Arc::new(fake_lsp_adapter)).await;
1594 self.adapters = vec![adapter];
1595 servers_rx
1596 }
1597
1598 pub fn name(&self) -> Arc<str> {
1599 self.config.name.clone()
1600 }
1601
1602 pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
1603 match self.adapters.first().as_ref() {
1604 Some(adapter) => &adapter.disk_based_diagnostic_sources,
1605 None => &[],
1606 }
1607 }
1608
1609 pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
1610 for adapter in &self.adapters {
1611 let token = adapter.disk_based_diagnostics_progress_token.as_deref();
1612 if token.is_some() {
1613 return token;
1614 }
1615 }
1616
1617 None
1618 }
1619
1620 pub async fn process_completion(self: &Arc<Self>, completion: &mut lsp::CompletionItem) {
1621 for adapter in &self.adapters {
1622 adapter.process_completion(completion).await;
1623 }
1624 }
1625
1626 pub async fn label_for_completion(
1627 self: &Arc<Self>,
1628 completion: &lsp::CompletionItem,
1629 ) -> Option<CodeLabel> {
1630 self.adapters
1631 .first()
1632 .as_ref()?
1633 .label_for_completion(completion, self)
1634 .await
1635 }
1636
1637 pub async fn label_for_symbol(
1638 self: &Arc<Self>,
1639 name: &str,
1640 kind: lsp::SymbolKind,
1641 ) -> Option<CodeLabel> {
1642 self.adapters
1643 .first()
1644 .as_ref()?
1645 .label_for_symbol(name, kind, self)
1646 .await
1647 }
1648
1649 pub fn highlight_text<'a>(
1650 self: &'a Arc<Self>,
1651 text: &'a Rope,
1652 range: Range<usize>,
1653 ) -> Vec<(Range<usize>, HighlightId)> {
1654 let mut result = Vec::new();
1655 if let Some(grammar) = &self.grammar {
1656 let tree = grammar.parse_text(text, None);
1657 let captures =
1658 SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
1659 grammar.highlights_query.as_ref()
1660 });
1661 let highlight_maps = vec![grammar.highlight_map()];
1662 let mut offset = 0;
1663 for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) {
1664 let end_offset = offset + chunk.text.len();
1665 if let Some(highlight_id) = chunk.syntax_highlight_id {
1666 if !highlight_id.is_default() {
1667 result.push((offset..end_offset, highlight_id));
1668 }
1669 }
1670 offset = end_offset;
1671 }
1672 }
1673 result
1674 }
1675
1676 pub fn path_suffixes(&self) -> &[String] {
1677 &self.config.path_suffixes
1678 }
1679
1680 pub fn should_autoclose_before(&self, c: char) -> bool {
1681 c.is_whitespace() || self.config.autoclose_before.contains(c)
1682 }
1683
1684 pub fn set_theme(&self, theme: &SyntaxTheme) {
1685 if let Some(grammar) = self.grammar.as_ref() {
1686 if let Some(highlights_query) = &grammar.highlights_query {
1687 *grammar.highlight_map.lock() =
1688 HighlightMap::new(highlights_query.capture_names(), theme);
1689 }
1690 }
1691 }
1692
1693 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
1694 self.grammar.as_ref()
1695 }
1696
1697 pub fn default_scope(self: &Arc<Self>) -> LanguageScope {
1698 LanguageScope {
1699 language: self.clone(),
1700 override_id: None,
1701 }
1702 }
1703
1704 pub fn prettier_parser_name(&self) -> Option<&str> {
1705 self.config.prettier_parser_name.as_deref()
1706 }
1707}
1708
1709impl LanguageScope {
1710 pub fn collapsed_placeholder(&self) -> &str {
1711 self.language.config.collapsed_placeholder.as_ref()
1712 }
1713
1714 /// Returns line prefix that is inserted in e.g. line continuations or
1715 /// in `toggle comments` action.
1716 pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
1717 Override::as_option(
1718 self.config_override().map(|o| &o.line_comment),
1719 self.language.config.line_comment.as_ref(),
1720 )
1721 }
1722
1723 pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
1724 Override::as_option(
1725 self.config_override().map(|o| &o.block_comment),
1726 self.language.config.block_comment.as_ref(),
1727 )
1728 .map(|e| (&e.0, &e.1))
1729 }
1730
1731 /// Returns a list of language-specific word characters.
1732 ///
1733 /// By default, Zed treats alphanumeric characters (and '_') as word characters for
1734 /// the purpose of actions like 'move to next word end` or whole-word search.
1735 /// It additionally accounts for language's additional word characters.
1736 pub fn word_characters(&self) -> Option<&HashSet<char>> {
1737 Override::as_option(
1738 self.config_override().map(|o| &o.word_characters),
1739 Some(&self.language.config.word_characters),
1740 )
1741 }
1742
1743 /// Returns a list of bracket pairs for a given language with an additional
1744 /// piece of information about whether the particular bracket pair is currently active for a given language.
1745 pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {
1746 let mut disabled_ids = self
1747 .config_override()
1748 .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice());
1749 self.language
1750 .config
1751 .brackets
1752 .pairs
1753 .iter()
1754 .enumerate()
1755 .map(move |(ix, bracket)| {
1756 let mut is_enabled = true;
1757 if let Some(next_disabled_ix) = disabled_ids.first() {
1758 if ix == *next_disabled_ix as usize {
1759 disabled_ids = &disabled_ids[1..];
1760 is_enabled = false;
1761 }
1762 }
1763 (bracket, is_enabled)
1764 })
1765 }
1766
1767 pub fn should_autoclose_before(&self, c: char) -> bool {
1768 c.is_whitespace() || self.language.config.autoclose_before.contains(c)
1769 }
1770
1771 pub fn language_allowed(&self, name: &LanguageServerName) -> bool {
1772 let config = &self.language.config;
1773 let opt_in_servers = &config.scope_opt_in_language_servers;
1774 if opt_in_servers.iter().any(|o| *o == *name.0) {
1775 if let Some(over) = self.config_override() {
1776 over.opt_into_language_servers.iter().any(|o| *o == *name.0)
1777 } else {
1778 false
1779 }
1780 } else {
1781 true
1782 }
1783 }
1784
1785 fn config_override(&self) -> Option<&LanguageConfigOverride> {
1786 let id = self.override_id?;
1787 let grammar = self.language.grammar.as_ref()?;
1788 let override_config = grammar.override_config.as_ref()?;
1789 override_config.values.get(&id).map(|e| &e.1)
1790 }
1791}
1792
1793impl Hash for Language {
1794 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1795 self.id().hash(state)
1796 }
1797}
1798
1799impl PartialEq for Language {
1800 fn eq(&self, other: &Self) -> bool {
1801 self.id().eq(&other.id())
1802 }
1803}
1804
1805impl Eq for Language {}
1806
1807impl Debug for Language {
1808 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1809 f.debug_struct("Language")
1810 .field("name", &self.config.name)
1811 .finish()
1812 }
1813}
1814
1815impl Grammar {
1816 pub fn id(&self) -> usize {
1817 self.id
1818 }
1819
1820 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
1821 PARSER.with(|parser| {
1822 let mut parser = parser.borrow_mut();
1823 parser
1824 .set_language(&self.ts_language)
1825 .expect("incompatible grammar");
1826 let mut chunks = text.chunks_in_range(0..text.len());
1827 parser
1828 .parse_with(
1829 &mut move |offset, _| {
1830 chunks.seek(offset);
1831 chunks.next().unwrap_or("").as_bytes()
1832 },
1833 old_tree.as_ref(),
1834 )
1835 .unwrap()
1836 })
1837 }
1838
1839 pub fn highlight_map(&self) -> HighlightMap {
1840 self.highlight_map.lock().clone()
1841 }
1842
1843 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
1844 let capture_id = self
1845 .highlights_query
1846 .as_ref()?
1847 .capture_index_for_name(name)?;
1848 Some(self.highlight_map.lock().get(capture_id))
1849 }
1850}
1851
1852impl CodeLabel {
1853 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
1854 let mut result = Self {
1855 runs: Vec::new(),
1856 filter_range: 0..text.len(),
1857 text,
1858 };
1859 if let Some(filter_text) = filter_text {
1860 if let Some(ix) = result.text.find(filter_text) {
1861 result.filter_range = ix..ix + filter_text.len();
1862 }
1863 }
1864 result
1865 }
1866}
1867
1868#[cfg(any(test, feature = "test-support"))]
1869impl Default for FakeLspAdapter {
1870 fn default() -> Self {
1871 Self {
1872 name: "the-fake-language-server",
1873 capabilities: lsp::LanguageServer::full_capabilities(),
1874 initializer: None,
1875 disk_based_diagnostics_progress_token: None,
1876 initialization_options: None,
1877 disk_based_diagnostics_sources: Vec::new(),
1878 prettier_plugins: Vec::new(),
1879 }
1880 }
1881}
1882
1883#[cfg(any(test, feature = "test-support"))]
1884#[async_trait]
1885impl LspAdapter for Arc<FakeLspAdapter> {
1886 async fn name(&self) -> LanguageServerName {
1887 LanguageServerName(self.name.into())
1888 }
1889
1890 fn short_name(&self) -> &'static str {
1891 "FakeLspAdapter"
1892 }
1893
1894 async fn fetch_latest_server_version(
1895 &self,
1896 _: &dyn LspAdapterDelegate,
1897 ) -> Result<Box<dyn 'static + Send + Any>> {
1898 unreachable!();
1899 }
1900
1901 async fn fetch_server_binary(
1902 &self,
1903 _: Box<dyn 'static + Send + Any>,
1904 _: PathBuf,
1905 _: &dyn LspAdapterDelegate,
1906 ) -> Result<LanguageServerBinary> {
1907 unreachable!();
1908 }
1909
1910 async fn cached_server_binary(
1911 &self,
1912 _: PathBuf,
1913 _: &dyn LspAdapterDelegate,
1914 ) -> Option<LanguageServerBinary> {
1915 unreachable!();
1916 }
1917
1918 async fn installation_test_binary(&self, _: PathBuf) -> Option<LanguageServerBinary> {
1919 unreachable!();
1920 }
1921
1922 fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
1923
1924 async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
1925 self.disk_based_diagnostics_sources.clone()
1926 }
1927
1928 async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
1929 self.disk_based_diagnostics_progress_token.clone()
1930 }
1931
1932 async fn initialization_options(&self) -> Option<Value> {
1933 self.initialization_options.clone()
1934 }
1935
1936 fn prettier_plugins(&self) -> &[&'static str] {
1937 &self.prettier_plugins
1938 }
1939}
1940
1941fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
1942 for (ix, name) in query.capture_names().iter().enumerate() {
1943 for (capture_name, index) in captures.iter_mut() {
1944 if capture_name == name {
1945 **index = Some(ix as u32);
1946 break;
1947 }
1948 }
1949 }
1950}
1951
1952pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
1953 lsp::Position::new(point.row, point.column)
1954}
1955
1956pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
1957 Unclipped(PointUtf16::new(point.line, point.character))
1958}
1959
1960pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
1961 lsp::Range {
1962 start: point_to_lsp(range.start),
1963 end: point_to_lsp(range.end),
1964 }
1965}
1966
1967pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
1968 let mut start = point_from_lsp(range.start);
1969 let mut end = point_from_lsp(range.end);
1970 if start > end {
1971 mem::swap(&mut start, &mut end);
1972 }
1973 start..end
1974}
1975
1976#[cfg(test)]
1977mod tests {
1978 use super::*;
1979 use gpui::TestAppContext;
1980
1981 #[gpui::test(iterations = 10)]
1982 async fn test_first_line_pattern(cx: &mut TestAppContext) {
1983 let mut languages = LanguageRegistry::test();
1984
1985 languages.set_executor(cx.executor());
1986 let languages = Arc::new(languages);
1987 languages.register(
1988 "/javascript",
1989 LanguageConfig {
1990 name: "JavaScript".into(),
1991 path_suffixes: vec!["js".into()],
1992 first_line_pattern: Some(Regex::new(r"\bnode\b").unwrap()),
1993 ..Default::default()
1994 },
1995 tree_sitter_typescript::language_tsx(),
1996 vec![],
1997 |_| Default::default(),
1998 );
1999
2000 languages
2001 .language_for_file("the/script", None)
2002 .await
2003 .unwrap_err();
2004 languages
2005 .language_for_file("the/script", Some(&"nothing".into()))
2006 .await
2007 .unwrap_err();
2008 assert_eq!(
2009 languages
2010 .language_for_file("the/script", Some(&"#!/bin/env node".into()))
2011 .await
2012 .unwrap()
2013 .name()
2014 .as_ref(),
2015 "JavaScript"
2016 );
2017 }
2018
2019 #[gpui::test(iterations = 10)]
2020 async fn test_language_loading(cx: &mut TestAppContext) {
2021 let mut languages = LanguageRegistry::test();
2022 languages.set_executor(cx.executor());
2023 let languages = Arc::new(languages);
2024 languages.register(
2025 "/JSON",
2026 LanguageConfig {
2027 name: "JSON".into(),
2028 path_suffixes: vec!["json".into()],
2029 ..Default::default()
2030 },
2031 tree_sitter_json::language(),
2032 vec![],
2033 |_| Default::default(),
2034 );
2035 languages.register(
2036 "/rust",
2037 LanguageConfig {
2038 name: "Rust".into(),
2039 path_suffixes: vec!["rs".into()],
2040 ..Default::default()
2041 },
2042 tree_sitter_rust::language(),
2043 vec![],
2044 |_| Default::default(),
2045 );
2046 assert_eq!(
2047 languages.language_names(),
2048 &[
2049 "JSON".to_string(),
2050 "Plain Text".to_string(),
2051 "Rust".to_string(),
2052 ]
2053 );
2054
2055 let rust1 = languages.language_for_name("Rust");
2056 let rust2 = languages.language_for_name("Rust");
2057
2058 // Ensure language is still listed even if it's being loaded.
2059 assert_eq!(
2060 languages.language_names(),
2061 &[
2062 "JSON".to_string(),
2063 "Plain Text".to_string(),
2064 "Rust".to_string(),
2065 ]
2066 );
2067
2068 let (rust1, rust2) = futures::join!(rust1, rust2);
2069 assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap()));
2070
2071 // Ensure language is still listed even after loading it.
2072 assert_eq!(
2073 languages.language_names(),
2074 &[
2075 "JSON".to_string(),
2076 "Plain Text".to_string(),
2077 "Rust".to_string(),
2078 ]
2079 );
2080
2081 // Loading an unknown language returns an error.
2082 assert!(languages.language_for_name("Unknown").await.is_err());
2083 }
2084}