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::{ops::Range, path::Path, str, sync::Arc};
 21use theme::SyntaxTheme;
 22use tree_sitter::{self, Query};
 23pub use tree_sitter::{Parser, Tree};
 24
 25lazy_static! {
 26    pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
 27        LanguageConfig {
 28            name: "Plain Text".to_string(),
 29            path_suffixes: Default::default(),
 30            brackets: Default::default(),
 31            line_comment: None,
 32            language_server: None,
 33        },
 34        None,
 35    ));
 36}
 37
 38pub trait ToPointUtf16 {
 39    fn to_point_utf16(self) -> PointUtf16;
 40}
 41
 42pub trait ToLspPosition {
 43    fn to_lsp_position(self) -> lsp::Position;
 44}
 45
 46pub trait LspPostProcessor: 'static + Send + Sync {
 47    fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams);
 48    fn label_for_completion(&self, _completion: &lsp::CompletionItem) -> Option<String> {
 49        None
 50    }
 51}
 52
 53#[derive(Default, Deserialize)]
 54pub struct LanguageConfig {
 55    pub name: String,
 56    pub path_suffixes: Vec<String>,
 57    pub brackets: Vec<BracketPair>,
 58    pub line_comment: Option<String>,
 59    pub language_server: Option<LanguageServerConfig>,
 60}
 61
 62#[derive(Default, Deserialize)]
 63pub struct LanguageServerConfig {
 64    pub binary: String,
 65    pub disk_based_diagnostic_sources: HashSet<String>,
 66    pub disk_based_diagnostics_progress_token: Option<String>,
 67    #[cfg(any(test, feature = "test-support"))]
 68    #[serde(skip)]
 69    pub fake_server: Option<(Arc<lsp::LanguageServer>, Arc<std::sync::atomic::AtomicBool>)>,
 70}
 71
 72#[derive(Clone, Debug, Deserialize)]
 73pub struct BracketPair {
 74    pub start: String,
 75    pub end: String,
 76    pub close: bool,
 77    pub newline: bool,
 78}
 79
 80pub struct Language {
 81    pub(crate) config: LanguageConfig,
 82    pub(crate) grammar: Option<Arc<Grammar>>,
 83    pub(crate) lsp_post_processor: Option<Box<dyn LspPostProcessor>>,
 84}
 85
 86pub struct Grammar {
 87    pub(crate) ts_language: tree_sitter::Language,
 88    pub(crate) highlights_query: Query,
 89    pub(crate) brackets_query: Query,
 90    pub(crate) indents_query: Query,
 91    pub(crate) outline_query: Query,
 92    pub(crate) highlight_map: Mutex<HighlightMap>,
 93}
 94
 95#[derive(Default)]
 96pub struct LanguageRegistry {
 97    languages: Vec<Arc<Language>>,
 98}
 99
