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 rope::point_utf16::PointUtf16;
  26use serde::{de, Deserialize, Deserializer};
  27use serde_json::Value;
  28use std::{
  29    any::Any,
  30    cell::RefCell,
  31    fmt::Debug,
  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}
 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    languages_by_pattern_ix: Vec<Option<Box<str>>>,
 330}
 331
 332struct BracketConfig {
 333    query: Query,
 334    open_capture_ix: u32,
 335    close_capture_ix: u32,
 336}
 337
 338#[derive(Clone)]
 339pub enum LanguageServerBinaryStatus {
 340    CheckingForUpdate,
 341    Downloading,
 342    Downloaded,
 343    Cached,
 344    Failed { error: String },
 345}
 346
 347pub struct LanguageRegistry {
 348    languages: RwLock<Vec<Arc<Language>>>,
 349    language_server_download_dir: Option<Arc<Path>>,
 350    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 351    lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
 352    login_shell_env_loaded: Shared<Task<()>>,
 353    #[allow(clippy::type_complexity)]
 354    lsp_binary_paths: Mutex<
 355        HashMap<
 356            LanguageServerName,
 357            Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>,
 358        >,
 359    >,
 360    subscription: RwLock<(watch::Sender<()>, watch::Receiver<()>)>,
 361    theme: RwLock<Option<Arc<Theme>>>,
 362}
 363
 364impl LanguageRegistry {
 365    pub fn new(login_shell_env_loaded: Task<()>) -> Self {
 366        let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
 367        Self {
 368            language_server_download_dir: None,
 369            languages: Default::default(),
 370            lsp_binary_statuses_tx,
 371            lsp_binary_statuses_rx,
 372            login_shell_env_loaded: login_shell_env_loaded.shared(),
 373            lsp_binary_paths: Default::default(),
 374            subscription: RwLock::new(watch::channel()),
 375            theme: Default::default(),
 376        }
 377    }
 378
 379    #[cfg(any(test, feature = "test-support"))]
 380    pub fn test() -> Self {
 381        Self::new(Task::ready(()))
 382    }
 383
 384    pub fn add(&self, language: Arc<Language>) {
 385        if let Some(theme) = self.theme.read().clone() {
 386            language.set_theme(&theme.editor.syntax);
 387        }
 388        self.languages.write().push(language);
 389        *self.subscription.write().0.borrow_mut() = ();
 390    }
 391
 392    pub fn subscribe(&self) -> watch::Receiver<()> {
 393        self.subscription.read().1.clone()
 394    }
 395
 396    pub fn set_theme(&self, theme: Arc<Theme>) {
 397        *self.theme.write() = Some(theme.clone());
 398        for language in self.languages.read().iter() {
 399            language.set_theme(&theme.editor.syntax);
 400        }
 401    }
 402
 403    pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
 404        self.language_server_download_dir = Some(path.into());
 405    }
 406
 407    pub fn get_language(&self, name: &str) -> Option<Arc<Language>> {
 408        self.languages
 409            .read()
 410            .iter()
 411            .find(|language| language.name().to_lowercase() == name.to_lowercase())
 412            .cloned()
 413    }
 414
 415    pub fn to_vec(&self) -> Vec<Arc<Language>> {
 416        self.languages.read().iter().cloned().collect()
 417    }
 418
 419    pub fn language_names(&self) -> Vec<String> {
 420        self.languages
 421            .read()
 422            .iter()
 423            .map(|language| language.name().to_string())
 424            .collect()
 425    }
 426
 427    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<Arc<Language>> {
 428        let path = path.as_ref();
 429        let filename = path.file_name().and_then(|name| name.to_str());
 430        let extension = path.extension().and_then(|name| name.to_str());
 431        let path_suffixes = [extension, filename];
 432        self.languages
 433            .read()
 434            .iter()
 435            .find(|language| {
 436                language
 437                    .config
 438                    .path_suffixes
 439                    .iter()
 440                    .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
 441            })
 442            .cloned()
 443    }
 444
 445    pub fn start_language_server(
 446        self: &Arc<Self>,
 447        server_id: usize,
 448        language: Arc<Language>,
 449        root_path: Arc<Path>,
 450        http_client: Arc<dyn HttpClient>,
 451        cx: &mut MutableAppContext,
 452    ) -> Option<Task<Result<lsp::LanguageServer>>> {
 453        #[cfg(any(test, feature = "test-support"))]
 454        if language.fake_adapter.is_some() {
 455            let language = language;
 456            return Some(cx.spawn(|cx| async move {
 457                let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
 458                let (server, mut fake_server) = lsp::LanguageServer::fake(
 459                    fake_adapter.name.to_string(),
 460                    fake_adapter.capabilities.clone(),
 461                    cx.clone(),
 462                );
 463
 464                if let Some(initializer) = &fake_adapter.initializer {
 465                    initializer(&mut fake_server);
 466                }
 467
 468                let servers_tx = servers_tx.clone();
 469                cx.background()
 470                    .spawn(async move {
 471                        if fake_server
 472                            .try_receive_notification::<lsp::notification::Initialized>()
 473                            .await
 474                            .is_some()
 475                        {
 476                            servers_tx.unbounded_send(fake_server).ok();
 477                        }
 478                    })
 479                    .detach();
 480                Ok(server)
 481            }));
 482        }
 483
 484        let download_dir = self
 485            .language_server_download_dir
 486            .clone()
 487            .ok_or_else(|| anyhow!("language server download directory has not been assigned"))
 488            .log_err()?;
 489
 490        let this = self.clone();
 491        let adapter = language.adapter.clone()?;
 492        let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
 493        let login_shell_env_loaded = self.login_shell_env_loaded.clone();
 494        Some(cx.spawn(|cx| async move {
 495            login_shell_env_loaded.await;
 496            let server_binary_path = this
 497                .lsp_binary_paths
 498                .lock()
 499                .entry(adapter.name.clone())
 500                .or_insert_with(|| {
 501                    get_server_binary_path(
 502                        adapter.clone(),
 503                        language.clone(),
 504                        http_client,
 505                        download_dir,
 506                        lsp_binary_statuses,
 507                    )
 508                    .map_err(Arc::new)
 509                    .boxed()
 510                    .shared()
 511                })
 512                .clone()
 513                .map_err(|e| anyhow!(e));
 514
 515            let server_binary_path = server_binary_path.await?;
 516            let server_args = &adapter.server_args;
 517            let server = lsp::LanguageServer::new(
 518                server_id,
 519                &server_binary_path,
 520                server_args,
 521                &root_path,
 522                cx,
 523            )?;
 524            Ok(server)
 525        }))
 526    }
 527
 528    pub fn language_server_binary_statuses(
 529        &self,
 530    ) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
 531        self.lsp_binary_statuses_rx.clone()
 532    }
 533}
 534
 535#[cfg(any(test, feature = "test-support"))]
 536impl Default for LanguageRegistry {
 537    fn default() -> Self {
 538        Self::test()
 539    }
 540}
 541
 542async fn get_server_binary_path(
 543    adapter: Arc<CachedLspAdapter>,
 544    language: Arc<Language>,
 545    http_client: Arc<dyn HttpClient>,
 546    download_dir: Arc<Path>,
 547    statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 548) -> Result<PathBuf> {
 549    let container_dir = download_dir.join(adapter.name.0.as_ref());
 550    if !container_dir.exists() {
 551        smol::fs::create_dir_all(&container_dir)
 552            .await
 553            .context("failed to create container directory")?;
 554    }
 555
 556    let path = fetch_latest_server_binary_path(
 557        adapter.clone(),
 558        language.clone(),
 559        http_client,
 560        &container_dir,
 561        statuses.clone(),
 562    )
 563    .await;
 564    if let Err(error) = path.as_ref() {
 565        if let Some(cached_path) = adapter.cached_server_binary(container_dir).await {
 566            statuses
 567                .broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
 568                .await?;
 569            return Ok(cached_path);
 570        } else {
 571            statuses
 572                .broadcast((
 573                    language.clone(),
 574                    LanguageServerBinaryStatus::Failed {
 575                        error: format!("{:?}", error),
 576                    },
 577                ))
 578                .await?;
 579        }
 580    }
 581    path
 582}
 583
 584async fn fetch_latest_server_binary_path(
 585    adapter: Arc<CachedLspAdapter>,
 586    language: Arc<Language>,
 587    http_client: Arc<dyn HttpClient>,
 588    container_dir: &Path,
 589    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
 590) -> Result<PathBuf> {
 591    let container_dir: Arc<Path> = container_dir.into();
 592    lsp_binary_statuses_tx
 593        .broadcast((
 594            language.clone(),
 595            LanguageServerBinaryStatus::CheckingForUpdate,
 596        ))
 597        .await?;
 598    let version_info = adapter
 599        .fetch_latest_server_version(http_client.clone())
 600        .await?;
 601    lsp_binary_statuses_tx
 602        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
 603        .await?;
 604    let path = adapter
 605        .fetch_server_binary(version_info, http_client, container_dir.to_path_buf())
 606        .await?;
 607    lsp_binary_statuses_tx
 608        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
 609        .await?;
 610    Ok(path)
 611}
 612
 613impl Language {
 614    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
 615        Self {
 616            config,
 617            grammar: ts_language.map(|ts_language| {
 618                Arc::new(Grammar {
 619                    id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst),
 620                    highlights_query: None,
 621                    brackets_config: None,
 622                    outline_config: None,
 623                    indents_config: None,
 624                    injection_config: None,
 625                    ts_language,
 626                    highlight_map: Default::default(),
 627                })
 628            }),
 629            adapter: None,
 630
 631            #[cfg(any(test, feature = "test-support"))]
 632            fake_adapter: None,
 633        }
 634    }
 635
 636    pub fn lsp_adapter(&self) -> Option<Arc<CachedLspAdapter>> {
 637        self.adapter.clone()
 638    }
 639
 640    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
 641        let grammar = self.grammar_mut();
 642        grammar.highlights_query = Some(Query::new(grammar.ts_language, source)?);
 643        Ok(self)
 644    }
 645
 646    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
 647        let grammar = self.grammar_mut();
 648        let query = Query::new(grammar.ts_language, source)?;
 649        let mut open_capture_ix = None;
 650        let mut close_capture_ix = None;
 651        get_capture_indices(
 652            &query,
 653            &mut [
 654                ("open", &mut open_capture_ix),
 655                ("close", &mut close_capture_ix),
 656            ],
 657        );
 658        if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
 659            grammar.brackets_config = Some(BracketConfig {
 660                query,
 661                open_capture_ix,
 662                close_capture_ix,
 663            });
 664        }
 665        Ok(self)
 666    }
 667
 668    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
 669        let grammar = self.grammar_mut();
 670        let query = Query::new(grammar.ts_language, source)?;
 671        let mut indent_capture_ix = None;
 672        let mut start_capture_ix = None;
 673        let mut end_capture_ix = None;
 674        get_capture_indices(
 675            &query,
 676            &mut [
 677                ("indent", &mut indent_capture_ix),
 678                ("start", &mut start_capture_ix),
 679                ("end", &mut end_capture_ix),
 680            ],
 681        );
 682        if let Some(indent_capture_ix) = indent_capture_ix {
 683            grammar.indents_config = Some(IndentConfig {
 684                query,
 685                indent_capture_ix,
 686                start_capture_ix,
 687                end_capture_ix,
 688            });
 689        }
 690        Ok(self)
 691    }
 692
 693    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
 694        let grammar = self.grammar_mut();
 695        let query = Query::new(grammar.ts_language, source)?;
 696        let mut item_capture_ix = None;
 697        let mut name_capture_ix = None;
 698        let mut context_capture_ix = None;
 699        get_capture_indices(
 700            &query,
 701            &mut [
 702                ("item", &mut item_capture_ix),
 703                ("name", &mut name_capture_ix),
 704                ("context", &mut context_capture_ix),
 705            ],
 706        );
 707        if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
 708            grammar.outline_config = Some(OutlineConfig {
 709                query,
 710                item_capture_ix,
 711                name_capture_ix,
 712                context_capture_ix,
 713            });
 714        }
 715        Ok(self)
 716    }
 717
 718    pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
 719        let grammar = self.grammar_mut();
 720        let query = Query::new(grammar.ts_language, source)?;
 721        let mut language_capture_ix = None;
 722        let mut content_capture_ix = None;
 723        get_capture_indices(
 724            &query,
 725            &mut [
 726                ("language", &mut language_capture_ix),
 727                ("content", &mut content_capture_ix),
 728            ],
 729        );
 730        let languages_by_pattern_ix = (0..query.pattern_count())
 731            .map(|ix| {
 732                query.property_settings(ix).iter().find_map(|setting| {
 733                    if setting.key.as_ref() == "language" {
 734                        return setting.value.clone();
 735                    } else {
 736                        None
 737                    }
 738                })
 739            })
 740            .collect();
 741        if let Some(content_capture_ix) = content_capture_ix {
 742            grammar.injection_config = Some(InjectionConfig {
 743                query,
 744                language_capture_ix,
 745                content_capture_ix,
 746                languages_by_pattern_ix,
 747            });
 748        }
 749        Ok(self)
 750    }
 751
 752    fn grammar_mut(&mut self) -> &mut Grammar {
 753        Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
 754    }
 755
 756    pub fn with_lsp_adapter(mut self, lsp_adapter: Arc<CachedLspAdapter>) -> Self {
 757        self.adapter = Some(lsp_adapter);
 758        self
 759    }
 760
 761    #[cfg(any(test, feature = "test-support"))]
 762    pub async fn set_fake_lsp_adapter(
 763        &mut self,
 764        fake_lsp_adapter: Arc<FakeLspAdapter>,
 765    ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
 766        let (servers_tx, servers_rx) = mpsc::unbounded();
 767        self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
 768        let adapter = CachedLspAdapter::new(fake_lsp_adapter).await;
 769        self.adapter = Some(adapter);
 770        servers_rx
 771    }
 772
 773    pub fn name(&self) -> Arc<str> {
 774        self.config.name.clone()
 775    }
 776
 777    pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
 778        self.config.line_comment.as_ref()
 779    }
 780
 781    pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
 782        self.config
 783            .block_comment
 784            .as_ref()
 785            .map(|(start, end)| (start, end))
 786    }
 787
 788    pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
 789        match self.adapter.as_ref() {
 790            Some(adapter) => &adapter.disk_based_diagnostic_sources,
 791            None => &[],
 792        }
 793    }
 794
 795    pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
 796        if let Some(adapter) = self.adapter.as_ref() {
 797            adapter.disk_based_diagnostics_progress_token.as_deref()
 798        } else {
 799            None
 800        }
 801    }
 802
 803    pub async fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
 804        if let Some(processor) = self.adapter.as_ref() {
 805            processor.process_diagnostics(diagnostics).await;
 806        }
 807    }
 808
 809    pub async fn label_for_completion(
 810        self: &Arc<Self>,
 811        completion: &lsp::CompletionItem,
 812    ) -> Option<CodeLabel> {
 813        self.adapter
 814            .as_ref()?
 815            .label_for_completion(completion, self)
 816            .await
 817    }
 818
 819    pub async fn label_for_symbol(
 820        self: &Arc<Self>,
 821        name: &str,
 822        kind: lsp::SymbolKind,
 823    ) -> Option<CodeLabel> {
 824        self.adapter
 825            .as_ref()?
 826            .label_for_symbol(name, kind, self)
 827            .await
 828    }
 829
 830    pub fn highlight_text<'a>(
 831        self: &'a Arc<Self>,
 832        text: &'a Rope,
 833        range: Range<usize>,
 834    ) -> Vec<(Range<usize>, HighlightId)> {
 835        let mut result = Vec::new();
 836        if let Some(grammar) = &self.grammar {
 837            let tree = grammar.parse_text(text, None);
 838            let captures =
 839                SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
 840                    grammar.highlights_query.as_ref()
 841                });
 842            let highlight_maps = vec![grammar.highlight_map()];
 843            let mut offset = 0;
 844            for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) {
 845                let end_offset = offset + chunk.text.len();
 846                if let Some(highlight_id) = chunk.syntax_highlight_id {
 847                    if !highlight_id.is_default() {
 848                        result.push((offset..end_offset, highlight_id));
 849                    }
 850                }
 851                offset = end_offset;
 852            }
 853        }
 854        result
 855    }
 856
 857    pub fn brackets(&self) -> &[BracketPair] {
 858        &self.config.brackets
 859    }
 860
 861    pub fn path_suffixes(&self) -> &[String] {
 862        &self.config.path_suffixes
 863    }
 864
 865    pub fn should_autoclose_before(&self, c: char) -> bool {
 866        c.is_whitespace() || self.config.autoclose_before.contains(c)
 867    }
 868
 869    pub fn set_theme(&self, theme: &SyntaxTheme) {
 870        if let Some(grammar) = self.grammar.as_ref() {
 871            if let Some(highlights_query) = &grammar.highlights_query {
 872                *grammar.highlight_map.lock() =
 873                    HighlightMap::new(highlights_query.capture_names(), theme);
 874            }
 875        }
 876    }
 877
 878    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
 879        self.grammar.as_ref()
 880    }
 881}
 882
 883impl Debug for Language {
 884    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 885        f.debug_struct("Language")
 886            .field("name", &self.config.name)
 887            .finish()
 888    }
 889}
 890
 891impl Grammar {
 892    pub fn id(&self) -> usize {
 893        self.id
 894    }
 895
 896    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
 897        PARSER.with(|parser| {
 898            let mut parser = parser.borrow_mut();
 899            parser
 900                .set_language(self.ts_language)
 901                .expect("incompatible grammar");
 902            let mut chunks = text.chunks_in_range(0..text.len());
 903            parser
 904                .parse_with(
 905                    &mut move |offset, _| {
 906                        chunks.seek(offset);
 907                        chunks.next().unwrap_or("").as_bytes()
 908                    },
 909                    old_tree.as_ref(),
 910                )
 911                .unwrap()
 912        })
 913    }
 914
 915    pub fn highlight_map(&self) -> HighlightMap {
 916        self.highlight_map.lock().clone()
 917    }
 918
 919    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
 920        let capture_id = self
 921            .highlights_query
 922            .as_ref()?
 923            .capture_index_for_name(name)?;
 924        Some(self.highlight_map.lock().get(capture_id))
 925    }
 926}
 927
 928impl CodeLabel {
 929    pub fn plain(text: String, filter_text: Option<&str>) -> Self {
 930        let mut result = Self {
 931            runs: Vec::new(),
 932            filter_range: 0..text.len(),
 933            text,
 934        };
 935        if let Some(filter_text) = filter_text {
 936            if let Some(ix) = result.text.find(filter_text) {
 937                result.filter_range = ix..ix + filter_text.len();
 938            }
 939        }
 940        result
 941    }
 942}
 943
 944#[cfg(any(test, feature = "test-support"))]
 945impl Default for FakeLspAdapter {
 946    fn default() -> Self {
 947        Self {
 948            name: "the-fake-language-server",
 949            capabilities: lsp::LanguageServer::full_capabilities(),
 950            initializer: None,
 951            disk_based_diagnostics_progress_token: None,
 952            disk_based_diagnostics_sources: Vec::new(),
 953        }
 954    }
 955}
 956
 957#[cfg(any(test, feature = "test-support"))]
 958#[async_trait]
 959impl LspAdapter for Arc<FakeLspAdapter> {
 960    async fn name(&self) -> LanguageServerName {
 961        LanguageServerName(self.name.into())
 962    }
 963
 964    async fn fetch_latest_server_version(
 965        &self,
 966        _: Arc<dyn HttpClient>,
 967    ) -> Result<Box<dyn 'static + Send + Any>> {
 968        unreachable!();
 969    }
 970
 971    async fn fetch_server_binary(
 972        &self,
 973        _: Box<dyn 'static + Send + Any>,
 974        _: Arc<dyn HttpClient>,
 975        _: PathBuf,
 976    ) -> Result<PathBuf> {
 977        unreachable!();
 978    }
 979
 980    async fn cached_server_binary(&self, _: PathBuf) -> Option<PathBuf> {
 981        unreachable!();
 982    }
 983
 984    async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
 985
 986    async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
 987        self.disk_based_diagnostics_sources.clone()
 988    }
 989
 990    async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
 991        self.disk_based_diagnostics_progress_token.clone()
 992    }
 993}
 994
 995fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
 996    for (ix, name) in query.capture_names().iter().enumerate() {
 997        for (capture_name, index) in captures.iter_mut() {
 998            if capture_name == name {
 999                **index = Some(ix as u32);
1000                break;
1001            }
1002        }
1003    }
1004}
1005
1006pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
1007    lsp::Position::new(point.row, point.column)
1008}
1009
1010pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
1011    PointUtf16::new(point.line, point.character)
1012}
1013
1014pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
1015    lsp::Range {
1016        start: point_to_lsp(range.start),
1017        end: point_to_lsp(range.end),
1018    }
1019}
1020
1021pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
1022    let mut start = point_from_lsp(range.start);
1023    let mut end = point_from_lsp(range.end);
1024    if start > end {
1025        mem::swap(&mut start, &mut end);
1026    }
1027    start..end
1028}