1mod buffer;
2mod diagnostic_set;
3mod highlight_map;
4mod outline;
5pub mod proto;
6mod syntax_map;
7
8#[cfg(test)]
9mod buffer_tests;
10
11use anyhow::{anyhow, Context, Result};
12use async_trait::async_trait;
13use client::http::HttpClient;
14use collections::HashMap;
15use futures::{
16 future::{BoxFuture, Shared},
17 FutureExt, TryFutureExt,
18};
19use gpui::{MutableAppContext, Task};
20use highlight_map::HighlightMap;
21use lazy_static::lazy_static;
22use parking_lot::{Mutex, RwLock};
23use postage::watch;
24use regex::Regex;
25use serde::{de, Deserialize, Deserializer};
26use serde_json::Value;
27use std::{
28 any::Any,
29 cell::RefCell,
30 fmt::Debug,
31 hash::Hash,
32 mem,
33 ops::Range,
34 path::{Path, PathBuf},
35 str,
36 sync::{
37 atomic::{AtomicUsize, Ordering::SeqCst},
38 Arc,
39 },
40};
41use syntax_map::SyntaxSnapshot;
42use theme::{SyntaxTheme, Theme};
43use tree_sitter::{self, Query};
44use util::ResultExt;
45
46#[cfg(any(test, feature = "test-support"))]
47use futures::channel::mpsc;
48
49pub use buffer::Operation;
50pub use buffer::*;
51pub use diagnostic_set::DiagnosticEntry;
52pub use outline::{Outline, OutlineItem};
53pub use tree_sitter::{Parser, Tree};
54
55thread_local! {
56 static PARSER: RefCell<Parser> = RefCell::new(Parser::new());
57}
58
59lazy_static! {
60 pub static ref NEXT_GRAMMAR_ID: AtomicUsize = Default::default();
61 pub static ref PLAIN_TEXT: Arc<Language> = Arc::new(Language::new(
62 LanguageConfig {
63 name: "Plain Text".into(),
64 ..Default::default()
65 },
66 None,
67 ));
68}
69
70pub trait ToLspPosition {
71 fn to_lsp_position(self) -> lsp::Position;
72}
73
74#[derive(Clone, Debug, PartialEq, Eq, Hash)]
75pub struct LanguageServerName(pub Arc<str>);
76
77/// Represents a Language Server, with certain cached sync properties.
78/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
79/// once at startup, and caches the results.
80pub struct CachedLspAdapter {
81 pub name: LanguageServerName,
82 pub server_args: Vec<String>,
83 pub initialization_options: Option<Value>,
84 pub disk_based_diagnostic_sources: Vec<String>,
85 pub disk_based_diagnostics_progress_token: Option<String>,
86 pub language_ids: HashMap<String, String>,
87 pub adapter: Box<dyn LspAdapter>,
88}
89
90impl CachedLspAdapter {
91 pub async fn new<T: LspAdapter>(adapter: T) -> Arc<Self> {
92 let adapter = Box::new(adapter);
93 let name = adapter.name().await;
94 let server_args = adapter.server_args().await;
95 let initialization_options = adapter.initialization_options().await;
96 let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources().await;
97 let disk_based_diagnostics_progress_token =
98 adapter.disk_based_diagnostics_progress_token().await;
99 let language_ids = adapter.language_ids().await;
100
101 Arc::new(CachedLspAdapter {
102 name,
103 server_args,
104 initialization_options,
105 disk_based_diagnostic_sources,
106 disk_based_diagnostics_progress_token,
107 language_ids,
108 adapter,
109 })
110 }
111
112 pub async fn fetch_latest_server_version(
113 &self,
114 http: Arc<dyn HttpClient>,
115 ) -> Result<Box<dyn 'static + Send + Any>> {
116 self.adapter.fetch_latest_server_version(http).await
117 }
118
119 pub async fn fetch_server_binary(
120 &self,
121 version: Box<dyn 'static + Send + Any>,
122 http: Arc<dyn HttpClient>,
123 container_dir: PathBuf,
124 ) -> Result<PathBuf> {
125 self.adapter
126 .fetch_server_binary(version, http, container_dir)
127 .await
128 }
129
130 pub async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf> {
131 self.adapter.cached_server_binary(container_dir).await
132 }
133
134 pub async fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
135 self.adapter.process_diagnostics(params).await
136 }
137
138 pub async fn process_completion(&self, completion_item: &mut lsp::CompletionItem) {
139 self.adapter.process_completion(completion_item).await
140 }
141
142 pub async fn label_for_completion(
143 &self,
144 completion_item: &lsp::CompletionItem,
145 language: &Arc<Language>,
146 ) -> Option<CodeLabel> {
147 self.adapter
148 .label_for_completion(completion_item, language)
149 .await
150 }
151
152 pub async fn label_for_symbol(
153 &self,
154 name: &str,
155 kind: lsp::SymbolKind,
156 language: &Arc<Language>,
157 ) -> Option<CodeLabel> {
158 self.adapter.label_for_symbol(name, kind, language).await
159 }
160}
161
162#[async_trait]
163pub trait LspAdapter: 'static + Send + Sync {
164 async fn name(&self) -> LanguageServerName;
165
166 async fn fetch_latest_server_version(
167 &self,
168 http: Arc<dyn HttpClient>,
169 ) -> Result<Box<dyn 'static + Send + Any>>;
170
171 async fn fetch_server_binary(
172 &self,
173 version: Box<dyn 'static + Send + Any>,
174 http: Arc<dyn HttpClient>,
175 container_dir: PathBuf,
176 ) -> Result<PathBuf>;
177
178 async fn cached_server_binary(&self, container_dir: PathBuf) -> Option<PathBuf>;
179
180 async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
181
182 async fn process_completion(&self, _: &mut lsp::CompletionItem) {}
183
184 async fn label_for_completion(
185 &self,
186 _: &lsp::CompletionItem,
187 _: &Arc<Language>,
188 ) -> Option<CodeLabel> {
189 None
190 }
191
192 async fn label_for_symbol(
193 &self,
194 _: &str,
195 _: lsp::SymbolKind,
196 _: &Arc<Language>,
197 ) -> Option<CodeLabel> {
198 None
199 }
200
201 async fn server_args(&self) -> Vec<String> {
202 Vec::new()
203 }
204
205 async fn initialization_options(&self) -> Option<Value> {
206 None
207 }
208
209 async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
210 Default::default()
211 }
212
213 async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
214 None
215 }
216
217 async fn language_ids(&self) -> HashMap<String, String> {
218 Default::default()
219 }
220}
221
222#[derive(Clone, Debug, PartialEq, Eq)]
223pub struct CodeLabel {
224 pub text: String,
225 pub runs: Vec<(Range<usize>, HighlightId)>,
226 pub filter_range: Range<usize>,
227}
228
229#[derive(Deserialize)]
230pub struct LanguageConfig {
231 pub name: Arc<str>,
232 pub path_suffixes: Vec<String>,
233 pub brackets: Vec<BracketPair>,
234 #[serde(default = "auto_indent_using_last_non_empty_line_default")]
235 pub auto_indent_using_last_non_empty_line: bool,
236 #[serde(default, deserialize_with = "deserialize_regex")]
237 pub increase_indent_pattern: Option<Regex>,
238 #[serde(default, deserialize_with = "deserialize_regex")]
239 pub decrease_indent_pattern: Option<Regex>,
240 #[serde(default)]
241 pub autoclose_before: String,
242 #[serde(default)]
243 pub line_comment: Option<Arc<str>>,
244 #[serde(default)]
245 pub block_comment: Option<(Arc<str>, Arc<str>)>,
246}
247
248impl Default for LanguageConfig {
249 fn default() -> Self {
250 Self {
251 name: "".into(),
252 path_suffixes: Default::default(),
253 brackets: Default::default(),
254 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
255 increase_indent_pattern: Default::default(),
256 decrease_indent_pattern: Default::default(),
257 autoclose_before: Default::default(),
258 line_comment: Default::default(),
259 block_comment: Default::default(),
260 }
261 }
262}
263
264fn auto_indent_using_last_non_empty_line_default() -> bool {
265 true
266}
267
268fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
269 let source = Option::<String>::deserialize(d)?;
270 if let Some(source) = source {
271 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
272 } else {
273 Ok(None)
274 }
275}
276
277#[cfg(any(test, feature = "test-support"))]
278pub struct FakeLspAdapter {
279 pub name: &'static str,
280 pub capabilities: lsp::ServerCapabilities,
281 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
282 pub disk_based_diagnostics_progress_token: Option<String>,
283 pub disk_based_diagnostics_sources: Vec<String>,
284}
285
286#[derive(Clone, Debug, Default, Deserialize)]
287pub struct BracketPair {
288 pub start: String,
289 pub end: String,
290 pub close: bool,
291 pub newline: bool,
292}
293
294pub struct Language {
295 pub(crate) config: LanguageConfig,
296 pub(crate) grammar: Option<Arc<Grammar>>,
297 pub(crate) adapter: Option<Arc<CachedLspAdapter>>,
298
299 #[cfg(any(test, feature = "test-support"))]
300 fake_adapter: Option<(
301 mpsc::UnboundedSender<lsp::FakeLanguageServer>,
302 Arc<FakeLspAdapter>,
303 )>,
304}
305
306pub struct Grammar {
307 id: usize,
308 pub(crate) ts_language: tree_sitter::Language,
309 pub(crate) highlights_query: Option<Query>,
310 pub(crate) brackets_config: Option<BracketConfig>,
311 pub(crate) indents_config: Option<IndentConfig>,
312 pub(crate) outline_config: Option<OutlineConfig>,
313 pub(crate) injection_config: Option<InjectionConfig>,
314 pub(crate) highlight_map: Mutex<HighlightMap>,
315}
316
317struct IndentConfig {
318 query: Query,
319 indent_capture_ix: u32,
320 start_capture_ix: Option<u32>,
321 end_capture_ix: Option<u32>,
322 outdent_capture_ix: Option<u32>,
323}
324
325struct OutlineConfig {
326 query: Query,
327 item_capture_ix: u32,
328 name_capture_ix: u32,
329 context_capture_ix: Option<u32>,
330}
331
332struct InjectionConfig {
333 query: Query,
334 content_capture_ix: u32,
335 language_capture_ix: Option<u32>,
336 patterns: Vec<InjectionPatternConfig>,
337}
338
339#[derive(Default, Clone)]
340struct InjectionPatternConfig {
341 language: Option<Box<str>>,
342 combined: bool,
343}
344
345struct BracketConfig {
346 query: Query,
347 open_capture_ix: u32,
348 close_capture_ix: u32,
349}
350
351#[derive(Clone)]
352pub enum LanguageServerBinaryStatus {
353 CheckingForUpdate,
354 Downloading,
355 Downloaded,
356 Cached,
357 Failed { error: String },
358}
359
360pub struct LanguageRegistry {
361 languages: RwLock<Vec<Arc<Language>>>,
362 language_server_download_dir: Option<Arc<Path>>,
363 lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
364 lsp_binary_statuses_rx: async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)>,
365 login_shell_env_loaded: Shared<Task<()>>,
366 #[allow(clippy::type_complexity)]
367 lsp_binary_paths: Mutex<
368 HashMap<
369 LanguageServerName,
370 Shared<BoxFuture<'static, Result<PathBuf, Arc<anyhow::Error>>>>,
371 >,
372 >,
373 subscription: RwLock<(watch::Sender<()>, watch::Receiver<()>)>,
374 theme: RwLock<Option<Arc<Theme>>>,
375}
376
377impl LanguageRegistry {
378 pub fn new(login_shell_env_loaded: Task<()>) -> Self {
379 let (lsp_binary_statuses_tx, lsp_binary_statuses_rx) = async_broadcast::broadcast(16);
380 Self {
381 language_server_download_dir: None,
382 languages: Default::default(),
383 lsp_binary_statuses_tx,
384 lsp_binary_statuses_rx,
385 login_shell_env_loaded: login_shell_env_loaded.shared(),
386 lsp_binary_paths: Default::default(),
387 subscription: RwLock::new(watch::channel()),
388 theme: Default::default(),
389 }
390 }
391
392 #[cfg(any(test, feature = "test-support"))]
393 pub fn test() -> Self {
394 Self::new(Task::ready(()))
395 }
396
397 pub fn add(&self, language: Arc<Language>) {
398 if let Some(theme) = self.theme.read().clone() {
399 language.set_theme(&theme.editor.syntax);
400 }
401 self.languages.write().push(language);
402 *self.subscription.write().0.borrow_mut() = ();
403 }
404
405 pub fn subscribe(&self) -> watch::Receiver<()> {
406 self.subscription.read().1.clone()
407 }
408
409 pub fn set_theme(&self, theme: Arc<Theme>) {
410 *self.theme.write() = Some(theme.clone());
411 for language in self.languages.read().iter() {
412 language.set_theme(&theme.editor.syntax);
413 }
414 }
415
416 pub fn set_language_server_download_dir(&mut self, path: impl Into<Arc<Path>>) {
417 self.language_server_download_dir = Some(path.into());
418 }
419
420 pub fn get_language(&self, name: &str) -> Option<Arc<Language>> {
421 self.languages
422 .read()
423 .iter()
424 .find(|language| language.name().to_lowercase() == name.to_lowercase())
425 .cloned()
426 }
427
428 pub fn to_vec(&self) -> Vec<Arc<Language>> {
429 self.languages.read().iter().cloned().collect()
430 }
431
432 pub fn language_names(&self) -> Vec<String> {
433 self.languages
434 .read()
435 .iter()
436 .map(|language| language.name().to_string())
437 .collect()
438 }
439
440 pub fn select_language(&self, path: impl AsRef<Path>) -> Option<Arc<Language>> {
441 let path = path.as_ref();
442 let filename = path.file_name().and_then(|name| name.to_str());
443 let extension = path.extension().and_then(|name| name.to_str());
444 let path_suffixes = [extension, filename];
445 self.languages
446 .read()
447 .iter()
448 .find(|language| {
449 language
450 .config
451 .path_suffixes
452 .iter()
453 .any(|suffix| path_suffixes.contains(&Some(suffix.as_str())))
454 })
455 .cloned()
456 }
457
458 pub fn start_language_server(
459 self: &Arc<Self>,
460 server_id: usize,
461 language: Arc<Language>,
462 root_path: Arc<Path>,
463 http_client: Arc<dyn HttpClient>,
464 cx: &mut MutableAppContext,
465 ) -> Option<Task<Result<lsp::LanguageServer>>> {
466 #[cfg(any(test, feature = "test-support"))]
467 if language.fake_adapter.is_some() {
468 let language = language;
469 return Some(cx.spawn(|cx| async move {
470 let (servers_tx, fake_adapter) = language.fake_adapter.as_ref().unwrap();
471 let (server, mut fake_server) = lsp::LanguageServer::fake(
472 fake_adapter.name.to_string(),
473 fake_adapter.capabilities.clone(),
474 cx.clone(),
475 );
476
477 if let Some(initializer) = &fake_adapter.initializer {
478 initializer(&mut fake_server);
479 }
480
481 let servers_tx = servers_tx.clone();
482 cx.background()
483 .spawn(async move {
484 if fake_server
485 .try_receive_notification::<lsp::notification::Initialized>()
486 .await
487 .is_some()
488 {
489 servers_tx.unbounded_send(fake_server).ok();
490 }
491 })
492 .detach();
493 Ok(server)
494 }));
495 }
496
497 let download_dir = self
498 .language_server_download_dir
499 .clone()
500 .ok_or_else(|| anyhow!("language server download directory has not been assigned"))
501 .log_err()?;
502
503 let this = self.clone();
504 let adapter = language.adapter.clone()?;
505 let lsp_binary_statuses = self.lsp_binary_statuses_tx.clone();
506 let login_shell_env_loaded = self.login_shell_env_loaded.clone();
507 Some(cx.spawn(|cx| async move {
508 login_shell_env_loaded.await;
509 let server_binary_path = this
510 .lsp_binary_paths
511 .lock()
512 .entry(adapter.name.clone())
513 .or_insert_with(|| {
514 get_server_binary_path(
515 adapter.clone(),
516 language.clone(),
517 http_client,
518 download_dir,
519 lsp_binary_statuses,
520 )
521 .map_err(Arc::new)
522 .boxed()
523 .shared()
524 })
525 .clone()
526 .map_err(|e| anyhow!(e));
527
528 let server_binary_path = server_binary_path.await?;
529 let server_args = &adapter.server_args;
530 let server = lsp::LanguageServer::new(
531 server_id,
532 &server_binary_path,
533 server_args,
534 &root_path,
535 cx,
536 )?;
537 Ok(server)
538 }))
539 }
540
541 pub fn language_server_binary_statuses(
542 &self,
543 ) -> async_broadcast::Receiver<(Arc<Language>, LanguageServerBinaryStatus)> {
544 self.lsp_binary_statuses_rx.clone()
545 }
546}
547
548#[cfg(any(test, feature = "test-support"))]
549impl Default for LanguageRegistry {
550 fn default() -> Self {
551 Self::test()
552 }
553}
554
555async fn get_server_binary_path(
556 adapter: Arc<CachedLspAdapter>,
557 language: Arc<Language>,
558 http_client: Arc<dyn HttpClient>,
559 download_dir: Arc<Path>,
560 statuses: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
561) -> Result<PathBuf> {
562 let container_dir = download_dir.join(adapter.name.0.as_ref());
563 if !container_dir.exists() {
564 smol::fs::create_dir_all(&container_dir)
565 .await
566 .context("failed to create container directory")?;
567 }
568
569 let path = fetch_latest_server_binary_path(
570 adapter.clone(),
571 language.clone(),
572 http_client,
573 &container_dir,
574 statuses.clone(),
575 )
576 .await;
577 if let Err(error) = path.as_ref() {
578 if let Some(cached_path) = adapter.cached_server_binary(container_dir).await {
579 statuses
580 .broadcast((language.clone(), LanguageServerBinaryStatus::Cached))
581 .await?;
582 return Ok(cached_path);
583 } else {
584 statuses
585 .broadcast((
586 language.clone(),
587 LanguageServerBinaryStatus::Failed {
588 error: format!("{:?}", error),
589 },
590 ))
591 .await?;
592 }
593 }
594 path
595}
596
597async fn fetch_latest_server_binary_path(
598 adapter: Arc<CachedLspAdapter>,
599 language: Arc<Language>,
600 http_client: Arc<dyn HttpClient>,
601 container_dir: &Path,
602 lsp_binary_statuses_tx: async_broadcast::Sender<(Arc<Language>, LanguageServerBinaryStatus)>,
603) -> Result<PathBuf> {
604 let container_dir: Arc<Path> = container_dir.into();
605 lsp_binary_statuses_tx
606 .broadcast((
607 language.clone(),
608 LanguageServerBinaryStatus::CheckingForUpdate,
609 ))
610 .await?;
611 let version_info = adapter
612 .fetch_latest_server_version(http_client.clone())
613 .await?;
614 lsp_binary_statuses_tx
615 .broadcast((language.clone(), LanguageServerBinaryStatus::Downloading))
616 .await?;
617 let path = adapter
618 .fetch_server_binary(version_info, http_client, container_dir.to_path_buf())
619 .await?;
620 lsp_binary_statuses_tx
621 .broadcast((language.clone(), LanguageServerBinaryStatus::Downloaded))
622 .await?;
623 Ok(path)
624}
625
626impl Language {
627 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
628 Self {
629 config,
630 grammar: ts_language.map(|ts_language| {
631 Arc::new(Grammar {
632 id: NEXT_GRAMMAR_ID.fetch_add(1, SeqCst),
633 highlights_query: None,
634 brackets_config: None,
635 outline_config: None,
636 indents_config: None,
637 injection_config: None,
638 ts_language,
639 highlight_map: Default::default(),
640 })
641 }),
642 adapter: None,
643
644 #[cfg(any(test, feature = "test-support"))]
645 fake_adapter: None,
646 }
647 }
648
649 pub fn lsp_adapter(&self) -> Option<Arc<CachedLspAdapter>> {
650 self.adapter.clone()
651 }
652
653 pub fn id(&self) -> Option<usize> {
654 self.grammar.as_ref().map(|g| g.id)
655 }
656
657 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
658 let grammar = self.grammar_mut();
659 grammar.highlights_query = Some(Query::new(grammar.ts_language, source)?);
660 Ok(self)
661 }
662
663 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
664 let grammar = self.grammar_mut();
665 let query = Query::new(grammar.ts_language, source)?;
666 let mut open_capture_ix = None;
667 let mut close_capture_ix = None;
668 get_capture_indices(
669 &query,
670 &mut [
671 ("open", &mut open_capture_ix),
672 ("close", &mut close_capture_ix),
673 ],
674 );
675 if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
676 grammar.brackets_config = Some(BracketConfig {
677 query,
678 open_capture_ix,
679 close_capture_ix,
680 });
681 }
682 Ok(self)
683 }
684
685 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
686 let grammar = self.grammar_mut();
687 let query = Query::new(grammar.ts_language, source)?;
688 let mut indent_capture_ix = None;
689 let mut start_capture_ix = None;
690 let mut end_capture_ix = None;
691 let mut outdent_capture_ix = None;
692 get_capture_indices(
693 &query,
694 &mut [
695 ("indent", &mut indent_capture_ix),
696 ("start", &mut start_capture_ix),
697 ("end", &mut end_capture_ix),
698 ("outdent", &mut outdent_capture_ix),
699 ],
700 );
701 if let Some(indent_capture_ix) = indent_capture_ix {
702 grammar.indents_config = Some(IndentConfig {
703 query,
704 indent_capture_ix,
705 start_capture_ix,
706 end_capture_ix,
707 outdent_capture_ix,
708 });
709 }
710 Ok(self)
711 }
712
713 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
714 let grammar = self.grammar_mut();
715 let query = Query::new(grammar.ts_language, source)?;
716 let mut item_capture_ix = None;
717 let mut name_capture_ix = None;
718 let mut context_capture_ix = None;
719 get_capture_indices(
720 &query,
721 &mut [
722 ("item", &mut item_capture_ix),
723 ("name", &mut name_capture_ix),
724 ("context", &mut context_capture_ix),
725 ],
726 );
727 if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
728 grammar.outline_config = Some(OutlineConfig {
729 query,
730 item_capture_ix,
731 name_capture_ix,
732 context_capture_ix,
733 });
734 }
735 Ok(self)
736 }
737
738 pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
739 let grammar = self.grammar_mut();
740 let query = Query::new(grammar.ts_language, source)?;
741 let mut language_capture_ix = None;
742 let mut content_capture_ix = None;
743 get_capture_indices(
744 &query,
745 &mut [
746 ("language", &mut language_capture_ix),
747 ("content", &mut content_capture_ix),
748 ],
749 );
750 let patterns = (0..query.pattern_count())
751 .map(|ix| {
752 let mut config = InjectionPatternConfig::default();
753 for setting in query.property_settings(ix) {
754 match setting.key.as_ref() {
755 "language" => {
756 config.language = setting.value.clone();
757 }
758 "combined" => {
759 config.combined = true;
760 }
761 _ => {}
762 }
763 }
764 config
765 })
766 .collect();
767 if let Some(content_capture_ix) = content_capture_ix {
768 grammar.injection_config = Some(InjectionConfig {
769 query,
770 language_capture_ix,
771 content_capture_ix,
772 patterns,
773 });
774 }
775 Ok(self)
776 }
777
778 fn grammar_mut(&mut self) -> &mut Grammar {
779 Arc::get_mut(self.grammar.as_mut().unwrap()).unwrap()
780 }
781
782 pub fn with_lsp_adapter(mut self, lsp_adapter: Arc<CachedLspAdapter>) -> Self {
783 self.adapter = Some(lsp_adapter);
784 self
785 }
786
787 #[cfg(any(test, feature = "test-support"))]
788 pub async fn set_fake_lsp_adapter(
789 &mut self,
790 fake_lsp_adapter: Arc<FakeLspAdapter>,
791 ) -> mpsc::UnboundedReceiver<lsp::FakeLanguageServer> {
792 let (servers_tx, servers_rx) = mpsc::unbounded();
793 self.fake_adapter = Some((servers_tx, fake_lsp_adapter.clone()));
794 let adapter = CachedLspAdapter::new(fake_lsp_adapter).await;
795 self.adapter = Some(adapter);
796 servers_rx
797 }
798
799 pub fn name(&self) -> Arc<str> {
800 self.config.name.clone()
801 }
802
803 pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
804 self.config.line_comment.as_ref()
805 }
806
807 pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
808 self.config
809 .block_comment
810 .as_ref()
811 .map(|(start, end)| (start, end))
812 }
813
814 pub async fn disk_based_diagnostic_sources(&self) -> &[String] {
815 match self.adapter.as_ref() {
816 Some(adapter) => &adapter.disk_based_diagnostic_sources,
817 None => &[],
818 }
819 }
820
821 pub async fn disk_based_diagnostics_progress_token(&self) -> Option<&str> {
822 if let Some(adapter) = self.adapter.as_ref() {
823 adapter.disk_based_diagnostics_progress_token.as_deref()
824 } else {
825 None
826 }
827 }
828
829 pub async fn process_diagnostics(&self, diagnostics: &mut lsp::PublishDiagnosticsParams) {
830 if let Some(processor) = self.adapter.as_ref() {
831 processor.process_diagnostics(diagnostics).await;
832 }
833 }
834
835 pub async fn process_completion(self: &Arc<Self>, completion: &mut lsp::CompletionItem) {
836 if let Some(adapter) = self.adapter.as_ref() {
837 adapter.process_completion(completion).await;
838 }
839 }
840
841 pub async fn label_for_completion(
842 self: &Arc<Self>,
843 completion: &lsp::CompletionItem,
844 ) -> Option<CodeLabel> {
845 self.adapter
846 .as_ref()?
847 .label_for_completion(completion, self)
848 .await
849 }
850
851 pub async fn label_for_symbol(
852 self: &Arc<Self>,
853 name: &str,
854 kind: lsp::SymbolKind,
855 ) -> Option<CodeLabel> {
856 self.adapter
857 .as_ref()?
858 .label_for_symbol(name, kind, self)
859 .await
860 }
861
862 pub fn highlight_text<'a>(
863 self: &'a Arc<Self>,
864 text: &'a Rope,
865 range: Range<usize>,
866 ) -> Vec<(Range<usize>, HighlightId)> {
867 let mut result = Vec::new();
868 if let Some(grammar) = &self.grammar {
869 let tree = grammar.parse_text(text, None);
870 let captures =
871 SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
872 grammar.highlights_query.as_ref()
873 });
874 let highlight_maps = vec![grammar.highlight_map()];
875 let mut offset = 0;
876 for chunk in BufferChunks::new(text, range, Some((captures, highlight_maps)), vec![]) {
877 let end_offset = offset + chunk.text.len();
878 if let Some(highlight_id) = chunk.syntax_highlight_id {
879 if !highlight_id.is_default() {
880 result.push((offset..end_offset, highlight_id));
881 }
882 }
883 offset = end_offset;
884 }
885 }
886 result
887 }
888
889 pub fn brackets(&self) -> &[BracketPair] {
890 &self.config.brackets
891 }
892
893 pub fn path_suffixes(&self) -> &[String] {
894 &self.config.path_suffixes
895 }
896
897 pub fn should_autoclose_before(&self, c: char) -> bool {
898 c.is_whitespace() || self.config.autoclose_before.contains(c)
899 }
900
901 pub fn set_theme(&self, theme: &SyntaxTheme) {
902 if let Some(grammar) = self.grammar.as_ref() {
903 if let Some(highlights_query) = &grammar.highlights_query {
904 *grammar.highlight_map.lock() =
905 HighlightMap::new(highlights_query.capture_names(), theme);
906 }
907 }
908 }
909
910 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
911 self.grammar.as_ref()
912 }
913}
914
915impl Hash for Language {
916 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
917 self.id().hash(state)
918 }
919}
920
921impl PartialEq for Language {
922 fn eq(&self, other: &Self) -> bool {
923 self.id().eq(&other.id())
924 }
925}
926
927impl Eq for Language {}
928
929impl Debug for Language {
930 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
931 f.debug_struct("Language")
932 .field("name", &self.config.name)
933 .finish()
934 }
935}
936
937impl Grammar {
938 pub fn id(&self) -> usize {
939 self.id
940 }
941
942 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
943 PARSER.with(|parser| {
944 let mut parser = parser.borrow_mut();
945 parser
946 .set_language(self.ts_language)
947 .expect("incompatible grammar");
948 let mut chunks = text.chunks_in_range(0..text.len());
949 parser
950 .parse_with(
951 &mut move |offset, _| {
952 chunks.seek(offset);
953 chunks.next().unwrap_or("").as_bytes()
954 },
955 old_tree.as_ref(),
956 )
957 .unwrap()
958 })
959 }
960
961 pub fn highlight_map(&self) -> HighlightMap {
962 self.highlight_map.lock().clone()
963 }
964
965 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
966 let capture_id = self
967 .highlights_query
968 .as_ref()?
969 .capture_index_for_name(name)?;
970 Some(self.highlight_map.lock().get(capture_id))
971 }
972}
973
974impl CodeLabel {
975 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
976 let mut result = Self {
977 runs: Vec::new(),
978 filter_range: 0..text.len(),
979 text,
980 };
981 if let Some(filter_text) = filter_text {
982 if let Some(ix) = result.text.find(filter_text) {
983 result.filter_range = ix..ix + filter_text.len();
984 }
985 }
986 result
987 }
988}
989
990#[cfg(any(test, feature = "test-support"))]
991impl Default for FakeLspAdapter {
992 fn default() -> Self {
993 Self {
994 name: "the-fake-language-server",
995 capabilities: lsp::LanguageServer::full_capabilities(),
996 initializer: None,
997 disk_based_diagnostics_progress_token: None,
998 disk_based_diagnostics_sources: Vec::new(),
999 }
1000 }
1001}
1002
1003#[cfg(any(test, feature = "test-support"))]
1004#[async_trait]
1005impl LspAdapter for Arc<FakeLspAdapter> {
1006 async fn name(&self) -> LanguageServerName {
1007 LanguageServerName(self.name.into())
1008 }
1009
1010 async fn fetch_latest_server_version(
1011 &self,
1012 _: Arc<dyn HttpClient>,
1013 ) -> Result<Box<dyn 'static + Send + Any>> {
1014 unreachable!();
1015 }
1016
1017 async fn fetch_server_binary(
1018 &self,
1019 _: Box<dyn 'static + Send + Any>,
1020 _: Arc<dyn HttpClient>,
1021 _: PathBuf,
1022 ) -> Result<PathBuf> {
1023 unreachable!();
1024 }
1025
1026 async fn cached_server_binary(&self, _: PathBuf) -> Option<PathBuf> {
1027 unreachable!();
1028 }
1029
1030 async fn process_diagnostics(&self, _: &mut lsp::PublishDiagnosticsParams) {}
1031
1032 async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
1033 self.disk_based_diagnostics_sources.clone()
1034 }
1035
1036 async fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
1037 self.disk_based_diagnostics_progress_token.clone()
1038 }
1039}
1040
1041fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
1042 for (ix, name) in query.capture_names().iter().enumerate() {
1043 for (capture_name, index) in captures.iter_mut() {
1044 if capture_name == name {
1045 **index = Some(ix as u32);
1046 break;
1047 }
1048 }
1049 }
1050}
1051
1052pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
1053 lsp::Position::new(point.row, point.column)
1054}
1055
1056pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
1057 Unclipped(PointUtf16::new(point.line, point.character))
1058}
1059
1060pub fn range_to_lsp(range: Range<PointUtf16>) -> lsp::Range {
1061 lsp::Range {
1062 start: point_to_lsp(range.start),
1063 end: point_to_lsp(range.end),
1064 }
1065}
1066
1067pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
1068 let mut start = point_from_lsp(range.start);
1069 let mut end = point_from_lsp(range.end);
1070 if start > end {
1071 mem::swap(&mut start, &mut end);
1072 }
1073 start..end
1074}