language.rs

   1mod buffer;
   2mod diagnostic_set;
   3mod highlight_map;
   4mod outline;
   5pub mod proto;
   6mod syntax_map;
   7
   8#[cfg(test)]
   9mod buffer_tests;
  10
  11use anyhow::{anyhow, Context, Result};
  12use async_trait::async_trait;
  13use client::http::HttpClient;
  14use collections::HashMap;
  15use futures::{
  16    future::{BoxFuture, Shared},
  17    FutureExt, TryFutureExt,
  18};
  19use gpui::{MutableAppContext, Task};
  20use highlight_map::HighlightMap;
  21use lazy_static::lazy_static;
  22use parking_lot::{Mutex, RwLock};
  23use postage::watch;
  24use regex::Regex;
  25use serde::{de, Deserialize, Deserializer};
  26use serde_json::Value;
  27use std::{
  28    any::Any,
  29    cell::RefCell,
  30    fmt::Debug,
  31    hash::Hash,
  32    mem,
  33    ops::Range,
  34    path::{Path, PathBuf},
  35    str,
  36    sync::{
  37        atomic::{AtomicUsize, Ordering::SeqCst},
  38        Arc,
  39    },
  40};
  41use syntax_map::SyntaxSnapshot;
  42use theme::{SyntaxTheme, Theme};
  43use tree_sitter::{self, Query};
  44use util::ResultExt;
  45
  46#[cfg(any(test, feature = "test-support"))]
  47use futures::channel::mpsc;
  48
  49pub use buffer::Operation;
  50pub use buffer::*;
  51pub use diagnostic_set::DiagnosticEntry;
  52pub use outline::{Outline, OutlineItem};
  53pub use tree_sitter::{Parser, Tree};
  54
  55thread_local! {
  56    static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
  57}
  58
  59lazy_static! {
  60    pub static ref NEXT_GRAMMAR_ID: AtomicUsize = Default::default();
  61    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
  62        LanguageConfig {
  63            name: "Plain Text".into(),
  64            ..Default::default()
  65        },
  66        None,
  67    ));
  68}
  69
  70pub trait ToLspPosition {
  71    fn to_lsp_position(self) -> lsp::Position;
  72}
  73
  74#[derive(Clone, Debug, PartialEq, Eq, Hash)]
  75pub struct LanguageServerName(pub Arc<str>);
  76
  77/// Represents a Language Server, with certain cached sync properties.
  78/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
  79/// once at startup, and caches the results.
  80pub struct CachedLspAdapter {
  81    pub name: LanguageServerName,
  82    pub server_args: Vec<String>,
  83    pub initialization_options: Option<Value>,
  84    pub disk_based_diagnostic_sources: Vec<String>,
  85    pub disk_based_diagnostics_progress_token: Option<String>,
  86    pub language_ids: HashMap<String, String>,
  87    pub adapter: Box<dyn LspAdapter>,
  88}
  89
  90impl CachedLspAdapter {
  91    pub async fn new<T: LspAdapter>(adapter: T) -> Arc<Self> {
  92        let adapter = Box::new(adapter);
  93        let name = adapter.name().await;
  94        let server_args = adapter.server_args().await;
  95        let initialization_options = adapter.initialization_options().await;
  96        let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await;
  97        let disk_based_diagnostics_progress_token =
  98            adapter.disk_based_diagnostics_progress_token().await;
  99        let language_ids = adapter.language_ids().await;
 100
 101        Arc::new(CachedLspAdapter {
 102            name,
 103            server_args,
 104            initialization_options,
 105            disk_based_diagnostic_sources,
 106            disk_based_diagnostics_progress_token,
 107            language_ids,
 108            adapter,
 109        })
 110    }
 111
 112    pub async fn fetch_latest_server_version(
 113        &self,
 114        http: Arc<dyn HttpClient>,
 115    ) -> Result<Box<dyn 'static + Send + Any>> {
 116        self.adapter.fetch_latest_server_version(http).await
 117    }
 118
 119    pub async fn fetch_server_binary(
 120        &self,
 121        version: Box<dyn 'static + Send + Any>,
 122        http: Arc<dyn HttpClient>,
 123        container_dir: PathBuf,
 124    ) -> Result<PathBuf> {
 125        self.adapter
 126            .fetch_server_binary(version, http, container_dir)
 127            .await
 128    }
 129
 130    pub async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
 131        self.adapter.cached_server_binary(container_dir).await
 132    }
 133
 134    pub async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
 135        self.adapter.process_diagnostics(params).await
 136    }
 137
 138    pub async fn label_for_completion(
 139        &self,
 140        completion_item: &lsp::CompletionItem,
 141        language: &Arc<Language>,
 142    ) -> Option<CodeLabel> {
 143        self.adapter
 144            .label_for_completion(completion_item, language)
 145            .await
 146    }
 147
 148    pub async fn label_for_symbol(
 149        &self,
 150        name: &str,
 151        kind: lsp::SymbolKind,
 152        language: &Arc<Language>,
 153    ) -> Option<CodeLabel> {
 154        self.adapter.label_for_symbol(name, kind, language).await
 155    }
 156}
 157
 158#[async_trait]
 159pub trait LspAdapter: 'static + Send + Sync {
 160    async fn name(&self) -> LanguageServerName;
 161
 162    async fn fetch_latest_server_version(
 163        &self,
 164        http: Arc<dyn HttpClient>,
 165    ) -> Result<Box<dyn 'static + Send + Any>>;
 166
 167    async fn fetch_server_binary(
 168        &self,
 169        version: Box<dyn 'static + Send + Any>,
 170        http: Arc<dyn HttpClient>,
 171        container_dir: PathBuf,
 172    ) -> Result<PathBuf>;
 173
 174    async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf>;
 175
 176    async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
 177
 178    async fn label_for_completion(
 179        &self,
 180        _: &lsp::CompletionItem,
 181        _: &Arc<Language>,
 182    ) -> Option<CodeLabel> {
 183        None
 184    }
 185
 186    async fn label_for_symbol(
 187        &self,
 188        _: &str,
 189        _: lsp::SymbolKind,
 190        _: &Arc<Language>,
 191    ) -> Option<CodeLabel> {
 192        None
 193    }
 194
 195    async fn server_args(&self) -> Vec<String> {
 196        Vec::new()
 197    }
 198
 199    async fn initialization_options(&self) -> Option<Value> {
 200        None
 201    }
 202
 203    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
 204        Default::default()
 205    }
 206
 207    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
 208        None
 209    }
 210
 211    async fn language_ids(&self) -> HashMap<String, String> {
 212        Default::default()
 213    }
 214}
 215
 216#[derive(Clone, Debug, PartialEq, Eq)]
 217pub struct CodeLabel {
 218    pub text: String,
 219    pub runs: Vec<(Range<usize>, HighlightId)>,
 220    pub filter_range: Range<usize>,
 221}
 222
 223#[derive(Deserialize)]
 224pub struct LanguageConfig {
 225    pub name: Arc<str>,
 226    pub path_suffixes: Vec<String>,
 227    pub brackets: Vec<BracketPair>,
 228    #[serde(default = "auto_indent_using_last_non_empty_line_default")]
 229    pub auto_indent_using_last_non_empty_line: bool,
 230    #[serde(default, deserialize_with = "deserialize_regex")]
 231    pub increase_indent_pattern: Option<Regex>,
 232    #[serde(default, deserialize_with = "deserialize_regex")]
 233    pub decrease_indent_pattern: Option<Regex>,
 234    #[serde(default)]
 235    pub autoclose_before: String,
 236    #[serde(default)]
 237    pub line_comment: Option<Arc<str>>,
 238    #[serde(default)]
 239    pub block_comment: Option<(Arc<str>, Arc<str>)>,
 240}
 241
 242impl Default for LanguageConfig {
 243    fn default() -> Self {
 244        Self {
 245            name: "".into(),
 246            path_suffixes: Default::default(),
 247            brackets: Default::default(),
 248            auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
 249            increase_indent_pattern: Default::default(),
 250            decrease_indent_pattern: Default::default(),
 251            autoclose_before: Default::default(),
 252            line_comment: Default::default(),
 253            block_comment: Default::default(),
 254        }
 255    }
 256}
 257
 258fn auto_indent_using_last_non_empty_line_default() -> bool {
 259    true
 260}
 261
 262fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
 263    let source = Option::<String>::deserialize(d)?;
 264    if let Some(source) = source {
 265        Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
 266    } else {
 267        Ok(None)
 268    }
 269}
 270
 271#[cfg(any(test, feature = "test-support"))]
 272pub struct FakeLspAdapter {
 273    pub name: &'static str,
 274    pub capabilities: lsp::ServerCapabilities,
 275    pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
 276    pub disk_based_diagnostics_progress_token: Option<String>,
 277    pub disk_based_diagnostics_sources: Vec<String>,
 278}
 279
 280#[derive(Clone, Debug, Default, Deserialize)]
 281pub struct BracketPair {
 282    pub start: String,
 283    pub end: String,
 284    pub close: bool,
 285    pub newline: bool,
 286}
 287
 288pub struct Language {
 289    pub(crate) config: LanguageConfig,
 290    pub(crate) grammar: Option<Arc<Grammar>>,
 291    pub(crate) adapter: Option<Arc<CachedLspAdapter>>,
 292
 293    #[cfg(any(test, feature = "test-support"))]
 294    fake_adapter: Option<(
 295        mpsc::UnboundedSender<lsp::FakeLanguageServer>,
 296        Arc<FakeLspAdapter>,
 297    )>,
 298}
 299
 300pub struct Grammar {
 301    id: usize,
 302    pub(crate) ts_language: tree_sitter::Language,
 303    pub(crate) highlights_query: Option<Query>,
 304    pub(crate) brackets_config: Option<BracketConfig>,
 305    pub(crate) indents_config: Option<IndentConfig>,
 306    pub(crate) outline_config: Option<OutlineConfig>,
 307    pub(crate) injection_config: Option<InjectionConfig>,
 308    pub(crate) highlight_map: Mutex<HighlightMap>,
 309}
 310
 311struct IndentConfig {
 312    query: Query,
 313    indent_capture_ix: u32,
 314    start_capture_ix: Option<u32>,
 315    end_capture_ix: Option<u32>,
 316    outdent_capture_ix: Option<u32>,
 317}
 318
 319struct OutlineConfig {
 320    query: Query,
 321    item_capture_ix: u32,
 322    name_capture_ix: u32,
 323    context_capture_ix: Option<u32>,
 324}
 325
 326struct InjectionConfig {
 327    query: Query,
 328    content_capture_ix: u32,
 329    language_capture_ix: Option<u32>,
 330    patterns: Vec<InjectionPatternConfig>,
 331}
 332
 333#[derive(Default, Clone)]
 334struct InjectionPatternConfig {
 335    language: Option<Box<str>>,
 336    combined: bool,
 337}
 338
 339struct BracketConfig {
 340    query: Query,
 341    open_capture_ix: u32,
 342    close_capture_ix: u32,
 343}
 344
 345#[derive(Clone)]
 346pub enum LanguageServerBinaryStatus {
 347    CheckingForUpdate,
 348    Downloading,
 349    Downloaded,
 350    Cached,
 351    Failed { error: String },
 352}
 353
 354pub struct LanguageRegistry {
 355    languages: RwLock<Vec<Arc<Language>>>,
 356    language_server_download_dir: Option<Arc<Path>>,
 357    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 358    lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
 359    login_shell_env_loaded: Shared<Task<()>>,
 360    #[allow(clippy::type_complexity)]
 361    lsp_binary_paths: Mutex<
 362        HashMap<
 363            LanguageServerName,
 364            Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>,
 365        >,
 366    >,
 367    subscription: RwLock<(watch::Sender<()>, watch::Receiver<()>)>,
 368    theme: RwLock<Option<Arc<Theme>>>,
 369}
 370
 371impl LanguageRegistry {
 372    pub fn new(login_shell_env_loaded: Task<()>) -> Self {
 373        let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
 374        Self {
 375            language_server_download_dir: None,
 376            languages: Default::default(),
 377            lsp_binary_statuses_tx,
 378            lsp_binary_statuses_rx,
 379            login_shell_env_loaded: login_shell_env_loaded.shared(),
 380            lsp_binary_paths: Default::default(),
 381            subscription: RwLock::new(watch::channel()),
 382            theme: Default::default(),
 383        }
 384    }
 385
 386    #[cfg(any(test, feature = "test-support"))]
 387    pub fn test() -> Self {
 388        Self::new(Task::ready(()))
 389    }
 390
 391    pub fn add(&self, language: Arc<Language>) {
 392        if let Some(theme) = self.theme.read().clone() {
 393            language.set_theme(&theme.editor.syntax);
 394        }
 395        self.languages.write().push(language);
 396        *self.subscription.write().0.borrow_mut() = ();
 397    }
 398
 399    pub fn subscribe(&self) -> watch::Receiver<()> {
 400        self.subscription.read().1.clone()
 401    }
 402
 403    pub fn set_theme(&self, theme: Arc<Theme>) {
 404        *self.theme.write() = Some(theme.clone());
 405        for language in self.languages.read().iter() {
 406            language.set_theme(&theme.editor.syntax);
 407        }
 408    }
 409
 410    pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
 411        self.language_server_download_dir = Some(path.into());
 412    }
 413
 414    pub fn get_language(&self, name: &str) -> Option<Arc<Language>> {
 415        self.languages
 416            .read()
 417            .iter()
 418            .find(|language| language.name().to_lowercase() == name.to_lowercase())
 419            .cloned()
 420    }
 421
 422    pub fn to_vec(&self) -> Vec<Arc<Language>> {
 423        self.languages.read().iter().cloned().collect()
 424    }
 425
 426    pub fn language_names(&self) -> Vec<String> {
 427        self.languages
 428            .read()
 429            .iter()
 430            .map(|language| language.name().to_string())
 431            .collect()
 432    }
 433
 434    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<Arc<Language>> {
 435        let path = path.as_ref();
 436        let filename = path.file_name().and_then(|name| name.to_str());
 437        let extension = path.extension().and_then(|name| name.to_str());
 438        let path_suffixes = [extension, filename];
 439        self.languages
 440            .read()
 441            .iter()
 442            .find(|language| {
 443                language
 444                    .config
 445                    .path_suffixes
 446                    .iter()
 447                    .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
 448            })
 449            .cloned()
 450    }
 451
 452    pub fn start_language_server(
 453        self: &Arc<Self>,
 454        server_id: usize,
 455        language: Arc<Language>,
 456        root_path: Arc<Path>,
 457        http_client: Arc<dyn HttpClient>,
 458        cx: &mut MutableAppContext,
 459    ) -> Option<Task<Result<lsp::LanguageServer>>> {
 460        #[cfg(any(test, feature = "test-support"))]
 461        if language.fake_adapter.is_some() {
 462            let language = language;
 463            return Some(cx.spawn(|cx| async move {
 464                let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
 465                let (server, mut fake_server) = lsp::LanguageServer::fake(
 466                    fake_adapter.name.to_string(),
 467                    fake_adapter.capabilities.clone(),
 468                    cx.clone(),
 469                );
 470
 471                if let Some(initializer) = &fake_adapter.initializer {
 472                    initializer(&mut fake_server);
 473                }
 474
 475                let servers_tx = servers_tx.clone();
 476                cx.background()
 477                    .spawn(async move {
 478                        if fake_server
 479                            .try_receive_notification::<lsp::notification::Initialized>()
 480                            .await
 481                            .is_some()
 482                        {
 483                            servers_tx.unbounded_send(fake_server).ok();
 484                        }
 485                    })
 486                    .detach();
 487                Ok(server)
 488            }));
 489        }
 490
 491        let download_dir = self
 492            .language_server_download_dir
 493            .clone()
 494            .ok_or_else(|| anyhow!("language server download directory has not been assigned"))
 495            .log_err()?;
 496
 497        let this = self.clone();
 498        let adapter = language.adapter.clone()?;
 499        let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
 500        let login_shell_env_loaded = self.login_shell_env_loaded.clone();
 501        Some(cx.spawn(|cx| async move {
 502            login_shell_env_loaded.await;
 503            let server_binary_path = this
 504                .lsp_binary_paths
 505                .lock()
 506                .entry(adapter.name.clone())
 507                .or_insert_with(|| {
 508                    get_server_binary_path(
 509                        adapter.clone(),
 510                        language.clone(),
 511                        http_client,
 512                        download_dir,
 513                        lsp_binary_statuses,
 514                    )
 515                    .map_err(Arc::new)
 516                    .boxed()
 517                    .shared()
 518                })
 519                .clone()
 520                .map_err(|e| anyhow!(e));
 521
 522            let server_binary_path = server_binary_path.await?;
 523            let server_args = &adapter.server_args;
 524            let server = lsp::LanguageServer::new(
 525                server_id,
 526                &server_binary_path,
 527                server_args,
 528                &root_path,
 529                cx,
 530            )?;
 531            Ok(server)
 532        }))
 533    }
 534
 535    pub fn language_server_binary_statuses(
 536        &self,
 537    ) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
 538        self.lsp_binary_statuses_rx.clone()
 539    }
 540}
 541
 542#[cfg(any(test, feature = "test-support"))]
 543impl Default for LanguageRegistry {
 544    fn default() -> Self {
 545        Self::test()
 546    }
 547}
 548
 549async fn get_server_binary_path(
 550    adapter: Arc<CachedLspAdapter>,
 551    language: Arc<Language>,
 552    http_client: Arc<dyn HttpClient>,
 553    download_dir: Arc<Path>,
 554    statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 555) -> Result<PathBuf> {
 556    let container_dir = download_dir.join(adapter.name.0.as_ref());
 557    if !container_dir.exists() {
 558        smol::fs::create_dir_all(&container_dir)
 559            .await
 560            .context("failed to create container directory")?;
 561    }
 562
 563    let path = fetch_latest_server_binary_path(
 564        adapter.clone(),
 565        language.clone(),
 566        http_client,
 567        &container_dir,
 568        statuses.clone(),
 569    )
 570    .await;
 571    if let Err(error) = path.as_ref() {
 572        if let Some(cached_path) = adapter.cached_server_binary(container_dir).await {
 573            statuses
 574                .broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
 575                .await?;
 576            return Ok(cached_path);
 577        } else {
 578            statuses
 579                .broadcast((
 580                    language.clone(),
 581                    LanguageServerBinaryStatus::Failed {
 582                        error: format!("{:?}", error),
 583                    },
 584                ))
 585                .await?;
 586        }
 587    }
 588    path
 589}
 590
 591async fn fetch_latest_server_binary_path(
 592    adapter: Arc<CachedLspAdapter>,
 593    language: Arc<Language>,
 594    http_client: Arc<dyn HttpClient>,
 595    container_dir: &Path,
 596    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 597) -> Result<PathBuf> {
 598    let container_dir: Arc<Path> = container_dir.into();
 599    lsp_binary_statuses_tx
 600        .broadcast((
 601            language.clone(),
 602            LanguageServerBinaryStatus::CheckingForUpdate,
 603        ))
 604        .await?;
 605    let version_info = adapter
 606        .fetch_latest_server_version(http_client.clone())
 607        .await?;
 608    lsp_binary_statuses_tx
 609        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
 610        .await?;
 611    let path = adapter
 612        .fetch_server_binary(version_info, http_client, container_dir.to_path_buf())
 613        .await?;
 614    lsp_binary_statuses_tx
 615        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
 616        .await?;
 617    Ok(path)
 618}
 619
 620impl Language {
 621    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
 622        Self {
 623            config,
 624            grammar: ts_language.map(|ts_language| {
 625                Arc::new(Grammar {
 626                    id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst),
 627                    highlights_query: None,
 628                    brackets_config: None,
 629                    outline_config: None,
 630                    indents_config: None,
 631                    injection_config: None,
 632                    ts_language,
 633                    highlight_map: Default::default(),
 634                })
 635            }),
 636            adapter: None,
 637
 638            #[cfg(any(test, feature = "test-support"))]
 639            fake_adapter: None,
 640        }
 641    }
 642
 643    pub fn lsp_adapter(&self) -> Option<Arc<CachedLspAdapter>> {
 644        self.adapter.clone()
 645    }
 646
 647    pub fn id(&self) -> Option<usize> {
 648        self.grammar.as_ref().map(|g| g.id)
 649    }
 650
 651    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
 652        let grammar = self.grammar_mut();
 653        grammar.highlights_query = Some(Query::new(grammar.ts_language, source)?);
 654        Ok(self)
 655    }
 656
 657    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
 658        let grammar = self.grammar_mut();
 659        let query = Query::new(grammar.ts_language, source)?;
 660        let mut open_capture_ix = None;
 661        let mut close_capture_ix = None;
 662        get_capture_indices(
 663            &query,
 664            &mut [
 665                ("open", &mut open_capture_ix),
 666                ("close", &mut close_capture_ix),
 667            ],
 668        );
 669        if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
 670            grammar.brackets_config = Some(BracketConfig {
 671                query,
 672                open_capture_ix,
 673                close_capture_ix,
 674            });
 675        }
 676        Ok(self)
 677    }
 678
 679    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
 680        let grammar = self.grammar_mut();
 681        let query = Query::new(grammar.ts_language, source)?;
 682        let mut indent_capture_ix = None;
 683        let mut start_capture_ix = None;
 684        let mut end_capture_ix = None;
 685        let mut outdent_capture_ix = None;
 686        get_capture_indices(
 687            &query,
 688            &mut [
 689                ("indent", &mut indent_capture_ix),
 690                ("start", &mut start_capture_ix),
 691                ("end", &mut end_capture_ix),
 692                ("outdent", &mut outdent_capture_ix),
 693            ],
 694        );
 695        if let Some(indent_capture_ix) = indent_capture_ix {
 696            grammar.indents_config = Some(IndentConfig {
 697                query,
 698                indent_capture_ix,
 699                start_capture_ix,
 700                end_capture_ix,
 701                outdent_capture_ix,
 702            });
 703        }
 704        Ok(self)
 705    }
 706
 707    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
 708        let grammar = self.grammar_mut();
 709        let query = Query::new(grammar.ts_language, source)?;
 710        let mut item_capture_ix = None;
 711        let mut name_capture_ix = None;
 712        let mut context_capture_ix = None;
 713        get_capture_indices(
 714            &query,
 715            &mut [
 716                ("item", &mut item_capture_ix),
 717                ("name", &mut name_capture_ix),
 718                ("context", &mut context_capture_ix),
 719            ],
 720        );
 721        if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
 722            grammar.outline_config = Some(OutlineConfig {
 723                query,
 724                item_capture_ix,
 725                name_capture_ix,
 726                context_capture_ix,
 727            });
 728        }
 729        Ok(self)
 730    }
 731
 732    pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
 733        let grammar = self.grammar_mut();
 734        let query = Query::new(grammar.ts_language, source)?;
 735        let mut language_capture_ix = None;
 736        let mut content_capture_ix = None;
 737        get_capture_indices(
 738            &query,
 739            &mut [
 740                ("language", &mut language_capture_ix),
 741                ("content", &mut content_capture_ix),
 742            ],
 743        );
 744        let patterns = (0..query.pattern_count())
 745            .map(|ix| {
 746                let mut config = InjectionPatternConfig::default();
 747                for setting in query.property_settings(ix) {
 748                    match setting.key.as_ref() {
 749                        "language" => {
 750                            config.language = setting.value.clone();
 751                        }
 752                        "combined" => {
 753                            config.combined = true;
 754                        }
 755                        _ => {}
 756                    }
 757                }
 758                config
 759            })
 760            .collect();
 761        if let Some(content_capture_ix) = content_capture_ix {
 762            grammar.injection_config = Some(InjectionConfig {
 763                query,
 764                language_capture_ix,
 765                content_capture_ix,
 766                patterns,
 767            });
 768        }
 769        Ok(self)
 770    }
 771
 772    fn grammar_mut(&mut self) -> &mut Grammar {
 773        Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
 774    }
 775
 776    pub fn with_lsp_adapter(mut self, lsp_adapter: Arc<CachedLspAdapter>) -> Self {
 777        self.adapter = Some(lsp_adapter);
 778        self
 779    }
 780
 781    #[cfg(any(test, feature = "test-support"))]
 782    pub async fn set_fake_lsp_adapter(
 783        &mut self,
 784        fake_lsp_adapter: Arc<FakeLspAdapter>,
 785    ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
 786        let (servers_tx, servers_rx) = mpsc::unbounded();
 787        self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
 788        let adapter = CachedLspAdapter::new(fake_lsp_adapter).await;
 789        self.adapter = Some(adapter);
 790        servers_rx
 791    }
 792
 793    pub fn name(&self) -> Arc<str> {
 794        self.config.name.clone()
 795    }
 796
 797    pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
 798        self.config.line_comment.as_ref()
 799    }
 800
 801    pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
 802        self.config
 803            .block_comment
 804            .as_ref()
 805            .map(|(start, end)| (start, end))
 806    }
 807
 808    pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
 809        match self.adapter.as_ref() {
 810            Some(adapter) => &adapter.disk_based_diagnostic_sources,
 811            None => &[],
 812        }
 813    }
 814
 815    pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
 816        if let Some(adapter) = self.adapter.as_ref() {
 817            adapter.disk_based_diagnostics_progress_token.as_deref()
 818        } else {
 819            None
 820        }
 821    }
 822
 823    pub async fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
 824        if let Some(processor) = self.adapter.as_ref() {
 825            processor.process_diagnostics(diagnostics).await;
 826        }
 827    }
 828
 829    pub async fn label_for_completion(
 830        self: &Arc<Self>,
 831        completion: &lsp::CompletionItem,
 832    ) -> Option<CodeLabel> {
 833        self.adapter
 834            .as_ref()?
 835            .label_for_completion(completion, self)
 836            .await
 837    }
 838
 839    pub async fn label_for_symbol(
 840        self: &Arc<Self>,
 841        name: &str,
 842        kind: lsp::SymbolKind,
 843    ) -> Option<CodeLabel> {
 844        self.adapter
 845            .as_ref()?
 846            .label_for_symbol(name, kind, self)
 847            .await
 848    }
 849
 850    pub fn highlight_text<'a>(
 851        self: &'a Arc<Self>,
 852        text: &'a Rope,
 853        range: Range<usize>,
 854    ) -> Vec<(Range<usize>, HighlightId)> {
 855        let mut result = Vec::new();
 856        if let Some(grammar) = &self.grammar {
 857            let tree = grammar.parse_text(text, None);
 858            let captures =
 859                SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
 860                    grammar.highlights_query.as_ref()
 861                });
 862            let highlight_maps = vec![grammar.highlight_map()];
 863            let mut offset = 0;
 864            for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) {
 865                let end_offset = offset + chunk.text.len();
 866                if let Some(highlight_id) = chunk.syntax_highlight_id {
 867                    if !highlight_id.is_default() {
 868                        result.push((offset..end_offset, highlight_id));
 869                    }
 870                }
 871                offset = end_offset;
 872            }
 873        }
 874        result
 875    }
 876
 877    pub fn brackets(&self) -> &[BracketPair] {
 878        &self.config.brackets
 879    }
 880
 881    pub fn path_suffixes(&self) -> &[String] {
 882        &self.config.path_suffixes
 883    }
 884
 885    pub fn should_autoclose_before(&self, c: char) -> bool {
 886        c.is_whitespace() || self.config.autoclose_before.contains(c)
 887    }
 888
 889    pub fn set_theme(&self, theme: &SyntaxTheme) {
 890        if let Some(grammar) = self.grammar.as_ref() {
 891            if let Some(highlights_query) = &grammar.highlights_query {
 892                *grammar.highlight_map.lock() =
 893                    HighlightMap::new(highlights_query.capture_names(), theme);
 894            }
 895        }
 896    }
 897
 898    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
 899        self.grammar.as_ref()
 900    }
 901}
 902
 903impl Hash for Language {
 904    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
 905        self.id().hash(state)
 906    }
 907}
 908
 909impl PartialEq for Language {
 910    fn eq(&self, other: &Self) -> bool {
 911        self.id().eq(&other.id())
 912    }
 913}
 914
 915impl Eq for Language {}
 916
 917impl Debug for Language {
 918    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 919        f.debug_struct("Language")
 920            .field("name", &self.config.name)
 921            .finish()
 922    }
 923}
 924
 925impl Grammar {
 926    pub fn id(&self) -> usize {
 927        self.id
 928    }
 929
 930    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
 931        PARSER.with(|parser| {
 932            let mut parser = parser.borrow_mut();
 933            parser
 934                .set_language(self.ts_language)
 935                .expect("incompatible grammar");
 936            let mut chunks = text.chunks_in_range(0..text.len());
 937            parser
 938                .parse_with(
 939                    &mut move |offset, _| {
 940                        chunks.seek(offset);
 941                        chunks.next().unwrap_or("").as_bytes()
 942                    },
 943                    old_tree.as_ref(),
 944                )
 945                .unwrap()
 946        })
 947    }
 948
 949    pub fn highlight_map(&self) -> HighlightMap {
 950        self.highlight_map.lock().clone()
 951    }
 952
 953    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
 954        let capture_id = self
 955            .highlights_query
 956            .as_ref()?
 957            .capture_index_for_name(name)?;
 958        Some(self.highlight_map.lock().get(capture_id))
 959    }
 960}
 961
 962impl CodeLabel {
 963    pub fn plain(text: String, filter_text: Option<&str>) -> Self {
 964        let mut result = Self {
 965            runs: Vec::new(),
 966            filter_range: 0..text.len(),
 967            text,
 968        };
 969        if let Some(filter_text) = filter_text {
 970            if let Some(ix) = result.text.find(filter_text) {
 971                result.filter_range = ix..ix + filter_text.len();
 972            }
 973        }
 974        result
 975    }
 976}
 977
 978#[cfg(any(test, feature = "test-support"))]
 979impl Default for FakeLspAdapter {
 980    fn default() -> Self {
 981        Self {
 982            name: "the-fake-language-server",
 983            capabilities: lsp::LanguageServer::full_capabilities(),
 984            initializer: None,
 985            disk_based_diagnostics_progress_token: None,
 986            disk_based_diagnostics_sources: Vec::new(),
 987        }
 988    }
 989}
 990
 991#[cfg(any(test, feature = "test-support"))]
 992#[async_trait]
 993impl LspAdapter for Arc<FakeLspAdapter> {
 994    async fn name(&self) -> LanguageServerName {
 995        LanguageServerName(self.name.into())
 996    }
 997
 998    async fn fetch_latest_server_version(
 999        &self,
1000        _: Arc<dyn HttpClient>,
1001    ) -> Result<Box<dyn 'static + Send + Any>> {
1002        unreachable!();
1003    }
1004
1005    async fn fetch_server_binary(
1006        &self,
1007        _: Box<dyn 'static + Send + Any>,
1008        _: Arc<dyn HttpClient>,
1009        _: PathBuf,
1010    ) -> Result<PathBuf> {
1011        unreachable!();
1012    }
1013
1014    async fn cached_server_binary(&self, _: PathBuf) -> Option<PathBuf> {
1015        unreachable!();
1016    }
1017
1018    async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
1019
1020    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
1021        self.disk_based_diagnostics_sources.clone()
1022    }
1023
1024    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
1025        self.disk_based_diagnostics_progress_token.clone()
1026    }
1027}
1028
1029fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
1030    for (ix, name) in query.capture_names().iter().enumerate() {
1031        for (capture_name, index) in captures.iter_mut() {
1032            if capture_name == name {
1033                **index = Some(ix as u32);
1034                break;
1035            }
1036        }
1037    }
1038}
1039
1040pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
1041    lsp::Position::new(point.row, point.column)
1042}
1043
1044pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
1045    PointUtf16::new(point.line, point.character)
1046}
1047
1048pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
1049    lsp::Range {
1050        start: point_to_lsp(range.start),
1051        end: point_to_lsp(range.end),
1052    }
1053}
1054
1055pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
1056    let mut start = point_from_lsp(range.start);
1057    let mut end = point_from_lsp(range.end);
1058    if start > end {
1059        mem::swap(&mut start, &mut end);
1060    }
1061    start..end
1062}