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