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 it's API.
   9mod buffer;
  10mod diagnostic_set;
  11mod highlight_map;
  12pub mod language_settings;
  13mod outline;
  14pub mod proto;
  15mod syntax_map;
  16
  17#[cfg(test)]
  18mod buffer_tests;
  19pub mod markdown;
  20
  21use anyhow::{anyhow, Context, Result};
  22use async_trait::async_trait;
  23use collections::{HashMap, HashSet};
  24use futures::{
  25    channel::{mpsc, oneshot},
  26    future::Shared,
  27    FutureExt, TryFutureExt as _,
  28};
  29use gpui::{AppContext, AsyncAppContext, BackgroundExecutor, Task};
  30pub use highlight_map::HighlightMap;
  31use lazy_static::lazy_static;
  32use lsp::{CodeActionKind, LanguageServerBinary};
  33use parking_lot::{Mutex, RwLock};
  34use postage::watch;
  35use regex::Regex;
  36use serde::{de, Deserialize, Deserializer};
  37use serde_json::Value;
  38use std::{
  39    any::Any,
  40    borrow::Cow,
  41    cell::RefCell,
  42    fmt::Debug,
  43    hash::Hash,
  44    mem,
  45    ops::{Not, Range},
  46    path::{Path, PathBuf},
  47    str,
  48    sync::{
  49        atomic::{AtomicU64, AtomicUsize, Ordering::SeqCst},
  50        Arc,
  51    },
  52};
  53use syntax_map::SyntaxSnapshot;
  54use theme::{SyntaxTheme, Theme};
  55use tree_sitter::{self, Query};
  56use unicase::UniCase;
  57use util::{http::HttpClient, paths::PathExt};
  58use util::{post_inc, ResultExt, TryFutureExt as _, UnwrapFuture};
  59
  60pub use buffer::Operation;
  61pub use buffer::*;
  62pub use diagnostic_set::DiagnosticEntry;
  63pub use lsp::LanguageServerId;
  64pub use outline::{Outline, OutlineItem};
  65pub use syntax_map::{OwnedSyntaxLayer, SyntaxLayer};
  66pub use text::LineEnding;
  67pub use tree_sitter::{Parser, Tree};
  68
  69/// Initializes the `language` crate.
  70///
  71/// This should be called before making use of items from the create.
  72pub fn init(cx: &mut AppContext) {
  73    language_settings::init(cx);
  74}
  75
  76#[derive(Clone, Default)]
  77struct LspBinaryStatusSender {
  78    txs: Arc<Mutex<Vec<mpsc::UnboundedSender<(Arc<Language>, LanguageServerBinaryStatus)>>>>,
  79}
  80
  81impl LspBinaryStatusSender {
  82    fn subscribe(&self) -> mpsc::UnboundedReceiver<(Arc<Language>, LanguageServerBinaryStatus)> {
  83        let (tx, rx) = mpsc::unbounded();
  84        self.txs.lock().push(tx);
  85        rx
  86    }
  87
  88    fn send(&self, language: Arc<Language>, status: LanguageServerBinaryStatus) {
  89        let mut txs = self.txs.lock();
  90        txs.retain(|tx| {
  91            tx.unbounded_send((language.clone(), status.clone()))
  92                .is_ok()
  93        });
  94    }
  95}
  96
  97thread_local! {
  98    static PARSER: RefCell<Parser> = {
  99        RefCell::new(Parser::new())
 100    };
 101}
 102
 103lazy_static! {
 104    pub(crate) static ref NEXT_GRAMMAR_ID: AtomicUsize = Default::default();
 105    /// A shared grammar for plain text, exposed for reuse by downstream crates.
 106    #[doc(hidden)]
 107    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
 108        LanguageConfig {
 109            name: "Plain Text".into(),
 110            ..Default::default()
 111        },
 112        None,
 113    ));
 114}
 115
 116/// Types that represent a position in a buffer, and can be converted into
 117/// an LSP position, to send to a language server.
 118pub trait ToLspPosition {
 119    /// Converts the value into an LSP position.
 120    fn to_lsp_position(self) -> lsp::Position;
 121}
 122
 123/// A name of a language server.
 124#[derive(Clone, Debug, PartialEq, Eq, Hash)]
 125pub struct LanguageServerName(pub Arc<str>);
 126
 127/// Represents a Language Server, with certain cached sync properties.
 128/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
 129/// once at startup, and caches the results.
 130pub struct CachedLspAdapter {
 131    pub name: LanguageServerName,
 132    pub short_name: &'static str,
 133    pub disk_based_diagnostic_sources: Vec<String>,
 134    pub disk_based_diagnostics_progress_token: Option<String>,
 135    pub language_ids: HashMap<String, String>,
 136    pub adapter: Arc<dyn LspAdapter>,
 137    pub reinstall_attempt_count: AtomicU64,
 138}
 139
 140impl CachedLspAdapter {
 141    pub async fn new(adapter: Arc<dyn LspAdapter>) -> Arc<Self> {
 142        let name = adapter.name().await;
 143        let short_name = adapter.short_name();
 144        let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await;
 145        let disk_based_diagnostics_progress_token =
 146            adapter.disk_based_diagnostics_progress_token().await;
 147        let language_ids = adapter.language_ids().await;
 148
 149        Arc::new(CachedLspAdapter {
 150            name,
 151            short_name,
 152            disk_based_diagnostic_sources,
 153            disk_based_diagnostics_progress_token,
 154            language_ids,
 155            adapter,
 156            reinstall_attempt_count: AtomicU64::new(0),
 157        })
 158    }
 159
 160    pub async fn fetch_latest_server_version(
 161        &self,
 162        delegate: &dyn LspAdapterDelegate,
 163    ) -> Result<Box<dyn 'static + Send + Any>> {
 164        self.adapter.fetch_latest_server_version(delegate).await
 165    }
 166
 167    pub fn will_fetch_server(
 168        &self,
 169        delegate: &Arc<dyn LspAdapterDelegate>,
 170        cx: &mut AsyncAppContext,
 171    ) -> Option<Task<Result<()>>> {
 172        self.adapter.will_fetch_server(delegate, cx)
 173    }
 174
 175    pub fn will_start_server(
 176        &self,
 177        delegate: &Arc<dyn LspAdapterDelegate>,
 178        cx: &mut AsyncAppContext,
 179    ) -> Option<Task<Result<()>>> {
 180        self.adapter.will_start_server(delegate, cx)
 181    }
 182
 183    pub async fn fetch_server_binary(
 184        &self,
 185        version: Box<dyn 'static + Send + Any>,
 186        container_dir: PathBuf,
 187        delegate: &dyn LspAdapterDelegate,
 188    ) -> Result<LanguageServerBinary> {
 189        self.adapter
 190            .fetch_server_binary(version, container_dir, delegate)
 191            .await
 192    }
 193
 194    pub async fn cached_server_binary(
 195        &self,
 196        container_dir: PathBuf,
 197        delegate: &dyn LspAdapterDelegate,
 198    ) -> Option<LanguageServerBinary> {
 199        self.adapter
 200            .cached_server_binary(container_dir, delegate)
 201            .await
 202    }
 203
 204    pub fn can_be_reinstalled(&self) -> bool {
 205        self.adapter.can_be_reinstalled()
 206    }
 207
 208    pub async fn installation_test_binary(
 209        &self,
 210        container_dir: PathBuf,
 211    ) -> Option<LanguageServerBinary> {
 212        self.adapter.installation_test_binary(container_dir).await
 213    }
 214
 215    pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
 216        self.adapter.code_action_kinds()
 217    }
 218
 219    pub fn workspace_configuration(&self, workspace_root: &Path, cx: &mut AppContext) -> Value {
 220        self.adapter.workspace_configuration(workspace_root, cx)
 221    }
 222
 223    pub fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
 224        self.adapter.process_diagnostics(params)
 225    }
 226
 227    pub async fn process_completion(&self, completion_item: &mut lsp::CompletionItem) {
 228        self.adapter.process_completion(completion_item).await
 229    }
 230
 231    pub async fn label_for_completion(
 232        &self,
 233        completion_item: &lsp::CompletionItem,
 234        language: &Arc<Language>,
 235    ) -> Option<CodeLabel> {
 236        self.adapter
 237            .label_for_completion(completion_item, language)
 238            .await
 239    }
 240
 241    pub async fn label_for_symbol(
 242        &self,
 243        name: &str,
 244        kind: lsp::SymbolKind,
 245        language: &Arc<Language>,
 246    ) -> Option<CodeLabel> {
 247        self.adapter.label_for_symbol(name, kind, language).await
 248    }
 249
 250    pub fn prettier_plugins(&self) -> &[&'static str] {
 251        self.adapter.prettier_plugins()
 252    }
 253}
 254
 255/// [`LspAdapterDelegate`] allows [`LspAdapter]` implementations to interface with the application
 256// e.g. to display a notification or fetch data from the web.
 257pub trait LspAdapterDelegate: Send + Sync {
 258    fn show_notification(&self, message: &str, cx: &mut AppContext);
 259    fn http_client(&self) -> Arc<dyn HttpClient>;
 260}
 261
 262#[async_trait]
 263pub trait LspAdapter: 'static + Send + Sync {
 264    async fn name(&self) -> LanguageServerName;
 265
 266    fn short_name(&self) -> &'static str;
 267
 268    async fn fetch_latest_server_version(
 269        &self,
 270        delegate: &dyn LspAdapterDelegate,
 271    ) -> Result<Box<dyn 'static + Send + Any>>;
 272
 273    fn will_fetch_server(
 274        &self,
 275        _: &Arc<dyn LspAdapterDelegate>,
 276        _: &mut AsyncAppContext,
 277    ) -> Option<Task<Result<()>>> {
 278        None
 279    }
 280
 281    fn will_start_server(
 282        &self,
 283        _: &Arc<dyn LspAdapterDelegate>,
 284        _: &mut AsyncAppContext,
 285    ) -> Option<Task<Result<()>>> {
 286        None
 287    }
 288
 289    async fn fetch_server_binary(
 290        &self,
 291        version: Box<dyn 'static + Send + Any>,
 292        container_dir: PathBuf,
 293        delegate: &dyn LspAdapterDelegate,
 294    ) -> Result<LanguageServerBinary>;
 295
 296    async fn cached_server_binary(
 297        &self,
 298        container_dir: PathBuf,
 299        delegate: &dyn LspAdapterDelegate,
 300    ) -> Option<LanguageServerBinary>;
 301
 302    /// Returns true if a language server can be reinstalled.
 303    /// If language server initialization fails, a reinstallation will be attempted unless the value returned from this method is false.
 304    /// Implementations that rely on software already installed on user's system
 305    /// should have [`can_be_reinstalled`] return false.
 306    fn can_be_reinstalled(&self) -> bool {
 307        true
 308    }
 309
 310    async fn installation_test_binary(
 311        &self,
 312        container_dir: PathBuf,
 313    ) -> Option<LanguageServerBinary>;
 314
 315    fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
 316
 317    /// A callback called for each [`lsp_types::CompletionItem`] obtained from LSP server.
 318    /// Some LspAdapter implementations might want to modify the obtained item to
 319    /// change how it's displayed.
 320    async fn process_completion(&self, _: &mut lsp::CompletionItem) {}
 321
 322    async fn label_for_completion(
 323        &self,
 324        _: &lsp::CompletionItem,
 325        _: &Arc<Language>,
 326    ) -> Option<CodeLabel> {
 327        None
 328    }
 329
 330    async fn label_for_symbol(
 331        &self,
 332        _: &str,
 333        _: lsp::SymbolKind,
 334        _: &Arc<Language>,
 335    ) -> Option<CodeLabel> {
 336        None
 337    }
 338
 339    /// Returns initialization options that are going to be sent to a LSP server as a part of [`lsp_types::InitializeParams`]
 340    async fn initialization_options(&self) -> Option<Value> {
 341        None
 342    }
 343
 344    fn workspace_configuration(&self, _: &Path, _: &mut AppContext) -> Value {
 345        serde_json::json!({})
 346    }
 347
 348    /// Returns a list of code actions supported by a given LspAdapter
 349    fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
 350        Some(vec![
 351            CodeActionKind::EMPTY,
 352            CodeActionKind::QUICKFIX,
 353            CodeActionKind::REFACTOR,
 354            CodeActionKind::REFACTOR_EXTRACT,
 355            CodeActionKind::SOURCE,
 356        ])
 357    }
 358
 359    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
 360        Default::default()
 361    }
 362
 363    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
 364        None
 365    }
 366
 367    async fn language_ids(&self) -> HashMap<String, String> {
 368        Default::default()
 369    }
 370
 371    fn prettier_plugins(&self) -> &[&'static str] {
 372        &[]
 373    }
 374}
 375
 376#[derive(Clone, Debug, PartialEq, Eq)]
 377pub struct CodeLabel {
 378    /// The text to display.
 379    pub text: String,
 380    /// Syntax highlighting runs.
 381    pub runs: Vec<(Range<usize>, HighlightId)>,
 382    /// The portion of the text that should be used in fuzzy filtering.
 383    pub filter_range: Range<usize>,
 384}
 385
 386#[derive(Clone, Deserialize)]
 387pub struct LanguageConfig {
 388    /// Human-readable name of the language.
 389    pub name: Arc<str>,
 390    // The name of the grammar in a WASM bundle (experimental).
 391    pub grammar_name: Option<Arc<str>>,
 392    /// 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`.
 393    pub path_suffixes: Vec<String>,
 394    /// List of bracket types in a language.
 395    pub brackets: BracketPairConfig,
 396    /// A regex pattern that determines whether the language should be assigned to a file or not.
 397    #[serde(default, deserialize_with = "deserialize_regex")]
 398    pub first_line_pattern: Option<Regex>,
 399    /// If set to true, auto indentation uses last non empty line to determine
 400    /// the indentation level for a new line.
 401    #[serde(default = "auto_indent_using_last_non_empty_line_default")]
 402    pub auto_indent_using_last_non_empty_line: bool,
 403    /// A regex that is used to determine whether the indentation level should be
 404    /// increased in the following line.
 405    #[serde(default, deserialize_with = "deserialize_regex")]
 406    pub increase_indent_pattern: Option<Regex>,
 407    /// A regex that is used to determine whether the indentation level should be
 408    /// decreased in the following line.
 409    #[serde(default, deserialize_with = "deserialize_regex")]
 410    pub decrease_indent_pattern: Option<Regex>,
 411    /// A list of characters that trigger the automatic insertion of a closing
 412    /// bracket when they immediately precede the point where an opening
 413    /// bracket is inserted.
 414    #[serde(default)]
 415    pub autoclose_before: String,
 416    /// A placeholder used internally by Semantic Index.
 417    #[serde(default)]
 418    pub collapsed_placeholder: String,
 419    /// A line comment string that is inserted in e.g. `toggle comments` action.
 420    #[serde(default)]
 421    pub line_comment: Option<Arc<str>>,
 422    /// Starting and closing characters of a block comment.
 423    #[serde(default)]
 424    pub block_comment: Option<(Arc<str>, Arc<str>)>,
 425    /// A list of language servers that are allowed to run on subranges of a given language.
 426    #[serde(default)]
 427    pub scope_opt_in_language_servers: Vec<String>,
 428    #[serde(default)]
 429    pub overrides: HashMap<String, LanguageConfigOverride>,
 430    /// A list of characters that Zed should treat as word characters for the
 431    /// purpose of features that operate on word boundaries, like 'move to next word end'
 432    /// or a whole-word search in buffer search.
 433    #[serde(default)]
 434    pub word_characters: HashSet<char>,
 435    /// The name of a Prettier parser that should be used for this language.
 436    #[serde(default)]
 437    pub prettier_parser_name: Option<String>,
 438}
 439
 440/// Tree-sitter language queries for a given language.
 441#[derive(Debug, Default)]
 442pub struct LanguageQueries {
 443    pub highlights: Option<Cow<'static, str>>,
 444    pub brackets: Option<Cow<'static, str>>,
 445    pub indents: Option<Cow<'static, str>>,
 446    pub outline: Option<Cow<'static, str>>,
 447    pub embedding: Option<Cow<'static, str>>,
 448    pub injections: Option<Cow<'static, str>>,
 449    pub overrides: Option<Cow<'static, str>>,
 450}
 451
 452/// Represents a language for the given range. Some languages (e.g. HTML)
 453/// interleave several languages together, thus a single buffer might actually contain
 454/// several nested scopes.
 455#[derive(Clone, Debug)]
 456pub struct LanguageScope {
 457    language: Arc<Language>,
 458    override_id: Option<u32>,
 459}
 460
 461#[derive(Clone, Deserialize, Default, Debug)]
 462pub struct LanguageConfigOverride {
 463    #[serde(default)]
 464    pub line_comment: Override<Arc<str>>,
 465    #[serde(default)]
 466    pub block_comment: Override<(Arc<str>, Arc<str>)>,
 467    #[serde(skip_deserializing)]
 468    pub disabled_bracket_ixs: Vec<u16>,
 469    #[serde(default)]
 470    pub word_characters: Override<HashSet<char>>,
 471    #[serde(default)]
 472    pub opt_into_language_servers: Vec<String>,
 473}
 474
 475#[derive(Clone, Deserialize, Debug)]
 476#[serde(untagged)]
 477pub enum Override<T> {
 478    Remove { remove: bool },
 479    Set(T),
 480}
 481
 482impl<T> Default for Override<T> {
 483    fn default() -> Self {
 484        Override::Remove { remove: false }
 485    }
 486}
 487
 488impl<T> Override<T> {
 489    fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> {
 490        match this {
 491            Some(Self::Set(value)) => Some(value),
 492            Some(Self::Remove { remove: true }) => None,
 493            Some(Self::Remove { remove: false }) | None => original,
 494        }
 495    }
 496}
 497
 498impl Default for LanguageConfig {
 499    fn default() -> Self {
 500        Self {
 501            name: "".into(),
 502            grammar_name: None,
 503            path_suffixes: Default::default(),
 504            brackets: Default::default(),
 505            auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
 506            first_line_pattern: Default::default(),
 507            increase_indent_pattern: Default::default(),
 508            decrease_indent_pattern: Default::default(),
 509            autoclose_before: Default::default(),
 510            line_comment: Default::default(),
 511            block_comment: Default::default(),
 512            scope_opt_in_language_servers: Default::default(),
 513            overrides: Default::default(),
 514            word_characters: Default::default(),
 515            prettier_parser_name: None,
 516            collapsed_placeholder: Default::default(),
 517        }
 518    }
 519}
 520
 521fn auto_indent_using_last_non_empty_line_default() -> bool {
 522    true
 523}
 524
 525fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
 526    let source = Option::<String>::deserialize(d)?;
 527    if let Some(source) = source {
 528        Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
 529    } else {
 530        Ok(None)
 531    }
 532}
 533
 534#[doc(hidden)]
 535#[cfg(any(test, feature = "test-support"))]
 536pub struct FakeLspAdapter {
 537    pub name: &'static str,
 538    pub initialization_options: Option<Value>,
 539    pub capabilities: lsp::ServerCapabilities,
 540    pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
 541    pub disk_based_diagnostics_progress_token: Option<String>,
 542    pub disk_based_diagnostics_sources: Vec<String>,
 543    pub prettier_plugins: Vec<&'static str>,
 544}
 545
 546/// Configuration of handling bracket pairs for a given language.
 547///
 548/// This struct includes settings for defining which pairs of characters are considered brackets and
 549/// also specifies any language-specific scopes where these pairs should be ignored for bracket matching purposes.
 550#[derive(Clone, Debug, Default)]
 551pub struct BracketPairConfig {
 552    /// A list of character pairs that should be treated as brackets in the context of a given language.
 553    pub pairs: Vec<BracketPair>,
 554    /// A list of tree-sitter scopes for which a given bracket should not be active.
 555    /// N-th entry in `[Self::disabled_scopes_by_bracket_ix]` contains a list of disabled scopes for an n-th entry in `[Self::pairs]`
 556    pub disabled_scopes_by_bracket_ix: Vec<Vec<String>>,
 557}
 558
 559impl<'de> Deserialize<'de> for BracketPairConfig {
 560    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
 561    where
 562        D: Deserializer<'de>,
 563    {
 564        #[derive(Deserialize)]
 565        pub struct Entry {
 566            #[serde(flatten)]
 567            pub bracket_pair: BracketPair,
 568            #[serde(default)]
 569            pub not_in: Vec<String>,
 570        }
 571
 572        let result = Vec::<Entry>::deserialize(deserializer)?;
 573        let mut brackets = Vec::with_capacity(result.len());
 574        let mut disabled_scopes_by_bracket_ix = Vec::with_capacity(result.len());
 575        for entry in result {
 576            brackets.push(entry.bracket_pair);
 577            disabled_scopes_by_bracket_ix.push(entry.not_in);
 578        }
 579
 580        Ok(BracketPairConfig {
 581            pairs: brackets,
 582            disabled_scopes_by_bracket_ix,
 583        })
 584    }
 585}
 586
 587/// Describes a single bracket pair and how an editor should react to e.g. inserting
 588/// an opening bracket or to a newline character insertion inbetween `start` and `end` characters.
 589#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
 590pub struct BracketPair {
 591    /// Starting substring for a bracket.
 592    pub start: String,
 593    /// Ending substring for a bracket.
 594    pub end: String,
 595    /// True if `end` should be automatically inserted right after `start` characters.
 596    pub close: bool,
 597    /// True if an extra newline should be inserted while the cursor is in the middle
 598    /// of that bracket pair.
 599    pub newline: bool,
 600}
 601
 602pub struct Language {
 603    pub(crate) config: LanguageConfig,
 604    pub(crate) grammar: Option<Arc<Grammar>>,
 605    pub(crate) adapters: Vec<Arc<CachedLspAdapter>>,
 606
 607    #[cfg(any(test, feature = "test-support"))]
 608    fake_adapter: Option<(
 609        mpsc::UnboundedSender<lsp::FakeLanguageServer>,
 610        Arc<FakeLspAdapter>,
 611    )>,
 612}
 613
 614pub struct Grammar {
 615    id: usize,
 616    pub ts_language: tree_sitter::Language,
 617    pub(crate) error_query: Query,
 618    pub(crate) highlights_query: Option<Query>,
 619    pub(crate) brackets_config: Option<BracketConfig>,
 620    pub(crate) indents_config: Option<IndentConfig>,
 621    pub outline_config: Option<OutlineConfig>,
 622    pub embedding_config: Option<EmbeddingConfig>,
 623    pub(crate) injection_config: Option<InjectionConfig>,
 624    pub(crate) override_config: Option<OverrideConfig>,
 625    pub(crate) highlight_map: Mutex<HighlightMap>,
 626}
 627
 628struct IndentConfig {
 629    query: Query,
 630    indent_capture_ix: u32,
 631    start_capture_ix: Option<u32>,
 632    end_capture_ix: Option<u32>,
 633    outdent_capture_ix: Option<u32>,
 634}
 635
 636pub struct OutlineConfig {
 637    pub query: Query,
 638    pub item_capture_ix: u32,
 639    pub name_capture_ix: u32,
 640    pub context_capture_ix: Option<u32>,
 641    pub extra_context_capture_ix: Option<u32>,
 642}
 643
 644#[derive(Debug)]
 645pub struct EmbeddingConfig {
 646    pub query: Query,
 647    pub item_capture_ix: u32,
 648    pub name_capture_ix: Option<u32>,
 649    pub context_capture_ix: Option<u32>,
 650    pub collapse_capture_ix: Option<u32>,
 651    pub keep_capture_ix: Option<u32>,
 652}
 653
 654struct InjectionConfig {
 655    query: Query,
 656    content_capture_ix: u32,
 657    language_capture_ix: Option<u32>,
 658    patterns: Vec<InjectionPatternConfig>,
 659}
 660
 661struct OverrideConfig {
 662    query: Query,
 663    values: HashMap<u32, (String, LanguageConfigOverride)>,
 664}
 665
 666#[derive(Default, Clone)]
 667struct InjectionPatternConfig {
 668    language: Option<Box<str>>,
 669    combined: bool,
 670}
 671
 672struct BracketConfig {
 673    query: Query,
 674    open_capture_ix: u32,
 675    close_capture_ix: u32,
 676}
 677
 678#[derive(Clone)]
 679pub enum LanguageServerBinaryStatus {
 680    CheckingForUpdate,
 681    Downloading,
 682    Downloaded,
 683    Cached,
 684    Failed { error: String },
 685}
 686
 687type AvailableLanguageId = usize;
 688
 689#[derive(Clone)]
 690struct AvailableLanguage {
 691    id: AvailableLanguageId,
 692    config: LanguageConfig,
 693    grammar: AvailableGrammar,
 694    lsp_adapters: Vec<Arc<dyn LspAdapter>>,
 695    loaded: bool,
 696}
 697
 698#[derive(Clone)]
 699enum AvailableGrammar {
 700    Native {
 701        grammar: tree_sitter::Language,
 702        asset_dir: &'static str,
 703        get_queries: fn(&str) -> LanguageQueries,
 704    },
 705    Wasm {
 706        _grammar_name: Arc<str>,
 707        _path: Arc<Path>,
 708    },
 709}
 710
 711pub struct LanguageRegistry {
 712    state: RwLock<LanguageRegistryState>,
 713    language_server_download_dir: Option<Arc<Path>>,
 714    login_shell_env_loaded: Shared<Task<()>>,
 715    #[allow(clippy::type_complexity)]
 716    lsp_binary_paths: Mutex<
 717        HashMap<LanguageServerName, Shared<Task<Result<LanguageServerBinary, Arc<anyhow::Error>>>>>,
 718    >,
 719    executor: Option<BackgroundExecutor>,
 720    lsp_binary_status_tx: LspBinaryStatusSender,
 721}
 722
 723struct LanguageRegistryState {
 724    next_language_server_id: usize,
 725    languages: Vec<Arc<Language>>,
 726    available_languages: Vec<AvailableLanguage>,
 727    next_available_language_id: AvailableLanguageId,
 728    loading_languages: HashMap<AvailableLanguageId, Vec<oneshot::Sender<Result<Arc<Language>>>>>,
 729    subscription: (watch::Sender<()>, watch::Receiver<()>),
 730    theme: Option<Arc<Theme>>,
 731    version: usize,
 732    reload_count: usize,
 733}
 734
 735pub struct PendingLanguageServer {
 736    pub server_id: LanguageServerId,
 737    pub task: Task<Result<lsp::LanguageServer>>,
 738    pub container_dir: Option<Arc<Path>>,
 739}
 740
 741impl LanguageRegistry {
 742    pub fn new(login_shell_env_loaded: Task<()>) -> Self {
 743        Self {
 744            state: RwLock::new(LanguageRegistryState {
 745                next_language_server_id: 0,
 746                languages: vec![PLAIN_TEXT.clone()],
 747                available_languages: Default::default(),
 748                next_available_language_id: 0,
 749                loading_languages: Default::default(),
 750                subscription: watch::channel(),
 751                theme: Default::default(),
 752                version: 0,
 753                reload_count: 0,
 754            }),
 755            language_server_download_dir: None,
 756            login_shell_env_loaded: login_shell_env_loaded.shared(),
 757            lsp_binary_paths: Default::default(),
 758            executor: None,
 759            lsp_binary_status_tx: Default::default(),
 760        }
 761    }
 762
 763    #[cfg(any(test, feature = "test-support"))]
 764    pub fn test() -> Self {
 765        Self::new(Task::ready(()))
 766    }
 767
 768    pub fn set_executor(&mut self, executor: BackgroundExecutor) {
 769        self.executor = Some(executor);
 770    }
 771
 772    /// Clear out all of the loaded languages and reload them from scratch.
 773    ///
 774    /// This is useful in development, when queries have changed.
 775    #[cfg(debug_assertions)]
 776    pub fn reload(&self) {
 777        self.state.write().reload();
 778    }
 779
 780    pub fn register(
 781        &self,
 782        asset_dir: &'static str,
 783        config: LanguageConfig,
 784        grammar: tree_sitter::Language,
 785        lsp_adapters: Vec<Arc<dyn LspAdapter>>,
 786        get_queries: fn(&str) -> LanguageQueries,
 787    ) {
 788        let state = &mut *self.state.write();
 789        state.available_languages.push(AvailableLanguage {
 790            id: post_inc(&mut state.next_available_language_id),
 791            config,
 792            grammar: AvailableGrammar::Native {
 793                grammar,
 794                get_queries,
 795                asset_dir,
 796            },
 797            lsp_adapters,
 798            loaded: false,
 799        });
 800    }
 801
 802    pub fn register_wasm(&self, path: Arc<Path>, grammar_name: Arc<str>, config: LanguageConfig) {
 803        let state = &mut *self.state.write();
 804        state.available_languages.push(AvailableLanguage {
 805            id: post_inc(&mut state.next_available_language_id),
 806            config,
 807            grammar: AvailableGrammar::Wasm {
 808                _grammar_name: grammar_name,
 809                _path: path,
 810            },
 811            lsp_adapters: Vec::new(),
 812            loaded: false,
 813        });
 814    }
 815
 816    pub fn language_names(&self) -> Vec<String> {
 817        let state = self.state.read();
 818        let mut result = state
 819            .available_languages
 820            .iter()
 821            .filter_map(|l| l.loaded.not().then_some(l.config.name.to_string()))
 822            .chain(state.languages.iter().map(|l| l.config.name.to_string()))
 823            .collect::<Vec<_>>();
 824        result.sort_unstable_by_key(|language_name| language_name.to_lowercase());
 825        result
 826    }
 827
 828    pub fn add(&self, language: Arc<Language>) {
 829        self.state.write().add(language);
 830    }
 831
 832    pub fn subscribe(&self) -> watch::Receiver<()> {
 833        self.state.read().subscription.1.clone()
 834    }
 835
 836    /// The number of times that the registry has been changed,
 837    /// by adding languages or reloading.
 838    pub fn version(&self) -> usize {
 839        self.state.read().version
 840    }
 841
 842    /// The number of times that the registry has been reloaded.
 843    pub fn reload_count(&self) -> usize {
 844        self.state.read().reload_count
 845    }
 846
 847    pub fn set_theme(&self, theme: Arc<Theme>) {
 848        let mut state = self.state.write();
 849        state.theme = Some(theme.clone());
 850        for language in &state.languages {
 851            language.set_theme(theme.syntax());
 852        }
 853    }
 854
 855    pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
 856        self.language_server_download_dir = Some(path.into());
 857    }
 858
 859    pub fn language_for_name(
 860        self: &Arc<Self>,
 861        name: &str,
 862    ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
 863        let name = UniCase::new(name);
 864        self.get_or_load_language(|config| UniCase::new(config.name.as_ref()) == name)
 865    }
 866
 867    pub fn language_for_name_or_extension(
 868        self: &Arc<Self>,
 869        string: &str,
 870    ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
 871        let string = UniCase::new(string);
 872        self.get_or_load_language(|config| {
 873            UniCase::new(config.name.as_ref()) == string
 874                || config
 875                    .path_suffixes
 876                    .iter()
 877                    .any(|suffix| UniCase::new(suffix) == string)
 878        })
 879    }
 880
 881    pub fn language_for_file(
 882        self: &Arc<Self>,
 883        path: impl AsRef<Path>,
 884        content: Option<&Rope>,
 885    ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
 886        let path = path.as_ref();
 887        let filename = path.file_name().and_then(|name| name.to_str());
 888        let extension = path.extension_or_hidden_file_name();
 889        let path_suffixes = [extension, filename];
 890        self.get_or_load_language(|config| {
 891            let path_matches = config
 892                .path_suffixes
 893                .iter()
 894                .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())));
 895            let content_matches = content.zip(config.first_line_pattern.as_ref()).map_or(
 896                false,
 897                |(content, pattern)| {
 898                    let end = content.clip_point(Point::new(0, 256), Bias::Left);
 899                    let end = content.point_to_offset(end);
 900                    let text = content.chunks_in_range(0..end).collect::<String>();
 901                    pattern.is_match(&text)
 902                },
 903            );
 904            path_matches || content_matches
 905        })
 906    }
 907
 908    fn get_or_load_language(
 909        self: &Arc<Self>,
 910        callback: impl Fn(&LanguageConfig) -> bool,
 911    ) -> UnwrapFuture<oneshot::Receiver<Result<Arc<Language>>>> {
 912        let (tx, rx) = oneshot::channel();
 913
 914        let mut state = self.state.write();
 915        if let Some(language) = state
 916            .languages
 917            .iter()
 918            .find(|language| callback(&language.config))
 919        {
 920            let _ = tx.send(Ok(language.clone()));
 921        } else if let Some(executor) = self.executor.clone() {
 922            if let Some(language) = state
 923                .available_languages
 924                .iter()
 925                .find(|l| !l.loaded && callback(&l.config))
 926                .cloned()
 927            {
 928                let txs = state
 929                    .loading_languages
 930                    .entry(language.id)
 931                    .or_insert_with(|| {
 932                        let this = self.clone();
 933                        executor
 934                            .spawn(async move {
 935                                let id = language.id;
 936                                let name = language.config.name.clone();
 937                                let language = async {
 938                                    let (grammar, queries) = match language.grammar {
 939                                        AvailableGrammar::Native {
 940                                            grammar,
 941                                            asset_dir,
 942                                            get_queries,
 943                                        } => (grammar, (get_queries)(asset_dir)),
 944                                        AvailableGrammar::Wasm { .. } => {
 945                                            Err(anyhow!("not supported"))?
 946                                        }
 947                                    };
 948                                    Language::new(language.config, Some(grammar))
 949                                        .with_lsp_adapters(language.lsp_adapters)
 950                                        .await
 951                                        .with_queries(queries)
 952                                }
 953                                .await;
 954
 955                                match language {
 956                                    Ok(language) => {
 957                                        let language = Arc::new(language);
 958                                        let mut state = this.state.write();
 959
 960                                        state.add(language.clone());
 961                                        state.mark_language_loaded(id);
 962                                        if let Some(mut txs) = state.loading_languages.remove(&id) {
 963                                            for tx in txs.drain(..) {
 964                                                let _ = tx.send(Ok(language.clone()));
 965                                            }
 966                                        }
 967                                    }
 968                                    Err(e) => {
 969                                        log::error!("failed to load language {name}:\n{:?}", e);
 970                                        let mut state = this.state.write();
 971                                        state.mark_language_loaded(id);
 972                                        if let Some(mut txs) = state.loading_languages.remove(&id) {
 973                                            for tx in txs.drain(..) {
 974                                                let _ = tx.send(Err(anyhow!(
 975                                                    "failed to load language {}: {}",
 976                                                    name,
 977                                                    e
 978                                                )));
 979                                            }
 980                                        }
 981                                    }
 982                                };
 983                            })
 984                            .detach();
 985
 986                        Vec::new()
 987                    });
 988                txs.push(tx);
 989            } else {
 990                let _ = tx.send(Err(anyhow!("language not found")));
 991            }
 992        } else {
 993            let _ = tx.send(Err(anyhow!("executor does not exist")));
 994        }
 995
 996        rx.unwrap()
 997    }
 998
 999    pub fn to_vec(&self) -> Vec<Arc<Language>> {
1000        self.state.read().languages.iter().cloned().collect()
1001    }
1002
1003    pub fn create_pending_language_server(
1004        self: &Arc<Self>,
1005        stderr_capture: Arc<Mutex<Option<String>>>,
1006        language: Arc<Language>,
1007        adapter: Arc<CachedLspAdapter>,
1008        root_path: Arc<Path>,
1009        delegate: Arc<dyn LspAdapterDelegate>,
1010        cx: &mut AppContext,
1011    ) -> Option<PendingLanguageServer> {
1012        let server_id = self.state.write().next_language_server_id();
1013        log::info!(
1014            "starting language server {:?}, path: {root_path:?}, id: {server_id}",
1015            adapter.name.0
1016        );
1017
1018        #[cfg(any(test, feature = "test-support"))]
1019        if language.fake_adapter.is_some() {
1020            let task = cx.spawn(|cx| async move {
1021                let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
1022                let (server, mut fake_server) = lsp::FakeLanguageServer::new(
1023                    fake_adapter.name.to_string(),
1024                    fake_adapter.capabilities.clone(),
1025                    cx.clone(),
1026                );
1027
1028                if let Some(initializer) = &fake_adapter.initializer {
1029                    initializer(&mut fake_server);
1030                }
1031
1032                let servers_tx = servers_tx.clone();
1033                cx.background_executor()
1034                    .spawn(async move {
1035                        if fake_server
1036                            .try_receive_notification::<lsp::notification::Initialized>()
1037                            .await
1038                            .is_some()
1039                        {
1040                            servers_tx.unbounded_send(fake_server).ok();
1041                        }
1042                    })
1043                    .detach();
1044
1045                Ok(server)
1046            });
1047
1048            return Some(PendingLanguageServer {
1049                server_id,
1050                task,
1051                container_dir: None,
1052            });
1053        }
1054
1055        let download_dir = self
1056            .language_server_download_dir
1057            .clone()
1058            .ok_or_else(|| anyhow!("language server download directory has not been assigned before starting server"))
1059            .log_err()?;
1060        let this = self.clone();
1061        let language = language.clone();
1062        let container_dir: Arc<Path> = Arc::from(download_dir.join(adapter.name.0.as_ref()));
1063        let root_path = root_path.clone();
1064        let adapter = adapter.clone();
1065        let login_shell_env_loaded = self.login_shell_env_loaded.clone();
1066        let lsp_binary_statuses = self.lsp_binary_status_tx.clone();
1067
1068        let task = {
1069            let container_dir = container_dir.clone();
1070            cx.spawn(move |mut cx| async move {
1071                login_shell_env_loaded.await;
1072
1073                let entry = this
1074                    .lsp_binary_paths
1075                    .lock()
1076                    .entry(adapter.name.clone())
1077                    .or_insert_with(|| {
1078                        let adapter = adapter.clone();
1079                        let language = language.clone();
1080                        let delegate = delegate.clone();
1081                        cx.spawn(|cx| {
1082                            get_binary(
1083                                adapter,
1084                                language,
1085                                delegate,
1086                                container_dir,
1087                                lsp_binary_statuses,
1088                                cx,
1089                            )
1090                            .map_err(Arc::new)
1091                        })
1092                        .shared()
1093                    })
1094                    .clone();
1095
1096                let binary = match entry.await {
1097                    Ok(binary) => binary,
1098                    Err(err) => anyhow::bail!("{err}"),
1099                };
1100
1101                if let Some(task) = adapter.will_start_server(&delegate, &mut cx) {
1102                    task.await?;
1103                }
1104
1105                lsp::LanguageServer::new(
1106                    stderr_capture,
1107                    server_id,
1108                    binary,
1109                    &root_path,
1110                    adapter.code_action_kinds(),
1111                    cx,
1112                )
1113            })
1114        };
1115
1116        Some(PendingLanguageServer {
1117            server_id,
1118            task,
1119            container_dir: Some(container_dir),
1120        })
1121    }
1122
1123    pub fn language_server_binary_statuses(
1124        &self,
1125    ) -> mpsc::UnboundedReceiver<(Arc<Language>, LanguageServerBinaryStatus)> {
1126        self.lsp_binary_status_tx.subscribe()
1127    }
1128
1129    pub fn delete_server_container(
1130        &self,
1131        adapter: Arc<CachedLspAdapter>,
1132        cx: &mut AppContext,
1133    ) -> Task<()> {
1134        log::info!("deleting server container");
1135
1136        let mut lock = self.lsp_binary_paths.lock();
1137        lock.remove(&adapter.name);
1138
1139        let download_dir = self
1140            .language_server_download_dir
1141            .clone()
1142            .expect("language server download directory has not been assigned before deleting server container");
1143
1144        cx.spawn(|_| async move {
1145            let container_dir = download_dir.join(adapter.name.0.as_ref());
1146            smol::fs::remove_dir_all(container_dir)
1147                .await
1148                .context("server container removal")
1149                .log_err();
1150        })
1151    }
1152
1153    pub fn next_language_server_id(&self) -> LanguageServerId {
1154        self.state.write().next_language_server_id()
1155    }
1156}
1157
1158impl LanguageRegistryState {
1159    fn next_language_server_id(&mut self) -> LanguageServerId {
1160        LanguageServerId(post_inc(&mut self.next_language_server_id))
1161    }
1162
1163    fn add(&mut self, language: Arc<Language>) {
1164        if let Some(theme) = self.theme.as_ref() {
1165            language.set_theme(theme.syntax());
1166        }
1167        self.languages.push(language);
1168        self.version += 1;
1169        *self.subscription.0.borrow_mut() = ();
1170    }
1171
1172    #[cfg(debug_assertions)]
1173    fn reload(&mut self) {
1174        self.languages.clear();
1175        self.version += 1;
1176        self.reload_count += 1;
1177        for language in &mut self.available_languages {
1178            language.loaded = false;
1179        }
1180        *self.subscription.0.borrow_mut() = ();
1181    }
1182
1183    /// Mark the given language a having been loaded, so that the
1184    /// language registry won't try to load it again.
1185    fn mark_language_loaded(&mut self, id: AvailableLanguageId) {
1186        for language in &mut self.available_languages {
1187            if language.id == id {
1188                language.loaded = true;
1189                break;
1190            }
1191        }
1192    }
1193}
1194
1195#[cfg(any(test, feature = "test-support"))]
1196impl Default for LanguageRegistry {
1197    fn default() -> Self {
1198        Self::test()
1199    }
1200}
1201
1202async fn get_binary(
1203    adapter: Arc<CachedLspAdapter>,
1204    language: Arc<Language>,
1205    delegate: Arc<dyn LspAdapterDelegate>,
1206    container_dir: Arc<Path>,
1207    statuses: LspBinaryStatusSender,
1208    mut cx: AsyncAppContext,
1209) -> Result<LanguageServerBinary> {
1210    if !container_dir.exists() {
1211        smol::fs::create_dir_all(&container_dir)
1212            .await
1213            .context("failed to create container directory")?;
1214    }
1215
1216    if let Some(task) = adapter.will_fetch_server(&delegate, &mut cx) {
1217        task.await?;
1218    }
1219
1220    let binary = fetch_latest_binary(
1221        adapter.clone(),
1222        language.clone(),
1223        delegate.as_ref(),
1224        &container_dir,
1225        statuses.clone(),
1226    )
1227    .await;
1228
1229    if let Err(error) = binary.as_ref() {
1230        if let Some(binary) = adapter
1231            .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref())
1232            .await
1233        {
1234            statuses.send(language.clone(), LanguageServerBinaryStatus::Cached);
1235            return Ok(binary);
1236        } else {
1237            statuses.send(
1238                language.clone(),
1239                LanguageServerBinaryStatus::Failed {
1240                    error: format!("{:?}", error),
1241                },
1242            );
1243        }
1244    }
1245
1246    binary
1247}
1248
1249async fn fetch_latest_binary(
1250    adapter: Arc<CachedLspAdapter>,
1251    language: Arc<Language>,
1252    delegate: &dyn LspAdapterDelegate,
1253    container_dir: &Path,
1254    lsp_binary_statuses_tx: LspBinaryStatusSender,
1255) -> Result<LanguageServerBinary> {
1256    let container_dir: Arc<Path> = container_dir.into();
1257    lsp_binary_statuses_tx.send(
1258        language.clone(),
1259        LanguageServerBinaryStatus::CheckingForUpdate,
1260    );
1261
1262    let version_info = adapter.fetch_latest_server_version(delegate).await?;
1263    lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloading);
1264
1265    let binary = adapter
1266        .fetch_server_binary(version_info, container_dir.to_path_buf(), delegate)
1267        .await?;
1268    lsp_binary_statuses_tx.send(language.clone(), LanguageServerBinaryStatus::Downloaded);
1269
1270    Ok(binary)
1271}
1272
1273impl Language {
1274    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
1275        Self {
1276            config,
1277            grammar: ts_language.map(|ts_language| {
1278                Arc::new(Grammar {
1279                    id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst),
1280                    highlights_query: None,
1281                    brackets_config: None,
1282                    outline_config: None,
1283                    embedding_config: None,
1284                    indents_config: None,
1285                    injection_config: None,
1286                    override_config: None,
1287                    error_query: Query::new(&ts_language, "(ERROR) @error").unwrap(),
1288                    ts_language,
1289                    highlight_map: Default::default(),
1290                })
1291            }),
1292            adapters: Vec::new(),
1293
1294            #[cfg(any(test, feature = "test-support"))]
1295            fake_adapter: None,
1296        }
1297    }
1298
1299    pub fn lsp_adapters(&self) -> &[Arc<CachedLspAdapter>] {
1300        &self.adapters
1301    }
1302
1303    pub fn id(&self) -> Option<usize> {
1304        self.grammar.as_ref().map(|g| g.id)
1305    }
1306
1307    pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
1308        if let Some(query) = queries.highlights {
1309            self = self
1310                .with_highlights_query(query.as_ref())
1311                .context("Error loading highlights query")?;
1312        }
1313        if let Some(query) = queries.brackets {
1314            self = self
1315                .with_brackets_query(query.as_ref())
1316                .context("Error loading brackets query")?;
1317        }
1318        if let Some(query) = queries.indents {
1319            self = self
1320                .with_indents_query(query.as_ref())
1321                .context("Error loading indents query")?;
1322        }
1323        if let Some(query) = queries.outline {
1324            self = self
1325                .with_outline_query(query.as_ref())
1326                .context("Error loading outline query")?;
1327        }
1328        if let Some(query) = queries.embedding {
1329            self = self
1330                .with_embedding_query(query.as_ref())
1331                .context("Error loading embedding query")?;
1332        }
1333        if let Some(query) = queries.injections {
1334            self = self
1335                .with_injection_query(query.as_ref())
1336                .context("Error loading injection query")?;
1337        }
1338        if let Some(query) = queries.overrides {
1339            self = self
1340                .with_override_query(query.as_ref())
1341                .context("Error loading override query")?;
1342        }
1343        Ok(self)
1344    }
1345
1346    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
1347        let grammar = self.grammar_mut();
1348        grammar.highlights_query = Some(Query::new(&grammar.ts_language, source)?);
1349        Ok(self)
1350    }
1351
1352    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
1353        let grammar = self.grammar_mut();
1354        let query = Query::new(&grammar.ts_language, source)?;
1355        let mut item_capture_ix = None;
1356        let mut name_capture_ix = None;
1357        let mut context_capture_ix = None;
1358        let mut extra_context_capture_ix = None;
1359        get_capture_indices(
1360            &query,
1361            &mut [
1362                ("item", &mut item_capture_ix),
1363                ("name", &mut name_capture_ix),
1364                ("context", &mut context_capture_ix),
1365                ("context.extra", &mut extra_context_capture_ix),
1366            ],
1367        );
1368        if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
1369            grammar.outline_config = Some(OutlineConfig {
1370                query,
1371                item_capture_ix,
1372                name_capture_ix,
1373                context_capture_ix,
1374                extra_context_capture_ix,
1375            });
1376        }
1377        Ok(self)
1378    }
1379
1380    pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
1381        let grammar = self.grammar_mut();
1382        let query = Query::new(&grammar.ts_language, source)?;
1383        let mut item_capture_ix = None;
1384        let mut name_capture_ix = None;
1385        let mut context_capture_ix = None;
1386        let mut collapse_capture_ix = None;
1387        let mut keep_capture_ix = None;
1388        get_capture_indices(
1389            &query,
1390            &mut [
1391                ("item", &mut item_capture_ix),
1392                ("name", &mut name_capture_ix),
1393                ("context", &mut context_capture_ix),
1394                ("keep", &mut keep_capture_ix),
1395                ("collapse", &mut collapse_capture_ix),
1396            ],
1397        );
1398        if let Some(item_capture_ix) = item_capture_ix {
1399            grammar.embedding_config = Some(EmbeddingConfig {
1400                query,
1401                item_capture_ix,
1402                name_capture_ix,
1403                context_capture_ix,
1404                collapse_capture_ix,
1405                keep_capture_ix,
1406            });
1407        }
1408        Ok(self)
1409    }
1410
1411    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
1412        let grammar = self.grammar_mut();
1413        let query = Query::new(&grammar.ts_language, source)?;
1414        let mut open_capture_ix = None;
1415        let mut close_capture_ix = None;
1416        get_capture_indices(
1417            &query,
1418            &mut [
1419                ("open", &mut open_capture_ix),
1420                ("close", &mut close_capture_ix),
1421            ],
1422        );
1423        if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
1424            grammar.brackets_config = Some(BracketConfig {
1425                query,
1426                open_capture_ix,
1427                close_capture_ix,
1428            });
1429        }
1430        Ok(self)
1431    }
1432
1433    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
1434        let grammar = self.grammar_mut();
1435        let query = Query::new(&grammar.ts_language, source)?;
1436        let mut indent_capture_ix = None;
1437        let mut start_capture_ix = None;
1438        let mut end_capture_ix = None;
1439        let mut outdent_capture_ix = None;
1440        get_capture_indices(
1441            &query,
1442            &mut [
1443                ("indent", &mut indent_capture_ix),
1444                ("start", &mut start_capture_ix),
1445                ("end", &mut end_capture_ix),
1446                ("outdent", &mut outdent_capture_ix),
1447            ],
1448        );
1449        if let Some(indent_capture_ix) = indent_capture_ix {
1450            grammar.indents_config = Some(IndentConfig {
1451                query,
1452                indent_capture_ix,
1453                start_capture_ix,
1454                end_capture_ix,
1455                outdent_capture_ix,
1456            });
1457        }
1458        Ok(self)
1459    }
1460
1461    pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
1462        let grammar = self.grammar_mut();
1463        let query = Query::new(&grammar.ts_language, source)?;
1464        let mut language_capture_ix = None;
1465        let mut content_capture_ix = None;
1466        get_capture_indices(
1467            &query,
1468            &mut [
1469                ("language", &mut language_capture_ix),
1470                ("content", &mut content_capture_ix),
1471            ],
1472        );
1473        let patterns = (0..query.pattern_count())
1474            .map(|ix| {
1475                let mut config = InjectionPatternConfig::default();
1476                for setting in query.property_settings(ix) {
1477                    match setting.key.as_ref() {
1478                        "language" => {
1479                            config.language = setting.value.clone();
1480                        }
1481                        "combined" => {
1482                            config.combined = true;
1483                        }
1484                        _ => {}
1485                    }
1486                }
1487                config
1488            })
1489            .collect();
1490        if let Some(content_capture_ix) = content_capture_ix {
1491            grammar.injection_config = Some(InjectionConfig {
1492                query,
1493                language_capture_ix,
1494                content_capture_ix,
1495                patterns,
1496            });
1497        }
1498        Ok(self)
1499    }
1500
1501    pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
1502        let query = Query::new(&self.grammar_mut().ts_language, source)?;
1503
1504        let mut override_configs_by_id = HashMap::default();
1505        for (ix, name) in query.capture_names().iter().enumerate() {
1506            if !name.starts_with('_') {
1507                let value = self.config.overrides.remove(*name).unwrap_or_default();
1508                for server_name in &value.opt_into_language_servers {
1509                    if !self
1510                        .config
1511                        .scope_opt_in_language_servers
1512                        .contains(server_name)
1513                    {
1514                        util::debug_panic!("Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server");
1515                    }
1516                }
1517
1518                override_configs_by_id.insert(ix as u32, (name.to_string(), value));
1519            }
1520        }
1521
1522        if !self.config.overrides.is_empty() {
1523            let keys = self.config.overrides.keys().collect::<Vec<_>>();
1524            Err(anyhow!(
1525                "language {:?} has overrides in config not in query: {keys:?}",
1526                self.config.name
1527            ))?;
1528        }
1529
1530        for disabled_scope_name in self
1531            .config
1532            .brackets
1533            .disabled_scopes_by_bracket_ix
1534            .iter()
1535            .flatten()
1536        {
1537            if !override_configs_by_id
1538                .values()
1539                .any(|(scope_name, _)| scope_name == disabled_scope_name)
1540            {
1541                Err(anyhow!(
1542                    "language {:?} has overrides in config not in query: {disabled_scope_name:?}",
1543                    self.config.name
1544                ))?;
1545            }
1546        }
1547
1548        for (name, override_config) in override_configs_by_id.values_mut() {
1549            override_config.disabled_bracket_ixs = self
1550                .config
1551                .brackets
1552                .disabled_scopes_by_bracket_ix
1553                .iter()
1554                .enumerate()
1555                .filter_map(|(ix, disabled_scope_names)| {
1556                    if disabled_scope_names.contains(name) {
1557                        Some(ix as u16)
1558                    } else {
1559                        None
1560                    }
1561                })
1562                .collect();
1563        }
1564
1565        self.config.brackets.disabled_scopes_by_bracket_ix.clear();
1566        self.grammar_mut().override_config = Some(OverrideConfig {
1567            query,
1568            values: override_configs_by_id,
1569        });
1570        Ok(self)
1571    }
1572
1573    fn grammar_mut(&mut self) -> &mut Grammar {
1574        Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
1575    }
1576
1577    pub async fn with_lsp_adapters(mut self, lsp_adapters: Vec<Arc<dyn LspAdapter>>) -> Self {
1578        for adapter in lsp_adapters {
1579            self.adapters.push(CachedLspAdapter::new(adapter).await);
1580        }
1581        self
1582    }
1583
1584    #[cfg(any(test, feature = "test-support"))]
1585    pub async fn set_fake_lsp_adapter(
1586        &mut self,
1587        fake_lsp_adapter: Arc<FakeLspAdapter>,
1588    ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
1589        let (servers_tx, servers_rx) = mpsc::unbounded();
1590        self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
1591        let adapter = CachedLspAdapter::new(Arc::new(fake_lsp_adapter)).await;
1592        self.adapters = vec![adapter];
1593        servers_rx
1594    }
1595
1596    pub fn name(&self) -> Arc<str> {
1597        self.config.name.clone()
1598    }
1599
1600    pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
1601        match self.adapters.first().as_ref() {
1602            Some(adapter) => &adapter.disk_based_diagnostic_sources,
1603            None => &[],
1604        }
1605    }
1606
1607    pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
1608        for adapter in &self.adapters {
1609            let token = adapter.disk_based_diagnostics_progress_token.as_deref();
1610            if token.is_some() {
1611                return token;
1612            }
1613        }
1614
1615        None
1616    }
1617
1618    pub async fn process_completion(self: &Arc<Self>, completion: &mut lsp::CompletionItem) {
1619        for adapter in &self.adapters {
1620            adapter.process_completion(completion).await;
1621        }
1622    }
1623
1624    pub async fn label_for_completion(
1625        self: &Arc<Self>,
1626        completion: &lsp::CompletionItem,
1627    ) -> Option<CodeLabel> {
1628        self.adapters
1629            .first()
1630            .as_ref()?
1631            .label_for_completion(completion, self)
1632            .await
1633    }
1634
1635    pub async fn label_for_symbol(
1636        self: &Arc<Self>,
1637        name: &str,
1638        kind: lsp::SymbolKind,
1639    ) -> Option<CodeLabel> {
1640        self.adapters
1641            .first()
1642            .as_ref()?
1643            .label_for_symbol(name, kind, self)
1644            .await
1645    }
1646
1647    pub fn highlight_text<'a>(
1648        self: &'a Arc<Self>,
1649        text: &'a Rope,
1650        range: Range<usize>,
1651    ) -> Vec<(Range<usize>, HighlightId)> {
1652        let mut result = Vec::new();
1653        if let Some(grammar) = &self.grammar {
1654            let tree = grammar.parse_text(text, None);
1655            let captures =
1656                SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
1657                    grammar.highlights_query.as_ref()
1658                });
1659            let highlight_maps = vec![grammar.highlight_map()];
1660            let mut offset = 0;
1661            for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) {
1662                let end_offset = offset + chunk.text.len();
1663                if let Some(highlight_id) = chunk.syntax_highlight_id {
1664                    if !highlight_id.is_default() {
1665                        result.push((offset..end_offset, highlight_id));
1666                    }
1667                }
1668                offset = end_offset;
1669            }
1670        }
1671        result
1672    }
1673
1674    pub fn path_suffixes(&self) -> &[String] {
1675        &self.config.path_suffixes
1676    }
1677
1678    pub fn should_autoclose_before(&self, c: char) -> bool {
1679        c.is_whitespace() || self.config.autoclose_before.contains(c)
1680    }
1681
1682    pub fn set_theme(&self, theme: &SyntaxTheme) {
1683        if let Some(grammar) = self.grammar.as_ref() {
1684            if let Some(highlights_query) = &grammar.highlights_query {
1685                *grammar.highlight_map.lock() =
1686                    HighlightMap::new(highlights_query.capture_names(), theme);
1687            }
1688        }
1689    }
1690
1691    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
1692        self.grammar.as_ref()
1693    }
1694
1695    pub fn default_scope(self: &Arc<Self>) -> LanguageScope {
1696        LanguageScope {
1697            language: self.clone(),
1698            override_id: None,
1699        }
1700    }
1701
1702    pub fn prettier_parser_name(&self) -> Option<&str> {
1703        self.config.prettier_parser_name.as_deref()
1704    }
1705}
1706
1707impl LanguageScope {
1708    pub fn collapsed_placeholder(&self) -> &str {
1709        self.language.config.collapsed_placeholder.as_ref()
1710    }
1711
1712    /// Returns line prefix that is inserted in e.g. line continuations or
1713    /// in `toggle comments` action.
1714    pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
1715        Override::as_option(
1716            self.config_override().map(|o| &o.line_comment),
1717            self.language.config.line_comment.as_ref(),
1718        )
1719    }
1720
1721    pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
1722        Override::as_option(
1723            self.config_override().map(|o| &o.block_comment),
1724            self.language.config.block_comment.as_ref(),
1725        )
1726        .map(|e| (&e.0, &e.1))
1727    }
1728
1729    /// Returns a list of language-specific word characters.
1730    ///
1731    /// By default, Zed treats alphanumeric characters (and '_') as word characters for
1732    /// the purpose of actions like 'move to next word end` or whole-word search.
1733    /// It additionally accounts for language's additional word characters.
1734    pub fn word_characters(&self) -> Option<&HashSet<char>> {
1735        Override::as_option(
1736            self.config_override().map(|o| &o.word_characters),
1737            Some(&self.language.config.word_characters),
1738        )
1739    }
1740
1741    /// Returns a list of bracket pairs for a given language with an additional
1742    /// piece of information about whether the particular bracket pair is currently active for a given language.
1743    pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {
1744        let mut disabled_ids = self
1745            .config_override()
1746            .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice());
1747        self.language
1748            .config
1749            .brackets
1750            .pairs
1751            .iter()
1752            .enumerate()
1753            .map(move |(ix, bracket)| {
1754                let mut is_enabled = true;
1755                if let Some(next_disabled_ix) = disabled_ids.first() {
1756                    if ix == *next_disabled_ix as usize {
1757                        disabled_ids = &disabled_ids[1..];
1758                        is_enabled = false;
1759                    }
1760                }
1761                (bracket, is_enabled)
1762            })
1763    }
1764
1765    pub fn should_autoclose_before(&self, c: char) -> bool {
1766        c.is_whitespace() || self.language.config.autoclose_before.contains(c)
1767    }
1768
1769    pub fn language_allowed(&self, name: &LanguageServerName) -> bool {
1770        let config = &self.language.config;
1771        let opt_in_servers = &config.scope_opt_in_language_servers;
1772        if opt_in_servers.iter().any(|o| *o == *name.0) {
1773            if let Some(over) = self.config_override() {
1774                over.opt_into_language_servers.iter().any(|o| *o == *name.0)
1775            } else {
1776                false
1777            }
1778        } else {
1779            true
1780        }
1781    }
1782
1783    fn config_override(&self) -> Option<&LanguageConfigOverride> {
1784        let id = self.override_id?;
1785        let grammar = self.language.grammar.as_ref()?;
1786        let override_config = grammar.override_config.as_ref()?;
1787        override_config.values.get(&id).map(|e| &e.1)
1788    }
1789}
1790
1791impl Hash for Language {
1792    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1793        self.id().hash(state)
1794    }
1795}
1796
1797impl PartialEq for Language {
1798    fn eq(&self, other: &Self) -> bool {
1799        self.id().eq(&other.id())
1800    }
1801}
1802
1803impl Eq for Language {}
1804
1805impl Debug for Language {
1806    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1807        f.debug_struct("Language")
1808            .field("name", &self.config.name)
1809            .finish()
1810    }
1811}
1812
1813impl Grammar {
1814    pub fn id(&self) -> usize {
1815        self.id
1816    }
1817
1818    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
1819        PARSER.with(|parser| {
1820            let mut parser = parser.borrow_mut();
1821            parser
1822                .set_language(&self.ts_language)
1823                .expect("incompatible grammar");
1824            let mut chunks = text.chunks_in_range(0..text.len());
1825            parser
1826                .parse_with(
1827                    &mut move |offset, _| {
1828                        chunks.seek(offset);
1829                        chunks.next().unwrap_or("").as_bytes()
1830                    },
1831                    old_tree.as_ref(),
1832                )
1833                .unwrap()
1834        })
1835    }
1836
1837    pub fn highlight_map(&self) -> HighlightMap {
1838        self.highlight_map.lock().clone()
1839    }
1840
1841    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
1842        let capture_id = self
1843            .highlights_query
1844            .as_ref()?
1845            .capture_index_for_name(name)?;
1846        Some(self.highlight_map.lock().get(capture_id))
1847    }
1848}
1849
1850impl CodeLabel {
1851    pub fn plain(text: String, filter_text: Option<&str>) -> Self {
1852        let mut result = Self {
1853            runs: Vec::new(),
1854            filter_range: 0..text.len(),
1855            text,
1856        };
1857        if let Some(filter_text) = filter_text {
1858            if let Some(ix) = result.text.find(filter_text) {
1859                result.filter_range = ix..ix + filter_text.len();
1860            }
1861        }
1862        result
1863    }
1864}
1865
1866#[cfg(any(test, feature = "test-support"))]
1867impl Default for FakeLspAdapter {
1868    fn default() -> Self {
1869        Self {
1870            name: "the-fake-language-server",
1871            capabilities: lsp::LanguageServer::full_capabilities(),
1872            initializer: None,
1873            disk_based_diagnostics_progress_token: None,
1874            initialization_options: None,
1875            disk_based_diagnostics_sources: Vec::new(),
1876            prettier_plugins: Vec::new(),
1877        }
1878    }
1879}
1880
1881#[cfg(any(test, feature = "test-support"))]
1882#[async_trait]
1883impl LspAdapter for Arc<FakeLspAdapter> {
1884    async fn name(&self) -> LanguageServerName {
1885        LanguageServerName(self.name.into())
1886    }
1887
1888    fn short_name(&self) -> &'static str {
1889        "FakeLspAdapter"
1890    }
1891
1892    async fn fetch_latest_server_version(
1893        &self,
1894        _: &dyn LspAdapterDelegate,
1895    ) -> Result<Box<dyn 'static + Send + Any>> {
1896        unreachable!();
1897    }
1898
1899    async fn fetch_server_binary(
1900        &self,
1901        _: Box<dyn 'static + Send + Any>,
1902        _: PathBuf,
1903        _: &dyn LspAdapterDelegate,
1904    ) -> Result<LanguageServerBinary> {
1905        unreachable!();
1906    }
1907
1908    async fn cached_server_binary(
1909        &self,
1910        _: PathBuf,
1911        _: &dyn LspAdapterDelegate,
1912    ) -> Option<LanguageServerBinary> {
1913        unreachable!();
1914    }
1915
1916    async fn installation_test_binary(&self, _: PathBuf) -> Option<LanguageServerBinary> {
1917        unreachable!();
1918    }
1919
1920    fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
1921
1922    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
1923        self.disk_based_diagnostics_sources.clone()
1924    }
1925
1926    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
1927        self.disk_based_diagnostics_progress_token.clone()
1928    }
1929
1930    async fn initialization_options(&self) -> Option<Value> {
1931        self.initialization_options.clone()
1932    }
1933
1934    fn prettier_plugins(&self) -> &[&'static str] {
1935        &self.prettier_plugins
1936    }
1937}
1938
1939fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
1940    for (ix, name) in query.capture_names().iter().enumerate() {
1941        for (capture_name, index) in captures.iter_mut() {
1942            if capture_name == name {
1943                **index = Some(ix as u32);
1944                break;
1945            }
1946        }
1947    }
1948}
1949
1950pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
1951    lsp::Position::new(point.row, point.column)
1952}
1953
1954pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
1955    Unclipped(PointUtf16::new(point.line, point.character))
1956}
1957
1958pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
1959    lsp::Range {
1960        start: point_to_lsp(range.start),
1961        end: point_to_lsp(range.end),
1962    }
1963}
1964
1965pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
1966    let mut start = point_from_lsp(range.start);
1967    let mut end = point_from_lsp(range.end);
1968    if start > end {
1969        mem::swap(&mut start, &mut end);
1970    }
1971    start..end
1972}
1973
1974#[cfg(test)]
1975mod tests {
1976    use super::*;
1977    use gpui::TestAppContext;
1978
1979    #[gpui::test(iterations = 10)]
1980    async fn test_first_line_pattern(cx: &mut TestAppContext) {
1981        let mut languages = LanguageRegistry::test();
1982
1983        languages.set_executor(cx.executor());
1984        let languages = Arc::new(languages);
1985        languages.register(
1986            "/javascript",
1987            LanguageConfig {
1988                name: "JavaScript".into(),
1989                path_suffixes: vec!["js".into()],
1990                first_line_pattern: Some(Regex::new(r"\bnode\b").unwrap()),
1991                ..Default::default()
1992            },
1993            tree_sitter_typescript::language_tsx(),
1994            vec![],
1995            |_| Default::default(),
1996        );
1997
1998        languages
1999            .language_for_file("the/script", None)
2000            .await
2001            .unwrap_err();
2002        languages
2003            .language_for_file("the/script", Some(&"nothing".into()))
2004            .await
2005            .unwrap_err();
2006        assert_eq!(
2007            languages
2008                .language_for_file("the/script", Some(&"#!/bin/env node".into()))
2009                .await
2010                .unwrap()
2011                .name()
2012                .as_ref(),
2013            "JavaScript"
2014        );
2015    }
2016
2017    #[gpui::test(iterations = 10)]
2018    async fn test_language_loading(cx: &mut TestAppContext) {
2019        let mut languages = LanguageRegistry::test();
2020        languages.set_executor(cx.executor());
2021        let languages = Arc::new(languages);
2022        languages.register(
2023            "/JSON",
2024            LanguageConfig {
2025                name: "JSON".into(),
2026                path_suffixes: vec!["json".into()],
2027                ..Default::default()
2028            },
2029            tree_sitter_json::language(),
2030            vec![],
2031            |_| Default::default(),
2032        );
2033        languages.register(
2034            "/rust",
2035            LanguageConfig {
2036                name: "Rust".into(),
2037                path_suffixes: vec!["rs".into()],
2038                ..Default::default()
2039            },
2040            tree_sitter_rust::language(),
2041            vec![],
2042            |_| Default::default(),
2043        );
2044        assert_eq!(
2045            languages.language_names(),
2046            &[
2047                "JSON".to_string(),
2048                "Plain Text".to_string(),
2049                "Rust".to_string(),
2050            ]
2051        );
2052
2053        let rust1 = languages.language_for_name("Rust");
2054        let rust2 = languages.language_for_name("Rust");
2055
2056        // Ensure language is still listed even if it's being loaded.
2057        assert_eq!(
2058            languages.language_names(),
2059            &[
2060                "JSON".to_string(),
2061                "Plain Text".to_string(),
2062                "Rust".to_string(),
2063            ]
2064        );
2065
2066        let (rust1, rust2) = futures::join!(rust1, rust2);
2067        assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap()));
2068
2069        // Ensure language is still listed even after loading it.
2070        assert_eq!(
2071            languages.language_names(),
2072            &[
2073                "JSON".to_string(),
2074                "Plain Text".to_string(),
2075                "Rust".to_string(),
2076            ]
2077        );
2078
2079        // Loading an unknown language returns an error.
2080        assert!(languages.language_for_name("Unknown").await.is_err());
2081    }
2082}