1mod buffer;
2mod diagnostic_set;
3mod highlight_map;
4mod outline;
5pub mod proto;
6#[cfg(test)]
7mod tests;
8
9use anyhow::{anyhow, Context, Result};
10use client::http::HttpClient;
11use collections::HashMap;
12use futures::{
13 future::{BoxFuture, Shared},
14 FutureExt, TryFutureExt,
15};
16use gpui::{MutableAppContext, Task};
17use highlight_map::HighlightMap;
18use lazy_static::lazy_static;
19use parking_lot::{Mutex, RwLock};
20use regex::Regex;
21use serde::{de, Deserialize, Deserializer};
22use serde_json::Value;
23use std::{
24 any::Any,
25 cell::RefCell,
26 mem,
27 ops::Range,
28 path::{Path, PathBuf},
29 str,
30 sync::Arc,
31};
32use theme::SyntaxTheme;
33use tree_sitter::{self, Query};
34use util::ResultExt;
35
36#[cfg(any(test, feature = "test-support"))]
37use futures::channel::mpsc;
38
39pub use buffer::Operation;
40pub use buffer::*;
41pub use diagnostic_set::DiagnosticEntry;
42pub use outline::{Outline, OutlineItem};
43pub use tree_sitter::{Parser, Tree};
44
45thread_local! {
46 static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
47}
48
49lazy_static! {
50 pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
51 LanguageConfig {
52 name: "Plain Text".into(),
53 ..Default::default()
54 },
55 None,
56 ));
57}
58
59pub trait ToLspPosition {
60 fn to_lsp_position(self) -> lsp::Position;
61}
62
63#[derive(Clone, Debug, PartialEq, Eq, Hash)]
64pub struct LanguageServerName(pub Arc<str>);
65
66use async_trait::async_trait;
67
68#[async_trait]
69pub trait LspAdapter: 'static + Send + Sync {
70 async fn name(&self) -> LanguageServerName;
71 async fn fetch_latest_server_version(
72 &self,
73 http: Arc<dyn HttpClient>,
74 ) -> Result<Box<dyn 'static + Send + Any>>;
75 async fn fetch_server_binary(
76 &self,
77 version: Box<dyn 'static + Send + Any>,
78 http: Arc<dyn HttpClient>,
79 container_dir: PathBuf,
80 ) -> Result<PathBuf>;
81 async fn cached_server_binary(
82 &self,
83 container_dir: PathBuf,
84 ) -> BoxFuture<'static, Option<PathBuf>>;
85
86 async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
87
88 async fn label_for_completion(
89 &self,
90 _: &lsp::CompletionItem,
91 _: &Language,
92 ) -> Option<CodeLabel> {
93 None
94 }
95
96 async fn label_for_symbol(
97 &self,
98 _: &str,
99 _: lsp::SymbolKind,
100 _: &Language,
101 ) -> Option<CodeLabel> {
102 None
103 }
104
105 async fn server_args(&self) -> Vec<String> {
106 Vec::new()
107 }
108
109 async fn initialization_options(&self) -> Option<Value> {
110 None
111 }
112
113 async fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
114 Default::default()
115 }
116
117 async fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
118 None
119 }
120
121 async fn id_for_language(&self, _name: &str) -> Option<String> {
122 None
123 }
124}
125
126#[derive(Clone, Debug, PartialEq, Eq)]
127pub struct CodeLabel {
128 pub text: String,
129 pub runs: Vec<(Range<usize>, HighlightId)>,
130 pub filter_range: Range<usize>,
131}
132
133#[derive(Deserialize)]
134pub struct LanguageConfig {
135 pub name: Arc<str>,
136 pub path_suffixes: Vec<String>,
137 pub brackets: Vec<BracketPair>,
138 #[serde(default = "auto_indent_using_last_non_empty_line_default")]
139 pub auto_indent_using_last_non_empty_line: bool,
140 #[serde(default, deserialize_with = "deserialize_regex")]
141 pub increase_indent_pattern: Option<Regex>,
142 #[serde(default, deserialize_with = "deserialize_regex")]
143 pub decrease_indent_pattern: Option<Regex>,
144 #[serde(default)]
145 pub autoclose_before: String,
146 pub line_comment: Option<String>,
147}
148
149impl Default for LanguageConfig {
150 fn default() -> Self {
151 Self {
152 name: "".into(),
153 path_suffixes: Default::default(),
154 brackets: Default::default(),
155 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
156 increase_indent_pattern: Default::default(),
157 decrease_indent_pattern: Default::default(),
158 autoclose_before: Default::default(),
159 line_comment: Default::default(),
160 }
161 }
162}
163
164fn auto_indent_using_last_non_empty_line_default() -> bool {
165 true
166}
167
168fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
169 let source = Option::<String>::deserialize(d)?;
170 if let Some(source) = source {
171 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
172 } else {
173 Ok(None)
174 }
175}
176
177#[cfg(any(test, feature = "test-support"))]
178pub struct FakeLspAdapter {
179 pub name: &'static str,
180 pub capabilities: lsp::ServerCapabilities,
181 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
182 pub disk_based_diagnostics_progress_token: Option<&'static str>,
183 pub disk_based_diagnostics_sources: &'static [&'static str],
184}
185
186#[derive(Clone, Debug, Deserialize)]
187pub struct BracketPair {
188 pub start: String,
189 pub end: String,
190 pub close: bool,
191 pub newline: bool,
192}
193
194pub struct Language {
195 pub(crate) config: LanguageConfig,
196 pub(crate) grammar: Option<Arc<Grammar>>,
197 pub(crate) adapter: Option<Arc<dyn LspAdapter>>,
198
199 #[cfg(any(test, feature = "test-support"))]
200 fake_adapter: Option<(
201 mpsc::UnboundedSender<lsp::FakeLanguageServer>,
202 Arc<FakeLspAdapter>,
203 )>,
204}
205
206pub struct Grammar {
207 pub(crate) ts_language: tree_sitter::Language,
208 pub(crate) highlights_query: Option<Query>,
209 pub(crate) brackets_query: Option<Query>,
210 pub(crate) indents_query: Option<Query>,
211 pub(crate) outline_query: Option<Query>,
212 pub(crate) highlight_map: Mutex<HighlightMap>,
213}
214
215#[derive(Clone)]
216pub enum LanguageServerBinaryStatus {
217 CheckingForUpdate,
218 Downloading,
219 Downloaded,
220 Cached,
221 Failed { error: String },
222}
223
224pub struct LanguageRegistry {
225 languages: RwLock<Vec<Arc<Language>>>,
226 language_server_download_dir: Option<Arc<Path>>,
227 lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
228 lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
229 login_shell_env_loaded: Shared<Task<()>>,
230 lsp_binary_paths: Mutex<
231 HashMap<
232 LanguageServerName,
233 Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>,
234 >,
235 >,
236}
237
238impl LanguageRegistry {
239 pub fn new(login_shell_env_loaded: Task<()>) -> Self {
240 let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
241 Self {
242 language_server_download_dir: None,
243 languages: Default::default(),
244 lsp_binary_statuses_tx,
245 lsp_binary_statuses_rx,
246 login_shell_env_loaded: login_shell_env_loaded.shared(),
247 lsp_binary_paths: Default::default(),
248 }
249 }
250
251 #[cfg(any(test, feature = "test-support"))]
252 pub fn test() -> Self {
253 Self::new(Task::ready(()))
254 }
255
256 pub fn add(&self, language: Arc<Language>) {
257 self.languages.write().push(language.clone());
258 }
259
260 pub fn set_theme(&self, theme: &SyntaxTheme) {
261 for language in self.languages.read().iter() {
262 language.set_theme(theme);
263 }
264 }
265
266 pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
267 self.language_server_download_dir = Some(path.into());
268 }
269
270 pub fn get_language(&self, name: &str) -> Option<Arc<Language>> {
271 self.languages
272 .read()
273 .iter()
274 .find(|language| language.name().to_lowercase() == name.to_lowercase())
275 .cloned()
276 }
277
278 pub fn to_vec(&self) -> Vec<Arc<Language>> {
279 self.languages.read().iter().cloned().collect()
280 }
281
282 pub fn language_names(&self) -> Vec<String> {
283 self.languages
284 .read()
285 .iter()
286 .map(|language| language.name().to_string())
287 .collect()
288 }
289
290 pub fn select_language(&self, path: impl AsRef<Path>) -> Option<Arc<Language>> {
291 let path = path.as_ref();
292 let filename = path.file_name().and_then(|name| name.to_str());
293 let extension = path.extension().and_then(|name| name.to_str());
294 let path_suffixes = [extension, filename];
295 self.languages
296 .read()
297 .iter()
298 .find(|language| {
299 language
300 .config
301 .path_suffixes
302 .iter()
303 .any(|suffix| dbg!(path_suffixes.contains(&Some(dbg!(suffix.as_str())))))
304 })
305 .cloned()
306 }
307
308 pub fn start_language_server(
309 self: &Arc<Self>,
310 server_id: usize,
311 language: Arc<Language>,
312 root_path: Arc<Path>,
313 http_client: Arc<dyn HttpClient>,
314 cx: &mut MutableAppContext,
315 ) -> Option<Task<Result<lsp::LanguageServer>>> {
316 #[cfg(any(test, feature = "test-support"))]
317 if language.fake_adapter.is_some() {
318 let language = language.clone();
319 return Some(cx.spawn(|cx| async move {
320 let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
321 let (server, mut fake_server) = lsp::LanguageServer::fake(
322 fake_adapter.name.to_string(),
323 fake_adapter.capabilities.clone(),
324 cx.clone(),
325 );
326
327 if let Some(initializer) = &fake_adapter.initializer {
328 initializer(&mut fake_server);
329 }
330
331 let servers_tx = servers_tx.clone();
332 cx.background()
333 .spawn(async move {
334 if fake_server
335 .try_receive_notification::<lsp::notification::Initialized>()
336 .await
337 .is_some()
338 {
339 servers_tx.unbounded_send(fake_server).ok();
340 }
341 })
342 .detach();
343 Ok(server)
344 }));
345 }
346
347 let download_dir = self
348 .language_server_download_dir
349 .clone()
350 .ok_or_else(|| anyhow!("language server download directory has not been assigned"))
351 .log_err()?;
352
353 let this = self.clone();
354 let adapter = language.adapter.clone()?;
355 let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
356 let login_shell_env_loaded = self.login_shell_env_loaded.clone();
357 Some(cx.spawn(|cx| async move {
358 login_shell_env_loaded.await;
359 let server_binary_path = this
360 .lsp_binary_paths
361 .lock()
362 .entry(adapter.name())
363 .or_insert_with(|| {
364 get_server_binary_path(
365 adapter.clone(),
366 language.clone(),
367 http_client,
368 download_dir,
369 lsp_binary_statuses,
370 )
371 .map_err(Arc::new)
372 .boxed()
373 .shared()
374 })
375 .clone()
376 .map_err(|e| anyhow!(e));
377
378 let server_binary_path = server_binary_path.await?;
379 let server_args = adapter.server_args();
380 let server = lsp::LanguageServer::new(
381 server_id,
382 &server_binary_path,
383 &server_args,
384 &root_path,
385 cx,
386 )?;
387 Ok(server)
388 }))
389 }
390
391 pub fn language_server_binary_statuses(
392 &self,
393 ) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
394 self.lsp_binary_statuses_rx.clone()
395 }
396}
397
398async fn get_server_binary_path(
399 adapter: Arc<dyn LspAdapter>,
400 language: Arc<Language>,
401 http_client: Arc<dyn HttpClient>,
402 download_dir: Arc<Path>,
403 statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
404) -> Result<PathBuf> {
405 let container_dir: Arc<Path> = download_dir.join(adapter.name().0.as_ref()).into();
406 if !container_dir.exists() {
407 smol::fs::create_dir_all(&container_dir)
408 .await
409 .context("failed to create container directory")?;
410 }
411
412 let path = fetch_latest_server_binary_path(
413 adapter.clone(),
414 language.clone(),
415 http_client,
416 &container_dir,
417 statuses.clone(),
418 )
419 .await;
420 if let Err(error) = path.as_ref() {
421 if let Some(cached_path) = adapter.cached_server_binary(container_dir).await {
422 statuses
423 .broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
424 .await?;
425 return Ok(cached_path);
426 } else {
427 statuses
428 .broadcast((
429 language.clone(),
430 LanguageServerBinaryStatus::Failed {
431 error: format!("{:?}", error),
432 },
433 ))
434 .await?;
435 }
436 }
437 path
438}
439
440async fn fetch_latest_server_binary_path(
441 adapter: Arc<dyn LspAdapter>,
442 language: Arc<Language>,
443 http_client: Arc<dyn HttpClient>,
444 container_dir: &Path,
445 lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
446) -> Result<PathBuf> {
447 let container_dir: Arc<Path> = container_dir.into();
448 lsp_binary_statuses_tx
449 .broadcast((
450 language.clone(),
451 LanguageServerBinaryStatus::CheckingForUpdate,
452 ))
453 .await?;
454 let version_info = adapter
455 .fetch_latest_server_version(http_client.clone())
456 .await?;
457 lsp_binary_statuses_tx
458 .broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
459 .await?;
460 let path = adapter
461 .fetch_server_binary(version_info, http_client, container_dir.clone())
462 .await?;
463 lsp_binary_statuses_tx
464 .broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
465 .await?;
466 Ok(path)
467}
468
469impl Language {
470 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
471 Self {
472 config,
473 grammar: ts_language.map(|ts_language| {
474 Arc::new(Grammar {
475 highlights_query: None,
476 brackets_query: None,
477 indents_query: None,
478 outline_query: None,
479 ts_language,
480 highlight_map: Default::default(),
481 })
482 }),
483 adapter: None,
484
485 #[cfg(any(test, feature = "test-support"))]
486 fake_adapter: None,
487 }
488 }
489
490 pub fn lsp_adapter(&self) -> Option<Arc<dyn LspAdapter>> {
491 self.adapter.clone()
492 }
493
494 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
495 let grammar = self.grammar_mut();
496 grammar.highlights_query = Some(Query::new(grammar.ts_language, source)?);
497 Ok(self)
498 }
499
500 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
501 let grammar = self.grammar_mut();
502 grammar.brackets_query = Some(Query::new(grammar.ts_language, source)?);
503 Ok(self)
504 }
505
506 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
507 let grammar = self.grammar_mut();
508 grammar.indents_query = Some(Query::new(grammar.ts_language, source)?);
509 Ok(self)
510 }
511
512 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
513 let grammar = self.grammar_mut();
514 grammar.outline_query = Some(Query::new(grammar.ts_language, source)?);
515 Ok(self)
516 }
517
518 fn grammar_mut(&mut self) -> &mut Grammar {
519 Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
520 }
521
522 pub fn with_lsp_adapter(mut self, lsp_adapter: Arc<dyn LspAdapter>) -> Self {
523 self.adapter = Some(lsp_adapter);
524 self
525 }
526
527 #[cfg(any(test, feature = "test-support"))]
528 pub fn set_fake_lsp_adapter(
529 &mut self,
530 fake_lsp_adapter: FakeLspAdapter,
531 ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
532 let (servers_tx, servers_rx) = mpsc::unbounded();
533 let adapter = Arc::new(fake_lsp_adapter);
534 self.fake_adapter = Some((servers_tx, adapter.clone()));
535 self.adapter = Some(adapter);
536 servers_rx
537 }
538
539 pub fn name(&self) -> Arc<str> {
540 self.config.name.clone()
541 }
542
543 pub fn line_comment_prefix(&self) -> Option<&str> {
544 self.config.line_comment.as_deref()
545 }
546
547 pub fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
548 self.adapter.as_ref().map_or(&[] as &[_], |adapter| {
549 adapter.disk_based_diagnostic_sources()
550 })
551 }
552
553 pub fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
554 self.adapter
555 .as_ref()
556 .and_then(|adapter| adapter.disk_based_diagnostics_progress_token())
557 }
558
559 pub fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
560 if let Some(processor) = self.adapter.as_ref() {
561 processor.process_diagnostics(diagnostics);
562 }
563 }
564
565 pub fn label_for_completion(&self, completion: &lsp::CompletionItem) -> Option<CodeLabel> {
566 self.adapter
567 .as_ref()?
568 .label_for_completion(completion, self)
569 }
570
571 pub fn label_for_symbol(&self, name: &str, kind: lsp::SymbolKind) -> Option<CodeLabel> {
572 self.adapter.as_ref()?.label_for_symbol(name, kind, self)
573 }
574
575 pub fn highlight_text<'a>(
576 &'a self,
577 text: &'a Rope,
578 range: Range<usize>,
579 ) -> Vec<(Range<usize>, HighlightId)> {
580 let mut result = Vec::new();
581 if let Some(grammar) = &self.grammar {
582 let tree = grammar.parse_text(text, None);
583 let mut offset = 0;
584 for chunk in BufferChunks::new(text, range, Some(&tree), self.grammar.as_ref(), vec![])
585 {
586 let end_offset = offset + chunk.text.len();
587 if let Some(highlight_id) = chunk.syntax_highlight_id {
588 if !highlight_id.is_default() {
589 result.push((offset..end_offset, highlight_id));
590 }
591 }
592 offset = end_offset;
593 }
594 }
595 result
596 }
597
598 pub fn brackets(&self) -> &[BracketPair] {
599 &self.config.brackets
600 }
601
602 pub fn path_suffixes(&self) -> &[String] {
603 &self.config.path_suffixes
604 }
605
606 pub fn should_autoclose_before(&self, c: char) -> bool {
607 c.is_whitespace() || self.config.autoclose_before.contains(c)
608 }
609
610 pub fn set_theme(&self, theme: &SyntaxTheme) {
611 if let Some(grammar) = self.grammar.as_ref() {
612 if let Some(highlights_query) = &grammar.highlights_query {
613 *grammar.highlight_map.lock() =
614 HighlightMap::new(highlights_query.capture_names(), theme);
615 }
616 }
617 }
618
619 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
620 self.grammar.as_ref()
621 }
622}
623
624impl Grammar {
625 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
626 PARSER.with(|parser| {
627 let mut parser = parser.borrow_mut();
628 parser
629 .set_language(self.ts_language)
630 .expect("incompatible grammar");
631 let mut chunks = text.chunks_in_range(0..text.len());
632 parser
633 .parse_with(
634 &mut move |offset, _| {
635 chunks.seek(offset);
636 chunks.next().unwrap_or("").as_bytes()
637 },
638 old_tree.as_ref(),
639 )
640 .unwrap()
641 })
642 }
643
644 pub fn highlight_map(&self) -> HighlightMap {
645 self.highlight_map.lock().clone()
646 }
647
648 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
649 let capture_id = self
650 .highlights_query
651 .as_ref()?
652 .capture_index_for_name(name)?;
653 Some(self.highlight_map.lock().get(capture_id))
654 }
655}
656
657impl CodeLabel {
658 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
659 let mut result = Self {
660 runs: Vec::new(),
661 filter_range: 0..text.len(),
662 text,
663 };
664 if let Some(filter_text) = filter_text {
665 if let Some(ix) = result.text.find(filter_text) {
666 result.filter_range = ix..ix + filter_text.len();
667 }
668 }
669 result
670 }
671}
672
673#[cfg(any(test, feature = "test-support"))]
674impl Default for FakeLspAdapter {
675 fn default() -> Self {
676 Self {
677 name: "the-fake-language-server",
678 capabilities: lsp::LanguageServer::full_capabilities(),
679 initializer: None,
680 disk_based_diagnostics_progress_token: None,
681 disk_based_diagnostics_sources: &[],
682 }
683 }
684}
685
686#[cfg(any(test, feature = "test-support"))]
687impl LspAdapter for FakeLspAdapter {
688 fn name(&self) -> LanguageServerName {
689 LanguageServerName(self.name.into())
690 }
691
692 fn fetch_latest_server_version(
693 &self,
694 _: Arc<dyn HttpClient>,
695 ) -> BoxFuture<'static, Result<Box<dyn 'static + Send + Any>>> {
696 unreachable!();
697 }
698
699 fn fetch_server_binary(
700 &self,
701 _: Box<dyn 'static + Send + Any>,
702 _: Arc<dyn HttpClient>,
703 _: Arc<Path>,
704 ) -> BoxFuture<'static, Result<PathBuf>> {
705 unreachable!();
706 }
707
708 fn cached_server_binary(&self, _: Arc<Path>) -> BoxFuture<'static, Option<PathBuf>> {
709 unreachable!();
710 }
711
712 fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
713
714 fn disk_based_diagnostic_sources(&self) -> &'static [&'static str] {
715 self.disk_based_diagnostics_sources
716 }
717
718 fn disk_based_diagnostics_progress_token(&self) -> Option<&'static str> {
719 self.disk_based_diagnostics_progress_token
720 }
721}
722
723pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
724 lsp::Position::new(point.row, point.column)
725}
726
727pub fn point_from_lsp(point: lsp::Position) -> PointUtf16 {
728 PointUtf16::new(point.line, point.character)
729}
730
731pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
732 lsp::Range {
733 start: point_to_lsp(range.start),
734 end: point_to_lsp(range.end),
735 }
736}
737
738pub fn range_from_lsp(range: lsp::Range) -> Range<PointUtf16> {
739 let mut start = point_from_lsp(range.start);
740 let mut end = point_from_lsp(range.end);
741 if start > end {
742 mem::swap(&mut start, &mut end);
743 }
744 start..end
745}