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