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