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