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                let bundled_path = cx
264                    .platform()
265                    .path_for_resource(Some(&config.binary), None)?;
266                std::fs::set_permissions(
267                    &bundled_path,
268                    <std::fs::Permissions as std::os::unix::fs::PermissionsExt>::from_mode(0o755),
269                )
270                .unwrap();
271                bundled_path
272            } else {
273                Path::new(&config.binary).to_path_buf()
274            };
275            lsp::LanguageServer::new(&binary_path, root_path, cx.background().clone()).map(Some)
276        } else {
277            Ok(None)
278        }
279    }
280
281    pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
282        self.config
283            .language_server
284            .as_ref()
285            .map(|config| &config.disk_based_diagnostic_sources)
286    }
287
288    pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
289        self.config
290            .language_server
291            .as_ref()
292            .and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
293    }
294
295    pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
296        if let Some(processor) = self.lsp_post_processor.as_ref() {
297            processor.process_diagnostics(diagnostics);
298        }
299    }
300
301    pub fn label_for_completion(
302        &self,
303        completion: &lsp::CompletionItem,
304    ) -> Option<CompletionLabel> {
305        self.lsp_post_processor
306            .as_ref()?
307            .label_for_completion(completion, self)
308    }
309
310    pub fn highlight_text<'a>(
311        &'a self,
312        text: &'a Rope,
313        range: Range<usize>,
314    ) -> Vec<(Range<usize>, HighlightId)> {
315        let mut result = Vec::new();
316        if let Some(grammar) = &self.grammar {
317            let tree = grammar.parse_text(text, None);
318            let mut offset = 0;
319            for chunk in BufferChunks::new(text, range, Some(&tree), self.grammar.as_ref(), vec![])
320            {
321                let end_offset = offset + chunk.text.len();
322                if let Some(highlight_id) = chunk.highlight_id {
323                    result.push((offset..end_offset, highlight_id));
324                }
325                offset = end_offset;
326            }
327        }
328        result
329    }
330
331    pub fn brackets(&self) -> &[BracketPair] {
332        &self.config.brackets
333    }
334
335    pub fn set_theme(&self, theme: &SyntaxTheme) {
336        if let Some(grammar) = self.grammar.as_ref() {
337            *grammar.highlight_map.lock() =
338                HighlightMap::new(grammar.highlights_query.capture_names(), theme);
339        }
340    }
341
342    pub fn grammar(&self) -> Option<&Arc<Grammar>> {
343        self.grammar.as_ref()
344    }
345}
346
347impl Grammar {
348    fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
349        PARSER.with(|parser| {
350            let mut parser = parser.borrow_mut();
351            parser
352                .set_language(self.ts_language)
353                .expect("incompatible grammar");
354            let mut chunks = text.chunks_in_range(0..text.len());
355            parser
356                .parse_with(
357                    &mut move |offset, _| {
358                        chunks.seek(offset);
359                        chunks.next().unwrap_or("").as_bytes()
360                    },
361                    old_tree.as_ref(),
362                )
363                .unwrap()
364        })
365    }
366
367    pub fn highlight_map(&self) -> HighlightMap {
368        self.highlight_map.lock().clone()
369    }
370
371    pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
372        let capture_id = self.highlights_query.capture_index_for_name(name)?;
373        Some(self.highlight_map.lock().get(capture_id))
374    }
375}
376
377impl CompletionLabel {
378    pub fn plain(completion: &lsp::CompletionItem) -> Self {
379        let mut result = Self {
380            text: completion.label.clone(),
381            runs: Vec::new(),
382            left_aligned_len: completion.label.len(),
383            filter_range: 0..completion.label.len(),
384        };
385        if let Some(filter_text) = &completion.filter_text {
386            if let Some(ix) = completion.label.find(filter_text) {
387                result.filter_range = ix..ix + filter_text.len();
388            }
389        }
390        result
391    }
392}
393
394#[cfg(any(test, feature = "test-support"))]
395impl LanguageServerConfig {
396    pub fn fake() -> (Self, mpsc::UnboundedReceiver<lsp::FakeLanguageServer>) {
397        let (servers_tx, servers_rx) = mpsc::unbounded();
398        (
399            Self {
400                fake_config: Some(FakeLanguageServerConfig {
401                    servers_tx,
402                    capabilities: Default::default(),
403                    initializer: None,
404                }),
405                disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
406                ..Default::default()
407            },
408            servers_rx,
409        )
410    }
411
412    pub fn set_fake_capabilities(&mut self, capabilities: lsp::ServerCapabilities) {
413        self.fake_config.as_mut().unwrap().capabilities = capabilities;
414    }
415
416    pub fn set_fake_initializer(
417        &mut self,
418        initializer: impl 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer),
419    ) {
420        self.fake_config.as_mut().unwrap().initializer = Some(Box::new(initializer));
421    }
422}
423
424impl ToLspPosition for PointUtf16 {
425    fn to_lsp_position(self) -> lsp::Position {
426        lsp::Position::new(self.row, self.column)
427    }
428}
429
430pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
431    PointUtf16::new(point.line, point.character)
432}
433
434pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
435    let start = PointUtf16::new(range.start.line, range.start.character);
436    let end = PointUtf16::new(range.end.line, range.end.character);
437    start..end
438}