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