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