language.rs

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