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
25pub use crate::language_settings::EditPredictionsMode;
26use crate::language_settings::SoftWrap;
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 preferred debuggers for this language.
784 #[serde(default)]
785 pub debuggers: IndexSet<SharedString>,
786}
787
788#[derive(Clone, Debug, Deserialize, Default, JsonSchema)]
789pub struct DecreaseIndentConfig {
790 #[serde(default, deserialize_with = "deserialize_regex")]
791 #[schemars(schema_with = "regex_json_schema")]
792 pub pattern: Option<Regex>,
793 #[serde(default)]
794 pub valid_after: Vec<String>,
795}
796
797#[derive(Clone, Debug, Serialize, Deserialize, Default, JsonSchema)]
798pub struct LanguageMatcher {
799 /// 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`.
800 #[serde(default)]
801 pub path_suffixes: Vec<String>,
802 /// A regex pattern that determines whether the language should be assigned to a file or not.
803 #[serde(
804 default,
805 serialize_with = "serialize_regex",
806 deserialize_with = "deserialize_regex"
807 )]
808 #[schemars(schema_with = "regex_json_schema")]
809 pub first_line_pattern: Option<Regex>,
810}
811
812/// The configuration for JSX tag auto-closing.
813#[derive(Clone, Deserialize, JsonSchema)]
814pub struct JsxTagAutoCloseConfig {
815 /// The name of the node for a opening tag
816 pub open_tag_node_name: String,
817 /// The name of the node for an closing tag
818 pub close_tag_node_name: String,
819 /// The name of the node for a complete element with children for open and close tags
820 pub jsx_element_node_name: String,
821 /// The name of the node found within both opening and closing
822 /// tags that describes the tag name
823 pub tag_name_node_name: String,
824 /// Alternate Node names for tag names.
825 /// Specifically needed as TSX represents the name in `<Foo.Bar>`
826 /// as `member_expression` rather than `identifier` as usual
827 #[serde(default)]
828 pub tag_name_node_name_alternates: Vec<String>,
829 /// Some grammars are smart enough to detect a closing tag
830 /// that is not valid i.e. doesn't match it's corresponding
831 /// opening tag or does not have a corresponding opening tag
832 /// This should be set to the name of the node for invalid
833 /// closing tags if the grammar contains such a node, otherwise
834 /// detecting already closed tags will not work properly
835 #[serde(default)]
836 pub erroneous_close_tag_node_name: Option<String>,
837 /// See above for erroneous_close_tag_node_name for details
838 /// This should be set if the node used for the tag name
839 /// within erroneous closing tags is different from the
840 /// normal tag name node name
841 #[serde(default)]
842 pub erroneous_close_tag_name_node_name: Option<String>,
843}
844
845/// The configuration for block comments for this language.
846#[derive(Clone, Debug, JsonSchema, PartialEq)]
847pub struct BlockCommentConfig {
848 /// A start tag of block comment.
849 pub start: Arc<str>,
850 /// A end tag of block comment.
851 pub end: Arc<str>,
852 /// A character to add as a prefix when a new line is added to a block comment.
853 pub prefix: Arc<str>,
854 /// A indent to add for prefix and end line upon new line.
855 pub tab_size: u32,
856}
857
858impl<'de> Deserialize<'de> for BlockCommentConfig {
859 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
860 where
861 D: Deserializer<'de>,
862 {
863 #[derive(Deserialize)]
864 #[serde(untagged)]
865 enum BlockCommentConfigHelper {
866 New {
867 start: Arc<str>,
868 end: Arc<str>,
869 prefix: Arc<str>,
870 tab_size: u32,
871 },
872 Old([Arc<str>; 2]),
873 }
874
875 match BlockCommentConfigHelper::deserialize(deserializer)? {
876 BlockCommentConfigHelper::New {
877 start,
878 end,
879 prefix,
880 tab_size,
881 } => Ok(BlockCommentConfig {
882 start,
883 end,
884 prefix,
885 tab_size,
886 }),
887 BlockCommentConfigHelper::Old([start, end]) => Ok(BlockCommentConfig {
888 start,
889 end,
890 prefix: "".into(),
891 tab_size: 0,
892 }),
893 }
894 }
895}
896
897/// Represents a language for the given range. Some languages (e.g. HTML)
898/// interleave several languages together, thus a single buffer might actually contain
899/// several nested scopes.
900#[derive(Clone, Debug)]
901pub struct LanguageScope {
902 language: Arc<Language>,
903 override_id: Option<u32>,
904}
905
906#[derive(Clone, Deserialize, Default, Debug, JsonSchema)]
907pub struct LanguageConfigOverride {
908 #[serde(default)]
909 pub line_comments: Override<Vec<Arc<str>>>,
910 #[serde(default)]
911 pub block_comment: Override<BlockCommentConfig>,
912 #[serde(skip)]
913 pub disabled_bracket_ixs: Vec<u16>,
914 #[serde(default)]
915 pub word_characters: Override<HashSet<char>>,
916 #[serde(default)]
917 pub completion_query_characters: Override<HashSet<char>>,
918 #[serde(default)]
919 pub opt_into_language_servers: Vec<LanguageServerName>,
920 #[serde(default)]
921 pub prefer_label_for_snippet: Option<bool>,
922}
923
924#[derive(Clone, Deserialize, Debug, Serialize, JsonSchema)]
925#[serde(untagged)]
926pub enum Override<T> {
927 Remove { remove: bool },
928 Set(T),
929}
930
931impl<T> Default for Override<T> {
932 fn default() -> Self {
933 Override::Remove { remove: false }
934 }
935}
936
937impl<T> Override<T> {
938 fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> {
939 match this {
940 Some(Self::Set(value)) => Some(value),
941 Some(Self::Remove { remove: true }) => None,
942 Some(Self::Remove { remove: false }) | None => original,
943 }
944 }
945}
946
947impl Default for LanguageConfig {
948 fn default() -> Self {
949 Self {
950 name: LanguageName::new(""),
951 code_fence_block_name: None,
952 grammar: None,
953 matcher: LanguageMatcher::default(),
954 brackets: Default::default(),
955 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
956 auto_indent_on_paste: None,
957 increase_indent_pattern: Default::default(),
958 decrease_indent_pattern: Default::default(),
959 decrease_indent_patterns: Default::default(),
960 autoclose_before: Default::default(),
961 line_comments: Default::default(),
962 block_comment: Default::default(),
963 documentation_comment: Default::default(),
964 rewrap_prefixes: Default::default(),
965 scope_opt_in_language_servers: Default::default(),
966 overrides: Default::default(),
967 word_characters: Default::default(),
968 collapsed_placeholder: Default::default(),
969 hard_tabs: None,
970 tab_size: None,
971 soft_wrap: None,
972 wrap_characters: None,
973 prettier_parser_name: None,
974 hidden: false,
975 jsx_tag_auto_close: None,
976 completion_query_characters: Default::default(),
977 debuggers: Default::default(),
978 }
979 }
980}
981
982#[derive(Clone, Debug, Deserialize, JsonSchema)]
983pub struct WrapCharactersConfig {
984 /// Opening token split into a prefix and suffix. The first caret goes
985 /// after the prefix (i.e., between prefix and suffix).
986 pub start_prefix: String,
987 pub start_suffix: String,
988 /// Closing token split into a prefix and suffix. The second caret goes
989 /// after the prefix (i.e., between prefix and suffix).
990 pub end_prefix: String,
991 pub end_suffix: String,
992}
993
994fn auto_indent_using_last_non_empty_line_default() -> bool {
995 true
996}
997
998fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
999 let source = Option::<String>::deserialize(d)?;
1000 if let Some(source) = source {
1001 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
1002 } else {
1003 Ok(None)
1004 }
1005}
1006
1007fn regex_json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
1008 json_schema!({
1009 "type": "string"
1010 })
1011}
1012
1013fn serialize_regex<S>(regex: &Option<Regex>, serializer: S) -> Result<S::Ok, S::Error>
1014where
1015 S: Serializer,
1016{
1017 match regex {
1018 Some(regex) => serializer.serialize_str(regex.as_str()),
1019 None => serializer.serialize_none(),
1020 }
1021}
1022
1023fn deserialize_regex_vec<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<Regex>, D::Error> {
1024 let sources = Vec::<String>::deserialize(d)?;
1025 sources
1026 .into_iter()
1027 .map(|source| regex::Regex::new(&source))
1028 .collect::<Result<_, _>>()
1029 .map_err(de::Error::custom)
1030}
1031
1032fn regex_vec_json_schema(_: &mut SchemaGenerator) -> schemars::Schema {
1033 json_schema!({
1034 "type": "array",
1035 "items": { "type": "string" }
1036 })
1037}
1038
1039#[doc(hidden)]
1040#[cfg(any(test, feature = "test-support"))]
1041pub struct FakeLspAdapter {
1042 pub name: &'static str,
1043 pub initialization_options: Option<Value>,
1044 pub prettier_plugins: Vec<&'static str>,
1045 pub disk_based_diagnostics_progress_token: Option<String>,
1046 pub disk_based_diagnostics_sources: Vec<String>,
1047 pub language_server_binary: LanguageServerBinary,
1048
1049 pub capabilities: lsp::ServerCapabilities,
1050 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
1051 pub label_for_completion: Option<
1052 Box<
1053 dyn 'static
1054 + Send
1055 + Sync
1056 + Fn(&lsp::CompletionItem, &Arc<Language>) -> Option<CodeLabel>,
1057 >,
1058 >,
1059}
1060
1061/// Configuration of handling bracket pairs for a given language.
1062///
1063/// This struct includes settings for defining which pairs of characters are considered brackets and
1064/// also specifies any language-specific scopes where these pairs should be ignored for bracket matching purposes.
1065#[derive(Clone, Debug, Default, JsonSchema)]
1066#[schemars(with = "Vec::<BracketPairContent>")]
1067pub struct BracketPairConfig {
1068 /// A list of character pairs that should be treated as brackets in the context of a given language.
1069 pub pairs: Vec<BracketPair>,
1070 /// A list of tree-sitter scopes for which a given bracket should not be active.
1071 /// N-th entry in `[Self::disabled_scopes_by_bracket_ix]` contains a list of disabled scopes for an n-th entry in `[Self::pairs]`
1072 pub disabled_scopes_by_bracket_ix: Vec<Vec<String>>,
1073}
1074
1075impl BracketPairConfig {
1076 pub fn is_closing_brace(&self, c: char) -> bool {
1077 self.pairs.iter().any(|pair| pair.end.starts_with(c))
1078 }
1079}
1080
1081#[derive(Deserialize, JsonSchema)]
1082pub struct BracketPairContent {
1083 #[serde(flatten)]
1084 pub bracket_pair: BracketPair,
1085 #[serde(default)]
1086 pub not_in: Vec<String>,
1087}
1088
1089impl<'de> Deserialize<'de> for BracketPairConfig {
1090 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
1091 where
1092 D: Deserializer<'de>,
1093 {
1094 let result = Vec::<BracketPairContent>::deserialize(deserializer)?;
1095 let (brackets, disabled_scopes_by_bracket_ix) = result
1096 .into_iter()
1097 .map(|entry| (entry.bracket_pair, entry.not_in))
1098 .unzip();
1099
1100 Ok(BracketPairConfig {
1101 pairs: brackets,
1102 disabled_scopes_by_bracket_ix,
1103 })
1104 }
1105}
1106
1107/// Describes a single bracket pair and how an editor should react to e.g. inserting
1108/// an opening bracket or to a newline character insertion in between `start` and `end` characters.
1109#[derive(Clone, Debug, Default, Deserialize, PartialEq, JsonSchema)]
1110pub struct BracketPair {
1111 /// Starting substring for a bracket.
1112 pub start: String,
1113 /// Ending substring for a bracket.
1114 pub end: String,
1115 /// True if `end` should be automatically inserted right after `start` characters.
1116 pub close: bool,
1117 /// True if selected text should be surrounded by `start` and `end` characters.
1118 #[serde(default = "default_true")]
1119 pub surround: bool,
1120 /// True if an extra newline should be inserted while the cursor is in the middle
1121 /// of that bracket pair.
1122 pub newline: bool,
1123}
1124
1125#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1126pub struct LanguageId(usize);
1127
1128impl LanguageId {
1129 pub(crate) fn new() -> Self {
1130 Self(NEXT_LANGUAGE_ID.fetch_add(1, SeqCst))
1131 }
1132}
1133
1134pub struct Language {
1135 pub(crate) id: LanguageId,
1136 pub(crate) config: LanguageConfig,
1137 pub(crate) grammar: Option<Arc<Grammar>>,
1138 pub(crate) context_provider: Option<Arc<dyn ContextProvider>>,
1139 pub(crate) toolchain: Option<Arc<dyn ToolchainLister>>,
1140 pub(crate) manifest_name: Option<ManifestName>,
1141}
1142
1143#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1144pub struct GrammarId(pub usize);
1145
1146impl GrammarId {
1147 pub(crate) fn new() -> Self {
1148 Self(NEXT_GRAMMAR_ID.fetch_add(1, SeqCst))
1149 }
1150}
1151
1152pub struct Grammar {
1153 id: GrammarId,
1154 pub ts_language: tree_sitter::Language,
1155 pub(crate) error_query: Option<Query>,
1156 pub highlights_config: Option<HighlightsConfig>,
1157 pub(crate) brackets_config: Option<BracketsConfig>,
1158 pub(crate) redactions_config: Option<RedactionConfig>,
1159 pub(crate) runnable_config: Option<RunnableConfig>,
1160 pub(crate) indents_config: Option<IndentConfig>,
1161 pub outline_config: Option<OutlineConfig>,
1162 pub text_object_config: Option<TextObjectConfig>,
1163 pub embedding_config: Option<EmbeddingConfig>,
1164 pub(crate) injection_config: Option<InjectionConfig>,
1165 pub(crate) override_config: Option<OverrideConfig>,
1166 pub(crate) debug_variables_config: Option<DebugVariablesConfig>,
1167 pub(crate) highlight_map: Mutex<HighlightMap>,
1168}
1169
1170pub struct HighlightsConfig {
1171 pub query: Query,
1172 pub identifier_capture_indices: Vec<u32>,
1173}
1174
1175struct IndentConfig {
1176 query: Query,
1177 indent_capture_ix: u32,
1178 start_capture_ix: Option<u32>,
1179 end_capture_ix: Option<u32>,
1180 outdent_capture_ix: Option<u32>,
1181 suffixed_start_captures: HashMap<u32, SharedString>,
1182}
1183
1184pub struct OutlineConfig {
1185 pub query: Query,
1186 pub item_capture_ix: u32,
1187 pub name_capture_ix: u32,
1188 pub context_capture_ix: Option<u32>,
1189 pub extra_context_capture_ix: Option<u32>,
1190 pub open_capture_ix: Option<u32>,
1191 pub close_capture_ix: Option<u32>,
1192 pub annotation_capture_ix: Option<u32>,
1193}
1194
1195#[derive(Debug, Clone, Copy, PartialEq)]
1196pub enum DebuggerTextObject {
1197 Variable,
1198 Scope,
1199}
1200
1201impl DebuggerTextObject {
1202 pub fn from_capture_name(name: &str) -> Option<DebuggerTextObject> {
1203 match name {
1204 "debug-variable" => Some(DebuggerTextObject::Variable),
1205 "debug-scope" => Some(DebuggerTextObject::Scope),
1206 _ => None,
1207 }
1208 }
1209}
1210
1211#[derive(Debug, Clone, Copy, PartialEq)]
1212pub enum TextObject {
1213 InsideFunction,
1214 AroundFunction,
1215 InsideClass,
1216 AroundClass,
1217 InsideComment,
1218 AroundComment,
1219}
1220
1221impl TextObject {
1222 pub fn from_capture_name(name: &str) -> Option<TextObject> {
1223 match name {
1224 "function.inside" => Some(TextObject::InsideFunction),
1225 "function.around" => Some(TextObject::AroundFunction),
1226 "class.inside" => Some(TextObject::InsideClass),
1227 "class.around" => Some(TextObject::AroundClass),
1228 "comment.inside" => Some(TextObject::InsideComment),
1229 "comment.around" => Some(TextObject::AroundComment),
1230 _ => None,
1231 }
1232 }
1233
1234 pub fn around(&self) -> Option<Self> {
1235 match self {
1236 TextObject::InsideFunction => Some(TextObject::AroundFunction),
1237 TextObject::InsideClass => Some(TextObject::AroundClass),
1238 TextObject::InsideComment => Some(TextObject::AroundComment),
1239 _ => None,
1240 }
1241 }
1242}
1243
1244pub struct TextObjectConfig {
1245 pub query: Query,
1246 pub text_objects_by_capture_ix: Vec<(u32, TextObject)>,
1247}
1248
1249#[derive(Debug)]
1250pub struct EmbeddingConfig {
1251 pub query: Query,
1252 pub item_capture_ix: u32,
1253 pub name_capture_ix: Option<u32>,
1254 pub context_capture_ix: Option<u32>,
1255 pub collapse_capture_ix: Option<u32>,
1256 pub keep_capture_ix: Option<u32>,
1257}
1258
1259struct InjectionConfig {
1260 query: Query,
1261 content_capture_ix: u32,
1262 language_capture_ix: Option<u32>,
1263 patterns: Vec<InjectionPatternConfig>,
1264}
1265
1266struct RedactionConfig {
1267 pub query: Query,
1268 pub redaction_capture_ix: u32,
1269}
1270
1271#[derive(Clone, Debug, PartialEq)]
1272enum RunnableCapture {
1273 Named(SharedString),
1274 Run,
1275}
1276
1277struct RunnableConfig {
1278 pub query: Query,
1279 /// A mapping from capture indice to capture kind
1280 pub extra_captures: Vec<RunnableCapture>,
1281}
1282
1283struct OverrideConfig {
1284 query: Query,
1285 values: HashMap<u32, OverrideEntry>,
1286}
1287
1288#[derive(Debug)]
1289struct OverrideEntry {
1290 name: String,
1291 range_is_inclusive: bool,
1292 value: LanguageConfigOverride,
1293}
1294
1295#[derive(Default, Clone)]
1296struct InjectionPatternConfig {
1297 language: Option<Box<str>>,
1298 combined: bool,
1299}
1300
1301#[derive(Debug)]
1302struct BracketsConfig {
1303 query: Query,
1304 open_capture_ix: u32,
1305 close_capture_ix: u32,
1306 patterns: Vec<BracketsPatternConfig>,
1307}
1308
1309#[derive(Clone, Debug, Default)]
1310struct BracketsPatternConfig {
1311 newline_only: bool,
1312}
1313
1314pub struct DebugVariablesConfig {
1315 pub query: Query,
1316 pub objects_by_capture_ix: Vec<(u32, DebuggerTextObject)>,
1317}
1318
1319impl Language {
1320 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
1321 Self::new_with_id(LanguageId::new(), config, ts_language)
1322 }
1323
1324 pub fn id(&self) -> LanguageId {
1325 self.id
1326 }
1327
1328 fn new_with_id(
1329 id: LanguageId,
1330 config: LanguageConfig,
1331 ts_language: Option<tree_sitter::Language>,
1332 ) -> Self {
1333 Self {
1334 id,
1335 config,
1336 grammar: ts_language.map(|ts_language| {
1337 Arc::new(Grammar {
1338 id: GrammarId::new(),
1339 highlights_config: None,
1340 brackets_config: None,
1341 outline_config: None,
1342 text_object_config: None,
1343 embedding_config: None,
1344 indents_config: None,
1345 injection_config: None,
1346 override_config: None,
1347 redactions_config: None,
1348 runnable_config: None,
1349 error_query: Query::new(&ts_language, "(ERROR) @error").ok(),
1350 debug_variables_config: None,
1351 ts_language,
1352 highlight_map: Default::default(),
1353 })
1354 }),
1355 context_provider: None,
1356 toolchain: None,
1357 manifest_name: None,
1358 }
1359 }
1360
1361 pub fn with_context_provider(mut self, provider: Option<Arc<dyn ContextProvider>>) -> Self {
1362 self.context_provider = provider;
1363 self
1364 }
1365
1366 pub fn with_toolchain_lister(mut self, provider: Option<Arc<dyn ToolchainLister>>) -> Self {
1367 self.toolchain = provider;
1368 self
1369 }
1370
1371 pub fn with_manifest(mut self, name: Option<ManifestName>) -> Self {
1372 self.manifest_name = name;
1373 self
1374 }
1375
1376 pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
1377 if let Some(query) = queries.highlights {
1378 self = self
1379 .with_highlights_query(query.as_ref())
1380 .context("Error loading highlights query")?;
1381 }
1382 if let Some(query) = queries.brackets {
1383 self = self
1384 .with_brackets_query(query.as_ref())
1385 .context("Error loading brackets query")?;
1386 }
1387 if let Some(query) = queries.indents {
1388 self = self
1389 .with_indents_query(query.as_ref())
1390 .context("Error loading indents query")?;
1391 }
1392 if let Some(query) = queries.outline {
1393 self = self
1394 .with_outline_query(query.as_ref())
1395 .context("Error loading outline query")?;
1396 }
1397 if let Some(query) = queries.embedding {
1398 self = self
1399 .with_embedding_query(query.as_ref())
1400 .context("Error loading embedding query")?;
1401 }
1402 if let Some(query) = queries.injections {
1403 self = self
1404 .with_injection_query(query.as_ref())
1405 .context("Error loading injection query")?;
1406 }
1407 if let Some(query) = queries.overrides {
1408 self = self
1409 .with_override_query(query.as_ref())
1410 .context("Error loading override query")?;
1411 }
1412 if let Some(query) = queries.redactions {
1413 self = self
1414 .with_redaction_query(query.as_ref())
1415 .context("Error loading redaction query")?;
1416 }
1417 if let Some(query) = queries.runnables {
1418 self = self
1419 .with_runnable_query(query.as_ref())
1420 .context("Error loading runnables query")?;
1421 }
1422 if let Some(query) = queries.text_objects {
1423 self = self
1424 .with_text_object_query(query.as_ref())
1425 .context("Error loading textobject query")?;
1426 }
1427 if let Some(query) = queries.debugger {
1428 self = self
1429 .with_debug_variables_query(query.as_ref())
1430 .context("Error loading debug variables query")?;
1431 }
1432 Ok(self)
1433 }
1434
1435 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
1436 let grammar = self.grammar_mut()?;
1437 let query = Query::new(&grammar.ts_language, source)?;
1438
1439 let mut identifier_capture_indices = Vec::new();
1440 for name in [
1441 "variable",
1442 "constant",
1443 "constructor",
1444 "function",
1445 "function.method",
1446 "function.method.call",
1447 "function.special",
1448 "property",
1449 "type",
1450 "type.interface",
1451 ] {
1452 identifier_capture_indices.extend(query.capture_index_for_name(name));
1453 }
1454
1455 grammar.highlights_config = Some(HighlightsConfig {
1456 query,
1457 identifier_capture_indices,
1458 });
1459
1460 Ok(self)
1461 }
1462
1463 pub fn with_runnable_query(mut self, source: &str) -> Result<Self> {
1464 let grammar = self.grammar_mut()?;
1465
1466 let query = Query::new(&grammar.ts_language, source)?;
1467 let extra_captures: Vec<_> = query
1468 .capture_names()
1469 .iter()
1470 .map(|&name| match name {
1471 "run" => RunnableCapture::Run,
1472 name => RunnableCapture::Named(name.to_string().into()),
1473 })
1474 .collect();
1475
1476 grammar.runnable_config = Some(RunnableConfig {
1477 extra_captures,
1478 query,
1479 });
1480
1481 Ok(self)
1482 }
1483
1484 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
1485 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1486 let mut item_capture_ix = 0;
1487 let mut name_capture_ix = 0;
1488 let mut context_capture_ix = None;
1489 let mut extra_context_capture_ix = None;
1490 let mut open_capture_ix = None;
1491 let mut close_capture_ix = None;
1492 let mut annotation_capture_ix = None;
1493 if populate_capture_indices(
1494 &query,
1495 &self.config.name,
1496 "outline",
1497 &[],
1498 &mut [
1499 Capture::Required("item", &mut item_capture_ix),
1500 Capture::Required("name", &mut name_capture_ix),
1501 Capture::Optional("context", &mut context_capture_ix),
1502 Capture::Optional("context.extra", &mut extra_context_capture_ix),
1503 Capture::Optional("open", &mut open_capture_ix),
1504 Capture::Optional("close", &mut close_capture_ix),
1505 Capture::Optional("annotation", &mut annotation_capture_ix),
1506 ],
1507 ) {
1508 self.grammar_mut()?.outline_config = Some(OutlineConfig {
1509 query,
1510 item_capture_ix,
1511 name_capture_ix,
1512 context_capture_ix,
1513 extra_context_capture_ix,
1514 open_capture_ix,
1515 close_capture_ix,
1516 annotation_capture_ix,
1517 });
1518 }
1519 Ok(self)
1520 }
1521
1522 pub fn with_text_object_query(mut self, source: &str) -> Result<Self> {
1523 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1524
1525 let mut text_objects_by_capture_ix = Vec::new();
1526 for (ix, name) in query.capture_names().iter().enumerate() {
1527 if let Some(text_object) = TextObject::from_capture_name(name) {
1528 text_objects_by_capture_ix.push((ix as u32, text_object));
1529 } else {
1530 log::warn!(
1531 "unrecognized capture name '{}' in {} textobjects TreeSitter query",
1532 name,
1533 self.config.name,
1534 );
1535 }
1536 }
1537
1538 self.grammar_mut()?.text_object_config = Some(TextObjectConfig {
1539 query,
1540 text_objects_by_capture_ix,
1541 });
1542 Ok(self)
1543 }
1544
1545 pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
1546 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1547 let mut item_capture_ix = 0;
1548 let mut name_capture_ix = None;
1549 let mut context_capture_ix = None;
1550 let mut collapse_capture_ix = None;
1551 let mut keep_capture_ix = None;
1552 if populate_capture_indices(
1553 &query,
1554 &self.config.name,
1555 "embedding",
1556 &[],
1557 &mut [
1558 Capture::Required("item", &mut item_capture_ix),
1559 Capture::Optional("name", &mut name_capture_ix),
1560 Capture::Optional("context", &mut context_capture_ix),
1561 Capture::Optional("keep", &mut keep_capture_ix),
1562 Capture::Optional("collapse", &mut collapse_capture_ix),
1563 ],
1564 ) {
1565 self.grammar_mut()?.embedding_config = Some(EmbeddingConfig {
1566 query,
1567 item_capture_ix,
1568 name_capture_ix,
1569 context_capture_ix,
1570 collapse_capture_ix,
1571 keep_capture_ix,
1572 });
1573 }
1574 Ok(self)
1575 }
1576
1577 pub fn with_debug_variables_query(mut self, source: &str) -> Result<Self> {
1578 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1579
1580 let mut objects_by_capture_ix = Vec::new();
1581 for (ix, name) in query.capture_names().iter().enumerate() {
1582 if let Some(text_object) = DebuggerTextObject::from_capture_name(name) {
1583 objects_by_capture_ix.push((ix as u32, text_object));
1584 } else {
1585 log::warn!(
1586 "unrecognized capture name '{}' in {} debugger TreeSitter query",
1587 name,
1588 self.config.name,
1589 );
1590 }
1591 }
1592
1593 self.grammar_mut()?.debug_variables_config = Some(DebugVariablesConfig {
1594 query,
1595 objects_by_capture_ix,
1596 });
1597 Ok(self)
1598 }
1599
1600 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
1601 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1602 let mut open_capture_ix = 0;
1603 let mut close_capture_ix = 0;
1604 if populate_capture_indices(
1605 &query,
1606 &self.config.name,
1607 "brackets",
1608 &[],
1609 &mut [
1610 Capture::Required("open", &mut open_capture_ix),
1611 Capture::Required("close", &mut close_capture_ix),
1612 ],
1613 ) {
1614 let patterns = (0..query.pattern_count())
1615 .map(|ix| {
1616 let mut config = BracketsPatternConfig::default();
1617 for setting in query.property_settings(ix) {
1618 if setting.key.as_ref() == "newline.only" {
1619 config.newline_only = true
1620 }
1621 }
1622 config
1623 })
1624 .collect();
1625 self.grammar_mut()?.brackets_config = Some(BracketsConfig {
1626 query,
1627 open_capture_ix,
1628 close_capture_ix,
1629 patterns,
1630 });
1631 }
1632 Ok(self)
1633 }
1634
1635 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
1636 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1637 let mut indent_capture_ix = 0;
1638 let mut start_capture_ix = None;
1639 let mut end_capture_ix = None;
1640 let mut outdent_capture_ix = None;
1641 if populate_capture_indices(
1642 &query,
1643 &self.config.name,
1644 "indents",
1645 &["start."],
1646 &mut [
1647 Capture::Required("indent", &mut indent_capture_ix),
1648 Capture::Optional("start", &mut start_capture_ix),
1649 Capture::Optional("end", &mut end_capture_ix),
1650 Capture::Optional("outdent", &mut outdent_capture_ix),
1651 ],
1652 ) {
1653 let mut suffixed_start_captures = HashMap::default();
1654 for (ix, name) in query.capture_names().iter().enumerate() {
1655 if let Some(suffix) = name.strip_prefix("start.") {
1656 suffixed_start_captures.insert(ix as u32, suffix.to_owned().into());
1657 }
1658 }
1659
1660 self.grammar_mut()?.indents_config = Some(IndentConfig {
1661 query,
1662 indent_capture_ix,
1663 start_capture_ix,
1664 end_capture_ix,
1665 outdent_capture_ix,
1666 suffixed_start_captures,
1667 });
1668 }
1669 Ok(self)
1670 }
1671
1672 pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
1673 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1674 let mut language_capture_ix = None;
1675 let mut injection_language_capture_ix = None;
1676 let mut content_capture_ix = None;
1677 let mut injection_content_capture_ix = None;
1678 if populate_capture_indices(
1679 &query,
1680 &self.config.name,
1681 "injections",
1682 &[],
1683 &mut [
1684 Capture::Optional("language", &mut language_capture_ix),
1685 Capture::Optional("injection.language", &mut injection_language_capture_ix),
1686 Capture::Optional("content", &mut content_capture_ix),
1687 Capture::Optional("injection.content", &mut injection_content_capture_ix),
1688 ],
1689 ) {
1690 language_capture_ix = match (language_capture_ix, injection_language_capture_ix) {
1691 (None, Some(ix)) => Some(ix),
1692 (Some(_), Some(_)) => {
1693 anyhow::bail!("both language and injection.language captures are present");
1694 }
1695 _ => language_capture_ix,
1696 };
1697 content_capture_ix = match (content_capture_ix, injection_content_capture_ix) {
1698 (None, Some(ix)) => Some(ix),
1699 (Some(_), Some(_)) => {
1700 anyhow::bail!("both content and injection.content captures are present")
1701 }
1702 _ => content_capture_ix,
1703 };
1704 let patterns = (0..query.pattern_count())
1705 .map(|ix| {
1706 let mut config = InjectionPatternConfig::default();
1707 for setting in query.property_settings(ix) {
1708 match setting.key.as_ref() {
1709 "language" | "injection.language" => {
1710 config.language.clone_from(&setting.value);
1711 }
1712 "combined" | "injection.combined" => {
1713 config.combined = true;
1714 }
1715 _ => {}
1716 }
1717 }
1718 config
1719 })
1720 .collect();
1721 if let Some(content_capture_ix) = content_capture_ix {
1722 self.grammar_mut()?.injection_config = Some(InjectionConfig {
1723 query,
1724 language_capture_ix,
1725 content_capture_ix,
1726 patterns,
1727 });
1728 } else {
1729 log::error!(
1730 "missing required capture in injections {} TreeSitter query: \
1731 content or injection.content",
1732 &self.config.name,
1733 );
1734 }
1735 }
1736 Ok(self)
1737 }
1738
1739 pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
1740 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1741
1742 let mut override_configs_by_id = HashMap::default();
1743 for (ix, mut name) in query.capture_names().iter().copied().enumerate() {
1744 let mut range_is_inclusive = false;
1745 if name.starts_with('_') {
1746 continue;
1747 }
1748 if let Some(prefix) = name.strip_suffix(".inclusive") {
1749 name = prefix;
1750 range_is_inclusive = true;
1751 }
1752
1753 let value = self.config.overrides.get(name).cloned().unwrap_or_default();
1754 for server_name in &value.opt_into_language_servers {
1755 if !self
1756 .config
1757 .scope_opt_in_language_servers
1758 .contains(server_name)
1759 {
1760 util::debug_panic!(
1761 "Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server"
1762 );
1763 }
1764 }
1765
1766 override_configs_by_id.insert(
1767 ix as u32,
1768 OverrideEntry {
1769 name: name.to_string(),
1770 range_is_inclusive,
1771 value,
1772 },
1773 );
1774 }
1775
1776 let referenced_override_names = self.config.overrides.keys().chain(
1777 self.config
1778 .brackets
1779 .disabled_scopes_by_bracket_ix
1780 .iter()
1781 .flatten(),
1782 );
1783
1784 for referenced_name in referenced_override_names {
1785 if !override_configs_by_id
1786 .values()
1787 .any(|entry| entry.name == *referenced_name)
1788 {
1789 anyhow::bail!(
1790 "language {:?} has overrides in config not in query: {referenced_name:?}",
1791 self.config.name
1792 );
1793 }
1794 }
1795
1796 for entry in override_configs_by_id.values_mut() {
1797 entry.value.disabled_bracket_ixs = self
1798 .config
1799 .brackets
1800 .disabled_scopes_by_bracket_ix
1801 .iter()
1802 .enumerate()
1803 .filter_map(|(ix, disabled_scope_names)| {
1804 if disabled_scope_names.contains(&entry.name) {
1805 Some(ix as u16)
1806 } else {
1807 None
1808 }
1809 })
1810 .collect();
1811 }
1812
1813 self.config.brackets.disabled_scopes_by_bracket_ix.clear();
1814
1815 let grammar = self.grammar_mut()?;
1816 grammar.override_config = Some(OverrideConfig {
1817 query,
1818 values: override_configs_by_id,
1819 });
1820 Ok(self)
1821 }
1822
1823 pub fn with_redaction_query(mut self, source: &str) -> anyhow::Result<Self> {
1824 let query = Query::new(&self.expect_grammar()?.ts_language, source)?;
1825 let mut redaction_capture_ix = 0;
1826 if populate_capture_indices(
1827 &query,
1828 &self.config.name,
1829 "redactions",
1830 &[],
1831 &mut [Capture::Required("redact", &mut redaction_capture_ix)],
1832 ) {
1833 self.grammar_mut()?.redactions_config = Some(RedactionConfig {
1834 query,
1835 redaction_capture_ix,
1836 });
1837 }
1838 Ok(self)
1839 }
1840
1841 fn expect_grammar(&self) -> Result<&Grammar> {
1842 self.grammar
1843 .as_ref()
1844 .map(|grammar| grammar.as_ref())
1845 .context("no grammar for language")
1846 }
1847
1848 fn grammar_mut(&mut self) -> Result<&mut Grammar> {
1849 Arc::get_mut(self.grammar.as_mut().context("no grammar for language")?)
1850 .context("cannot mutate grammar")
1851 }
1852
1853 pub fn name(&self) -> LanguageName {
1854 self.config.name.clone()
1855 }
1856 pub fn manifest(&self) -> Option<&ManifestName> {
1857 self.manifest_name.as_ref()
1858 }
1859
1860 pub fn code_fence_block_name(&self) -> Arc<str> {
1861 self.config
1862 .code_fence_block_name
1863 .clone()
1864 .unwrap_or_else(|| self.config.name.as_ref().to_lowercase().into())
1865 }
1866
1867 pub fn context_provider(&self) -> Option<Arc<dyn ContextProvider>> {
1868 self.context_provider.clone()
1869 }
1870
1871 pub fn toolchain_lister(&self) -> Option<Arc<dyn ToolchainLister>> {
1872 self.toolchain.clone()
1873 }
1874
1875 pub fn highlight_text<'a>(
1876 self: &'a Arc<Self>,
1877 text: &'a Rope,
1878 range: Range<usize>,
1879 ) -> Vec<(Range<usize>, HighlightId)> {
1880 let mut result = Vec::new();
1881 if let Some(grammar) = &self.grammar {
1882 let tree = grammar.parse_text(text, None);
1883 let captures =
1884 SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
1885 grammar
1886 .highlights_config
1887 .as_ref()
1888 .map(|config| &config.query)
1889 });
1890 let highlight_maps = vec![grammar.highlight_map()];
1891 let mut offset = 0;
1892 for chunk in
1893 BufferChunks::new(text, range, Some((captures, highlight_maps)), false, None)
1894 {
1895 let end_offset = offset + chunk.text.len();
1896 if let Some(highlight_id) = chunk.syntax_highlight_id
1897 && !highlight_id.is_default()
1898 {
1899 result.push((offset..end_offset, highlight_id));
1900 }
1901 offset = end_offset;
1902 }
1903 }
1904 result
1905 }
1906
1907 pub fn path_suffixes(&self) -> &[String] {
1908 &self.config.matcher.path_suffixes
1909 }
1910
1911 pub fn should_autoclose_before(&self, c: char) -> bool {
1912 c.is_whitespace() || self.config.autoclose_before.contains(c)
1913 }
1914
1915 pub fn set_theme(&self, theme: &SyntaxTheme) {
1916 if let Some(grammar) = self.grammar.as_ref()
1917 && let Some(highlights_config) = &grammar.highlights_config
1918 {
1919 *grammar.highlight_map.lock() =
1920 HighlightMap::new(highlights_config.query.capture_names(), theme);
1921 }
1922 }
1923
1924 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
1925 self.grammar.as_ref()
1926 }
1927
1928 pub fn default_scope(self: &Arc<Self>) -> LanguageScope {
1929 LanguageScope {
1930 language: self.clone(),
1931 override_id: None,
1932 }
1933 }
1934
1935 pub fn lsp_id(&self) -> String {
1936 self.config.name.lsp_id()
1937 }
1938
1939 pub fn prettier_parser_name(&self) -> Option<&str> {
1940 self.config.prettier_parser_name.as_deref()
1941 }
1942
1943 pub fn config(&self) -> &LanguageConfig {
1944 &self.config
1945 }
1946}
1947
1948impl LanguageScope {
1949 pub fn path_suffixes(&self) -> &[String] {
1950 self.language.path_suffixes()
1951 }
1952
1953 pub fn language_name(&self) -> LanguageName {
1954 self.language.config.name.clone()
1955 }
1956
1957 pub fn collapsed_placeholder(&self) -> &str {
1958 self.language.config.collapsed_placeholder.as_ref()
1959 }
1960
1961 /// Returns line prefix that is inserted in e.g. line continuations or
1962 /// in `toggle comments` action.
1963 pub fn line_comment_prefixes(&self) -> &[Arc<str>] {
1964 Override::as_option(
1965 self.config_override().map(|o| &o.line_comments),
1966 Some(&self.language.config.line_comments),
1967 )
1968 .map_or([].as_slice(), |e| e.as_slice())
1969 }
1970
1971 /// Config for block comments for this language.
1972 pub fn block_comment(&self) -> Option<&BlockCommentConfig> {
1973 Override::as_option(
1974 self.config_override().map(|o| &o.block_comment),
1975 self.language.config.block_comment.as_ref(),
1976 )
1977 }
1978
1979 /// Config for documentation-style block comments for this language.
1980 pub fn documentation_comment(&self) -> Option<&BlockCommentConfig> {
1981 self.language.config.documentation_comment.as_ref()
1982 }
1983
1984 /// Returns additional regex patterns that act as prefix markers for creating
1985 /// boundaries during rewrapping.
1986 ///
1987 /// By default, Zed treats as paragraph and comment prefixes as boundaries.
1988 pub fn rewrap_prefixes(&self) -> &[Regex] {
1989 &self.language.config.rewrap_prefixes
1990 }
1991
1992 /// Returns a list of language-specific word characters.
1993 ///
1994 /// By default, Zed treats alphanumeric characters (and '_') as word characters for
1995 /// the purpose of actions like 'move to next word end` or whole-word search.
1996 /// It additionally accounts for language's additional word characters.
1997 pub fn word_characters(&self) -> Option<&HashSet<char>> {
1998 Override::as_option(
1999 self.config_override().map(|o| &o.word_characters),
2000 Some(&self.language.config.word_characters),
2001 )
2002 }
2003
2004 /// Returns a list of language-specific characters that are considered part of
2005 /// a completion query.
2006 pub fn completion_query_characters(&self) -> Option<&HashSet<char>> {
2007 Override::as_option(
2008 self.config_override()
2009 .map(|o| &o.completion_query_characters),
2010 Some(&self.language.config.completion_query_characters),
2011 )
2012 }
2013
2014 /// Returns whether to prefer snippet `label` over `new_text` to replace text when
2015 /// completion is accepted.
2016 ///
2017 /// In cases like when cursor is in string or renaming existing function,
2018 /// you don't want to expand function signature instead just want function name
2019 /// to replace existing one.
2020 pub fn prefers_label_for_snippet_in_completion(&self) -> bool {
2021 self.config_override()
2022 .and_then(|o| o.prefer_label_for_snippet)
2023 .unwrap_or(false)
2024 }
2025
2026 /// Returns a list of bracket pairs for a given language with an additional
2027 /// piece of information about whether the particular bracket pair is currently active for a given language.
2028 pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {
2029 let mut disabled_ids = self
2030 .config_override()
2031 .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice());
2032 self.language
2033 .config
2034 .brackets
2035 .pairs
2036 .iter()
2037 .enumerate()
2038 .map(move |(ix, bracket)| {
2039 let mut is_enabled = true;
2040 if let Some(next_disabled_ix) = disabled_ids.first()
2041 && ix == *next_disabled_ix as usize
2042 {
2043 disabled_ids = &disabled_ids[1..];
2044 is_enabled = false;
2045 }
2046 (bracket, is_enabled)
2047 })
2048 }
2049
2050 pub fn should_autoclose_before(&self, c: char) -> bool {
2051 c.is_whitespace() || self.language.config.autoclose_before.contains(c)
2052 }
2053
2054 pub fn language_allowed(&self, name: &LanguageServerName) -> bool {
2055 let config = &self.language.config;
2056 let opt_in_servers = &config.scope_opt_in_language_servers;
2057 if opt_in_servers.contains(name) {
2058 if let Some(over) = self.config_override() {
2059 over.opt_into_language_servers.contains(name)
2060 } else {
2061 false
2062 }
2063 } else {
2064 true
2065 }
2066 }
2067
2068 pub fn override_name(&self) -> Option<&str> {
2069 let id = self.override_id?;
2070 let grammar = self.language.grammar.as_ref()?;
2071 let override_config = grammar.override_config.as_ref()?;
2072 override_config.values.get(&id).map(|e| e.name.as_str())
2073 }
2074
2075 fn config_override(&self) -> Option<&LanguageConfigOverride> {
2076 let id = self.override_id?;
2077 let grammar = self.language.grammar.as_ref()?;
2078 let override_config = grammar.override_config.as_ref()?;
2079 override_config.values.get(&id).map(|e| &e.value)
2080 }
2081}
2082
2083impl Hash for Language {
2084 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
2085 self.id.hash(state)
2086 }
2087}
2088
2089impl PartialEq for Language {
2090 fn eq(&self, other: &Self) -> bool {
2091 self.id.eq(&other.id)
2092 }
2093}
2094
2095impl Eq for Language {}
2096
2097impl Debug for Language {
2098 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2099 f.debug_struct("Language")
2100 .field("name", &self.config.name)
2101 .finish()
2102 }
2103}
2104
2105impl Grammar {
2106 pub fn id(&self) -> GrammarId {
2107 self.id
2108 }
2109
2110 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
2111 with_parser(|parser| {
2112 parser
2113 .set_language(&self.ts_language)
2114 .expect("incompatible grammar");
2115 let mut chunks = text.chunks_in_range(0..text.len());
2116 parser
2117 .parse_with_options(
2118 &mut move |offset, _| {
2119 chunks.seek(offset);
2120 chunks.next().unwrap_or("").as_bytes()
2121 },
2122 old_tree.as_ref(),
2123 None,
2124 )
2125 .unwrap()
2126 })
2127 }
2128
2129 pub fn highlight_map(&self) -> HighlightMap {
2130 self.highlight_map.lock().clone()
2131 }
2132
2133 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
2134 let capture_id = self
2135 .highlights_config
2136 .as_ref()?
2137 .query
2138 .capture_index_for_name(name)?;
2139 Some(self.highlight_map.lock().get(capture_id))
2140 }
2141
2142 pub fn debug_variables_config(&self) -> Option<&DebugVariablesConfig> {
2143 self.debug_variables_config.as_ref()
2144 }
2145}
2146
2147impl CodeLabel {
2148 pub fn fallback_for_completion(
2149 item: &lsp::CompletionItem,
2150 language: Option<&Language>,
2151 ) -> Self {
2152 let highlight_id = item.kind.and_then(|kind| {
2153 let grammar = language?.grammar()?;
2154 use lsp::CompletionItemKind as Kind;
2155 match kind {
2156 Kind::CLASS => grammar.highlight_id_for_name("type"),
2157 Kind::CONSTANT => grammar.highlight_id_for_name("constant"),
2158 Kind::CONSTRUCTOR => grammar.highlight_id_for_name("constructor"),
2159 Kind::ENUM => grammar
2160 .highlight_id_for_name("enum")
2161 .or_else(|| grammar.highlight_id_for_name("type")),
2162 Kind::ENUM_MEMBER => grammar
2163 .highlight_id_for_name("variant")
2164 .or_else(|| grammar.highlight_id_for_name("property")),
2165 Kind::FIELD => grammar.highlight_id_for_name("property"),
2166 Kind::FUNCTION => grammar.highlight_id_for_name("function"),
2167 Kind::INTERFACE => grammar.highlight_id_for_name("type"),
2168 Kind::METHOD => grammar
2169 .highlight_id_for_name("function.method")
2170 .or_else(|| grammar.highlight_id_for_name("function")),
2171 Kind::OPERATOR => grammar.highlight_id_for_name("operator"),
2172 Kind::PROPERTY => grammar.highlight_id_for_name("property"),
2173 Kind::STRUCT => grammar.highlight_id_for_name("type"),
2174 Kind::VARIABLE => grammar.highlight_id_for_name("variable"),
2175 Kind::KEYWORD => grammar.highlight_id_for_name("keyword"),
2176 _ => None,
2177 }
2178 });
2179
2180 let label = &item.label;
2181 let label_length = label.len();
2182 let runs = highlight_id
2183 .map(|highlight_id| vec![(0..label_length, highlight_id)])
2184 .unwrap_or_default();
2185 let text = if let Some(detail) = item.detail.as_deref().filter(|detail| detail != label) {
2186 format!("{label} {detail}")
2187 } else if let Some(description) = item
2188 .label_details
2189 .as_ref()
2190 .and_then(|label_details| label_details.description.as_deref())
2191 .filter(|description| description != label)
2192 {
2193 format!("{label} {description}")
2194 } else {
2195 label.clone()
2196 };
2197 let filter_range = item
2198 .filter_text
2199 .as_deref()
2200 .and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
2201 .unwrap_or(0..label_length);
2202 Self {
2203 text,
2204 runs,
2205 filter_range,
2206 }
2207 }
2208
2209 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
2210 let filter_range = filter_text
2211 .and_then(|filter| text.find(filter).map(|ix| ix..ix + filter.len()))
2212 .unwrap_or(0..text.len());
2213 Self {
2214 runs: Vec::new(),
2215 filter_range,
2216 text,
2217 }
2218 }
2219
2220 pub fn push_str(&mut self, text: &str, highlight: Option<HighlightId>) {
2221 let start_ix = self.text.len();
2222 self.text.push_str(text);
2223 let end_ix = self.text.len();
2224 if let Some(highlight) = highlight {
2225 self.runs.push((start_ix..end_ix, highlight));
2226 }
2227 }
2228
2229 pub fn text(&self) -> &str {
2230 self.text.as_str()
2231 }
2232
2233 pub fn filter_text(&self) -> &str {
2234 &self.text[self.filter_range.clone()]
2235 }
2236}
2237
2238impl From<String> for CodeLabel {
2239 fn from(value: String) -> Self {
2240 Self::plain(value, None)
2241 }
2242}
2243
2244impl From<&str> for CodeLabel {
2245 fn from(value: &str) -> Self {
2246 Self::plain(value.to_string(), None)
2247 }
2248}
2249
2250impl Ord for LanguageMatcher {
2251 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2252 self.path_suffixes.cmp(&other.path_suffixes).then_with(|| {
2253 self.first_line_pattern
2254 .as_ref()
2255 .map(Regex::as_str)
2256 .cmp(&other.first_line_pattern.as_ref().map(Regex::as_str))
2257 })
2258 }
2259}
2260
2261impl PartialOrd for LanguageMatcher {
2262 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2263 Some(self.cmp(other))
2264 }
2265}
2266
2267impl Eq for LanguageMatcher {}
2268
2269impl PartialEq for LanguageMatcher {
2270 fn eq(&self, other: &Self) -> bool {
2271 self.path_suffixes == other.path_suffixes
2272 && self.first_line_pattern.as_ref().map(Regex::as_str)
2273 == other.first_line_pattern.as_ref().map(Regex::as_str)
2274 }
2275}
2276
2277#[cfg(any(test, feature = "test-support"))]
2278impl Default for FakeLspAdapter {
2279 fn default() -> Self {
2280 Self {
2281 name: "the-fake-language-server",
2282 capabilities: lsp::LanguageServer::full_capabilities(),
2283 initializer: None,
2284 disk_based_diagnostics_progress_token: None,
2285 initialization_options: None,
2286 disk_based_diagnostics_sources: Vec::new(),
2287 prettier_plugins: Vec::new(),
2288 language_server_binary: LanguageServerBinary {
2289 path: "/the/fake/lsp/path".into(),
2290 arguments: vec![],
2291 env: Default::default(),
2292 },
2293 label_for_completion: None,
2294 }
2295 }
2296}
2297
2298#[cfg(any(test, feature = "test-support"))]
2299impl LspInstaller for FakeLspAdapter {
2300 type BinaryVersion = ();
2301
2302 async fn fetch_latest_server_version(
2303 &self,
2304 _: &dyn LspAdapterDelegate,
2305 _: bool,
2306 _: &mut AsyncApp,
2307 ) -> Result<Self::BinaryVersion> {
2308 unreachable!()
2309 }
2310
2311 async fn check_if_user_installed(
2312 &self,
2313 _: &dyn LspAdapterDelegate,
2314 _: Option<Toolchain>,
2315 _: &AsyncApp,
2316 ) -> Option<LanguageServerBinary> {
2317 Some(self.language_server_binary.clone())
2318 }
2319
2320 async fn fetch_server_binary(
2321 &self,
2322 _: (),
2323 _: PathBuf,
2324 _: &dyn LspAdapterDelegate,
2325 ) -> Result<LanguageServerBinary> {
2326 unreachable!();
2327 }
2328
2329 async fn cached_server_binary(
2330 &self,
2331 _: PathBuf,
2332 _: &dyn LspAdapterDelegate,
2333 ) -> Option<LanguageServerBinary> {
2334 unreachable!();
2335 }
2336}
2337
2338#[cfg(any(test, feature = "test-support"))]
2339#[async_trait(?Send)]
2340impl LspAdapter for FakeLspAdapter {
2341 fn name(&self) -> LanguageServerName {
2342 LanguageServerName(self.name.into())
2343 }
2344
2345 fn disk_based_diagnostic_sources(&self) -> Vec<String> {
2346 self.disk_based_diagnostics_sources.clone()
2347 }
2348
2349 fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
2350 self.disk_based_diagnostics_progress_token.clone()
2351 }
2352
2353 async fn initialization_options(
2354 self: Arc<Self>,
2355 _: &Arc<dyn LspAdapterDelegate>,
2356 ) -> Result<Option<Value>> {
2357 Ok(self.initialization_options.clone())
2358 }
2359
2360 async fn label_for_completion(
2361 &self,
2362 item: &lsp::CompletionItem,
2363 language: &Arc<Language>,
2364 ) -> Option<CodeLabel> {
2365 let label_for_completion = self.label_for_completion.as_ref()?;
2366 label_for_completion(item, language)
2367 }
2368
2369 fn is_extension(&self) -> bool {
2370 false
2371 }
2372}
2373
2374enum Capture<'a> {
2375 Required(&'static str, &'a mut u32),
2376 Optional(&'static str, &'a mut Option<u32>),
2377}
2378
2379fn populate_capture_indices(
2380 query: &Query,
2381 language_name: &LanguageName,
2382 query_type: &str,
2383 expected_prefixes: &[&str],
2384 captures: &mut [Capture<'_>],
2385) -> bool {
2386 let mut found_required_indices = Vec::new();
2387 'outer: for (ix, name) in query.capture_names().iter().enumerate() {
2388 for (required_ix, capture) in captures.iter_mut().enumerate() {
2389 match capture {
2390 Capture::Required(capture_name, index) if capture_name == name => {
2391 **index = ix as u32;
2392 found_required_indices.push(required_ix);
2393 continue 'outer;
2394 }
2395 Capture::Optional(capture_name, index) if capture_name == name => {
2396 **index = Some(ix as u32);
2397 continue 'outer;
2398 }
2399 _ => {}
2400 }
2401 }
2402 if !name.starts_with("_")
2403 && !expected_prefixes
2404 .iter()
2405 .any(|&prefix| name.starts_with(prefix))
2406 {
2407 log::warn!(
2408 "unrecognized capture name '{}' in {} {} TreeSitter query \
2409 (suppress this warning by prefixing with '_')",
2410 name,
2411 language_name,
2412 query_type
2413 );
2414 }
2415 }
2416 let mut missing_required_captures = Vec::new();
2417 for (capture_ix, capture) in captures.iter().enumerate() {
2418 if let Capture::Required(capture_name, _) = capture
2419 && !found_required_indices.contains(&capture_ix)
2420 {
2421 missing_required_captures.push(*capture_name);
2422 }
2423 }
2424 let success = missing_required_captures.is_empty();
2425 if !success {
2426 log::error!(
2427 "missing required capture(s) in {} {} TreeSitter query: {}",
2428 language_name,
2429 query_type,
2430 missing_required_captures.join(", ")
2431 );
2432 }
2433 success
2434}
2435
2436pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
2437 lsp::Position::new(point.row, point.column)
2438}
2439
2440pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
2441 Unclipped(PointUtf16::new(point.line, point.character))
2442}
2443
2444pub fn range_to_lsp(range: Range<PointUtf16>) -> Result<lsp::Range> {
2445 anyhow::ensure!(
2446 range.start <= range.end,
2447 "Inverted range provided to an LSP request: {:?}-{:?}",
2448 range.start,
2449 range.end
2450 );
2451 Ok(lsp::Range {
2452 start: point_to_lsp(range.start),
2453 end: point_to_lsp(range.end),
2454 })
2455}
2456
2457pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
2458 let mut start = point_from_lsp(range.start);
2459 let mut end = point_from_lsp(range.end);
2460 if start > end {
2461 log::warn!("range_from_lsp called with inverted range {start:?}-{end:?}");
2462 mem::swap(&mut start, &mut end);
2463 }
2464 start..end
2465}
2466
2467#[cfg(test)]
2468mod tests {
2469 use super::*;
2470 use gpui::TestAppContext;
2471 use pretty_assertions::assert_matches;
2472
2473 #[gpui::test(iterations = 10)]
2474 async fn test_language_loading(cx: &mut TestAppContext) {
2475 let languages = LanguageRegistry::test(cx.executor());
2476 let languages = Arc::new(languages);
2477 languages.register_native_grammars([
2478 ("json", tree_sitter_json::LANGUAGE),
2479 ("rust", tree_sitter_rust::LANGUAGE),
2480 ]);
2481 languages.register_test_language(LanguageConfig {
2482 name: "JSON".into(),
2483 grammar: Some("json".into()),
2484 matcher: LanguageMatcher {
2485 path_suffixes: vec!["json".into()],
2486 ..Default::default()
2487 },
2488 ..Default::default()
2489 });
2490 languages.register_test_language(LanguageConfig {
2491 name: "Rust".into(),
2492 grammar: Some("rust".into()),
2493 matcher: LanguageMatcher {
2494 path_suffixes: vec!["rs".into()],
2495 ..Default::default()
2496 },
2497 ..Default::default()
2498 });
2499 assert_eq!(
2500 languages.language_names(),
2501 &[
2502 LanguageName::new("JSON"),
2503 LanguageName::new("Plain Text"),
2504 LanguageName::new("Rust"),
2505 ]
2506 );
2507
2508 let rust1 = languages.language_for_name("Rust");
2509 let rust2 = languages.language_for_name("Rust");
2510
2511 // Ensure language is still listed even if it's being loaded.
2512 assert_eq!(
2513 languages.language_names(),
2514 &[
2515 LanguageName::new("JSON"),
2516 LanguageName::new("Plain Text"),
2517 LanguageName::new("Rust"),
2518 ]
2519 );
2520
2521 let (rust1, rust2) = futures::join!(rust1, rust2);
2522 assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap()));
2523
2524 // Ensure language is still listed even after loading it.
2525 assert_eq!(
2526 languages.language_names(),
2527 &[
2528 LanguageName::new("JSON"),
2529 LanguageName::new("Plain Text"),
2530 LanguageName::new("Rust"),
2531 ]
2532 );
2533
2534 // Loading an unknown language returns an error.
2535 assert!(languages.language_for_name("Unknown").await.is_err());
2536 }
2537
2538 #[gpui::test]
2539 async fn test_completion_label_omits_duplicate_data() {
2540 let regular_completion_item_1 = lsp::CompletionItem {
2541 label: "regular1".to_string(),
2542 detail: Some("detail1".to_string()),
2543 label_details: Some(lsp::CompletionItemLabelDetails {
2544 detail: None,
2545 description: Some("description 1".to_string()),
2546 }),
2547 ..lsp::CompletionItem::default()
2548 };
2549
2550 let regular_completion_item_2 = lsp::CompletionItem {
2551 label: "regular2".to_string(),
2552 label_details: Some(lsp::CompletionItemLabelDetails {
2553 detail: None,
2554 description: Some("description 2".to_string()),
2555 }),
2556 ..lsp::CompletionItem::default()
2557 };
2558
2559 let completion_item_with_duplicate_detail_and_proper_description = lsp::CompletionItem {
2560 detail: Some(regular_completion_item_1.label.clone()),
2561 ..regular_completion_item_1.clone()
2562 };
2563
2564 let completion_item_with_duplicate_detail = lsp::CompletionItem {
2565 detail: Some(regular_completion_item_1.label.clone()),
2566 label_details: None,
2567 ..regular_completion_item_1.clone()
2568 };
2569
2570 let completion_item_with_duplicate_description = lsp::CompletionItem {
2571 label_details: Some(lsp::CompletionItemLabelDetails {
2572 detail: None,
2573 description: Some(regular_completion_item_2.label.clone()),
2574 }),
2575 ..regular_completion_item_2.clone()
2576 };
2577
2578 assert_eq!(
2579 CodeLabel::fallback_for_completion(®ular_completion_item_1, None).text,
2580 format!(
2581 "{} {}",
2582 regular_completion_item_1.label,
2583 regular_completion_item_1.detail.unwrap()
2584 ),
2585 "LSP completion items with both detail and label_details.description should prefer detail"
2586 );
2587 assert_eq!(
2588 CodeLabel::fallback_for_completion(®ular_completion_item_2, None).text,
2589 format!(
2590 "{} {}",
2591 regular_completion_item_2.label,
2592 regular_completion_item_2
2593 .label_details
2594 .as_ref()
2595 .unwrap()
2596 .description
2597 .as_ref()
2598 .unwrap()
2599 ),
2600 "LSP completion items without detail but with label_details.description should use that"
2601 );
2602 assert_eq!(
2603 CodeLabel::fallback_for_completion(
2604 &completion_item_with_duplicate_detail_and_proper_description,
2605 None
2606 )
2607 .text,
2608 format!(
2609 "{} {}",
2610 regular_completion_item_1.label,
2611 regular_completion_item_1
2612 .label_details
2613 .as_ref()
2614 .unwrap()
2615 .description
2616 .as_ref()
2617 .unwrap()
2618 ),
2619 "LSP completion items with both detail and label_details.description should prefer description only if the detail duplicates the completion label"
2620 );
2621 assert_eq!(
2622 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_detail, None).text,
2623 regular_completion_item_1.label,
2624 "LSP completion items with duplicate label and detail, should omit the detail"
2625 );
2626 assert_eq!(
2627 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_description, None)
2628 .text,
2629 regular_completion_item_2.label,
2630 "LSP completion items with duplicate label and detail, should omit the detail"
2631 );
2632 }
2633
2634 #[test]
2635 fn test_deserializing_comments_backwards_compat() {
2636 // current version of `block_comment` and `documentation_comment` work
2637 {
2638 let config: LanguageConfig = ::toml::from_str(
2639 r#"
2640 name = "Foo"
2641 block_comment = { start = "a", end = "b", prefix = "c", tab_size = 1 }
2642 documentation_comment = { start = "d", end = "e", prefix = "f", tab_size = 2 }
2643 "#,
2644 )
2645 .unwrap();
2646 assert_matches!(config.block_comment, Some(BlockCommentConfig { .. }));
2647 assert_matches!(
2648 config.documentation_comment,
2649 Some(BlockCommentConfig { .. })
2650 );
2651
2652 let block_config = config.block_comment.unwrap();
2653 assert_eq!(block_config.start.as_ref(), "a");
2654 assert_eq!(block_config.end.as_ref(), "b");
2655 assert_eq!(block_config.prefix.as_ref(), "c");
2656 assert_eq!(block_config.tab_size, 1);
2657
2658 let doc_config = config.documentation_comment.unwrap();
2659 assert_eq!(doc_config.start.as_ref(), "d");
2660 assert_eq!(doc_config.end.as_ref(), "e");
2661 assert_eq!(doc_config.prefix.as_ref(), "f");
2662 assert_eq!(doc_config.tab_size, 2);
2663 }
2664
2665 // former `documentation` setting is read into `documentation_comment`
2666 {
2667 let config: LanguageConfig = ::toml::from_str(
2668 r#"
2669 name = "Foo"
2670 documentation = { start = "a", end = "b", prefix = "c", tab_size = 1}
2671 "#,
2672 )
2673 .unwrap();
2674 assert_matches!(
2675 config.documentation_comment,
2676 Some(BlockCommentConfig { .. })
2677 );
2678
2679 let config = config.documentation_comment.unwrap();
2680 assert_eq!(config.start.as_ref(), "a");
2681 assert_eq!(config.end.as_ref(), "b");
2682 assert_eq!(config.prefix.as_ref(), "c");
2683 assert_eq!(config.tab_size, 1);
2684 }
2685
2686 // old block_comment format is read into BlockCommentConfig
2687 {
2688 let config: LanguageConfig = ::toml::from_str(
2689 r#"
2690 name = "Foo"
2691 block_comment = ["a", "b"]
2692 "#,
2693 )
2694 .unwrap();
2695 assert_matches!(config.block_comment, Some(BlockCommentConfig { .. }));
2696
2697 let config = config.block_comment.unwrap();
2698 assert_eq!(config.start.as_ref(), "a");
2699 assert_eq!(config.end.as_ref(), "b");
2700 assert_eq!(config.prefix.as_ref(), "");
2701 assert_eq!(config.tab_size, 0);
2702 }
2703 }
2704}