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