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