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 collections::HashSet;
 11use futures::{
 12    future::{BoxFuture, Shared},
 13    FutureExt,
 14};
 15use gpui::{AppContext, Task};
 16use highlight_map::HighlightMap;
 17use lazy_static::lazy_static;
 18use parking_lot::Mutex;
 19use serde::Deserialize;
 20use std::{
 21    cell::RefCell,
 22    future::Future,
 23    ops::Range,
 24    path::{Path, PathBuf},
 25    str,
 26    sync::Arc,
 27};
 28use theme::SyntaxTheme;
 29use tree_sitter::{self, Query};
 30
 31#[cfg(any(test, feature = "test-support"))]
 32use futures::channel::mpsc;
 33
 34pub use buffer::Operation;
 35pub use buffer::*;
 36pub use diagnostic_set::DiagnosticEntry;
 37pub use outline::{Outline, OutlineItem};
 38pub use tree_sitter::{Parser, Tree};
 39
 40thread_local! {
 41    static PARSER: RefCell<Parser>  = RefCell::new(Parser::new());
 42}
 43
 44lazy_static! {
 45    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
 46        LanguageConfig {
 47            name: "Plain Text".to_string(),
 48            path_suffixes: Default::default(),
 49            brackets: Default::default(),
 50            line_comment: None,
 51            language_server: None,
 52        },
 53        None,
 54    ));
 55}
 56
 57pub trait ToLspPosition {
 58    fn to_lsp_position(self) -> lsp::Position;
 59}
 60
 61pub trait LspExt: 'static + Send + Sync {
 62    fn server_bin_path(&self) -> BoxFuture<'static, Option<PathBuf>>;
 63    fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
 64    fn label_for_completion(
 65        &self,
 66        _: &lsp::CompletionItem,
 67        _: &Language,
 68    ) -> Option<CompletionLabel> {
 69        None
 70    }
 71}
 72
 73#[derive(Clone, Debug, PartialEq, Eq)]
 74pub struct CompletionLabel {
 75    pub text: String,
 76    pub runs: Vec<(Range<usize>, HighlightId)>,
 77    pub filter_range: Range<usize>,
 78    pub left_aligned_len: usize,
 79}
 80
 81#[derive(Default, Deserialize)]
 82pub struct LanguageConfig {
 83    pub name: String,
 84    pub path_suffixes: Vec<String>,
 85    pub brackets: Vec<BracketPair>,
 86    pub line_comment: Option<String>,
 87    pub language_server: Option<LanguageServerConfig>,
 88}
 89
 90#[derive(Default, Deserialize)]
 91pub struct LanguageServerConfig {
 92    pub disk_based_diagnostic_sources: HashSet<String>,
 93    pub disk_based_diagnostics_progress_token: Option<String>,
 94    #[cfg(any(test, feature = "test-support"))]
 95    #[serde(skip)]
 96    fake_config: Option<FakeLanguageServerConfig>,
 97}
 98
 99#[cfg(any(test, feature = "test-support"))]
