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 parking_lot::Mutex;
 15use serde::Deserialize;
 16use std::{cell::RefCell, ops::Range, path::Path, str, sync::Arc};
 17use theme::SyntaxTheme;
 18use tree_sitter::{self, Query};
 19
 20#[cfg(any(test, feature = "test-support"))]
 21use futures::channel::mpsc;
 22
 23pub use buffer::Operation;
 24pub use buffer::*;
 25pub use diagnostic_set::DiagnosticEntry;
 26pub use outline::{Outline, OutlineItem};
 27pub use tree_sitter::{Parser, Tree};
 28
 29thread_local! {
 30    static PARSER: RefCell<Parser>  = RefCell::new(Parser::new());
 31}
 32
 33lazy_static! {
 34    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
 35        LanguageConfig {
 36            name: "Plain Text".to_string(),
 37            path_suffixes: Default::default(),
 38            brackets: Default::default(),
 39            line_comment: None,
 40            language_server: None,
 41        },
 42        None,
 43    ));
 44}
 45
 46pub trait ToLspPosition {
 47    fn to_lsp_position(self) -> lsp::Position;
 48}
 49
 50pub trait LspPostProcessor: 'static + Send + Sync {
 51    fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
 52    fn label_for_completion(
 53        &self,
 54        _: &lsp::CompletionItem,
 55        _: &Language,
 56    ) -> Option<CompletionLabel> {
 57        None
 58    }
 59}
 60
 61#[derive(Clone, Debug, PartialEq, Eq)]
 62pub struct CompletionLabel {
 63    pub text: String,
 64    pub runs: Vec<(Range<usize>, HighlightId)>,
 65    pub filter_range: Range<usize>,
 66    pub left_aligned_len: usize,
 67}
 68
 69#[derive(Default, Deserialize)]
 70pub struct LanguageConfig {
 71    pub name: String,
 72    pub path_suffixes: Vec<String>,
 73    pub brackets: Vec<BracketPair>,
 74    pub line_comment: Option<String>,
 75    pub language_server: Option<LanguageServerConfig>,
 76}
 77
 78#[derive(Default, Deserialize)]
 79pub struct LanguageServerConfig {
 80    pub binary: String,
 81    pub disk_based_diagnostic_sources: HashSet<String>,
 82    pub disk_based_diagnostics_progress_token: Option<String>,
 83    #[cfg(any(test, feature = "test-support"))]
 84    #[serde(skip)]
 85    fake_config: Option<FakeLanguageServerConfig>,
 86}
 87
 88#[cfg(any(test, feature = "test-support"))]
 89struct FakeLanguageServerConfig {
 90    servers_tx: mpsc::UnboundedSender<lsp::FakeLanguageServer>,
 91    capabilities: lsp::ServerCapabilities,
 92    initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
 93}
 94
 95#[derive(Clone, Debug, Deserialize)]
 96pub struct BracketPair {
 97    pub start: String,
 98    pub end: String,
 99    pub close: bool,
100    pub newline: bool,
101}
102
103pub struct Language {
104    pub(crate) config: LanguageConfig,
105    pub(crate) grammar: Option<Arc<Grammar>>,
106    pub(crate) lsp_post_processor: Option<Box<dyn LspPostProcessor>>,
107}
108
109pub struct Grammar {
110    pub(crate) ts_language: tree_sitter::Language,
111    pub(crate) highlights_query: Query,
112    pub(crate) brackets_query: Query,
113    pub(crate) indents_query: Query,
114    pub(crate) outline_query: Query,
115    pub(crate) highlight_map: Mutex<HighlightMap>,
116}
117
118#[derive(Default)]
119pub struct LanguageRegistry {
120    languages: Vec<Arc<Language>>,
121}
122
123impl LanguageRegistry {
124    pub fn new() -> Self {
125        Self::default()
126    }
127
128    pub fn add(&mut self, language: Arc<Language>) {
129        self.languages.push(language);
130    }
131
132    pub fn set_theme(&self, theme: &SyntaxTheme) {
133        for language in &self.languages {
134            language.set_theme(theme);
135        }
136    }
137
138    pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
139        self.languages
140            .iter()
141            .find(|language| language.name() == name)
142    }
143
144    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
145        let path = path.as_ref();
146        let filename = path.file_name().and_then(|name| name.to_str());
147        let extension = path.extension().and_then(|name| name.to_str());
148        let path_suffixes = [extension, filename];
149        self.languages.iter().find(|language| {
150            language
151                .config
152                .path_suffixes
153                .iter()
154                .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
155        })
156    }
157}
158
159impl Language {
160    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
161        Self {
162            config,
163            grammar: ts_language.map(|ts_language| {
164                Arc::new(Grammar {
165                    brackets_query: Query::new(ts_language, "").unwrap(),
166                    highlights_query: Query::new(ts_language, "").unwrap(),
167                    indents_query: Query::new(ts_language, "").unwrap(),
168                    outline_query: Query::new(ts_language, "").unwrap(),
169                    ts_language,
170                    highlight_map: Default::default(),
171                })
172            }),
173            lsp_post_processor: None,
174        }
175    }
176
177    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
178        let grammar = self
179            .grammar
180            .as_mut()
181            .and_then(Arc::get_mut)
182            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
183        grammar.highlights_query = Query::new(grammar.ts_language, source)?;
184        Ok(self)
185    }
186
187    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
188        let grammar = self
189            .grammar
190            .as_mut()
191            .and_then(Arc::get_mut)
192            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
193        grammar.brackets_query = Query::new(grammar.ts_language, source)?;
194        Ok(self)
195    }
196
197    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
198        let grammar = self
199            .grammar
200            .as_mut()
201            .and_then(Arc::get_mut)
202            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
203        grammar.indents_query = Query::new(grammar.ts_language, source)?;
204        Ok(self)
205    }
206
207    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
208        let grammar = self
209            .grammar
210            .as_mut()
211            .and_then(Arc::get_mut)
212            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
213        grammar.outline_query = Query::new(grammar.ts_language, source)?;
214        Ok(self)
215    }
216
217    pub fn with_lsp_post_processor(mut self, processor: impl LspPostProcessor) -> Self {
218        self.lsp_post_processor = Some(Box::new(processor));
219        self
220    }
221
222    pub fn name(&self) -> &str {
223        self.config.name.as_str()
224    }
225
226    pub fn line_comment_prefix(&self) -> Option<&str> {
227        self.config.line_comment.as_deref()
228    }
229
230    pub fn start_server(
231        &self,
232        root_path: &Path,
233        cx: &AppContext,
234    ) -> Result<Option<Arc<lsp::LanguageServer>>> {
235        if let Some(config) = &self.config.language_server {
236            #[cfg(any(test, feature = "test-support"))]
237            if let Some(fake_config) = &config.fake_config {
238                use postage::prelude::Stream;
239
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 lsp::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}