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