language.rs

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