language.rs

  1mod buffer;
  2mod diagnostic_set;
  3mod highlight_map;
  4mod outline;
  5pub mod proto;
  6#[cfg(test)]
  7mod tests;
  8
  9use anyhow::{anyhow, Result};
 10use client::http::{self, HttpClient};
 11use collections::HashSet;
 12use futures::{
 13    future::{BoxFuture, Shared},
 14    FutureExt, TryFutureExt,
 15};
 16use gpui::{AppContext, Task};
 17use highlight_map::HighlightMap;
 18use lazy_static::lazy_static;
 19use parking_lot::Mutex;
 20use serde::Deserialize;
 21use std::{
 22    cell::RefCell,
 23    ops::Range,
 24    path::{Path, PathBuf},
 25    str,
 26    sync::Arc,
 27};
 28use theme::SyntaxTheme;
 29use tree_sitter::{self, Query};
 30use util::ResultExt;
 31
 32#[cfg(any(test, feature = "test-support"))]
 33use futures::channel::mpsc;
 34
 35pub use buffer::Operation;
 36pub use buffer::*;
 37pub use diagnostic_set::DiagnosticEntry;
 38pub use outline::{Outline, OutlineItem};
 39pub use tree_sitter::{Parser, Tree};
 40
 41thread_local! {
 42    static PARSER: RefCell<Parser>  = RefCell::new(Parser::new());
 43}
 44
 45lazy_static! {
 46    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
 47        LanguageConfig {
 48            name: "Plain Text".to_string(),
 49            path_suffixes: Default::default(),
 50            brackets: Default::default(),
 51            line_comment: None,
 52            language_server: None,
 53        },
 54        None,
 55    ));
 56}
 57
 58pub trait ToLspPosition {
 59    fn to_lsp_position(self) -> lsp::Position;
 60}
 61
 62pub struct LspBinaryVersion {
 63    pub name: String,
 64    pub url: http::Url,
 65}
 66
 67pub trait LspExt: 'static + Send + Sync {
 68    fn fetch_latest_server_version(
 69        &self,
 70        http: Arc<dyn HttpClient>,
 71    ) -> BoxFuture<'static, Result<LspBinaryVersion>>;
 72    fn fetch_server_binary(
 73        &self,
 74        version: LspBinaryVersion,
 75        http: Arc<dyn HttpClient>,
 76        download_dir: Arc<Path>,
 77    ) -> BoxFuture<'static, Result<PathBuf>>;
 78    fn cached_server_binary(&self, download_dir: Arc<Path>) -> BoxFuture<'static, Option<PathBuf>>;
 79    fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
 80    fn label_for_completion(&self, _: &lsp::CompletionItem, _: &Language) -> Option<CodeLabel> {
 81        None
 82    }
 83    fn label_for_symbol(&self, _: &lsp::SymbolInformation, _: &Language) -> Option<CodeLabel> {
 84        None
 85    }
 86}
 87
 88#[derive(Clone, Debug, PartialEq, Eq)]
 89pub struct CodeLabel {
 90    pub text: String,
 91    pub runs: Vec<(Range<usize>, HighlightId)>,
 92    pub filter_range: Range<usize>,
 93}
 94
 95#[derive(Default, Deserialize)]
 96pub struct LanguageConfig {
 97    pub name: String,
 98    pub path_suffixes: Vec<String>,
 99    pub brackets: Vec<BracketPair>,
100    pub line_comment: Option<String>,
101    pub language_server: Option<LanguageServerConfig>,
102}
103
104#[derive(Default, Deserialize)]
105pub struct LanguageServerConfig {
106    pub disk_based_diagnostic_sources: HashSet<String>,
107    pub disk_based_diagnostics_progress_token: Option<String>,
108    #[cfg(any(test, feature = "test-support"))]
109    #[serde(skip)]
110    fake_config: Option<FakeLanguageServerConfig>,
111}
112
113#[cfg(any(test, feature = "test-support"))]
114struct FakeLanguageServerConfig {
115    servers_tx: mpsc::UnboundedSender<lsp::FakeLanguageServer>,
116    capabilities: lsp::ServerCapabilities,
117    initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
118}
119
120#[derive(Clone, Debug, Deserialize)]
121pub struct BracketPair {
122    pub start: String,
123    pub end: String,
124    pub close: bool,
125    pub newline: bool,
126}
127
128pub struct Language {
129    pub(crate) config: LanguageConfig,
130    pub(crate) grammar: Option<Arc<Grammar>>,
131    pub(crate) lsp_ext: Option<Arc<dyn LspExt>>,
132    lsp_binary_path: Mutex<Option<Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>>>,
133}
134
135pub struct Grammar {
136    pub(crate) ts_language: tree_sitter::Language,
137    pub(crate) highlights_query: Query,
138    pub(crate) brackets_query: Query,
139    pub(crate) indents_query: Query,
140    pub(crate) outline_query: Query,
141    pub(crate) highlight_map: Mutex<HighlightMap>,
142}
143
144#[derive(Clone)]
145pub enum LanguageServerBinaryStatus {
146    CheckingForUpdate,
147    Downloading,
148    Downloaded,
149    Cached,
150    Failed,
151}
152
153pub struct LanguageRegistry {
154    languages: Vec<Arc<Language>>,
155    language_server_download_dir: Option<Arc<Path>>,
156    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
157    lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
158}
159
160impl LanguageRegistry {
161    pub fn new() -> Self {
162        let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
163        Self {
164            language_server_download_dir: None,
165            languages: Default::default(),
166            lsp_binary_statuses_tx,
167            lsp_binary_statuses_rx,
168        }
169    }
170
171    pub fn add(&mut self, language: Arc<Language>) {
172        self.languages.push(language.clone());
173    }
174
175    pub fn set_theme(&self, theme: &SyntaxTheme) {
176        for language in &self.languages {
177            language.set_theme(theme);
178        }
179    }
180
181    pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
182        self.language_server_download_dir = Some(path.into());
183    }
184
185    pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
186        self.languages
187            .iter()
188            .find(|language| language.name() == name)
189    }
190
191    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
192        let path = path.as_ref();
193        let filename = path.file_name().and_then(|name| name.to_str());
194        let extension = path.extension().and_then(|name| name.to_str());
195        let path_suffixes = [extension, filename];
196        self.languages.iter().find(|language| {
197            language
198                .config
199                .path_suffixes
200                .iter()
201                .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
202        })
203    }
204
205    pub fn start_language_server(
206        &self,
207        language: &Arc<Language>,
208        root_path: Arc<Path>,
209        http_client: Arc<dyn HttpClient>,
210        cx: &AppContext,
211    ) -> Option<Task<Result<Arc<lsp::LanguageServer>>>> {
212        #[cfg(any(test, feature = "test-support"))]
213        if let Some(config) = &language.config.language_server {
214            if let Some(fake_config) = &config.fake_config {
215                use postage::prelude::Stream;
216
217                let (server, mut fake_server) = lsp::LanguageServer::fake_with_capabilities(
218                    fake_config.capabilities.clone(),
219                    cx.background().clone(),
220                );
221
222                if let Some(initalizer) = &fake_config.initializer {
223                    initalizer(&mut fake_server);
224                }
225
226                let servers_tx = fake_config.servers_tx.clone();
227                let mut initialized = server.capabilities();
228                cx.background()
229                    .spawn(async move {
230                        while initialized.recv().await.is_none() {}
231                        servers_tx.unbounded_send(fake_server).ok();
232                    })
233                    .detach();
234
235                return Some(Task::ready(Ok(server.clone())));
236            }
237        }
238
239        let download_dir = self
240            .language_server_download_dir
241            .clone()
242            .ok_or_else(|| anyhow!("language server download directory has not been assigned"))
243            .log_err()?;
244
245        let lsp_ext = language.lsp_ext.clone()?;
246        let background = cx.background().clone();
247        let server_binary_path = {
248            Some(
249                language
250                    .lsp_binary_path
251                    .lock()
252                    .get_or_insert_with(|| {
253                        get_server_binary_path(
254                            lsp_ext,
255                            language.clone(),
256                            http_client,
257                            download_dir,
258                            self.lsp_binary_statuses_tx.clone(),
259                        )
260                        .map_err(Arc::new)
261                        .boxed()
262                        .shared()
263                    })
264                    .clone()
265                    .map_err(|e| anyhow!(e)),
266            )
267        }?;
268        Some(cx.background().spawn(async move {
269            let server_binary_path = server_binary_path.await?;
270            let server = lsp::LanguageServer::new(&server_binary_path, &root_path, background)?;
271            Ok(server)
272        }))
273    }
274
275    pub fn language_server_binary_statuses(
276        &self,
277    ) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
278        self.lsp_binary_statuses_rx.clone()
279    }
280}
281
282async fn get_server_binary_path(
283    lsp_ext: Arc<dyn LspExt>,
284    language: Arc<Language>,
285    http_client: Arc<dyn HttpClient>,
286    download_dir: Arc<Path>,
287    statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
288) -> Result<PathBuf> {
289    let path = fetch_latest_server_binary_path(
290        lsp_ext.clone(),
291        language.clone(),
292        http_client,
293        download_dir.clone(),
294        statuses.clone(),
295    )
296    .await;
297    if path.is_err() {
298        if let Some(cached_path) = lsp_ext.cached_server_binary(download_dir).await {
299            statuses
300                .broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
301                .await?;
302            return Ok(cached_path);
303        } else {
304            statuses
305                .broadcast((language.clone(), LanguageServerBinaryStatus::Failed))
306                .await?;
307        }
308    }
309    path
310}
311
312async fn fetch_latest_server_binary_path(
313    lsp_ext: Arc<dyn LspExt>,
314    language: Arc<Language>,
315    http_client: Arc<dyn HttpClient>,
316    download_dir: Arc<Path>,
317    lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
318) -> Result<PathBuf> {
319    lsp_binary_statuses_tx
320        .broadcast((
321            language.clone(),
322            LanguageServerBinaryStatus::CheckingForUpdate,
323        ))
324        .await?;
325    let version_info = lsp_ext
326        .fetch_latest_server_version(http_client.clone())
327        .await?;
328    lsp_binary_statuses_tx
329        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
330        .await?;
331    let path = lsp_ext
332        .fetch_server_binary(version_info, http_client, download_dir)
333        .await?;
334    lsp_binary_statuses_tx
335        .broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
336        .await?;
337    Ok(path)
338}
339
340impl Language {
341    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
342        Self {
343            config,
344            grammar: ts_language.map(|ts_language| {
345                Arc::new(Grammar {
346                    brackets_query: Query::new(ts_language, "").unwrap(),
347                    highlights_query: Query::new(ts_language, "").unwrap(),
348                    indents_query: Query::new(ts_language, "").unwrap(),
349                    outline_query: Query::new(ts_language, "").unwrap(),
350                    ts_language,
351                    highlight_map: Default::default(),
352                })
353            }),
354            lsp_ext: None,
355            lsp_binary_path: Default::default(),
356        }
357    }
358
359    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
360        let grammar = self
361            .grammar
362            .as_mut()
363            .and_then(Arc::get_mut)
364            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
365        grammar.highlights_query = Query::new(grammar.ts_language, source)?;
366        Ok(self)
367    }
368
369    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
370        let grammar = self
371            .grammar
372            .as_mut()
373            .and_then(Arc::get_mut)
374            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
375        grammar.brackets_query = Query::new(grammar.ts_language, source)?;
376        Ok(self)
377    }
378
379    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
380        let grammar = self
381            .grammar
382            .as_mut()
383            .and_then(Arc::get_mut)
384            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
385        grammar.indents_query = Query::new(grammar.ts_language, source)?;
386        Ok(self)
387    }
388
389    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
390        let grammar = self
391            .grammar
392            .as_mut()
393            .and_then(Arc::get_mut)
394            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
395        grammar.outline_query = Query::new(grammar.ts_language, source)?;
396        Ok(self)
397    }
398
399    pub fn with_lsp_ext(mut self, lsp_ext: impl LspExt) -> Self {
400        self.lsp_ext = Some(Arc::new(lsp_ext));
401        self
402    }
403
404    pub fn name(&self) -> &str {
405        self.config.name.as_str()
406    }
407
408    pub fn line_comment_prefix(&self) -> Option<&str> {
409        self.config.line_comment.as_deref()
410    }
411
412    pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
413        self.config
414            .language_server
415            .as_ref()
416            .map(|config| &config.disk_based_diagnostic_sources)
417    }
418
419    pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
420        self.config
421            .language_server
422            .as_ref()
423            .and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
424    }
425
426    pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
427        if let Some(processor) = self.lsp_ext.as_ref() {
428            processor.process_diagnostics(diagnostics);
429        }
430    }
431
432    pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<CodeLabel> {
433        self.lsp_ext
434            .as_ref()?
435            .label_for_completion(completion, self)
436    }
437
438    pub fn label_for_symbol(&self, symbol: &lsp::SymbolInformation) -> Option<CodeLabel> {
439        self.lsp_ext.as_ref()?.label_for_symbol(symbol, self)
440    }
441
442    pub fn highlight_text<'a>(
443        &'a self,
444        text: &'a Rope,
445        range: Range<usize>,
446    ) -> Vec<(Range<usize>, HighlightId)> {
447        let mut result = Vec::new();
448        if let Some(grammar) = &self.grammar {
449            let tree = grammar.parse_text(text, None);
450            let mut offset = 0;
451            for chunk in BufferChunks::new(text, range, Some(&tree), self.grammar.as_ref(), vec![])
452            {
453                let end_offset = offset + chunk.text.len();
454                if let Some(highlight_id) = chunk.highlight_id {
455                    result.push((offset..end_offset, highlight_id));
456                }
457                offset = end_offset;
458            }
459        }
460        result
461    }
462
463    pub fn brackets(&self) -> &[BracketPair] {
464        &self.config.brackets
465    }
466
467    pub fn set_theme(&self, theme: &SyntaxTheme) {
468        if let Some(grammar) = self.grammar.as_ref() {
469            *grammar.highlight_map.lock() =
470                HighlightMap::new(grammar.highlights_query.capture_names(), theme);
471        }
472    }
473
474    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
475        self.grammar.as_ref()
476    }
477}
478
479impl Grammar {
480    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
481        PARSER.with(|parser| {
482            let mut parser = parser.borrow_mut();
483            parser
484                .set_language(self.ts_language)
485                .expect("incompatible grammar");
486            let mut chunks = text.chunks_in_range(0..text.len());
487            parser
488                .parse_with(
489                    &mut move |offset, _| {
490                        chunks.seek(offset);
491                        chunks.next().unwrap_or("").as_bytes()
492                    },
493                    old_tree.as_ref(),
494                )
495                .unwrap()
496        })
497    }
498
499    pub fn highlight_map(&self) -> HighlightMap {
500        self.highlight_map.lock().clone()
501    }
502
503    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
504        let capture_id = self.highlights_query.capture_index_for_name(name)?;
505        Some(self.highlight_map.lock().get(capture_id))
506    }
507}
508
509impl CodeLabel {
510    pub fn plain(text: String, filter_text: Option<&str>) -> Self {
511        let mut result = Self {
512            runs: Vec::new(),
513            filter_range: 0..text.len(),
514            text,
515        };
516        if let Some(filter_text) = filter_text {
517            if let Some(ix) = result.text.find(filter_text) {
518                result.filter_range = ix..ix + filter_text.len();
519            }
520        }
521        result
522    }
523}
524
525#[cfg(any(test, feature = "test-support"))]
526impl LanguageServerConfig {
527    pub fn fake() -> (Self, mpsc::UnboundedReceiver<lsp::FakeLanguageServer>) {
528        let (servers_tx, servers_rx) = mpsc::unbounded();
529        (
530            Self {
531                fake_config: Some(FakeLanguageServerConfig {
532                    servers_tx,
533                    capabilities: Default::default(),
534                    initializer: None,
535                }),
536                disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
537                ..Default::default()
538            },
539            servers_rx,
540        )
541    }
542
543    pub fn set_fake_capabilities(&mut self, capabilities: lsp::ServerCapabilities) {
544        self.fake_config.as_mut().unwrap().capabilities = capabilities;
545    }
546
547    pub fn set_fake_initializer(
548        &mut self,
549        initializer: impl 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer),
550    ) {
551        self.fake_config.as_mut().unwrap().initializer = Some(Box::new(initializer));
552    }
553}
554
555impl ToLspPosition for PointUtf16 {
556    fn to_lsp_position(self) -> lsp::Position {
557        lsp::Position::new(self.row, self.column)
558    }
559}
560
561pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
562    PointUtf16::new(point.line, point.character)
563}
564
565pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
566    let start = PointUtf16::new(range.start.line, range.start.character);
567    let end = PointUtf16::new(range.end.line, range.end.character);
568    start..end
569}