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