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 manifest;
15mod outline;
16pub mod proto;
17mod syntax_map;
18mod task_context;
19mod text_diff;
20mod toolchain;
21
22#[cfg(test)]
23pub mod buffer_tests;
24
25use crate::language_settings::SoftWrap;
26pub use crate::language_settings::{EditPredictionsMode, IndentGuideSettings};
27use anyhow::{Context as _, Result};
28use async_trait::async_trait;
29use collections::{HashMap, HashSet, IndexSet};
30use futures::Future;
31use futures::future::LocalBoxFuture;
32use futures::lock::OwnedMutexGuard;
33use gpui::{App, AsyncApp, Entity, SharedString};
34pub use highlight_map::HighlightMap;
35use http_client::HttpClient;
36pub use language_registry::{
37 LanguageName, LanguageServerStatusUpdate, LoadedLanguage, ServerHealth,
38};
39use lsp::{
40 CodeActionKind, InitializeParams, LanguageServerBinary, LanguageServerBinaryOptions, Uri,
41};
42pub use manifest::{ManifestDelegate, ManifestName, ManifestProvider, ManifestQuery};
43use parking_lot::Mutex;
44use regex::Regex;
45use schemars::{JsonSchema, SchemaGenerator, json_schema};
46use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
47use serde_json::Value;
48use settings::WorktreeId;
49use smol::future::FutureExt as _;
50use std::num::NonZeroU32;
51use std::{
52 ffi::OsStr,
53 fmt::Debug,
54 hash::Hash,
55 mem,
56 ops::{DerefMut, Range},
57 path::{Path, PathBuf},
58 str,
59 sync::{
60 Arc, LazyLock,
61 atomic::{AtomicUsize, Ordering::SeqCst},
62 },
63};
64use syntax_map::{QueryCursorHandle, SyntaxSnapshot};
65use task::RunnableTag;
66pub use task_context::{ContextLocation, ContextProvider, RunnableRange};
67pub use text_diff::{
68 DiffOptions, apply_diff_patch, line_diff, text_diff, text_diff_with_options, unified_diff,
69};
70use theme::SyntaxTheme;
71pub use toolchain::{
72 LanguageToolchainStore, LocalLanguageToolchainStore, Toolchain, ToolchainList, ToolchainLister,
73 ToolchainMetadata, ToolchainScope,
74};
75use tree_sitter::{self, Query, QueryCursor, WasmStore, wasmtime};
76use util::rel_path::RelPath;
77use util::serde::default_true;
78
79pub use buffer::Operation;
80pub use buffer::*;
81pub use diagnostic_set::{DiagnosticEntry, DiagnosticEntryRef, DiagnosticGroup};
82pub use language_registry::{
83 AvailableLanguage, BinaryStatus, LanguageNotFound, LanguageQueries, LanguageRegistry,
84 QUERY_FILENAME_PREFIXES,
85};
86pub use lsp::{LanguageServerId, LanguageServerName};
87pub use outline::*;
88pub use syntax_map::{
89 OwnedSyntaxLayer, SyntaxLayer, SyntaxMapMatches, ToTreeSitterPoint, TreeSitterOptions,
90};
91pub use text::{AnchorRangeExt, LineEnding};
92pub use tree_sitter::{Node, Parser, Tree, TreeCursor};
93
94static QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Mutex::new(vec![]);
95static PARSERS: Mutex<Vec<Parser>> = Mutex::new(vec![]);
96
97pub fn with_parser<F, R>(func: F) -> R
98where
99 F: FnOnce(&mut Parser) -> R,
100{
101 let mut parser = PARSERS.lock().pop().unwrap_or_else(|| {
102 let mut parser = Parser::new();
103 parser
104 .set_wasm_store(WasmStore::new(&WASM_ENGINE).unwrap())
105 .unwrap();
106 parser
107 });
108 parser.set_included_ranges(&[]).unwrap();
109 let result = func(&mut parser);
110 PARSERS.lock().push(parser);
111 result
112}
113
114pub fn with_query_cursor<F, R>(func: F) -> R
115where
116 F: FnOnce(&mut QueryCursor) -> R,
117{
118 let mut cursor = QueryCursorHandle::new();
119 func(cursor.deref_mut())
120}
121
122static NEXT_LANGUAGE_ID: AtomicUsize = AtomicUsize::new(0);
123static NEXT_GRAMMAR_ID: AtomicUsize = AtomicUsize::new(0);
124static WASM_ENGINE: LazyLock<wasmtime::Engine> = LazyLock::new(|| {
125 wasmtime::Engine::new(&wasmtime::Config::new()).expect("Failed to create Wasmtime engine")
126});
127
128/// A shared grammar for plain text, exposed for reuse by downstream crates.
129pub static PLAIN_TEXT: LazyLock<Arc<Language>> = LazyLock::new(|| {
130 Arc::new(Language::new(
131 LanguageConfig {
132 name: "Plain Text".into(),
133 soft_wrap: Some(SoftWrap::EditorWidth),
134 matcher: LanguageMatcher {
135 path_suffixes: vec!["txt".to_owned()],
136 first_line_pattern: None,
137 },
138 ..Default::default()
139 },
140 None,
141 ))
142});
143
144/// Types that represent a position in a buffer, and can be converted into
145/// an LSP position, to send to a language server.
146pub trait ToLspPosition {
147 /// Converts the value into an LSP position.
148 fn to_lsp_position(self) -> lsp::Position;
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Hash)]
152pub struct Location {
153 pub buffer: Entity<Buffer>,
154 pub range: Range<Anchor>,
155}
156
157type ServerBinaryCache = futures::lock::Mutex<Option<(bool, LanguageServerBinary)>>;
158type DownloadableLanguageServerBinary = LocalBoxFuture<'static, Result<LanguageServerBinary>>;
159pub type LanguageServerBinaryLocations = LocalBoxFuture<
160 'static,
161 (
162 Result<LanguageServerBinary>,
163 Option<DownloadableLanguageServerBinary>,
164 ),
165>;
166/// Represents a Language Server, with certain cached sync properties.
167/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
168/// once at startup, and caches the results.
169pub struct CachedLspAdapter {
170 pub name: LanguageServerName,
171 pub disk_based_diagnostic_sources: Vec<String>,
172 pub disk_based_diagnostics_progress_token: Option<String>,
173 language_ids: HashMap<LanguageName, String>,
174 pub adapter: Arc<dyn LspAdapter>,
175 cached_binary: Arc<ServerBinaryCache>,
176}
177
178impl Debug for CachedLspAdapter {
179 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180 f.debug_struct("CachedLspAdapter")
181 .field("name", &self.name)
182 .field(
183 "disk_based_diagnostic_sources",
184 &self.disk_based_diagnostic_sources,
185 )
186 .field(
187 "disk_based_diagnostics_progress_token",
188 &self.disk_based_diagnostics_progress_token,
189 )
190 .field("language_ids", &self.language_ids)
191 .finish_non_exhaustive()
192 }
193}
194
195impl CachedLspAdapter {
196 pub fn new(adapter: Arc<dyn LspAdapter>) -> Arc<Self> {
197 let name = adapter.name();
198 let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources();
199 let disk_based_diagnostics_progress_token = adapter.disk_based_diagnostics_progress_token();
200 let language_ids = adapter.language_ids();
201
202 Arc::new(CachedLspAdapter {
203 name,
204 disk_based_diagnostic_sources,
205 disk_based_diagnostics_progress_token,
206 language_ids,
207 adapter,
208 cached_binary: Default::default(),
209 })
210 }
211
212 pub fn name(&self) -> LanguageServerName {
213 self.adapter.name()
214 }
215
216 pub async fn get_language_server_command(
217 self: Arc<Self>,
218 delegate: Arc<dyn LspAdapterDelegate>,
219 toolchains: Option<Toolchain>,
220 binary_options: LanguageServerBinaryOptions,
221 cx: &mut AsyncApp,
222 ) -> LanguageServerBinaryLocations {
223 let cached_binary = self.cached_binary.clone().lock_owned().await;
224 self.adapter.clone().get_language_server_command(
225 delegate,
226 toolchains,
227 binary_options,
228 cached_binary,
229 cx.clone(),
230 )
231 }
232
233 pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
234 self.adapter.code_action_kinds()
235 }
236
237 pub fn process_diagnostics(
238 &self,
239 params: &mut lsp::PublishDiagnosticsParams,
240 server_id: LanguageServerId,
241 existing_diagnostics: Option<&'_ Buffer>,
242 ) {
243 self.adapter
244 .process_diagnostics(params, server_id, existing_diagnostics)
245 }
246
247 pub fn retain_old_diagnostic(&self, previous_diagnostic: &Diagnostic, cx: &App) -> bool {
248 self.adapter.retain_old_diagnostic(previous_diagnostic, cx)
249 }
250
251 pub fn underline_diagnostic(&self, diagnostic: &lsp::Diagnostic) -> bool {
252 self.adapter.underline_diagnostic(diagnostic)
253 }
254
255 pub fn diagnostic_message_to_markdown(&self, message: &str) -> Option<String> {
256 self.adapter.diagnostic_message_to_markdown(message)
257 }
258
259 pub async fn process_completions(&self, completion_items: &mut [lsp::CompletionItem]) {
260 self.adapter.process_completions(completion_items).await
261 }
262
263 pub async fn labels_for_completions(
264 &self,
265 completion_items: &[lsp::CompletionItem],
266 language: &Arc<Language>,
267 ) -> Result<Vec<Option<CodeLabel>>> {
268 self.adapter
269 .clone()
270 .labels_for_completions(completion_items, language)
271 .await
272 }
273
274 pub async fn labels_for_symbols(
275 &self,
276 symbols: &[(String, lsp::SymbolKind)],
277 language: &Arc<Language>,
278 ) -> Result<Vec<Option<CodeLabel>>> {
279 self.adapter
280 .clone()
281 .labels_for_symbols(symbols, language)
282 .await
283 }
284
285 pub fn language_id(&self, language_name: &LanguageName) -> String {
286 self.language_ids
287 .get(language_name)
288 .cloned()
289 .unwrap_or_else(|| language_name.lsp_id())
290 }
291}
292
293/// [`LspAdapterDelegate`] allows [`LspAdapter]` implementations to interface with the application
294// e.g. to display a notification or fetch data from the web.
295#[async_trait]
296pub trait LspAdapterDelegate: Send + Sync {
297 fn show_notification(&self, message: &str, cx: &mut App);
298 fn http_client(&self) -> Arc<dyn HttpClient>;
299 fn worktree_id(&self) -> WorktreeId;
300 fn worktree_root_path(&self) -> &Path;
301 fn resolve_executable_path(&self, path: PathBuf) -> PathBuf;
302 fn update_status(&self, language: LanguageServerName, status: BinaryStatus);
303 fn registered_lsp_adapters(&self) -> Vec<Arc<dyn LspAdapter>>;
304 async fn language_server_download_dir(&self, name: &LanguageServerName) -> Option<Arc<Path>>;
305
306 async fn npm_package_installed_version(
307 &self,
308 package_name: &str,
309 ) -> Result<Option<(PathBuf, String)>>;
310 async fn which(&self, command: &OsStr) -> Option<PathBuf>;
311 async fn shell_env(&self) -> HashMap<String, String>;
312 async fn read_text_file(&self, path: &RelPath) -> Result<String>;
313 async fn try_exec(&self, binary: LanguageServerBinary) -> Result<()>;
314}
315
316#[async_trait(?Send)]
317pub trait LspAdapter: 'static + Send + Sync + DynLspInstaller {
318 fn name(&self) -> LanguageServerName;
319
320 fn process_diagnostics(
321 &self,
322 _: &mut lsp::PublishDiagnosticsParams,
323 _: LanguageServerId,
324 _: Option<&'_ Buffer>,
325 ) {
326 }
327
328 /// When processing new `lsp::PublishDiagnosticsParams` diagnostics, whether to retain previous one(s) or not.
329 fn retain_old_diagnostic(&self, _previous_diagnostic: &Diagnostic, _cx: &App) -> bool {
330 false
331 }
332
333 /// Whether to underline a given diagnostic or not, when rendering in the editor.
334 ///
335 /// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnosticTag
336 /// states that
337 /// > Clients are allowed to render diagnostics with this tag faded out instead of having an error squiggle.
338 /// for the unnecessary diagnostics, so do not underline them.
339 fn underline_diagnostic(&self, _diagnostic: &lsp::Diagnostic) -> bool {
340 true
341 }
342
343 /// Post-processes completions provided by the language server.
344 async fn process_completions(&self, _: &mut [lsp::CompletionItem]) {}
345
346 fn diagnostic_message_to_markdown(&self, _message: &str) -> Option<String> {
347 None
348 }
349
350 async fn labels_for_completions(
351 self: Arc<Self>,
352 completions: &[lsp::CompletionItem],
353 language: &Arc<Language>,
354 ) -> Result<Vec<Option<CodeLabel>>> {
355 let mut labels = Vec::new();
356 for (ix, completion) in completions.iter().enumerate() {
357 let label = self.label_for_completion(completion, language).await;
358 if let Some(label) = label {
359 labels.resize(ix + 1, None);
360 *labels.last_mut().unwrap() = Some(label);
361 }
362 }
363 Ok(labels)
364 }
365
366 async fn label_for_completion(
367 &self,
368 _: &lsp::CompletionItem,
369 _: &Arc<Language>,
370 ) -> Option<CodeLabel> {
371 None
372 }
373
374 async fn labels_for_symbols(
375 self: Arc<Self>,
376 symbols: &[(String, lsp::SymbolKind)],
377 language: &Arc<Language>,
378 ) -> Result<Vec<Option<CodeLabel>>> {
379 let mut labels = Vec::new();
380 for (ix, (name, kind)) in symbols.iter().enumerate() {
381 let label = self.label_for_symbol(name, *kind, language).await;
382 if let Some(label) = label {
383 labels.resize(ix + 1, None);
384 *labels.last_mut().unwrap() = Some(label);
385 }
386 }
387 Ok(labels)
388 }
389
390 async fn label_for_symbol(
391 &self,
392 _: &str,
393 _: lsp::SymbolKind,
394 _: &Arc<Language>,
395 ) -> Option<CodeLabel> {
396 None
397 }
398
399 /// Returns initialization options that are going to be sent to a LSP server as a part of [`lsp::InitializeParams`]
400 async fn initialization_options(
401 self: Arc<Self>,
402 _: &Arc<dyn LspAdapterDelegate>,
403 ) -> Result<Option<Value>> {
404 Ok(None)
405 }
406
407 async fn workspace_configuration(
408 self: Arc<Self>,
409 _: &Arc<dyn LspAdapterDelegate>,
410 _: Option<Toolchain>,
411 _: Option<Uri>,
412 _cx: &mut AsyncApp,
413 ) -> Result<Value> {
414 Ok(serde_json::json!({}))
415 }
416
417 async fn additional_initialization_options(
418 self: Arc<Self>,
419 _target_language_server_id: LanguageServerName,
420 _: &Arc<dyn LspAdapterDelegate>,
421 ) -> Result<Option<Value>> {
422 Ok(None)
423 }
424
425 async fn additional_workspace_configuration(
426 self: Arc<Self>,
427 _target_language_server_id: LanguageServerName,
428 _: &Arc<dyn LspAdapterDelegate>,
429 _cx: &mut AsyncApp,
430 ) -> Result<Option<Value>> {
431 Ok(None)
432 }
433
434 /// Returns a list of code actions supported by a given LspAdapter
435 fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
436 None
437 }
438
439 fn disk_based_diagnostic_sources(&self) -> Vec<String> {
440 Default::default()
441 }
442
443 fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
444 None
445 }
446
447 fn language_ids(&self) -> HashMap<LanguageName, String> {
448 HashMap::default()
449 }
450
451 /// Support custom initialize params.
452 fn prepare_initialize_params(
453 &self,
454 original: InitializeParams,
455 _: &App,
456 ) -> Result<InitializeParams> {
457 Ok(original)
458 }
459
460 /// Method only implemented by the default JSON language server adapter.
461 /// Used to provide dynamic reloading of the JSON schemas used to
462 /// provide autocompletion and diagnostics in Zed setting and keybind
463 /// files
464 fn is_primary_zed_json_schema_adapter(&self) -> bool {
465 false
466 }
467
468 /// True for the extension adapter and false otherwise.
469 fn is_extension(&self) -> bool {
470 false
471 }
472}
473
474pub trait LspInstaller {
475 type BinaryVersion;
476 fn check_if_user_installed(
477 &self,
478 _: &dyn LspAdapterDelegate,
479 _: Option<Toolchain>,
480 _: &AsyncApp,
481 ) -> impl Future<Output = Option<LanguageServerBinary>> {
482 async { None }
483 }
484
485 fn fetch_latest_server_version(
486 &self,
487 delegate: &dyn LspAdapterDelegate,
488 pre_release: bool,
489 cx: &mut AsyncApp,
490 ) -> impl Future<Output = Result<Self::BinaryVersion>>;
491
492 fn check_if_version_installed(
493 &self,
494 _version: &Self::BinaryVersion,
495 _container_dir: &PathBuf,
496 _delegate: &dyn LspAdapterDelegate,
497 ) -> impl Future<Output = Option<LanguageServerBinary>> {
498 async { None }
499 }
500
501 fn fetch_server_binary(
502 &self,
503 latest_version: Self::BinaryVersion,
504 container_dir: PathBuf,
505 delegate: &dyn LspAdapterDelegate,
506 ) -> impl Future<Output = Result<LanguageServerBinary>>;
507
508 fn cached_server_binary(
509 &self,
510 container_dir: PathBuf,
511 delegate: &dyn LspAdapterDelegate,
512 ) -> impl Future<Output = Option<LanguageServerBinary>>;
513}
514
515#[async_trait(?Send)]
516pub trait DynLspInstaller {
517 async fn try_fetch_server_binary(
518 &self,
519 delegate: &Arc<dyn LspAdapterDelegate>,
520 container_dir: PathBuf,
521 pre_release: bool,
522 cx: &mut AsyncApp,
523 ) -> Result<LanguageServerBinary>;
524 fn get_language_server_command(
525 self: Arc<Self>,
526 delegate: Arc<dyn LspAdapterDelegate>,
527 toolchains: Option<Toolchain>,
528 binary_options: LanguageServerBinaryOptions,
529 cached_binary: OwnedMutexGuard<Option<(bool, LanguageServerBinary)>>,
530 cx: AsyncApp,
531 ) -> LanguageServerBinaryLocations;
532}
533
534#[async_trait(?Send)]
535impl<LI, BinaryVersion> DynLspInstaller for LI
536where
537 LI: LspInstaller<BinaryVersion = BinaryVersion> + LspAdapter,
538{
539 async fn try_fetch_server_binary(
540 &self,
541 delegate: &Arc<dyn LspAdapterDelegate>,
542 container_dir: PathBuf,
543 pre_release: bool,
544 cx: &mut AsyncApp,
545 ) -> Result<LanguageServerBinary> {
546 let name = self.name();
547
548 log::debug!("fetching latest version of language server {:?}", name.0);
549 delegate.update_status(name.clone(), BinaryStatus::CheckingForUpdate);
550
551 let latest_version = self
552 .fetch_latest_server_version(delegate.as_ref(), pre_release, cx)
553 .await?;
554
555 if let Some(binary) = self
556 .check_if_version_installed(&latest_version, &container_dir, delegate.as_ref())
557 .await
558 {
559 log::debug!("language server {:?} is already installed", name.0);
560 delegate.update_status(name.clone(), BinaryStatus::None);
561 Ok(binary)
562 } else {
563 log::debug!("downloading language server {:?}", name.0);
564 delegate.update_status(name.clone(), BinaryStatus::Downloading);
565 let binary = self
566 .fetch_server_binary(latest_version, container_dir, delegate.as_ref())
567 .await;
568
569 delegate.update_status(name.clone(), BinaryStatus::None);
570 binary
571 }
572 }
573 fn get_language_server_command(
574 self: Arc<Self>,
575 delegate: Arc<dyn LspAdapterDelegate>,
576 toolchain: Option<Toolchain>,
577 binary_options: LanguageServerBinaryOptions,
578 mut cached_binary: OwnedMutexGuard<Option<(bool, LanguageServerBinary)>>,
579 mut cx: AsyncApp,
580 ) -> LanguageServerBinaryLocations {
581 async move {
582 let cached_binary_deref = cached_binary.deref_mut();
583 // First we check whether the adapter can give us a user-installed binary.
584 // If so, we do *not* want to cache that, because each worktree might give us a different
585 // binary:
586 //
587 // worktree 1: user-installed at `.bin/gopls`
588 // worktree 2: user-installed at `~/bin/gopls`
589 // worktree 3: no gopls found in PATH -> fallback to Zed installation
590 //
591 // We only want to cache when we fall back to the global one,
592 // because we don't want to download and overwrite our global one
593 // for each worktree we might have open.
594 if binary_options.allow_path_lookup
595 && let Some(binary) = self
596 .check_if_user_installed(delegate.as_ref(), toolchain, &mut cx)
597 .await
598 {
599 log::info!(
600 "found user-installed language server for {}. path: {:?}, arguments: {:?}",
601 self.name().0,
602 binary.path,
603 binary.arguments
604 );
605 return (Ok(binary), None);
606 }
607
608 if !binary_options.allow_binary_download {
609 return (
610 Err(anyhow::anyhow!("downloading language servers disabled")),
611 None,
612 );
613 }
614
615 if let Some((pre_release, cached_binary)) = cached_binary_deref
616 && *pre_release == binary_options.pre_release
617 {
618 return (Ok(cached_binary.clone()), None);
619 }
620
621 let Some(container_dir) = delegate.language_server_download_dir(&self.name()).await
622 else {
623 return (
624 Err(anyhow::anyhow!("no language server download dir defined")),
625 None,
626 );
627 };
628
629 let last_downloaded_binary = self
630 .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref())
631 .await
632 .context(
633 "did not find existing language server binary, falling back to downloading",
634 );
635 let download_binary = async move {
636 let mut binary = self
637 .try_fetch_server_binary(
638 &delegate,
639 container_dir.to_path_buf(),
640 binary_options.pre_release,
641 &mut cx,
642 )
643 .await;
644
645 if let Err(error) = binary.as_ref() {
646 if let Some(prev_downloaded_binary) = self
647 .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref())
648 .await
649 {
650 log::info!(
651 "failed to fetch newest version of language server {:?}. \
652 error: {:?}, falling back to using {:?}",
653 self.name(),
654 error,
655 prev_downloaded_binary.path
656 );
657 binary = Ok(prev_downloaded_binary);
658 } else {
659 delegate.update_status(
660 self.name(),
661 BinaryStatus::Failed {
662 error: format!("{error:?}"),
663 },
664 );
665 }
666 }
667
668 if let Ok(binary) = &binary {
669 *cached_binary = Some((binary_options.pre_release, binary.clone()));
670 }
671
672 binary
673 }
674 .boxed_local();
675 (last_downloaded_binary, Some(download_binary))
676 }
677 .boxed_local()
678 }
679}
680
681#[derive(Clone, Debug, Default, PartialEq, Eq)]
682pub struct CodeLabel {
683 /// The text to display.
684 pub text: String,
685 /// Syntax highlighting runs.
686 pub runs: Vec<(Range<usize>, HighlightId)>,
687 /// The portion of the text that should be used in fuzzy filtering.
688 pub filter_range: Range<usize>,
689}
690
691#[derive(Clone, Debug, Default, PartialEq, Eq)]
692pub struct CodeLabelBuilder {
693 /// The text to display.
694 text: String,
695 /// Syntax highlighting runs.
696 runs: Vec<(Range<usize>, HighlightId)>,
697 /// The portion of the text that should be used in fuzzy filtering.
698 filter_range: Range<usize>,
699}
700
701#[derive(Clone, Deserialize, JsonSchema, Debug)]
702pub struct LanguageConfig {
703 /// Human-readable name of the language.
704 pub name: LanguageName,
705 /// The name of this language for a Markdown code fence block
706 pub code_fence_block_name: Option<Arc<str>>,
707 // The name of the grammar in a WASM bundle (experimental).
708 pub grammar: Option<Arc<str>>,
709 /// The criteria for matching this language to a given file.
710 #[serde(flatten)]
711 pub matcher: LanguageMatcher,
712 /// List of bracket types in a language.
713 #[serde(default)]
714 pub brackets: BracketPairConfig,
715 /// If set to true, auto indentation uses last non empty line to determine
716 /// the indentation level for a new line.
717 #[serde(default = "auto_indent_using_last_non_empty_line_default")]
718 pub auto_indent_using_last_non_empty_line: bool,
719 // Whether indentation of pasted content should be adjusted based on the context.
720 #[serde(default)]
721 pub auto_indent_on_paste: Option<bool>,
722 /// A regex that is used to determine whether the indentation level should be
723 /// increased in the following line.
724 #[serde(default, deserialize_with = "deserialize_regex")]
725 #[schemars(schema_with = "regex_json_schema")]
726 pub increase_indent_pattern: Option<Regex>,
727 /// A regex that is used to determine whether the indentation level should be
728 /// decreased in the following line.
729 #[serde(default, deserialize_with = "deserialize_regex")]
730 #[schemars(schema_with = "regex_json_schema")]
731 pub decrease_indent_pattern: Option<Regex>,
732 /// A list of rules for decreasing indentation. Each rule pairs a regex with a set of valid
733 /// "block-starting" tokens. When a line matches a pattern, its indentation is aligned with
734 /// the most recent line that began with a corresponding token. This enables context-aware
735 /// outdenting, like aligning an `else` with its `if`.
736 #[serde(default)]
737 pub decrease_indent_patterns: Vec<DecreaseIndentConfig>,
738 /// A list of characters that trigger the automatic insertion of a closing
739 /// bracket when they immediately precede the point where an opening
740 /// bracket is inserted.
741 #[serde(default)]
742 pub autoclose_before: String,
743 /// A placeholder used internally by Semantic Index.
744 #[serde(default)]
745 pub collapsed_placeholder: String,
746 /// A line comment string that is inserted in e.g. `toggle comments` action.
747 /// A language can have multiple flavours of line comments. All of the provided line comments are
748 /// used for comment continuations on the next line, but only the first one is used for Editor::ToggleComments.
749 #[serde(default)]
750 pub line_comments: Vec<Arc<str>>,
751 /// Delimiters and configuration for recognizing and formatting block comments.
752 #[serde(default)]
753 pub block_comment: Option<BlockCommentConfig>,
754 /// Delimiters and configuration for recognizing and formatting documentation comments.
755 #[serde(default, alias = "documentation")]
756 pub documentation_comment: Option<BlockCommentConfig>,
757 /// A list of additional regex patterns that should be treated as prefixes
758 /// for creating boundaries during rewrapping, ensuring content from one
759 /// prefixed section doesn't merge with another (e.g., markdown list items).
760 /// By default, Zed treats as paragraph and comment prefixes as boundaries.
761 #[serde(default, deserialize_with = "deserialize_regex_vec")]
762 #[schemars(schema_with = "regex_vec_json_schema")]
763 pub rewrap_prefixes: Vec<Regex>,
764 /// A list of language servers that are allowed to run on subranges of a given language.
765 #[serde(default)]
766 pub scope_opt_in_language_servers: Vec<LanguageServerName>,
767 #[serde(default)]
768 pub overrides: HashMap<String, LanguageConfigOverride>,
769 /// A list of characters that Zed should treat as word characters for the
770 /// purpose of features that operate on word boundaries, like 'move to next word end'
771 /// or a whole-word search in buffer search.
772 #[serde(default)]
773 pub word_characters: HashSet<char>,
774 /// Whether to indent lines using tab characters, as opposed to multiple
775 /// spaces.
776 #[serde(default)]
777 pub hard_tabs: Option<bool>,
778 /// How many columns a tab should occupy.
779 #[serde(default)]
780 #[schemars(range(min = 1, max = 128))]
781 pub tab_size: Option<NonZeroU32>,
782 /// How to soft-wrap long lines of text.
783 #[serde(default)]
784 pub soft_wrap: Option<SoftWrap>,
785 /// When set, selections can be wrapped using prefix/suffix pairs on both sides.
786 #[serde(default)]
787 pub wrap_characters: Option<WrapCharactersConfig>,
788 /// The name of a Prettier parser that will be used for this language when no file path is available.
789 /// If there's a parser name in the language settings, that will be used instead.
790 #[serde(default)]
791 pub prettier_parser_name: Option<String>,
792 /// If true, this language is only for syntax highlighting via an injection into other
793 /// languages, but should not appear to the user as a distinct language.
794 #[serde(default)]
795 pub hidden: bool,
796 /// If configured, this language contains JSX style tags, and should support auto-closing of those tags.
797 #[serde(default)]
798 pub jsx_tag_auto_close: Option<JsxTagAutoCloseConfig>,
799 /// A list of characters that Zed should treat as word characters for completion queries.
800 #[serde(default)]
801 pub completion_query_characters: HashSet<char>,
802 /// A list of characters that Zed should treat as word characters for linked edit operations.
803 #[serde(default)]
804 pub linked_edit_characters: HashSet<char>,
805 /// A list of preferred debuggers for this language.
806 #[serde(default)]
807 pub debuggers: IndexSet<SharedString>,
808 /// A list of import namespace segments that aren't expected to appear in file paths. For
809 /// example, "super" and "crate" in Rust.
810 #[serde(default)]
811 pub ignored_import_segments: HashSet<Arc<str>>,
812 /// Regular expression that matches substrings to omit from import paths, to make the paths more
813 /// similar to how they are specified when imported. For example, "/mod\.rs$" or "/__init__\.py$".
814 #[serde(default, deserialize_with = "deserialize_regex")]
815 #[schemars(schema_with = "regex_json_schema")]
816 pub import_path_strip_regex: Option<Regex>,
817}
818
819#[derive(Clone, Debug, Deserialize, Default, JsonSchema)]
820pub struct DecreaseIndentConfig {
821 #[serde(default, deserialize_with = "deserialize_regex")]
822 #[schemars(schema_with = "regex_json_schema")]
823 pub pattern: Option<Regex>,
824 #[serde(default)]
825 pub valid_after: Vec<String>,
826}
827
828#[derive(Clone, Debug, Serialize, Deserialize, Default, JsonSchema)]
829pub struct LanguageMatcher {
830 /// 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`.
831 #[serde(default)]
832 pub path_suffixes: Vec<String>,
833 /// A regex pattern that determines whether the language should be assigned to a file or not.
834 #[serde(
835 default,
836 serialize_with = "serialize_regex",
837 deserialize_with = "deserialize_regex"
838 )]
839 #[schemars(schema_with = "regex_json_schema")]
840 pub first_line_pattern: Option<Regex>,
841}
842
843/// The configuration for JSX tag auto-closing.
844#[derive(Clone, Deserialize, JsonSchema, Debug)]
845pub struct JsxTagAutoCloseConfig {
846 /// The name of the node for a opening tag
847 pub open_tag_node_name: String,
848 /// The name of the node for an closing tag
849 pub close_tag_node_name: String,
850 /// The name of the node for a complete element with children for open and close tags
851 pub jsx_element_node_name: String,
852 /// The name of the node found within both opening and closing
853 /// tags that describes the tag name
854 pub tag_name_node_name: String,
855 /// Alternate Node names for tag names.
856 /// Specifically needed as TSX represents the name in `<Foo.Bar>`
857 /// as `member_expression` rather than `identifier` as usual
858 #[serde(default)]
859 pub tag_name_node_name_alternates: Vec<String>,
860 /// Some grammars are smart enough to detect a closing tag
861 /// that is not valid i.e. doesn't match it's corresponding
862 /// opening tag or does not have a corresponding opening tag
863 /// This should be set to the name of the node for invalid
864 /// closing tags if the grammar contains such a node, otherwise
865 /// detecting already closed tags will not work properly
866 #[serde(default)]
867 pub erroneous_close_tag_node_name: Option<String>,
868 /// See above for erroneous_close_tag_node_name for details
869 /// This should be set if the node used for the tag name
870 /// within erroneous closing tags is different from the
871 /// normal tag name node name
872 #[serde(default)]
873 pub erroneous_close_tag_name_node_name: Option<String>,
874}
875
876/// The configuration for block comments for this language.
877#[derive(Clone, Debug, JsonSchema, PartialEq)]
878pub struct BlockCommentConfig {
879 /// A start tag of block comment.
880 pub start: Arc<str>,
881 /// A end tag of block comment.
882 pub end: Arc<str>,
883 /// A character to add as a prefix when a new line is added to a block comment.
884 pub prefix: Arc<str>,
885 /// A indent to add for prefix and end line upon new line.
886 #[schemars(range(min = 1, max = 128))]
887 pub tab_size: u32,
888}
889
890impl<'de> Deserialize<'de> for BlockCommentConfig {
891 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
892 where
893 D: Deserializer<'de>,
894 {
895 #[derive(Deserialize)]
896 #[serde(untagged)]
897 enum BlockCommentConfigHelper {
898 New {
899 start: Arc<str>,
900 end: Arc<str>,
901 prefix: Arc<str>,
902 tab_size: u32,
903 },
904 Old([Arc<str>; 2]),
905 }
906
907 match BlockCommentConfigHelper::deserialize(deserializer)? {
908 BlockCommentConfigHelper::New {
909 start,
910 end,
911 prefix,
912 tab_size,
913 } => Ok(BlockCommentConfig {
914 start,
915 end,
916 prefix,
917 tab_size,
918 }),
919 BlockCommentConfigHelper::Old([start, end]) => Ok(BlockCommentConfig {
920 start,
921 end,
922 prefix: "".into(),
923 tab_size: 0,
924 }),
925 }
926 }
927}
928
929/// Represents a language for the given range. Some languages (e.g. HTML)
930/// interleave several languages together, thus a single buffer might actually contain
931/// several nested scopes.
932#[derive(Clone, Debug)]
933pub struct LanguageScope {
934 language: Arc<Language>,
935 override_id: Option<u32>,
936}
937
938#[derive(Clone, Deserialize, Default, Debug, JsonSchema)]
939pub struct LanguageConfigOverride {
940 #[serde(default)]
941 pub line_comments: Override<Vec<Arc<str>>>,
942 #[serde(default)]
943 pub block_comment: Override<BlockCommentConfig>,
944 #[serde(skip)]
945 pub disabled_bracket_ixs: Vec<u16>,
946 #[serde(default)]
947 pub word_characters: Override<HashSet<char>>,
948 #[serde(default)]
949 pub completion_query_characters: Override<HashSet<char>>,
950 #[serde(default)]
951 pub linked_edit_characters: Override<HashSet<char>>,
952 #[serde(default)]
953 pub opt_into_language_servers: Vec<LanguageServerName>,
954 #[serde(default)]
955 pub prefer_label_for_snippet: Option<bool>,
956}
957
958#[derive(Clone, Deserialize, Debug, Serialize, JsonSchema)]
959#[serde(untagged)]
960pub enum Override<T> {
961 Remove { remove: bool },
962 Set(T),
963}
964
965impl<T> Default for Override<T> {
966 fn default() -> Self {
967 Override::Remove { remove: false }
968 }
969}
970
971impl<T> Override<T> {
972 fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> {
973 match this {
974 Some(Self::Set(value)) => Some(value),
975 Some(Self::Remove { remove: true }) => None,
976 Some(Self::Remove { remove: false }) | None => original,
977 }
978 }
979}
980
981impl Default for LanguageConfig {
982 fn default() -> Self {
983 Self {
984 name: LanguageName::new(""),
985 code_fence_block_name: None,
986 grammar: None,
987 matcher: LanguageMatcher::default(),
988 brackets: Default::default(),
989 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
990 auto_indent_on_paste: None,
991 increase_indent_pattern: Default::default(),
992 decrease_indent_pattern: Default::default(),
993 decrease_indent_patterns: Default::default(),
994 autoclose_before: Default::default(),
995 line_comments: Default::default(),
996 block_comment: Default::default(),
997 documentation_comment: Default::default(),
998 rewrap_prefixes: Default::default(),
999 scope_opt_in_language_servers: Default::default(),
1000 overrides: Default::default(),
1001 word_characters: Default::default(),
1002 collapsed_placeholder: Default::default(),
1003 hard_tabs: None,
1004 tab_size: None,
1005 soft_wrap: None,
1006 wrap_characters: None,
1007 prettier_parser_name: None,
1008 hidden: false,
1009 jsx_tag_auto_close: None,
1010 completion_query_characters: Default::default(),
1011 linked_edit_characters: Default::default(),
1012 debuggers: Default::default(),
1013 ignored_import_segments: Default::default(),
1014 import_path_strip_regex: None,
1015 }
1016 }
1017}
1018
1019#[derive(Clone, Debug, Deserialize, JsonSchema)]
1020pub struct WrapCharactersConfig {
1021 /// Opening token split into a prefix and suffix. The first caret goes
1022 /// after the prefix (i.e., between prefix and suffix).
1023 pub start_prefix: String,
1024 pub start_suffix: String,
1025 /// Closing token split into a prefix and suffix. The second caret goes
1026 /// after the prefix (i.e., between prefix and suffix).
1027 pub end_prefix: String,
1028 pub end_suffix: String,
1029}
1030
1031fn auto_indent_using_last_non_empty_line_default() -> bool {
1032 true
1033}
1034
1035fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
1036 let source = Option::<String>::deserialize(d)?;
1037 if let Some(source) = source {
1038 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
1039 } else {
1040 Ok(None)
1041 }
1042}
1043
1044fn regex_json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
1045 json_schema!({
1046 "type": "string"
1047 })
1048}
1049
1050fn serialize_regex<S>(regex: &Option<Regex>, serializer: S) -> Result<S::Ok, S::Error>
1051where
1052 S: Serializer,
1053{
1054 match regex {
1055 Some(regex) => serializer.serialize_str(regex.as_str()),
1056 None => serializer.serialize_none(),
1057 }
1058}
1059
1060fn deserialize_regex_vec<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<Regex>, D::Error> {
1061 let sources = Vec::<String>::deserialize(d)?;
1062 sources
1063 .into_iter()
1064 .map(|source| regex::Regex::new(&source))
1065 .collect::<Result<_, _>>()
1066 .map_err(de::Error::custom)
1067}
1068
1069fn regex_vec_json_schema(_: &mut SchemaGenerator) -> schemars::Schema {
1070 json_schema!({
1071 "type": "array",
1072 "items": { "type": "string" }
1073 })
1074}
1075
1076#[doc(hidden)]
1077#[cfg(any(test, feature = "test-support"))]
1078pub struct FakeLspAdapter {
1079 pub name: &'static str,
1080 pub initialization_options: Option<Value>,
1081 pub prettier_plugins: Vec<&'static str>,
1082 pub disk_based_diagnostics_progress_token: Option<String>,
1083 pub disk_based_diagnostics_sources: Vec<String>,
1084 pub language_server_binary: LanguageServerBinary,
1085
1086 pub capabilities: lsp::ServerCapabilities,
1087 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
1088 pub label_for_completion: Option<
1089 Box<
1090 dyn 'static
1091 + Send
1092 + Sync
1093 + Fn(&lsp::CompletionItem, &Arc<Language>) -> Option<CodeLabel>,
1094 >,
1095 >,
1096}
1097
1098/// Configuration of handling bracket pairs for a given language.
1099///
1100/// This struct includes settings for defining which pairs of characters are considered brackets and
1101/// also specifies any language-specific scopes where these pairs should be ignored for bracket matching purposes.
1102#[derive(Clone, Debug, Default, JsonSchema)]
1103#[schemars(with = "Vec::<BracketPairContent>")]
1104pub struct BracketPairConfig {
1105 /// A list of character pairs that should be treated as brackets in the context of a given language.
1106 pub pairs: Vec<BracketPair>,
1107 /// A list of tree-sitter scopes for which a given bracket should not be active.
1108 /// N-th entry in `[Self::disabled_scopes_by_bracket_ix]` contains a list of disabled scopes for an n-th entry in `[Self::pairs]`
1109 pub disabled_scopes_by_bracket_ix: Vec<Vec<String>>,
1110}
1111
1112impl BracketPairConfig {
1113 pub fn is_closing_brace(&self, c: char) -> bool {
1114 self.pairs.iter().any(|pair| pair.end.starts_with(c))
1115 }
1116}
1117
1118#[derive(Deserialize, JsonSchema)]
1119pub struct BracketPairContent {
1120 #[serde(flatten)]
1121 pub bracket_pair: BracketPair,
1122 #[serde(default)]
1123 pub not_in: Vec<String>,
1124}
1125
1126impl<'de> Deserialize<'de> for BracketPairConfig {
1127 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1128 where
1129 D: Deserializer<'de>,
1130 {
1131 let result = Vec::<BracketPairContent>::deserialize(deserializer)?;
1132 let (brackets, disabled_scopes_by_bracket_ix) = result
1133 .into_iter()
1134 .map(|entry| (entry.bracket_pair, entry.not_in))
1135 .unzip();
1136
1137 Ok(BracketPairConfig {
1138 pairs: brackets,
1139 disabled_scopes_by_bracket_ix,
1140 })
1141 }
1142}
1143
1144/// Describes a single bracket pair and how an editor should react to e.g. inserting
1145/// an opening bracket or to a newline character insertion in between `start` and `end` characters.
1146#[derive(Clone, Debug, Default, Deserialize, PartialEq, JsonSchema)]
1147pub struct BracketPair {
1148 /// Starting substring for a bracket.
1149 pub start: String,
1150 /// Ending substring for a bracket.
1151 pub end: String,
1152 /// True if `end` should be automatically inserted right after `start` characters.
1153 pub close: bool,
1154 /// True if selected text should be surrounded by `start` and `end` characters.
1155 #[serde(default = "default_true")]
1156 pub surround: bool,
1157 /// True if an extra newline should be inserted while the cursor is in the middle
1158 /// of that bracket pair.
1159 pub newline: bool,
1160}
1161
1162#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1163pub struct LanguageId(usize);
1164
1165impl LanguageId {
1166 pub(crate) fn new() -> Self {
1167 Self(NEXT_LANGUAGE_ID.fetch_add(1, SeqCst))
1168 }
1169}
1170
1171pub struct Language {
1172 pub(crate) id: LanguageId,
1173 pub(crate) config: LanguageConfig,
1174 pub(crate) grammar: Option<Arc<Grammar>>,
1175 pub(crate) context_provider: Option<Arc<dyn ContextProvider>>,
1176 pub(crate) toolchain: Option<Arc<dyn ToolchainLister>>,
1177 pub(crate) manifest_name: Option<ManifestName>,
1178}
1179
1180#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1181pub struct GrammarId(pub usize);
1182
1183impl GrammarId {
1184 pub(crate) fn new() -> Self {
1185 Self(NEXT_GRAMMAR_ID.fetch_add(1, SeqCst))
1186 }
1187}
1188
1189pub struct Grammar {
1190 id: GrammarId,
1191 pub ts_language: tree_sitter::Language,
1192 pub(crate) error_query: Option<Query>,
1193 pub highlights_config: Option<HighlightsConfig>,
1194 pub(crate) brackets_config: Option<BracketsConfig>,
1195 pub(crate) redactions_config: Option<RedactionConfig>,
1196 pub(crate) runnable_config: Option<RunnableConfig>,
1197 pub(crate) indents_config: Option<IndentConfig>,
1198 pub outline_config: Option<OutlineConfig>,
1199 pub text_object_config: Option<TextObjectConfig>,
1200 pub embedding_config: Option<EmbeddingConfig>,
1201 pub(crate) injection_config: Option<InjectionConfig>,
1202 pub(crate) override_config: Option<OverrideConfig>,
1203 pub(crate) debug_variables_config: Option<DebugVariablesConfig>,
1204 pub(crate) imports_config: Option<ImportsConfig>,
1205 pub(crate) highlight_map: Mutex<HighlightMap>,
1206}
1207
1208pub struct HighlightsConfig {
1209 pub query: Query,
1210 pub identifier_capture_indices: Vec<u32>,
1211}
1212
1213struct IndentConfig {
1214 query: Query,
1215 indent_capture_ix: u32,
1216 start_capture_ix: Option<u32>,
1217 end_capture_ix: Option<u32>,
1218 outdent_capture_ix: Option<u32>,
1219 suffixed_start_captures: HashMap<u32, SharedString>,
1220}
1221
1222pub struct OutlineConfig {
1223 pub query: Query,
1224 pub item_capture_ix: u32,
1225 pub name_capture_ix: u32,
1226 pub context_capture_ix: Option<u32>,
1227 pub extra_context_capture_ix: Option<u32>,
1228 pub open_capture_ix: Option<u32>,
1229 pub close_capture_ix: Option<u32>,
1230 pub annotation_capture_ix: Option<u32>,
1231}
1232
1233#[derive(Debug, Clone, Copy, PartialEq)]
1234pub enum DebuggerTextObject {
1235 Variable,
1236 Scope,
1237}
1238
1239impl DebuggerTextObject {
1240 pub fn from_capture_name(name: &str) -> Option<DebuggerTextObject> {
1241 match name {
1242 "debug-variable" => Some(DebuggerTextObject::Variable),
1243 "debug-scope" => Some(DebuggerTextObject::Scope),
1244 _ => None,
1245 }
1246 }
1247}
1248
1249#[derive(Debug, Clone, Copy, PartialEq)]
1250pub enum TextObject {
1251 InsideFunction,
1252 AroundFunction,
1253 InsideClass,
1254 AroundClass,
1255 InsideComment,
1256 AroundComment,
1257}
1258
1259impl TextObject {
1260 pub fn from_capture_name(name: &str) -> Option<TextObject> {
1261 match name {
1262 "function.inside" => Some(TextObject::InsideFunction),
1263 "function.around" => Some(TextObject::AroundFunction),
1264 "class.inside" => Some(TextObject::InsideClass),
1265 "class.around" => Some(TextObject::AroundClass),
1266 "comment.inside" => Some(TextObject::InsideComment),
1267 "comment.around" => Some(TextObject::AroundComment),
1268 _ => None,
1269 }
1270 }
1271
1272 pub fn around(&self) -> Option<Self> {
1273 match self {
1274 TextObject::InsideFunction => Some(TextObject::AroundFunction),
1275 TextObject::InsideClass => Some(TextObject::AroundClass),
1276 TextObject::InsideComment => Some(TextObject::AroundComment),
1277 _ => None,
1278 }
1279 }
1280}
1281
1282pub struct TextObjectConfig {
1283 pub query: Query,
1284 pub text_objects_by_capture_ix: Vec<(u32, TextObject)>,
1285}
1286
1287#[derive(Debug)]
1288pub struct EmbeddingConfig {
1289 pub query: Query,
1290 pub item_capture_ix: u32,
1291 pub name_capture_ix: Option<u32>,
1292 pub context_capture_ix: Option<u32>,
1293 pub collapse_capture_ix: Option<u32>,
1294 pub keep_capture_ix: Option<u32>,
1295}
1296
1297struct InjectionConfig {
1298 query: Query,
1299 content_capture_ix: u32,
1300 language_capture_ix: Option<u32>,
1301 patterns: Vec<InjectionPatternConfig>,
1302}
1303
1304struct RedactionConfig {
1305 pub query: Query,
1306 pub redaction_capture_ix: u32,
1307}
1308
1309#[derive(Clone, Debug, PartialEq)]
1310enum RunnableCapture {
1311 Named(SharedString),
1312 Run,
1313}
1314
1315struct RunnableConfig {
1316 pub query: Query,
1317 /// A mapping from capture indice to capture kind
1318 pub extra_captures: Vec<RunnableCapture>,
1319}
1320
1321struct OverrideConfig {
1322 query: Query,
1323 values: HashMap<u32, OverrideEntry>,
1324}
1325
1326#[derive(Debug)]
1327struct OverrideEntry {
1328 name: String,
1329 range_is_inclusive: bool,
1330 value: LanguageConfigOverride,
1331}
1332
1333#[derive(Default, Clone)]
1334struct InjectionPatternConfig {
1335 language: Option<Box<str>>,
1336 combined: bool,
1337}
1338
1339#[derive(Debug)]
1340struct BracketsConfig {
1341 query: Query,
1342 open_capture_ix: u32,
1343 close_capture_ix: u32,
1344 patterns: Vec<BracketsPatternConfig>,
1345}
1346
1347#[derive(Clone, Debug, Default)]
1348struct BracketsPatternConfig {
1349 newline_only: bool,
1350 rainbow_exclude: bool,
1351}
1352
1353pub struct DebugVariablesConfig {
1354 pub query: Query,
1355 pub objects_by_capture_ix: Vec<(u32, DebuggerTextObject)>,
1356}
1357
1358pub struct ImportsConfig {
1359 pub query: Query,
1360 pub import_ix: u32,
1361 pub name_ix: Option<u32>,
1362 pub namespace_ix: Option<u32>,
1363 pub source_ix: Option<u32>,
1364 pub list_ix: Option<u32>,
1365 pub wildcard_ix: Option<u32>,
1366 pub alias_ix: Option<u32>,
1367}
1368
1369impl Language {
1370 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
1371 Self::new_with_id(LanguageId::new(), config, ts_language)
1372 }
1373
1374 pub fn id(&self) -> LanguageId {
1375 self.id
1376 }
1377
1378 fn new_with_id(
1379 id: LanguageId,
1380 config: LanguageConfig,
1381 ts_language: Option<tree_sitter::Language>,
1382 ) -> Self {
1383 Self {
1384 id,
1385 config,
1386 grammar: ts_language.map(|ts_language| {
1387 Arc::new(Grammar {
1388 id: GrammarId::new(),
1389 highlights_config: None,
1390 brackets_config: None,
1391 outline_config: None,
1392 text_object_config: None,
1393 embedding_config: None,
1394 indents_config: None,
1395 injection_config: None,
1396 override_config: None,
1397 redactions_config: None,
1398 runnable_config: None,
1399 error_query: Query::new(&ts_language, "(ERROR) @error").ok(),
1400 debug_variables_config: None,
1401 imports_config: None,
1402 ts_language,
1403 highlight_map: Default::default(),
1404 })
1405 }),
1406 context_provider: None,
1407 toolchain: None,
1408 manifest_name: None,
1409 }
1410 }
1411
1412 pub fn with_context_provider(mut self, provider: Option<Arc<dyn ContextProvider>>) -> Self {
1413 self.context_provider = provider;
1414 self
1415 }
1416
1417 pub fn with_toolchain_lister(mut self, provider: Option<Arc<dyn ToolchainLister>>) -> Self {
1418 self.toolchain = provider;
1419 self
1420 }
1421
1422 pub fn with_manifest(mut self, name: Option<ManifestName>) -> Self {
1423 self.manifest_name = name;
1424 self
1425 }
1426
1427 pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
1428 if let Some(query) = queries.highlights {
1429 self = self
1430 .with_highlights_query(query.as_ref())
1431 .context("Error loading highlights query")?;
1432 }
1433 if let Some(query) = queries.brackets {
1434 self = self
1435 .with_brackets_query(query.as_ref())
1436 .context("Error loading brackets query")?;
1437 }
1438 if let Some(query) = queries.indents {
1439 self = self
1440 .with_indents_query(query.as_ref())
1441 .context("Error loading indents query")?;
1442 }
1443 if let Some(query) = queries.outline {
1444 self = self
1445 .with_outline_query(query.as_ref())
1446 .context("Error loading outline query")?;
1447 }
1448 if let Some(query) = queries.embedding {
1449 self = self
1450 .with_embedding_query(query.as_ref())
1451 .context("Error loading embedding query")?;
1452 }
1453 if let Some(query) = queries.injections {
1454 self = self
1455 .with_injection_query(query.as_ref())
1456 .context("Error loading injection query")?;
1457 }
1458 if let Some(query) = queries.overrides {
1459 self = self
1460 .with_override_query(query.as_ref())
1461 .context("Error loading override query")?;
1462 }
1463 if let Some(query) = queries.redactions {
1464 self = self
1465 .with_redaction_query(query.as_ref())
1466 .context("Error loading redaction query")?;
1467 }
1468 if let Some(query) = queries.runnables {
1469 self = self
1470 .with_runnable_query(query.as_ref())
1471 .context("Error loading runnables query")?;
1472 }
1473 if let Some(query) = queries.text_objects {
1474 self = self
1475 .with_text_object_query(query.as_ref())
1476 .context("Error loading textobject query")?;
1477 }
1478 if let Some(query) = queries.debugger {
1479 self = self
1480 .with_debug_variables_query(query.as_ref())
1481 .context("Error loading debug variables query")?;
1482 }
1483 if let Some(query) = queries.imports {
1484 self = self
1485 .with_imports_query(query.as_ref())
1486 .context("Error loading imports query")?;
1487 }
1488 Ok(self)
1489 }
1490
1491 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
1492 let grammar = self.grammar_mut()?;
1493 let query = Query::new(&grammar.ts_language, source)?;
1494
1495 let mut identifier_capture_indices = Vec::new();
1496 for name in [
1497 "variable",
1498 "constant",
1499 "constructor",
1500 "function",
1501 "function.method",
1502 "function.method.call",
1503 "function.special",
1504 "property",
1505 "type",
1506 "type.interface",
1507 ] {
1508 identifier_capture_indices.extend(query.capture_index_for_name(name));
1509 }
1510
1511 grammar.highlights_config = Some(HighlightsConfig {
1512 query,
1513 identifier_capture_indices,
1514 });
1515
1516 Ok(self)
1517 }
1518
1519 pub fn with_runnable_query(mut self, source: &str) -> Result<Self> {
1520 let grammar = self.grammar_mut()?;
1521
1522 let query = Query::new(&grammar.ts_language, source)?;
1523 let extra_captures: Vec<_> = query
1524 .capture_names()
1525 .iter()
1526 .map(|&name| match name {
1527 "run" => RunnableCapture::Run,
1528 name => RunnableCapture::Named(name.to_string().into()),
1529 })
1530 .collect();
1531
1532 grammar.runnable_config = Some(RunnableConfig {
1533 extra_captures,
1534 query,
1535 });
1536
1537 Ok(self)
1538 }
1539
1540 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
1541 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1542 let mut item_capture_ix = 0;
1543 let mut name_capture_ix = 0;
1544 let mut context_capture_ix = None;
1545 let mut extra_context_capture_ix = None;
1546 let mut open_capture_ix = None;
1547 let mut close_capture_ix = None;
1548 let mut annotation_capture_ix = None;
1549 if populate_capture_indices(
1550 &query,
1551 &self.config.name,
1552 "outline",
1553 &[],
1554 &mut [
1555 Capture::Required("item", &mut item_capture_ix),
1556 Capture::Required("name", &mut name_capture_ix),
1557 Capture::Optional("context", &mut context_capture_ix),
1558 Capture::Optional("context.extra", &mut extra_context_capture_ix),
1559 Capture::Optional("open", &mut open_capture_ix),
1560 Capture::Optional("close", &mut close_capture_ix),
1561 Capture::Optional("annotation", &mut annotation_capture_ix),
1562 ],
1563 ) {
1564 self.grammar_mut()?.outline_config = Some(OutlineConfig {
1565 query,
1566 item_capture_ix,
1567 name_capture_ix,
1568 context_capture_ix,
1569 extra_context_capture_ix,
1570 open_capture_ix,
1571 close_capture_ix,
1572 annotation_capture_ix,
1573 });
1574 }
1575 Ok(self)
1576 }
1577
1578 pub fn with_text_object_query(mut self, source: &str) -> Result<Self> {
1579 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1580
1581 let mut text_objects_by_capture_ix = Vec::new();
1582 for (ix, name) in query.capture_names().iter().enumerate() {
1583 if let Some(text_object) = TextObject::from_capture_name(name) {
1584 text_objects_by_capture_ix.push((ix as u32, text_object));
1585 } else {
1586 log::warn!(
1587 "unrecognized capture name '{}' in {} textobjects TreeSitter query",
1588 name,
1589 self.config.name,
1590 );
1591 }
1592 }
1593
1594 self.grammar_mut()?.text_object_config = Some(TextObjectConfig {
1595 query,
1596 text_objects_by_capture_ix,
1597 });
1598 Ok(self)
1599 }
1600
1601 pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
1602 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1603 let mut item_capture_ix = 0;
1604 let mut name_capture_ix = None;
1605 let mut context_capture_ix = None;
1606 let mut collapse_capture_ix = None;
1607 let mut keep_capture_ix = None;
1608 if populate_capture_indices(
1609 &query,
1610 &self.config.name,
1611 "embedding",
1612 &[],
1613 &mut [
1614 Capture::Required("item", &mut item_capture_ix),
1615 Capture::Optional("name", &mut name_capture_ix),
1616 Capture::Optional("context", &mut context_capture_ix),
1617 Capture::Optional("keep", &mut keep_capture_ix),
1618 Capture::Optional("collapse", &mut collapse_capture_ix),
1619 ],
1620 ) {
1621 self.grammar_mut()?.embedding_config = Some(EmbeddingConfig {
1622 query,
1623 item_capture_ix,
1624 name_capture_ix,
1625 context_capture_ix,
1626 collapse_capture_ix,
1627 keep_capture_ix,
1628 });
1629 }
1630 Ok(self)
1631 }
1632
1633 pub fn with_debug_variables_query(mut self, source: &str) -> Result<Self> {
1634 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1635
1636 let mut objects_by_capture_ix = Vec::new();
1637 for (ix, name) in query.capture_names().iter().enumerate() {
1638 if let Some(text_object) = DebuggerTextObject::from_capture_name(name) {
1639 objects_by_capture_ix.push((ix as u32, text_object));
1640 } else {
1641 log::warn!(
1642 "unrecognized capture name '{}' in {} debugger TreeSitter query",
1643 name,
1644 self.config.name,
1645 );
1646 }
1647 }
1648
1649 self.grammar_mut()?.debug_variables_config = Some(DebugVariablesConfig {
1650 query,
1651 objects_by_capture_ix,
1652 });
1653 Ok(self)
1654 }
1655
1656 pub fn with_imports_query(mut self, source: &str) -> Result<Self> {
1657 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1658
1659 let mut import_ix = 0;
1660 let mut name_ix = None;
1661 let mut namespace_ix = None;
1662 let mut source_ix = None;
1663 let mut list_ix = None;
1664 let mut wildcard_ix = None;
1665 let mut alias_ix = None;
1666 if populate_capture_indices(
1667 &query,
1668 &self.config.name,
1669 "imports",
1670 &[],
1671 &mut [
1672 Capture::Required("import", &mut import_ix),
1673 Capture::Optional("name", &mut name_ix),
1674 Capture::Optional("namespace", &mut namespace_ix),
1675 Capture::Optional("source", &mut source_ix),
1676 Capture::Optional("list", &mut list_ix),
1677 Capture::Optional("wildcard", &mut wildcard_ix),
1678 Capture::Optional("alias", &mut alias_ix),
1679 ],
1680 ) {
1681 self.grammar_mut()?.imports_config = Some(ImportsConfig {
1682 query,
1683 import_ix,
1684 name_ix,
1685 namespace_ix,
1686 source_ix,
1687 list_ix,
1688 wildcard_ix,
1689 alias_ix,
1690 });
1691 }
1692 return Ok(self);
1693 }
1694
1695 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
1696 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1697 let mut open_capture_ix = 0;
1698 let mut close_capture_ix = 0;
1699 if populate_capture_indices(
1700 &query,
1701 &self.config.name,
1702 "brackets",
1703 &[],
1704 &mut [
1705 Capture::Required("open", &mut open_capture_ix),
1706 Capture::Required("close", &mut close_capture_ix),
1707 ],
1708 ) {
1709 let patterns = (0..query.pattern_count())
1710 .map(|ix| {
1711 let mut config = BracketsPatternConfig::default();
1712 for setting in query.property_settings(ix) {
1713 let setting_key = setting.key.as_ref();
1714 if setting_key == "newline.only" {
1715 config.newline_only = true
1716 }
1717 if setting_key == "rainbow.exclude" {
1718 config.rainbow_exclude = true
1719 }
1720 }
1721 config
1722 })
1723 .collect();
1724 self.grammar_mut()?.brackets_config = Some(BracketsConfig {
1725 query,
1726 open_capture_ix,
1727 close_capture_ix,
1728 patterns,
1729 });
1730 }
1731 Ok(self)
1732 }
1733
1734 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
1735 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1736 let mut indent_capture_ix = 0;
1737 let mut start_capture_ix = None;
1738 let mut end_capture_ix = None;
1739 let mut outdent_capture_ix = None;
1740 if populate_capture_indices(
1741 &query,
1742 &self.config.name,
1743 "indents",
1744 &["start."],
1745 &mut [
1746 Capture::Required("indent", &mut indent_capture_ix),
1747 Capture::Optional("start", &mut start_capture_ix),
1748 Capture::Optional("end", &mut end_capture_ix),
1749 Capture::Optional("outdent", &mut outdent_capture_ix),
1750 ],
1751 ) {
1752 let mut suffixed_start_captures = HashMap::default();
1753 for (ix, name) in query.capture_names().iter().enumerate() {
1754 if let Some(suffix) = name.strip_prefix("start.") {
1755 suffixed_start_captures.insert(ix as u32, suffix.to_owned().into());
1756 }
1757 }
1758
1759 self.grammar_mut()?.indents_config = Some(IndentConfig {
1760 query,
1761 indent_capture_ix,
1762 start_capture_ix,
1763 end_capture_ix,
1764 outdent_capture_ix,
1765 suffixed_start_captures,
1766 });
1767 }
1768 Ok(self)
1769 }
1770
1771 pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
1772 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1773 let mut language_capture_ix = None;
1774 let mut injection_language_capture_ix = None;
1775 let mut content_capture_ix = None;
1776 let mut injection_content_capture_ix = None;
1777 if populate_capture_indices(
1778 &query,
1779 &self.config.name,
1780 "injections",
1781 &[],
1782 &mut [
1783 Capture::Optional("language", &mut language_capture_ix),
1784 Capture::Optional("injection.language", &mut injection_language_capture_ix),
1785 Capture::Optional("content", &mut content_capture_ix),
1786 Capture::Optional("injection.content", &mut injection_content_capture_ix),
1787 ],
1788 ) {
1789 language_capture_ix = match (language_capture_ix, injection_language_capture_ix) {
1790 (None, Some(ix)) => Some(ix),
1791 (Some(_), Some(_)) => {
1792 anyhow::bail!("both language and injection.language captures are present");
1793 }
1794 _ => language_capture_ix,
1795 };
1796 content_capture_ix = match (content_capture_ix, injection_content_capture_ix) {
1797 (None, Some(ix)) => Some(ix),
1798 (Some(_), Some(_)) => {
1799 anyhow::bail!("both content and injection.content captures are present")
1800 }
1801 _ => content_capture_ix,
1802 };
1803 let patterns = (0..query.pattern_count())
1804 .map(|ix| {
1805 let mut config = InjectionPatternConfig::default();
1806 for setting in query.property_settings(ix) {
1807 match setting.key.as_ref() {
1808 "language" | "injection.language" => {
1809 config.language.clone_from(&setting.value);
1810 }
1811 "combined" | "injection.combined" => {
1812 config.combined = true;
1813 }
1814 _ => {}
1815 }
1816 }
1817 config
1818 })
1819 .collect();
1820 if let Some(content_capture_ix) = content_capture_ix {
1821 self.grammar_mut()?.injection_config = Some(InjectionConfig {
1822 query,
1823 language_capture_ix,
1824 content_capture_ix,
1825 patterns,
1826 });
1827 } else {
1828 log::error!(
1829 "missing required capture in injections {} TreeSitter query: \
1830 content or injection.content",
1831 &self.config.name,
1832 );
1833 }
1834 }
1835 Ok(self)
1836 }
1837
1838 pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
1839 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1840
1841 let mut override_configs_by_id = HashMap::default();
1842 for (ix, mut name) in query.capture_names().iter().copied().enumerate() {
1843 let mut range_is_inclusive = false;
1844 if name.starts_with('_') {
1845 continue;
1846 }
1847 if let Some(prefix) = name.strip_suffix(".inclusive") {
1848 name = prefix;
1849 range_is_inclusive = true;
1850 }
1851
1852 let value = self.config.overrides.get(name).cloned().unwrap_or_default();
1853 for server_name in &value.opt_into_language_servers {
1854 if !self
1855 .config
1856 .scope_opt_in_language_servers
1857 .contains(server_name)
1858 {
1859 util::debug_panic!(
1860 "Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server"
1861 );
1862 }
1863 }
1864
1865 override_configs_by_id.insert(
1866 ix as u32,
1867 OverrideEntry {
1868 name: name.to_string(),
1869 range_is_inclusive,
1870 value,
1871 },
1872 );
1873 }
1874
1875 let referenced_override_names = self.config.overrides.keys().chain(
1876 self.config
1877 .brackets
1878 .disabled_scopes_by_bracket_ix
1879 .iter()
1880 .flatten(),
1881 );
1882
1883 for referenced_name in referenced_override_names {
1884 if !override_configs_by_id
1885 .values()
1886 .any(|entry| entry.name == *referenced_name)
1887 {
1888 anyhow::bail!(
1889 "language {:?} has overrides in config not in query: {referenced_name:?}",
1890 self.config.name
1891 );
1892 }
1893 }
1894
1895 for entry in override_configs_by_id.values_mut() {
1896 entry.value.disabled_bracket_ixs = self
1897 .config
1898 .brackets
1899 .disabled_scopes_by_bracket_ix
1900 .iter()
1901 .enumerate()
1902 .filter_map(|(ix, disabled_scope_names)| {
1903 if disabled_scope_names.contains(&entry.name) {
1904 Some(ix as u16)
1905 } else {
1906 None
1907 }
1908 })
1909 .collect();
1910 }
1911
1912 self.config.brackets.disabled_scopes_by_bracket_ix.clear();
1913
1914 let grammar = self.grammar_mut()?;
1915 grammar.override_config = Some(OverrideConfig {
1916 query,
1917 values: override_configs_by_id,
1918 });
1919 Ok(self)
1920 }
1921
1922 pub fn with_redaction_query(mut self, source: &str) -> anyhow::Result<Self> {
1923 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1924 let mut redaction_capture_ix = 0;
1925 if populate_capture_indices(
1926 &query,
1927 &self.config.name,
1928 "redactions",
1929 &[],
1930 &mut [Capture::Required("redact", &mut redaction_capture_ix)],
1931 ) {
1932 self.grammar_mut()?.redactions_config = Some(RedactionConfig {
1933 query,
1934 redaction_capture_ix,
1935 });
1936 }
1937 Ok(self)
1938 }
1939
1940 fn expect_grammar(&self) -> Result<&Grammar> {
1941 self.grammar
1942 .as_ref()
1943 .map(|grammar| grammar.as_ref())
1944 .context("no grammar for language")
1945 }
1946
1947 fn grammar_mut(&mut self) -> Result<&mut Grammar> {
1948 Arc::get_mut(self.grammar.as_mut().context("no grammar for language")?)
1949 .context("cannot mutate grammar")
1950 }
1951
1952 pub fn name(&self) -> LanguageName {
1953 self.config.name.clone()
1954 }
1955 pub fn manifest(&self) -> Option<&ManifestName> {
1956 self.manifest_name.as_ref()
1957 }
1958
1959 pub fn code_fence_block_name(&self) -> Arc<str> {
1960 self.config
1961 .code_fence_block_name
1962 .clone()
1963 .unwrap_or_else(|| self.config.name.as_ref().to_lowercase().into())
1964 }
1965
1966 pub fn context_provider(&self) -> Option<Arc<dyn ContextProvider>> {
1967 self.context_provider.clone()
1968 }
1969
1970 pub fn toolchain_lister(&self) -> Option<Arc<dyn ToolchainLister>> {
1971 self.toolchain.clone()
1972 }
1973
1974 pub fn highlight_text<'a>(
1975 self: &'a Arc<Self>,
1976 text: &'a Rope,
1977 range: Range<usize>,
1978 ) -> Vec<(Range<usize>, HighlightId)> {
1979 let mut result = Vec::new();
1980 if let Some(grammar) = &self.grammar {
1981 let tree = grammar.parse_text(text, None);
1982 let captures =
1983 SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
1984 grammar
1985 .highlights_config
1986 .as_ref()
1987 .map(|config| &config.query)
1988 });
1989 let highlight_maps = vec![grammar.highlight_map()];
1990 let mut offset = 0;
1991 for chunk in
1992 BufferChunks::new(text, range, Some((captures, highlight_maps)), false, None)
1993 {
1994 let end_offset = offset + chunk.text.len();
1995 if let Some(highlight_id) = chunk.syntax_highlight_id
1996 && !highlight_id.is_default()
1997 {
1998 result.push((offset..end_offset, highlight_id));
1999 }
2000 offset = end_offset;
2001 }
2002 }
2003 result
2004 }
2005
2006 pub fn path_suffixes(&self) -> &[String] {
2007 &self.config.matcher.path_suffixes
2008 }
2009
2010 pub fn should_autoclose_before(&self, c: char) -> bool {
2011 c.is_whitespace() || self.config.autoclose_before.contains(c)
2012 }
2013
2014 pub fn set_theme(&self, theme: &SyntaxTheme) {
2015 if let Some(grammar) = self.grammar.as_ref()
2016 && let Some(highlights_config) = &grammar.highlights_config
2017 {
2018 *grammar.highlight_map.lock() =
2019 HighlightMap::new(highlights_config.query.capture_names(), theme);
2020 }
2021 }
2022
2023 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
2024 self.grammar.as_ref()
2025 }
2026
2027 pub fn default_scope(self: &Arc<Self>) -> LanguageScope {
2028 LanguageScope {
2029 language: self.clone(),
2030 override_id: None,
2031 }
2032 }
2033
2034 pub fn lsp_id(&self) -> String {
2035 self.config.name.lsp_id()
2036 }
2037
2038 pub fn prettier_parser_name(&self) -> Option<&str> {
2039 self.config.prettier_parser_name.as_deref()
2040 }
2041
2042 pub fn config(&self) -> &LanguageConfig {
2043 &self.config
2044 }
2045}
2046
2047impl LanguageScope {
2048 pub fn path_suffixes(&self) -> &[String] {
2049 self.language.path_suffixes()
2050 }
2051
2052 pub fn language_name(&self) -> LanguageName {
2053 self.language.config.name.clone()
2054 }
2055
2056 pub fn collapsed_placeholder(&self) -> &str {
2057 self.language.config.collapsed_placeholder.as_ref()
2058 }
2059
2060 /// Returns line prefix that is inserted in e.g. line continuations or
2061 /// in `toggle comments` action.
2062 pub fn line_comment_prefixes(&self) -> &[Arc<str>] {
2063 Override::as_option(
2064 self.config_override().map(|o| &o.line_comments),
2065 Some(&self.language.config.line_comments),
2066 )
2067 .map_or([].as_slice(), |e| e.as_slice())
2068 }
2069
2070 /// Config for block comments for this language.
2071 pub fn block_comment(&self) -> Option<&BlockCommentConfig> {
2072 Override::as_option(
2073 self.config_override().map(|o| &o.block_comment),
2074 self.language.config.block_comment.as_ref(),
2075 )
2076 }
2077
2078 /// Config for documentation-style block comments for this language.
2079 pub fn documentation_comment(&self) -> Option<&BlockCommentConfig> {
2080 self.language.config.documentation_comment.as_ref()
2081 }
2082
2083 /// Returns additional regex patterns that act as prefix markers for creating
2084 /// boundaries during rewrapping.
2085 ///
2086 /// By default, Zed treats as paragraph and comment prefixes as boundaries.
2087 pub fn rewrap_prefixes(&self) -> &[Regex] {
2088 &self.language.config.rewrap_prefixes
2089 }
2090
2091 /// Returns a list of language-specific word characters.
2092 ///
2093 /// By default, Zed treats alphanumeric characters (and '_') as word characters for
2094 /// the purpose of actions like 'move to next word end` or whole-word search.
2095 /// It additionally accounts for language's additional word characters.
2096 pub fn word_characters(&self) -> Option<&HashSet<char>> {
2097 Override::as_option(
2098 self.config_override().map(|o| &o.word_characters),
2099 Some(&self.language.config.word_characters),
2100 )
2101 }
2102
2103 /// Returns a list of language-specific characters that are considered part of
2104 /// a completion query.
2105 pub fn completion_query_characters(&self) -> Option<&HashSet<char>> {
2106 Override::as_option(
2107 self.config_override()
2108 .map(|o| &o.completion_query_characters),
2109 Some(&self.language.config.completion_query_characters),
2110 )
2111 }
2112
2113 /// Returns a list of language-specific characters that are considered part of
2114 /// identifiers during linked editing operations.
2115 pub fn linked_edit_characters(&self) -> Option<&HashSet<char>> {
2116 Override::as_option(
2117 self.config_override().map(|o| &o.linked_edit_characters),
2118 Some(&self.language.config.linked_edit_characters),
2119 )
2120 }
2121
2122 /// Returns whether to prefer snippet `label` over `new_text` to replace text when
2123 /// completion is accepted.
2124 ///
2125 /// In cases like when cursor is in string or renaming existing function,
2126 /// you don't want to expand function signature instead just want function name
2127 /// to replace existing one.
2128 pub fn prefers_label_for_snippet_in_completion(&self) -> bool {
2129 self.config_override()
2130 .and_then(|o| o.prefer_label_for_snippet)
2131 .unwrap_or(false)
2132 }
2133
2134 /// Returns a list of bracket pairs for a given language with an additional
2135 /// piece of information about whether the particular bracket pair is currently active for a given language.
2136 pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {
2137 let mut disabled_ids = self
2138 .config_override()
2139 .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice());
2140 self.language
2141 .config
2142 .brackets
2143 .pairs
2144 .iter()
2145 .enumerate()
2146 .map(move |(ix, bracket)| {
2147 let mut is_enabled = true;
2148 if let Some(next_disabled_ix) = disabled_ids.first()
2149 && ix == *next_disabled_ix as usize
2150 {
2151 disabled_ids = &disabled_ids[1..];
2152 is_enabled = false;
2153 }
2154 (bracket, is_enabled)
2155 })
2156 }
2157
2158 pub fn should_autoclose_before(&self, c: char) -> bool {
2159 c.is_whitespace() || self.language.config.autoclose_before.contains(c)
2160 }
2161
2162 pub fn language_allowed(&self, name: &LanguageServerName) -> bool {
2163 let config = &self.language.config;
2164 let opt_in_servers = &config.scope_opt_in_language_servers;
2165 if opt_in_servers.contains(name) {
2166 if let Some(over) = self.config_override() {
2167 over.opt_into_language_servers.contains(name)
2168 } else {
2169 false
2170 }
2171 } else {
2172 true
2173 }
2174 }
2175
2176 pub fn override_name(&self) -> Option<&str> {
2177 let id = self.override_id?;
2178 let grammar = self.language.grammar.as_ref()?;
2179 let override_config = grammar.override_config.as_ref()?;
2180 override_config.values.get(&id).map(|e| e.name.as_str())
2181 }
2182
2183 fn config_override(&self) -> Option<&LanguageConfigOverride> {
2184 let id = self.override_id?;
2185 let grammar = self.language.grammar.as_ref()?;
2186 let override_config = grammar.override_config.as_ref()?;
2187 override_config.values.get(&id).map(|e| &e.value)
2188 }
2189}
2190
2191impl Hash for Language {
2192 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
2193 self.id.hash(state)
2194 }
2195}
2196
2197impl PartialEq for Language {
2198 fn eq(&self, other: &Self) -> bool {
2199 self.id.eq(&other.id)
2200 }
2201}
2202
2203impl Eq for Language {}
2204
2205impl Debug for Language {
2206 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2207 f.debug_struct("Language")
2208 .field("name", &self.config.name)
2209 .finish()
2210 }
2211}
2212
2213impl Grammar {
2214 pub fn id(&self) -> GrammarId {
2215 self.id
2216 }
2217
2218 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
2219 with_parser(|parser| {
2220 parser
2221 .set_language(&self.ts_language)
2222 .expect("incompatible grammar");
2223 let mut chunks = text.chunks_in_range(0..text.len());
2224 parser
2225 .parse_with_options(
2226 &mut move |offset, _| {
2227 chunks.seek(offset);
2228 chunks.next().unwrap_or("").as_bytes()
2229 },
2230 old_tree.as_ref(),
2231 None,
2232 )
2233 .unwrap()
2234 })
2235 }
2236
2237 pub fn highlight_map(&self) -> HighlightMap {
2238 self.highlight_map.lock().clone()
2239 }
2240
2241 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
2242 let capture_id = self
2243 .highlights_config
2244 .as_ref()?
2245 .query
2246 .capture_index_for_name(name)?;
2247 Some(self.highlight_map.lock().get(capture_id))
2248 }
2249
2250 pub fn debug_variables_config(&self) -> Option<&DebugVariablesConfig> {
2251 self.debug_variables_config.as_ref()
2252 }
2253
2254 pub fn imports_config(&self) -> Option<&ImportsConfig> {
2255 self.imports_config.as_ref()
2256 }
2257}
2258
2259impl CodeLabelBuilder {
2260 pub fn respan_filter_range(&mut self, filter_text: Option<&str>) {
2261 self.filter_range = filter_text
2262 .and_then(|filter| self.text.find(filter).map(|ix| ix..ix + filter.len()))
2263 .unwrap_or(0..self.text.len());
2264 }
2265
2266 pub fn push_str(&mut self, text: &str, highlight: Option<HighlightId>) {
2267 let start_ix = self.text.len();
2268 self.text.push_str(text);
2269 if let Some(highlight) = highlight {
2270 let end_ix = self.text.len();
2271 self.runs.push((start_ix..end_ix, highlight));
2272 }
2273 }
2274
2275 pub fn build(mut self) -> CodeLabel {
2276 if self.filter_range.end == 0 {
2277 self.respan_filter_range(None);
2278 }
2279 CodeLabel {
2280 text: self.text,
2281 runs: self.runs,
2282 filter_range: self.filter_range,
2283 }
2284 }
2285}
2286
2287impl CodeLabel {
2288 pub fn fallback_for_completion(
2289 item: &lsp::CompletionItem,
2290 language: Option<&Language>,
2291 ) -> Self {
2292 let highlight_id = item.kind.and_then(|kind| {
2293 let grammar = language?.grammar()?;
2294 use lsp::CompletionItemKind as Kind;
2295 match kind {
2296 Kind::CLASS => grammar.highlight_id_for_name("type"),
2297 Kind::CONSTANT => grammar.highlight_id_for_name("constant"),
2298 Kind::CONSTRUCTOR => grammar.highlight_id_for_name("constructor"),
2299 Kind::ENUM => grammar
2300 .highlight_id_for_name("enum")
2301 .or_else(|| grammar.highlight_id_for_name("type")),
2302 Kind::ENUM_MEMBER => grammar
2303 .highlight_id_for_name("variant")
2304 .or_else(|| grammar.highlight_id_for_name("property")),
2305 Kind::FIELD => grammar.highlight_id_for_name("property"),
2306 Kind::FUNCTION => grammar.highlight_id_for_name("function"),
2307 Kind::INTERFACE => grammar.highlight_id_for_name("type"),
2308 Kind::METHOD => grammar
2309 .highlight_id_for_name("function.method")
2310 .or_else(|| grammar.highlight_id_for_name("function")),
2311 Kind::OPERATOR => grammar.highlight_id_for_name("operator"),
2312 Kind::PROPERTY => grammar.highlight_id_for_name("property"),
2313 Kind::STRUCT => grammar.highlight_id_for_name("type"),
2314 Kind::VARIABLE => grammar.highlight_id_for_name("variable"),
2315 Kind::KEYWORD => grammar.highlight_id_for_name("keyword"),
2316 _ => None,
2317 }
2318 });
2319
2320 let label = &item.label;
2321 let label_length = label.len();
2322 let runs = highlight_id
2323 .map(|highlight_id| vec![(0..label_length, highlight_id)])
2324 .unwrap_or_default();
2325 let text = if let Some(detail) = item.detail.as_deref().filter(|detail| detail != label) {
2326 format!("{label} {detail}")
2327 } else if let Some(description) = item
2328 .label_details
2329 .as_ref()
2330 .and_then(|label_details| label_details.description.as_deref())
2331 .filter(|description| description != label)
2332 {
2333 format!("{label} {description}")
2334 } else {
2335 label.clone()
2336 };
2337 let filter_range = item
2338 .filter_text
2339 .as_deref()
2340 .and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
2341 .unwrap_or(0..label_length);
2342 Self {
2343 text,
2344 runs,
2345 filter_range,
2346 }
2347 }
2348
2349 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
2350 Self::filtered(text.clone(), text.len(), filter_text, Vec::new())
2351 }
2352
2353 pub fn filtered(
2354 text: String,
2355 label_len: usize,
2356 filter_text: Option<&str>,
2357 runs: Vec<(Range<usize>, HighlightId)>,
2358 ) -> Self {
2359 assert!(label_len <= text.len());
2360 let filter_range = filter_text
2361 .and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
2362 .unwrap_or(0..label_len);
2363 Self::new(text, filter_range, runs)
2364 }
2365
2366 pub fn new(
2367 text: String,
2368 filter_range: Range<usize>,
2369 runs: Vec<(Range<usize>, HighlightId)>,
2370 ) -> Self {
2371 assert!(
2372 text.get(filter_range.clone()).is_some(),
2373 "invalid filter range"
2374 );
2375 runs.iter().for_each(|(range, _)| {
2376 assert!(text.get(range.clone()).is_some(), "invalid run range");
2377 });
2378 Self {
2379 runs,
2380 filter_range,
2381 text,
2382 }
2383 }
2384
2385 pub fn text(&self) -> &str {
2386 self.text.as_str()
2387 }
2388
2389 pub fn filter_text(&self) -> &str {
2390 &self.text[self.filter_range.clone()]
2391 }
2392}
2393
2394impl From<String> for CodeLabel {
2395 fn from(value: String) -> Self {
2396 Self::plain(value, None)
2397 }
2398}
2399
2400impl From<&str> for CodeLabel {
2401 fn from(value: &str) -> Self {
2402 Self::plain(value.to_string(), None)
2403 }
2404}
2405
2406impl Ord for LanguageMatcher {
2407 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2408 self.path_suffixes.cmp(&other.path_suffixes).then_with(|| {
2409 self.first_line_pattern
2410 .as_ref()
2411 .map(Regex::as_str)
2412 .cmp(&other.first_line_pattern.as_ref().map(Regex::as_str))
2413 })
2414 }
2415}
2416
2417impl PartialOrd for LanguageMatcher {
2418 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2419 Some(self.cmp(other))
2420 }
2421}
2422
2423impl Eq for LanguageMatcher {}
2424
2425impl PartialEq for LanguageMatcher {
2426 fn eq(&self, other: &Self) -> bool {
2427 self.path_suffixes == other.path_suffixes
2428 && self.first_line_pattern.as_ref().map(Regex::as_str)
2429 == other.first_line_pattern.as_ref().map(Regex::as_str)
2430 }
2431}
2432
2433#[cfg(any(test, feature = "test-support"))]
2434impl Default for FakeLspAdapter {
2435 fn default() -> Self {
2436 Self {
2437 name: "the-fake-language-server",
2438 capabilities: lsp::LanguageServer::full_capabilities(),
2439 initializer: None,
2440 disk_based_diagnostics_progress_token: None,
2441 initialization_options: None,
2442 disk_based_diagnostics_sources: Vec::new(),
2443 prettier_plugins: Vec::new(),
2444 language_server_binary: LanguageServerBinary {
2445 path: "/the/fake/lsp/path".into(),
2446 arguments: vec![],
2447 env: Default::default(),
2448 },
2449 label_for_completion: None,
2450 }
2451 }
2452}
2453
2454#[cfg(any(test, feature = "test-support"))]
2455impl LspInstaller for FakeLspAdapter {
2456 type BinaryVersion = ();
2457
2458 async fn fetch_latest_server_version(
2459 &self,
2460 _: &dyn LspAdapterDelegate,
2461 _: bool,
2462 _: &mut AsyncApp,
2463 ) -> Result<Self::BinaryVersion> {
2464 unreachable!()
2465 }
2466
2467 async fn check_if_user_installed(
2468 &self,
2469 _: &dyn LspAdapterDelegate,
2470 _: Option<Toolchain>,
2471 _: &AsyncApp,
2472 ) -> Option<LanguageServerBinary> {
2473 Some(self.language_server_binary.clone())
2474 }
2475
2476 async fn fetch_server_binary(
2477 &self,
2478 _: (),
2479 _: PathBuf,
2480 _: &dyn LspAdapterDelegate,
2481 ) -> Result<LanguageServerBinary> {
2482 unreachable!();
2483 }
2484
2485 async fn cached_server_binary(
2486 &self,
2487 _: PathBuf,
2488 _: &dyn LspAdapterDelegate,
2489 ) -> Option<LanguageServerBinary> {
2490 unreachable!();
2491 }
2492}
2493
2494#[cfg(any(test, feature = "test-support"))]
2495#[async_trait(?Send)]
2496impl LspAdapter for FakeLspAdapter {
2497 fn name(&self) -> LanguageServerName {
2498 LanguageServerName(self.name.into())
2499 }
2500
2501 fn disk_based_diagnostic_sources(&self) -> Vec<String> {
2502 self.disk_based_diagnostics_sources.clone()
2503 }
2504
2505 fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
2506 self.disk_based_diagnostics_progress_token.clone()
2507 }
2508
2509 async fn initialization_options(
2510 self: Arc<Self>,
2511 _: &Arc<dyn LspAdapterDelegate>,
2512 ) -> Result<Option<Value>> {
2513 Ok(self.initialization_options.clone())
2514 }
2515
2516 async fn label_for_completion(
2517 &self,
2518 item: &lsp::CompletionItem,
2519 language: &Arc<Language>,
2520 ) -> Option<CodeLabel> {
2521 let label_for_completion = self.label_for_completion.as_ref()?;
2522 label_for_completion(item, language)
2523 }
2524
2525 fn is_extension(&self) -> bool {
2526 false
2527 }
2528}
2529
2530enum Capture<'a> {
2531 Required(&'static str, &'a mut u32),
2532 Optional(&'static str, &'a mut Option<u32>),
2533}
2534
2535fn populate_capture_indices(
2536 query: &Query,
2537 language_name: &LanguageName,
2538 query_type: &str,
2539 expected_prefixes: &[&str],
2540 captures: &mut [Capture<'_>],
2541) -> bool {
2542 let mut found_required_indices = Vec::new();
2543 'outer: for (ix, name) in query.capture_names().iter().enumerate() {
2544 for (required_ix, capture) in captures.iter_mut().enumerate() {
2545 match capture {
2546 Capture::Required(capture_name, index) if capture_name == name => {
2547 **index = ix as u32;
2548 found_required_indices.push(required_ix);
2549 continue 'outer;
2550 }
2551 Capture::Optional(capture_name, index) if capture_name == name => {
2552 **index = Some(ix as u32);
2553 continue 'outer;
2554 }
2555 _ => {}
2556 }
2557 }
2558 if !name.starts_with("_")
2559 && !expected_prefixes
2560 .iter()
2561 .any(|&prefix| name.starts_with(prefix))
2562 {
2563 log::warn!(
2564 "unrecognized capture name '{}' in {} {} TreeSitter query \
2565 (suppress this warning by prefixing with '_')",
2566 name,
2567 language_name,
2568 query_type
2569 );
2570 }
2571 }
2572 let mut missing_required_captures = Vec::new();
2573 for (capture_ix, capture) in captures.iter().enumerate() {
2574 if let Capture::Required(capture_name, _) = capture
2575 && !found_required_indices.contains(&capture_ix)
2576 {
2577 missing_required_captures.push(*capture_name);
2578 }
2579 }
2580 let success = missing_required_captures.is_empty();
2581 if !success {
2582 log::error!(
2583 "missing required capture(s) in {} {} TreeSitter query: {}",
2584 language_name,
2585 query_type,
2586 missing_required_captures.join(", ")
2587 );
2588 }
2589 success
2590}
2591
2592pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
2593 lsp::Position::new(point.row, point.column)
2594}
2595
2596pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
2597 Unclipped(PointUtf16::new(point.line, point.character))
2598}
2599
2600pub fn range_to_lsp(range: Range<PointUtf16>) -> Result<lsp::Range> {
2601 anyhow::ensure!(
2602 range.start <= range.end,
2603 "Inverted range provided to an LSP request: {:?}-{:?}",
2604 range.start,
2605 range.end
2606 );
2607 Ok(lsp::Range {
2608 start: point_to_lsp(range.start),
2609 end: point_to_lsp(range.end),
2610 })
2611}
2612
2613pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
2614 let mut start = point_from_lsp(range.start);
2615 let mut end = point_from_lsp(range.end);
2616 if start > end {
2617 // We debug instead of warn so that this is not logged by default unless explicitly requested.
2618 // Using warn would write to the log file, and since we receive an enormous amount of
2619 // range_from_lsp calls (especially during completions), that can hang the main thread.
2620 //
2621 // See issue #36223.
2622 zlog::debug!("range_from_lsp called with inverted range {start:?}-{end:?}");
2623 mem::swap(&mut start, &mut end);
2624 }
2625 start..end
2626}
2627
2628#[doc(hidden)]
2629#[cfg(any(test, feature = "test-support"))]
2630pub fn rust_lang() -> Arc<Language> {
2631 use std::borrow::Cow;
2632
2633 let language = Language::new(
2634 LanguageConfig {
2635 name: "Rust".into(),
2636 matcher: LanguageMatcher {
2637 path_suffixes: vec!["rs".to_string()],
2638 ..Default::default()
2639 },
2640 line_comments: vec!["// ".into(), "/// ".into(), "//! ".into()],
2641 ..Default::default()
2642 },
2643 Some(tree_sitter_rust::LANGUAGE.into()),
2644 )
2645 .with_queries(LanguageQueries {
2646 outline: Some(Cow::from(include_str!(
2647 "../../languages/src/rust/outline.scm"
2648 ))),
2649 indents: Some(Cow::from(include_str!(
2650 "../../languages/src/rust/indents.scm"
2651 ))),
2652 brackets: Some(Cow::from(include_str!(
2653 "../../languages/src/rust/brackets.scm"
2654 ))),
2655 text_objects: Some(Cow::from(include_str!(
2656 "../../languages/src/rust/textobjects.scm"
2657 ))),
2658 ..LanguageQueries::default()
2659 })
2660 .expect("Could not parse queries");
2661 Arc::new(language)
2662}
2663
2664#[doc(hidden)]
2665#[cfg(any(test, feature = "test-support"))]
2666pub fn markdown_lang() -> Arc<Language> {
2667 use std::borrow::Cow;
2668
2669 let language = Language::new(
2670 LanguageConfig {
2671 name: "Markdown".into(),
2672 matcher: LanguageMatcher {
2673 path_suffixes: vec!["md".into()],
2674 ..Default::default()
2675 },
2676 ..LanguageConfig::default()
2677 },
2678 Some(tree_sitter_md::LANGUAGE.into()),
2679 )
2680 .with_queries(LanguageQueries {
2681 brackets: Some(Cow::from(include_str!(
2682 "../../languages/src/markdown/brackets.scm"
2683 ))),
2684 injections: Some(Cow::from(include_str!(
2685 "../../languages/src/markdown/injections.scm"
2686 ))),
2687 ..LanguageQueries::default()
2688 })
2689 .expect("Could not parse markdown queries");
2690 Arc::new(language)
2691}
2692
2693#[cfg(test)]
2694mod tests {
2695 use super::*;
2696 use gpui::TestAppContext;
2697 use pretty_assertions::assert_matches;
2698
2699 #[gpui::test(iterations = 10)]
2700 async fn test_language_loading(cx: &mut TestAppContext) {
2701 let languages = LanguageRegistry::test(cx.executor());
2702 let languages = Arc::new(languages);
2703 languages.register_native_grammars([
2704 ("json", tree_sitter_json::LANGUAGE),
2705 ("rust", tree_sitter_rust::LANGUAGE),
2706 ]);
2707 languages.register_test_language(LanguageConfig {
2708 name: "JSON".into(),
2709 grammar: Some("json".into()),
2710 matcher: LanguageMatcher {
2711 path_suffixes: vec!["json".into()],
2712 ..Default::default()
2713 },
2714 ..Default::default()
2715 });
2716 languages.register_test_language(LanguageConfig {
2717 name: "Rust".into(),
2718 grammar: Some("rust".into()),
2719 matcher: LanguageMatcher {
2720 path_suffixes: vec!["rs".into()],
2721 ..Default::default()
2722 },
2723 ..Default::default()
2724 });
2725 assert_eq!(
2726 languages.language_names(),
2727 &[
2728 LanguageName::new("JSON"),
2729 LanguageName::new("Plain Text"),
2730 LanguageName::new("Rust"),
2731 ]
2732 );
2733
2734 let rust1 = languages.language_for_name("Rust");
2735 let rust2 = languages.language_for_name("Rust");
2736
2737 // Ensure language is still listed even if it's being loaded.
2738 assert_eq!(
2739 languages.language_names(),
2740 &[
2741 LanguageName::new("JSON"),
2742 LanguageName::new("Plain Text"),
2743 LanguageName::new("Rust"),
2744 ]
2745 );
2746
2747 let (rust1, rust2) = futures::join!(rust1, rust2);
2748 assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap()));
2749
2750 // Ensure language is still listed even after loading it.
2751 assert_eq!(
2752 languages.language_names(),
2753 &[
2754 LanguageName::new("JSON"),
2755 LanguageName::new("Plain Text"),
2756 LanguageName::new("Rust"),
2757 ]
2758 );
2759
2760 // Loading an unknown language returns an error.
2761 assert!(languages.language_for_name("Unknown").await.is_err());
2762 }
2763
2764 #[gpui::test]
2765 async fn test_completion_label_omits_duplicate_data() {
2766 let regular_completion_item_1 = lsp::CompletionItem {
2767 label: "regular1".to_string(),
2768 detail: Some("detail1".to_string()),
2769 label_details: Some(lsp::CompletionItemLabelDetails {
2770 detail: None,
2771 description: Some("description 1".to_string()),
2772 }),
2773 ..lsp::CompletionItem::default()
2774 };
2775
2776 let regular_completion_item_2 = lsp::CompletionItem {
2777 label: "regular2".to_string(),
2778 label_details: Some(lsp::CompletionItemLabelDetails {
2779 detail: None,
2780 description: Some("description 2".to_string()),
2781 }),
2782 ..lsp::CompletionItem::default()
2783 };
2784
2785 let completion_item_with_duplicate_detail_and_proper_description = lsp::CompletionItem {
2786 detail: Some(regular_completion_item_1.label.clone()),
2787 ..regular_completion_item_1.clone()
2788 };
2789
2790 let completion_item_with_duplicate_detail = lsp::CompletionItem {
2791 detail: Some(regular_completion_item_1.label.clone()),
2792 label_details: None,
2793 ..regular_completion_item_1.clone()
2794 };
2795
2796 let completion_item_with_duplicate_description = lsp::CompletionItem {
2797 label_details: Some(lsp::CompletionItemLabelDetails {
2798 detail: None,
2799 description: Some(regular_completion_item_2.label.clone()),
2800 }),
2801 ..regular_completion_item_2.clone()
2802 };
2803
2804 assert_eq!(
2805 CodeLabel::fallback_for_completion(®ular_completion_item_1, None).text,
2806 format!(
2807 "{} {}",
2808 regular_completion_item_1.label,
2809 regular_completion_item_1.detail.unwrap()
2810 ),
2811 "LSP completion items with both detail and label_details.description should prefer detail"
2812 );
2813 assert_eq!(
2814 CodeLabel::fallback_for_completion(®ular_completion_item_2, None).text,
2815 format!(
2816 "{} {}",
2817 regular_completion_item_2.label,
2818 regular_completion_item_2
2819 .label_details
2820 .as_ref()
2821 .unwrap()
2822 .description
2823 .as_ref()
2824 .unwrap()
2825 ),
2826 "LSP completion items without detail but with label_details.description should use that"
2827 );
2828 assert_eq!(
2829 CodeLabel::fallback_for_completion(
2830 &completion_item_with_duplicate_detail_and_proper_description,
2831 None
2832 )
2833 .text,
2834 format!(
2835 "{} {}",
2836 regular_completion_item_1.label,
2837 regular_completion_item_1
2838 .label_details
2839 .as_ref()
2840 .unwrap()
2841 .description
2842 .as_ref()
2843 .unwrap()
2844 ),
2845 "LSP completion items with both detail and label_details.description should prefer description only if the detail duplicates the completion label"
2846 );
2847 assert_eq!(
2848 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_detail, None).text,
2849 regular_completion_item_1.label,
2850 "LSP completion items with duplicate label and detail, should omit the detail"
2851 );
2852 assert_eq!(
2853 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_description, None)
2854 .text,
2855 regular_completion_item_2.label,
2856 "LSP completion items with duplicate label and detail, should omit the detail"
2857 );
2858 }
2859
2860 #[test]
2861 fn test_deserializing_comments_backwards_compat() {
2862 // current version of `block_comment` and `documentation_comment` work
2863 {
2864 let config: LanguageConfig = ::toml::from_str(
2865 r#"
2866 name = "Foo"
2867 block_comment = { start = "a", end = "b", prefix = "c", tab_size = 1 }
2868 documentation_comment = { start = "d", end = "e", prefix = "f", tab_size = 2 }
2869 "#,
2870 )
2871 .unwrap();
2872 assert_matches!(config.block_comment, Some(BlockCommentConfig { .. }));
2873 assert_matches!(
2874 config.documentation_comment,
2875 Some(BlockCommentConfig { .. })
2876 );
2877
2878 let block_config = config.block_comment.unwrap();
2879 assert_eq!(block_config.start.as_ref(), "a");
2880 assert_eq!(block_config.end.as_ref(), "b");
2881 assert_eq!(block_config.prefix.as_ref(), "c");
2882 assert_eq!(block_config.tab_size, 1);
2883
2884 let doc_config = config.documentation_comment.unwrap();
2885 assert_eq!(doc_config.start.as_ref(), "d");
2886 assert_eq!(doc_config.end.as_ref(), "e");
2887 assert_eq!(doc_config.prefix.as_ref(), "f");
2888 assert_eq!(doc_config.tab_size, 2);
2889 }
2890
2891 // former `documentation` setting is read into `documentation_comment`
2892 {
2893 let config: LanguageConfig = ::toml::from_str(
2894 r#"
2895 name = "Foo"
2896 documentation = { start = "a", end = "b", prefix = "c", tab_size = 1}
2897 "#,
2898 )
2899 .unwrap();
2900 assert_matches!(
2901 config.documentation_comment,
2902 Some(BlockCommentConfig { .. })
2903 );
2904
2905 let config = config.documentation_comment.unwrap();
2906 assert_eq!(config.start.as_ref(), "a");
2907 assert_eq!(config.end.as_ref(), "b");
2908 assert_eq!(config.prefix.as_ref(), "c");
2909 assert_eq!(config.tab_size, 1);
2910 }
2911
2912 // old block_comment format is read into BlockCommentConfig
2913 {
2914 let config: LanguageConfig = ::toml::from_str(
2915 r#"
2916 name = "Foo"
2917 block_comment = ["a", "b"]
2918 "#,
2919 )
2920 .unwrap();
2921 assert_matches!(config.block_comment, Some(BlockCommentConfig { .. }));
2922
2923 let config = config.block_comment.unwrap();
2924 assert_eq!(config.start.as_ref(), "a");
2925 assert_eq!(config.end.as_ref(), "b");
2926 assert_eq!(config.prefix.as_ref(), "");
2927 assert_eq!(config.tab_size, 0);
2928 }
2929 }
2930}