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