100impl LanguageRegistry {
101    pub fn new() -> Self {
102        Self::default()
103    }
104
105    pub fn add(&mut self, language: Arc<Language>) {
106        self.languages.push(language);
107    }
108
109    pub fn set_theme(&self, theme: &SyntaxTheme) {
110        for language in &self.languages {
111            language.set_theme(theme);
112        }
113    }
114
115    pub fn get_language(&self, name: &str) -> Option<&Arc<Language>> {
116        self.languages
117            .iter()
118            .find(|language| language.name() == name)
119    }
120
121    pub fn select_language(&self, path: impl AsRef<Path>) -> Option<&Arc<Language>> {
122        let path = path.as_ref();
123        let filename = path.file_name().and_then(|name| name.to_str());
124        let extension = path.extension().and_then(|name| name.to_str());
125        let path_suffixes = [extension, filename];
126        self.languages.iter().find(|language| {
127            language
128                .config
129                .path_suffixes
130                .iter()
131                .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
132        })
133    }
134}
135
136impl Language {
137    pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
138        Self {
139            config,
140            grammar: ts_language.map(|ts_language| {
141                Arc::new(Grammar {
142                    brackets_query: Query::new(ts_language, "").unwrap(),
143                    highlights_query: Query::new(ts_language, "").unwrap(),
144                    indents_query: Query::new(ts_language, "").unwrap(),
145                    outline_query: Query::new(ts_language, "").unwrap(),
146                    ts_language,
147                    highlight_map: Default::default(),
148                })
149            }),
150            lsp_post_processor: None,
151        }
152    }
153
154    pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
155        let grammar = self
156            .grammar
157            .as_mut()
158            .and_then(Arc::get_mut)
159            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
160        grammar.highlights_query = Query::new(grammar.ts_language, source)?;
161        Ok(self)
162    }
163
164    pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
165        let grammar = self
166            .grammar
167            .as_mut()
168            .and_then(Arc::get_mut)
169            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
170        grammar.brackets_query = Query::new(grammar.ts_language, source)?;
171        Ok(self)
172    }
173
174    pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
175        let grammar = self
176            .grammar
177            .as_mut()
178            .and_then(Arc::get_mut)
179            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
180        grammar.indents_query = Query::new(grammar.ts_language, source)?;
181        Ok(self)
182    }
183
184    pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
185        let grammar = self
186            .grammar
187            .as_mut()
188            .and_then(Arc::get_mut)
189            .ok_or_else(|| anyhow!("grammar does not exist or is already being used"))?;
190        grammar.outline_query = Query::new(grammar.ts_language, source)?;
191        Ok(self)
192    }
193
194    pub fn with_lsp_post_processor(mut self, processor: impl LspPostProcessor) -> Self {
195        self.lsp_post_processor = Some(Box::new(processor));
196        self
197    }
198
199    pub fn name(&self) -> &str {
200        self.config.name.as_str()
201    }
202
203    pub fn line_comment_prefix(&self) -> Option<&str> {
204        self.config.line_comment.as_deref()
205    }
206
207    pub fn start_server(
208        &self,
209        root_path: &Path,
210        cx: &AppContext,
211    ) -> Result<Option<Arc<lsp::LanguageServer>>> {
212        if let Some(config) = &self.config.language_server {
213            #[cfg(any(test, feature = "test-support"))]
214            if let Some((server, started)) = &config.fake_server {
215                started.store(true, std::sync::atomic::Ordering::SeqCst);
216                return Ok(Some(server.clone()));
217            }
218
219            const ZED_BUNDLE: Option<&'static str> = option_env!("ZED_BUNDLE");
220            let binary_path = if ZED_BUNDLE.map_or(Ok(false), |b| b.parse())? {
221                cx.platform()
222                    .path_for_resource(Some(&config.binary), None)?
223            } else {
224                Path::new(&config.binary).to_path_buf()
225            };
226            lsp::LanguageServer::new(&binary_path, root_path, cx.background().clone()).map(Some)
227        } else {
228            Ok(None)
229        }
230    }
231
232    pub fn disk_based_diagnostic_sources(&self) -> Option<&HashSet<String>> {
233        self.config
234            .language_server
235            .as_ref()
236            .map(|config| &config.disk_based_diagnostic_sources)
237    }
238
239    pub fn disk_based_diagnostics_progress_token(&self) -> Option<&String> {
240        self.config
241            .language_server
242            .as_ref()
243            .and_then(|config| config.disk_based_diagnostics_progress_token.as_ref())
244    }
245
246    pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
247        if let Some(processor) = self.lsp_post_processor.as_ref() {
248            processor.process_diagnostics(diagnostics);
249        }
250    }
251
252    pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<String> {
253        self.lsp_post_processor
254            .as_ref()
255            .and_then(|p| p.label_for_completion(completion))
256    }
257
258    pub fn brackets(&self) -> &[BracketPair] {
259        &self.config.brackets
260    }
261
262    pub fn set_theme(&self, theme: &SyntaxTheme) {
263        if let Some(grammar) = self.grammar.as_ref() {
264            *grammar.highlight_map.lock() =
265                HighlightMap::new(grammar.highlights_query.capture_names(), theme);
266        }
267    }
268}
269
270impl Grammar {
271    pub fn highlight_map(&self) -> HighlightMap {
272        self.highlight_map.lock().clone()
273    }
274}
275
276#[cfg(any(test, feature = "test-support"))]
277impl LanguageServerConfig {
278    pub async fn fake(
279        executor: Arc<gpui::executor::Background>,
280    ) -> (Self, lsp::FakeLanguageServer) {
281        let (server, fake) = lsp::LanguageServer::fake(executor).await;
282        fake.started
283            .store(false, std::sync::atomic::Ordering::SeqCst);
284        let started = fake.started.clone();
285        (
286            Self {
287                fake_server: Some((server, started)),
288                disk_based_diagnostics_progress_token: Some("fakeServer/check".to_string()),
289                ..Default::default()
290            },
291            fake,
292        )
293    }
294}
295
296impl ToPointUtf16 for lsp::Position {
297    fn to_point_utf16(self) -> PointUtf16 {
298        PointUtf16::new(self.line, self.character)
299    }
300}
301
302impl ToLspPosition for PointUtf16 {
303    fn to_lsp_position(self) -> lsp::Position {
304        lsp::Position::new(self.row, self.column)
305    }
306}
307
308pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
309    let start = PointUtf16::new(range.start.line, range.start.character);
310    let end = PointUtf16::new(range.end.line, range.end.character);
311    start..end
312}