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