language.rs

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