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