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