100struct FakeLanguageServerConfig {
101    servers_tx: mpsc::UnboundedSender<lsp::FakeLanguageServer>,
102    capabilities: lsp::ServerCapabilities,
103    initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
104}
105
106#[derive(Clone, Debug, Deserialize)]
107pub struct BracketPair {
108    pub start: String,
109    pub end: String,
110    pub close: bool,
111    pub newline: bool,
112}
113
114pub struct Language {
115    pub(crate) config: LanguageConfig,
116    pub(crate) grammar: Option<Arc<Grammar>>,
117    pub(crate) lsp_ext: Option<Box<dyn LspExt>>,
118    lsp_binary_path: Mutex<Option<Shared<BoxFuture<'static, Option<PathBuf>>>>>,
119}
120
121pub struct Grammar {
122    pub(crate) ts_language: tree_sitter::Language,
123    pub(crate) highlights_query: Query,
124    pub(crate) brackets_query: Query,
125    pub(crate) indents_query: Query,
126    pub(crate) outline_query: Query,
127    pub(crate) highlight_map: Mutex<HighlightMap>,
128}
129
130#[derive(Default)]
131pub struct LanguageRegistry {
132    languages: Vec<Arc<Language>>,
133}
134
135impl LanguageRegistry {
136    pub fn new() -> Self {
137        Self::default()
138    }
139
140    pub fn add(&mut self, language: Arc<Language>) {
141        self.languages.push(language);
142    }
143
144    pub fn set_theme(&self, theme: &SyntaxTheme) {
145        for language in &self.languages {
146            language.set_theme(theme);
147        }
148    }
149
150    pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
151        self.languages
152            .iter()
153            .find(|language| language.name() == name)
154    }
155
156    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
157        let path = path.as_ref();
158        let filename = path.file_name().and_then(|name| name.to_str());
159        let extension = path.extension().and_then(|name| name.to_str());
160        let path_suffixes = [extension, filename];
161        self.languages.iter().find(|language| {
162            language
163                .config
164                .path_suffixes
165                .iter()
166                .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
167        })
168    }
169}
170
171impl Language {
172    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
173        Self {
174            config,
175            grammar: ts_language.map(|ts_language| {
176                Arc::new(Grammar {
177                    brackets_query: Query::new(ts_language, "").unwrap(),
178                    highlights_query: Query::new(ts_language, "").unwrap(),
179                    indents_query: Query::new(ts_language, "").unwrap(),
180                    outline_query: Query::new(ts_language, "").unwrap(),
181                    ts_language,
182                    highlight_map: Default::default(),
183                })
184            }),
185            lsp_ext: None,
186            lsp_binary_path: Default::default(),
187        }
188    }
189
190    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
191        let grammar = self
192            .grammar
193            .as_mut()
194            .and_then(Arc::get_mut)
195            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
196        grammar.highlights_query = Query::new(grammar.ts_language, source)?;
197        Ok(self)
198    }
199
200    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
201        let grammar = self
202            .grammar
203            .as_mut()
204            .and_then(Arc::get_mut)
205            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
206        grammar.brackets_query = Query::new(grammar.ts_language, source)?;
207        Ok(self)
208    }
209
210    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
211        let grammar = self
212            .grammar
213            .as_mut()
214            .and_then(Arc::get_mut)
215            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
216        grammar.indents_query = Query::new(grammar.ts_language, source)?;
217        Ok(self)
218    }
219
220    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
221        let grammar = self
222            .grammar
223            .as_mut()
224            .and_then(Arc::get_mut)
225            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
226        grammar.outline_query = Query::new(grammar.ts_language, source)?;
227        Ok(self)
228    }
229
230    pub fn with_lsp_ext(mut self, processor: impl LspExt) -> Self {
231        self.lsp_ext = Some(Box::new(processor));
232        self
233    }
234
235    pub fn name(&self) -> &str {
236        self.config.name.as_str()
237    }
238
239    pub fn line_comment_prefix(&self) -> Option<&str> {
240        self.config.line_comment.as_deref()
241    }
242
243    pub fn start_server(
244        &self,
245        root_path: Arc<Path>,
246        cx: &AppContext,
247    ) -> Task<Result<Option<Arc<lsp::LanguageServer>>>> {
248        #[cfg(any(test, feature = "test-support"))]
249        if let Some(config) = &self.config.language_server {
250            if let Some(fake_config) = &config.fake_config {
251                use postage::prelude::Stream;
252
253                let (server, mut fake_server) = lsp::LanguageServer::fake_with_capabilities(
254                    fake_config.capabilities.clone(),
255                    cx.background().clone(),
256                );
257
258                if let Some(initalizer) = &fake_config.initializer {
259                    initalizer(&mut fake_server);
260                }
261
262                let servers_tx = fake_config.servers_tx.clone();
263                let mut initialized = server.capabilities();
264                cx.background()
265                    .spawn(async move {
266                        while initialized.recv().await.is_none() {}
267                        servers_tx.unbounded_send(fake_server).ok();
268                    })
269                    .detach();
270
271                return Task::ready(Ok(Some(server.clone())));
272            }
273        }
274
275        let background = cx.background().clone();
276        let server_binary_path = self
277            .lsp_binary_path()
278            .ok_or_else(|| anyhow!("cannot locate or download language server"));
279        cx.background().spawn(async move {
280            if let Some(server_binary_path) = server_binary_path?.await {
281                let server = lsp::LanguageServer::new(&server_binary_path, &root_path, background)?;
282                Ok(Some(server))
283            } else {
284                Ok(None)
285            }
286        })
287    }
288
289    pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
290        self.config
291            .language_server
292            .as_ref()
293            .map(|config| &config.disk_based_diagnostic_sources)
294    }
295
296    pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
297        self.config
298            .language_server
299            .as_ref()
300            .and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
301    }
302
303    pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
304        if let Some(processor) = self.lsp_ext.as_ref() {
305            processor.process_diagnostics(diagnostics);
306        }
307    }
308
309    pub fn label_for_completion(
310        &self,
311        completion: &lsp::CompletionItem,
312    ) -> Option<CompletionLabel> {
313        self.lsp_ext
314            .as_ref()?
315            .label_for_completion(completion, self)
316    }
317
318    pub fn highlight_text<'a>(
319        &'a self,
320        text: &'a Rope,
321        range: Range<usize>,
322    ) -> Vec<(Range<usize>, HighlightId)> {
323        let mut result = Vec::new();
324        if let Some(grammar) = &self.grammar {
325            let tree = grammar.parse_text(text, None);
326            let mut offset = 0;
327            for chunk in BufferChunks::new(text, range, Some(&tree), self.grammar.as_ref(), vec![])
328            {
329                let end_offset = offset + chunk.text.len();
330                if let Some(highlight_id) = chunk.highlight_id {
331                    result.push((offset..end_offset, highlight_id));
332                }
333                offset = end_offset;
334            }
335        }
336        result
337    }
338
339    fn lsp_binary_path(&self) -> Option<impl Future<Output = Option<PathBuf>>> {
340        if let Some(lsp_ext) = self.lsp_ext.as_ref() {
341            Some(
342                self.lsp_binary_path
343                    .lock()
344                    .get_or_insert_with(|| lsp_ext.server_bin_path().shared())
345                    .clone(),
346            )
347        } else {
348            None
349        }
350    }
351
352    pub fn brackets(&self) -> &[BracketPair] {
353        &self.config.brackets
354    }
355
356    pub fn set_theme(&self, theme: &SyntaxTheme) {
357        if let Some(grammar) = self.grammar.as_ref() {
358            *grammar.highlight_map.lock() =
359                HighlightMap::new(grammar.highlights_query.capture_names(), theme);
360        }
361    }
362
363    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
364        self.grammar.as_ref()
365    }
366}
367
368impl Grammar {
369    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
370        PARSER.with(|parser| {
371            let mut parser = parser.borrow_mut();
372            parser
373                .set_language(self.ts_language)
374                .expect("incompatible grammar");
375            let mut chunks = text.chunks_in_range(0..text.len());
376            parser
377                .parse_with(
378                    &mut move |offset, _| {
379                        chunks.seek(offset);
380                        chunks.next().unwrap_or("").as_bytes()
381                    },
382                    old_tree.as_ref(),
383                )
384                .unwrap()
385        })
386    }
387
388    pub fn highlight_map(&self) -> HighlightMap {
389        self.highlight_map.lock().clone()
390    }
391
392    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
393        let capture_id = self.highlights_query.capture_index_for_name(name)?;
394        Some(self.highlight_map.lock().get(capture_id))
395    }
396}
397
398impl CompletionLabel {
399    pub fn plain(completion: &lsp::CompletionItem) -> Self {
400        let mut result = Self {
401            text: completion.label.clone(),
402            runs: Vec::new(),
403            left_aligned_len: completion.label.len(),
404            filter_range: 0..completion.label.len(),
405        };
406        if let Some(filter_text) = &completion.filter_text {
407            if let Some(ix) = completion.label.find(filter_text) {
408                result.filter_range = ix..ix + filter_text.len();
409            }
410        }
411        result
412    }
413}
414
415#[cfg(any(test, feature = "test-support"))]
416impl LanguageServerConfig {
417    pub fn fake() -> (Self, mpsc::UnboundedReceiver<lsp::FakeLanguageServer>) {
418        let (servers_tx, servers_rx) = mpsc::unbounded();
419        (
420            Self {
421                fake_config: Some(FakeLanguageServerConfig {
422                    servers_tx,
423                    capabilities: Default::default(),
424                    initializer: None,
425                }),
426                disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
427                ..Default::default()
428            },
429            servers_rx,
430        )
431    }
432
433    pub fn set_fake_capabilities(&mut self, capabilities: lsp::ServerCapabilities) {
434        self.fake_config.as_mut().unwrap().capabilities = capabilities;
435    }
436
437    pub fn set_fake_initializer(
438        &mut self,
439        initializer: impl 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer),
440    ) {
441        self.fake_config.as_mut().unwrap().initializer = Some(Box::new(initializer));
442    }
443}
444
445impl ToLspPosition for PointUtf16 {
446    fn to_lsp_position(self) -> lsp::Position {
447        lsp::Position::new(self.row, self.column)
448    }
449}
450
451pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
452    PointUtf16::new(point.line, point.character)
453}
454
455pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
456    let start = PointUtf16::new(range.start.line, range.start.character);
457    let end = PointUtf16::new(range.end.line, range.end.character);
458    start..end
459}