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