1//! The `language` crate provides a large chunk of Zed's language-related
2//! features (the other big contributors being project and lsp crates that revolve around LSP features).
3//! Namely, this crate:
4//! - Provides [`Language`], [`Grammar`] and [`LanguageRegistry`] types that
5//! use Tree-sitter to provide syntax highlighting to the editor; note though that `language` doesn't perform the highlighting by itself. It only maps ranges in a buffer to colors. Treesitter is also used for buffer outlines (lists of symbols in a buffer)
6//! - Exposes [`LanguageConfig`] that describes how constructs (like brackets or line comments) should be handled by the editor for a source file of a particular language.
7//!
8//! Notably we do *not* assign a single language to a single file; in real world a single file can consist of multiple programming languages - HTML is a good example of that - and `language` crate tends to reflect that status quo in its API.
9mod buffer;
10mod diagnostic_set;
11mod highlight_map;
12mod language_registry;
13pub mod language_settings;
14mod manifest;
15mod outline;
16pub mod proto;
17mod syntax_map;
18mod task_context;
19mod text_diff;
20mod toolchain;
21
22#[cfg(test)]
23pub mod buffer_tests;
24
25pub use crate::language_settings::EditPredictionsMode;
26use crate::language_settings::SoftWrap;
27use anyhow::{Context as _, Result, anyhow};
28use async_trait::async_trait;
29use collections::{HashMap, HashSet, IndexSet};
30use fs::Fs;
31use futures::Future;
32use gpui::{App, AsyncApp, Entity, SharedString, Task};
33pub use highlight_map::HighlightMap;
34use http_client::HttpClient;
35pub use language_registry::{LanguageName, LoadedLanguage};
36use lsp::{CodeActionKind, InitializeParams, LanguageServerBinary, LanguageServerBinaryOptions};
37pub use manifest::{ManifestName, ManifestProvider, ManifestQuery};
38use parking_lot::Mutex;
39use regex::Regex;
40use schemars::{
41 JsonSchema,
42 r#gen::SchemaGenerator,
43 schema::{InstanceType, Schema, SchemaObject},
44};
45use serde::{Deserialize, Deserializer, Serialize, Serializer, de};
46use serde_json::Value;
47use settings::WorktreeId;
48use smol::future::FutureExt as _;
49use std::{
50 any::Any,
51 ffi::OsStr,
52 fmt::Debug,
53 hash::Hash,
54 mem,
55 ops::{DerefMut, Range},
56 path::{Path, PathBuf},
57 pin::Pin,
58 str,
59 sync::{
60 Arc, LazyLock,
61 atomic::{AtomicU64, AtomicUsize, Ordering::SeqCst},
62 },
63};
64use std::{num::NonZeroU32, sync::OnceLock};
65use syntax_map::{QueryCursorHandle, SyntaxSnapshot};
66use task::RunnableTag;
67pub use task_context::{ContextProvider, RunnableRange};
68pub use text_diff::{DiffOptions, line_diff, text_diff, text_diff_with_options, unified_diff};
69use theme::SyntaxTheme;
70pub use toolchain::{LanguageToolchainStore, Toolchain, ToolchainList, ToolchainLister};
71use tree_sitter::{self, Query, QueryCursor, WasmStore, wasmtime};
72use util::serde::default_true;
73
74pub use buffer::Operation;
75pub use buffer::*;
76pub use diagnostic_set::{DiagnosticEntry, DiagnosticGroup};
77pub use language_registry::{
78 AvailableLanguage, BinaryStatus, LanguageNotFound, LanguageQueries, LanguageRegistry,
79 QUERY_FILENAME_PREFIXES,
80};
81pub use lsp::{LanguageServerId, LanguageServerName};
82pub use outline::*;
83pub use syntax_map::{OwnedSyntaxLayer, SyntaxLayer, ToTreeSitterPoint, TreeSitterOptions};
84pub use text::{AnchorRangeExt, LineEnding};
85pub use tree_sitter::{Node, Parser, Tree, TreeCursor};
86
87/// Initializes the `language` crate.
88///
89/// This should be called before making use of items from the create.
90pub fn init(cx: &mut App) {
91 language_settings::init(cx);
92}
93
94static QUERY_CURSORS: Mutex<Vec<QueryCursor>> = Mutex::new(vec![]);
95static PARSERS: Mutex<Vec<Parser>> = Mutex::new(vec![]);
96
97pub fn with_parser<F, R>(func: F) -> R
98where
99 F: FnOnce(&mut Parser) -> R,
100{
101 let mut parser = PARSERS.lock().pop().unwrap_or_else(|| {
102 let mut parser = Parser::new();
103 parser
104 .set_wasm_store(WasmStore::new(&WASM_ENGINE).unwrap())
105 .unwrap();
106 parser
107 });
108 parser.set_included_ranges(&[]).unwrap();
109 let result = func(&mut parser);
110 PARSERS.lock().push(parser);
111 result
112}
113
114pub fn with_query_cursor<F, R>(func: F) -> R
115where
116 F: FnOnce(&mut QueryCursor) -> R,
117{
118 let mut cursor = QueryCursorHandle::new();
119 func(cursor.deref_mut())
120}
121
122static NEXT_LANGUAGE_ID: LazyLock<AtomicUsize> = LazyLock::new(Default::default);
123static NEXT_GRAMMAR_ID: LazyLock<AtomicUsize> = LazyLock::new(Default::default);
124static WASM_ENGINE: LazyLock<wasmtime::Engine> = LazyLock::new(|| {
125 wasmtime::Engine::new(&wasmtime::Config::new()).expect("Failed to create Wasmtime engine")
126});
127
128/// A shared grammar for plain text, exposed for reuse by downstream crates.
129pub static PLAIN_TEXT: LazyLock<Arc<Language>> = LazyLock::new(|| {
130 Arc::new(Language::new(
131 LanguageConfig {
132 name: "Plain Text".into(),
133 soft_wrap: Some(SoftWrap::EditorWidth),
134 matcher: LanguageMatcher {
135 path_suffixes: vec!["txt".to_owned()],
136 first_line_pattern: None,
137 },
138 ..Default::default()
139 },
140 None,
141 ))
142});
143
144/// Types that represent a position in a buffer, and can be converted into
145/// an LSP position, to send to a language server.
146pub trait ToLspPosition {
147 /// Converts the value into an LSP position.
148 fn to_lsp_position(self) -> lsp::Position;
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, Hash)]
152pub struct Location {
153 pub buffer: Entity<Buffer>,
154 pub range: Range<Anchor>,
155}
156
157/// Represents a Language Server, with certain cached sync properties.
158/// Uses [`LspAdapter`] under the hood, but calls all 'static' methods
159/// once at startup, and caches the results.
160pub struct CachedLspAdapter {
161 pub name: LanguageServerName,
162 pub disk_based_diagnostic_sources: Vec<String>,
163 pub disk_based_diagnostics_progress_token: Option<String>,
164 language_ids: HashMap<String, String>,
165 pub adapter: Arc<dyn LspAdapter>,
166 pub reinstall_attempt_count: AtomicU64,
167 cached_binary: futures::lock::Mutex<Option<LanguageServerBinary>>,
168 manifest_name: OnceLock<Option<ManifestName>>,
169 attach_kind: OnceLock<Attach>,
170}
171
172impl Debug for CachedLspAdapter {
173 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
174 f.debug_struct("CachedLspAdapter")
175 .field("name", &self.name)
176 .field(
177 "disk_based_diagnostic_sources",
178 &self.disk_based_diagnostic_sources,
179 )
180 .field(
181 "disk_based_diagnostics_progress_token",
182 &self.disk_based_diagnostics_progress_token,
183 )
184 .field("language_ids", &self.language_ids)
185 .field("reinstall_attempt_count", &self.reinstall_attempt_count)
186 .finish_non_exhaustive()
187 }
188}
189
190impl CachedLspAdapter {
191 pub fn new(adapter: Arc<dyn LspAdapter>) -> Arc<Self> {
192 let name = adapter.name();
193 let disk_based_diagnostic_sources = adapter.disk_based_diagnostic_sources();
194 let disk_based_diagnostics_progress_token = adapter.disk_based_diagnostics_progress_token();
195 let language_ids = adapter.language_ids();
196
197 Arc::new(CachedLspAdapter {
198 name,
199 disk_based_diagnostic_sources,
200 disk_based_diagnostics_progress_token,
201 language_ids,
202 adapter,
203 cached_binary: Default::default(),
204 reinstall_attempt_count: AtomicU64::new(0),
205 attach_kind: Default::default(),
206 manifest_name: Default::default(),
207 })
208 }
209
210 pub fn name(&self) -> LanguageServerName {
211 self.adapter.name().clone()
212 }
213
214 pub async fn get_language_server_command(
215 self: Arc<Self>,
216 delegate: Arc<dyn LspAdapterDelegate>,
217 toolchains: Arc<dyn LanguageToolchainStore>,
218 binary_options: LanguageServerBinaryOptions,
219 cx: &mut AsyncApp,
220 ) -> Result<LanguageServerBinary> {
221 let cached_binary = self.cached_binary.lock().await;
222 self.adapter
223 .clone()
224 .get_language_server_command(delegate, toolchains, binary_options, cached_binary, cx)
225 .await
226 }
227
228 pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
229 self.adapter.code_action_kinds()
230 }
231
232 pub fn process_diagnostics(
233 &self,
234 params: &mut lsp::PublishDiagnosticsParams,
235 server_id: LanguageServerId,
236 existing_diagnostics: Option<&'_ Buffer>,
237 ) {
238 self.adapter
239 .process_diagnostics(params, server_id, existing_diagnostics)
240 }
241
242 pub fn retain_old_diagnostic(&self, previous_diagnostic: &Diagnostic, cx: &App) -> bool {
243 self.adapter.retain_old_diagnostic(previous_diagnostic, cx)
244 }
245
246 pub fn diagnostic_message_to_markdown(&self, message: &str) -> Option<String> {
247 self.adapter.diagnostic_message_to_markdown(message)
248 }
249
250 pub async fn process_completions(&self, completion_items: &mut [lsp::CompletionItem]) {
251 self.adapter.process_completions(completion_items).await
252 }
253
254 pub async fn labels_for_completions(
255 &self,
256 completion_items: &[lsp::CompletionItem],
257 language: &Arc<Language>,
258 ) -> Result<Vec<Option<CodeLabel>>> {
259 self.adapter
260 .clone()
261 .labels_for_completions(completion_items, language)
262 .await
263 }
264
265 pub async fn labels_for_symbols(
266 &self,
267 symbols: &[(String, lsp::SymbolKind)],
268 language: &Arc<Language>,
269 ) -> Result<Vec<Option<CodeLabel>>> {
270 self.adapter
271 .clone()
272 .labels_for_symbols(symbols, language)
273 .await
274 }
275
276 pub fn language_id(&self, language_name: &LanguageName) -> String {
277 self.language_ids
278 .get(language_name.as_ref())
279 .cloned()
280 .unwrap_or_else(|| language_name.lsp_id())
281 }
282 pub fn manifest_name(&self) -> Option<ManifestName> {
283 self.manifest_name
284 .get_or_init(|| self.adapter.manifest_name())
285 .clone()
286 }
287 pub fn attach_kind(&self) -> Attach {
288 *self.attach_kind.get_or_init(|| self.adapter.attach_kind())
289 }
290}
291
292#[derive(Clone, Copy, Debug, PartialEq)]
293pub enum Attach {
294 /// Create a single language server instance per subproject root.
295 InstancePerRoot,
296 /// Use one shared language server instance for all subprojects within a project.
297 Shared,
298}
299
300impl Attach {
301 pub fn root_path(
302 &self,
303 root_subproject_path: (WorktreeId, Arc<Path>),
304 ) -> (WorktreeId, Arc<Path>) {
305 match self {
306 Attach::InstancePerRoot => root_subproject_path,
307 Attach::Shared => (root_subproject_path.0, Arc::from(Path::new(""))),
308 }
309 }
310}
311
312/// [`LspAdapterDelegate`] allows [`LspAdapter]` implementations to interface with the application
313// e.g. to display a notification or fetch data from the web.
314#[async_trait]
315pub trait LspAdapterDelegate: Send + Sync {
316 fn show_notification(&self, message: &str, cx: &mut App);
317 fn http_client(&self) -> Arc<dyn HttpClient>;
318 fn worktree_id(&self) -> WorktreeId;
319 fn worktree_root_path(&self) -> &Path;
320 fn exists(&self, path: &Path, is_dir: Option<bool>) -> bool;
321 fn update_status(&self, language: LanguageServerName, status: BinaryStatus);
322 fn registered_lsp_adapters(&self) -> Vec<Arc<dyn LspAdapter>>;
323 async fn language_server_download_dir(&self, name: &LanguageServerName) -> Option<Arc<Path>>;
324
325 async fn npm_package_installed_version(
326 &self,
327 package_name: &str,
328 ) -> Result<Option<(PathBuf, String)>>;
329 async fn which(&self, command: &OsStr) -> Option<PathBuf>;
330 async fn shell_env(&self) -> HashMap<String, String>;
331 async fn read_text_file(&self, path: PathBuf) -> Result<String>;
332 async fn try_exec(&self, binary: LanguageServerBinary) -> Result<()>;
333}
334
335#[async_trait(?Send)]
336pub trait LspAdapter: 'static + Send + Sync {
337 fn name(&self) -> LanguageServerName;
338
339 fn get_language_server_command<'a>(
340 self: Arc<Self>,
341 delegate: Arc<dyn LspAdapterDelegate>,
342 toolchains: Arc<dyn LanguageToolchainStore>,
343 binary_options: LanguageServerBinaryOptions,
344 mut cached_binary: futures::lock::MutexGuard<'a, Option<LanguageServerBinary>>,
345 cx: &'a mut AsyncApp,
346 ) -> Pin<Box<dyn 'a + Future<Output = Result<LanguageServerBinary>>>> {
347 async move {
348 // First we check whether the adapter can give us a user-installed binary.
349 // If so, we do *not* want to cache that, because each worktree might give us a different
350 // binary:
351 //
352 // worktree 1: user-installed at `.bin/gopls`
353 // worktree 2: user-installed at `~/bin/gopls`
354 // worktree 3: no gopls found in PATH -> fallback to Zed installation
355 //
356 // We only want to cache when we fall back to the global one,
357 // because we don't want to download and overwrite our global one
358 // for each worktree we might have open.
359 if binary_options.allow_path_lookup {
360 if let Some(binary) = self.check_if_user_installed(delegate.as_ref(), toolchains, cx).await {
361 log::info!(
362 "found user-installed language server for {}. path: {:?}, arguments: {:?}",
363 self.name().0,
364 binary.path,
365 binary.arguments
366 );
367 return Ok(binary);
368 }
369 }
370
371 if !binary_options.allow_binary_download {
372 return Err(anyhow!("downloading language servers disabled"));
373 }
374
375 if let Some(cached_binary) = cached_binary.as_ref() {
376 return Ok(cached_binary.clone());
377 }
378
379 let Some(container_dir) = delegate.language_server_download_dir(&self.name()).await else {
380 anyhow::bail!("no language server download dir defined")
381 };
382
383 let mut binary = try_fetch_server_binary(self.as_ref(), &delegate, container_dir.to_path_buf(), cx).await;
384
385 if let Err(error) = binary.as_ref() {
386 if let Some(prev_downloaded_binary) = self
387 .cached_server_binary(container_dir.to_path_buf(), delegate.as_ref())
388 .await
389 {
390 log::info!(
391 "failed to fetch newest version of language server {:?}. error: {:?}, falling back to using {:?}",
392 self.name(),
393 error,
394 prev_downloaded_binary.path
395 );
396 binary = Ok(prev_downloaded_binary);
397 } else {
398 delegate.update_status(
399 self.name(),
400 BinaryStatus::Failed {
401 error: format!("{error:?}"),
402 },
403 );
404 }
405 }
406
407 if let Ok(binary) = &binary {
408 *cached_binary = Some(binary.clone());
409 }
410
411 binary
412 }
413 .boxed_local()
414 }
415
416 async fn check_if_user_installed(
417 &self,
418 _: &dyn LspAdapterDelegate,
419 _: Arc<dyn LanguageToolchainStore>,
420 _: &AsyncApp,
421 ) -> Option<LanguageServerBinary> {
422 None
423 }
424
425 async fn fetch_latest_server_version(
426 &self,
427 delegate: &dyn LspAdapterDelegate,
428 ) -> Result<Box<dyn 'static + Send + Any>>;
429
430 fn will_fetch_server(
431 &self,
432 _: &Arc<dyn LspAdapterDelegate>,
433 _: &mut AsyncApp,
434 ) -> Option<Task<Result<()>>> {
435 None
436 }
437
438 async fn check_if_version_installed(
439 &self,
440 _version: &(dyn 'static + Send + Any),
441 _container_dir: &PathBuf,
442 _delegate: &dyn LspAdapterDelegate,
443 ) -> Option<LanguageServerBinary> {
444 None
445 }
446
447 async fn fetch_server_binary(
448 &self,
449 latest_version: Box<dyn 'static + Send + Any>,
450 container_dir: PathBuf,
451 delegate: &dyn LspAdapterDelegate,
452 ) -> Result<LanguageServerBinary>;
453
454 async fn cached_server_binary(
455 &self,
456 container_dir: PathBuf,
457 delegate: &dyn LspAdapterDelegate,
458 ) -> Option<LanguageServerBinary>;
459
460 fn process_diagnostics(
461 &self,
462 _: &mut lsp::PublishDiagnosticsParams,
463 _: LanguageServerId,
464 _: Option<&'_ Buffer>,
465 ) {
466 }
467
468 /// When processing new `lsp::PublishDiagnosticsParams` diagnostics, whether to retain previous one(s) or not.
469 fn retain_old_diagnostic(&self, _previous_diagnostic: &Diagnostic, _cx: &App) -> bool {
470 false
471 }
472
473 /// Post-processes completions provided by the language server.
474 async fn process_completions(&self, _: &mut [lsp::CompletionItem]) {}
475
476 fn diagnostic_message_to_markdown(&self, _message: &str) -> Option<String> {
477 None
478 }
479
480 async fn labels_for_completions(
481 self: Arc<Self>,
482 completions: &[lsp::CompletionItem],
483 language: &Arc<Language>,
484 ) -> Result<Vec<Option<CodeLabel>>> {
485 let mut labels = Vec::new();
486 for (ix, completion) in completions.iter().enumerate() {
487 let label = self.label_for_completion(completion, language).await;
488 if let Some(label) = label {
489 labels.resize(ix + 1, None);
490 *labels.last_mut().unwrap() = Some(label);
491 }
492 }
493 Ok(labels)
494 }
495
496 async fn label_for_completion(
497 &self,
498 _: &lsp::CompletionItem,
499 _: &Arc<Language>,
500 ) -> Option<CodeLabel> {
501 None
502 }
503
504 async fn labels_for_symbols(
505 self: Arc<Self>,
506 symbols: &[(String, lsp::SymbolKind)],
507 language: &Arc<Language>,
508 ) -> Result<Vec<Option<CodeLabel>>> {
509 let mut labels = Vec::new();
510 for (ix, (name, kind)) in symbols.iter().enumerate() {
511 let label = self.label_for_symbol(name, *kind, language).await;
512 if let Some(label) = label {
513 labels.resize(ix + 1, None);
514 *labels.last_mut().unwrap() = Some(label);
515 }
516 }
517 Ok(labels)
518 }
519
520 async fn label_for_symbol(
521 &self,
522 _: &str,
523 _: lsp::SymbolKind,
524 _: &Arc<Language>,
525 ) -> Option<CodeLabel> {
526 None
527 }
528
529 /// Returns initialization options that are going to be sent to a LSP server as a part of [`lsp::InitializeParams`]
530 async fn initialization_options(
531 self: Arc<Self>,
532 _: &dyn Fs,
533 _: &Arc<dyn LspAdapterDelegate>,
534 ) -> Result<Option<Value>> {
535 Ok(None)
536 }
537
538 async fn workspace_configuration(
539 self: Arc<Self>,
540 _: &dyn Fs,
541 _: &Arc<dyn LspAdapterDelegate>,
542 _: Arc<dyn LanguageToolchainStore>,
543 _cx: &mut AsyncApp,
544 ) -> Result<Value> {
545 Ok(serde_json::json!({}))
546 }
547
548 async fn additional_initialization_options(
549 self: Arc<Self>,
550 _target_language_server_id: LanguageServerName,
551 _: &dyn Fs,
552 _: &Arc<dyn LspAdapterDelegate>,
553 ) -> Result<Option<Value>> {
554 Ok(None)
555 }
556
557 async fn additional_workspace_configuration(
558 self: Arc<Self>,
559 _target_language_server_id: LanguageServerName,
560 _: &dyn Fs,
561 _: &Arc<dyn LspAdapterDelegate>,
562 _: Arc<dyn LanguageToolchainStore>,
563 _cx: &mut AsyncApp,
564 ) -> Result<Option<Value>> {
565 Ok(None)
566 }
567
568 /// Returns a list of code actions supported by a given LspAdapter
569 fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
570 None
571 }
572
573 fn disk_based_diagnostic_sources(&self) -> Vec<String> {
574 Default::default()
575 }
576
577 fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
578 None
579 }
580
581 fn language_ids(&self) -> HashMap<String, String> {
582 Default::default()
583 }
584
585 /// Support custom initialize params.
586 fn prepare_initialize_params(
587 &self,
588 original: InitializeParams,
589 _: &App,
590 ) -> Result<InitializeParams> {
591 Ok(original)
592 }
593
594 fn attach_kind(&self) -> Attach {
595 Attach::Shared
596 }
597
598 fn manifest_name(&self) -> Option<ManifestName> {
599 None
600 }
601
602 /// Method only implemented by the default JSON language server adapter.
603 /// Used to provide dynamic reloading of the JSON schemas used to
604 /// provide autocompletion and diagnostics in Zed setting and keybind
605 /// files
606 fn is_primary_zed_json_schema_adapter(&self) -> bool {
607 false
608 }
609
610 /// Method only implemented by the default JSON language server adapter.
611 /// Used to clear the cache of JSON schemas that are used to provide
612 /// autocompletion and diagnostics in Zed settings and keybinds files.
613 /// Should not be called unless the callee is sure that
614 /// `Self::is_primary_zed_json_schema_adapter` returns `true`
615 async fn clear_zed_json_schema_cache(&self) {
616 unreachable!(
617 "Not implemented for this adapter. This method should only be called on the default JSON language server adapter"
618 );
619 }
620}
621
622async fn try_fetch_server_binary<L: LspAdapter + 'static + Send + Sync + ?Sized>(
623 adapter: &L,
624 delegate: &Arc<dyn LspAdapterDelegate>,
625 container_dir: PathBuf,
626 cx: &mut AsyncApp,
627) -> Result<LanguageServerBinary> {
628 if let Some(task) = adapter.will_fetch_server(delegate, cx) {
629 task.await?;
630 }
631
632 let name = adapter.name();
633 log::info!("fetching latest version of language server {:?}", name.0);
634 delegate.update_status(name.clone(), BinaryStatus::CheckingForUpdate);
635
636 let latest_version = adapter
637 .fetch_latest_server_version(delegate.as_ref())
638 .await?;
639
640 if let Some(binary) = adapter
641 .check_if_version_installed(latest_version.as_ref(), &container_dir, delegate.as_ref())
642 .await
643 {
644 log::info!("language server {:?} is already installed", name.0);
645 delegate.update_status(name.clone(), BinaryStatus::None);
646 Ok(binary)
647 } else {
648 log::info!("downloading language server {:?}", name.0);
649 delegate.update_status(adapter.name(), BinaryStatus::Downloading);
650 let binary = adapter
651 .fetch_server_binary(latest_version, container_dir, delegate.as_ref())
652 .await;
653
654 delegate.update_status(name.clone(), BinaryStatus::None);
655 binary
656 }
657}
658
659#[derive(Clone, Debug, Default, PartialEq, Eq)]
660pub struct CodeLabel {
661 /// The text to display.
662 pub text: String,
663 /// Syntax highlighting runs.
664 pub runs: Vec<(Range<usize>, HighlightId)>,
665 /// The portion of the text that should be used in fuzzy filtering.
666 pub filter_range: Range<usize>,
667}
668
669#[derive(Clone, Deserialize, JsonSchema, Serialize, Debug)]
670pub struct LanguageConfig {
671 /// Human-readable name of the language.
672 pub name: LanguageName,
673 /// The name of this language for a Markdown code fence block
674 pub code_fence_block_name: Option<Arc<str>>,
675 // The name of the grammar in a WASM bundle (experimental).
676 pub grammar: Option<Arc<str>>,
677 /// The criteria for matching this language to a given file.
678 #[serde(flatten)]
679 pub matcher: LanguageMatcher,
680 /// List of bracket types in a language.
681 #[serde(default)]
682 #[schemars(schema_with = "bracket_pair_config_json_schema")]
683 pub brackets: BracketPairConfig,
684 /// If set to true, indicates the language uses significant whitespace/indentation
685 /// for syntax structure (like Python) rather than brackets/braces for code blocks.
686 #[serde(default)]
687 pub significant_indentation: bool,
688 /// If set to true, auto indentation uses last non empty line to determine
689 /// the indentation level for a new line.
690 #[serde(default = "auto_indent_using_last_non_empty_line_default")]
691 pub auto_indent_using_last_non_empty_line: bool,
692 // Whether indentation of pasted content should be adjusted based on the context.
693 #[serde(default)]
694 pub auto_indent_on_paste: Option<bool>,
695 /// A regex that is used to determine whether the indentation level should be
696 /// increased in the following line.
697 #[serde(
698 default,
699 deserialize_with = "deserialize_regex",
700 serialize_with = "serialize_regex"
701 )]
702 #[schemars(schema_with = "regex_json_schema")]
703 pub increase_indent_pattern: Option<Regex>,
704 /// A regex that is used to determine whether the indentation level should be
705 /// decreased in the following line.
706 #[serde(
707 default,
708 deserialize_with = "deserialize_regex",
709 serialize_with = "serialize_regex"
710 )]
711 #[schemars(schema_with = "regex_json_schema")]
712 pub decrease_indent_pattern: Option<Regex>,
713 /// A list of characters that trigger the automatic insertion of a closing
714 /// bracket when they immediately precede the point where an opening
715 /// bracket is inserted.
716 #[serde(default)]
717 pub autoclose_before: String,
718 /// A placeholder used internally by Semantic Index.
719 #[serde(default)]
720 pub collapsed_placeholder: String,
721 /// A line comment string that is inserted in e.g. `toggle comments` action.
722 /// A language can have multiple flavours of line comments. All of the provided line comments are
723 /// used for comment continuations on the next line, but only the first one is used for Editor::ToggleComments.
724 #[serde(default)]
725 pub line_comments: Vec<Arc<str>>,
726 /// Starting and closing characters of a block comment.
727 #[serde(default)]
728 pub block_comment: Option<(Arc<str>, Arc<str>)>,
729 /// A list of language servers that are allowed to run on subranges of a given language.
730 #[serde(default)]
731 pub scope_opt_in_language_servers: Vec<LanguageServerName>,
732 #[serde(default)]
733 pub overrides: HashMap<String, LanguageConfigOverride>,
734 /// A list of characters that Zed should treat as word characters for the
735 /// purpose of features that operate on word boundaries, like 'move to next word end'
736 /// or a whole-word search in buffer search.
737 #[serde(default)]
738 pub word_characters: HashSet<char>,
739 /// Whether to indent lines using tab characters, as opposed to multiple
740 /// spaces.
741 #[serde(default)]
742 pub hard_tabs: Option<bool>,
743 /// How many columns a tab should occupy.
744 #[serde(default)]
745 pub tab_size: Option<NonZeroU32>,
746 /// How to soft-wrap long lines of text.
747 #[serde(default)]
748 pub soft_wrap: Option<SoftWrap>,
749 /// The name of a Prettier parser that will be used for this language when no file path is available.
750 /// If there's a parser name in the language settings, that will be used instead.
751 #[serde(default)]
752 pub prettier_parser_name: Option<String>,
753 /// If true, this language is only for syntax highlighting via an injection into other
754 /// languages, but should not appear to the user as a distinct language.
755 #[serde(default)]
756 pub hidden: bool,
757 /// If configured, this language contains JSX style tags, and should support auto-closing of those tags.
758 #[serde(default)]
759 pub jsx_tag_auto_close: Option<JsxTagAutoCloseConfig>,
760 /// A list of characters that Zed should treat as word characters for completion queries.
761 #[serde(default)]
762 pub completion_query_characters: HashSet<char>,
763 /// A list of preferred debuggers for this language.
764 #[serde(default)]
765 pub debuggers: IndexSet<String>,
766}
767
768#[derive(Clone, Debug, Serialize, Deserialize, Default, JsonSchema)]
769pub struct LanguageMatcher {
770 /// Given a list of `LanguageConfig`'s, the language of a file can be determined based on the path extension matching any of the `path_suffixes`.
771 #[serde(default)]
772 pub path_suffixes: Vec<String>,
773 /// A regex pattern that determines whether the language should be assigned to a file or not.
774 #[serde(
775 default,
776 serialize_with = "serialize_regex",
777 deserialize_with = "deserialize_regex"
778 )]
779 #[schemars(schema_with = "regex_json_schema")]
780 pub first_line_pattern: Option<Regex>,
781}
782
783/// The configuration for JSX tag auto-closing.
784#[derive(Clone, Deserialize, JsonSchema, Serialize, Debug)]
785pub struct JsxTagAutoCloseConfig {
786 /// The name of the node for a opening tag
787 pub open_tag_node_name: String,
788 /// The name of the node for an closing tag
789 pub close_tag_node_name: String,
790 /// The name of the node for a complete element with children for open and close tags
791 pub jsx_element_node_name: String,
792 /// The name of the node found within both opening and closing
793 /// tags that describes the tag name
794 pub tag_name_node_name: String,
795 /// Alternate Node names for tag names.
796 /// Specifically needed as TSX represents the name in `<Foo.Bar>`
797 /// as `member_expression` rather than `identifier` as usual
798 #[serde(default)]
799 pub tag_name_node_name_alternates: Vec<String>,
800 /// Some grammars are smart enough to detect a closing tag
801 /// that is not valid i.e. doesn't match it's corresponding
802 /// opening tag or does not have a corresponding opening tag
803 /// This should be set to the name of the node for invalid
804 /// closing tags if the grammar contains such a node, otherwise
805 /// detecting already closed tags will not work properly
806 #[serde(default)]
807 pub erroneous_close_tag_node_name: Option<String>,
808 /// See above for erroneous_close_tag_node_name for details
809 /// This should be set if the node used for the tag name
810 /// within erroneous closing tags is different from the
811 /// normal tag name node name
812 #[serde(default)]
813 pub erroneous_close_tag_name_node_name: Option<String>,
814}
815
816/// Represents a language for the given range. Some languages (e.g. HTML)
817/// interleave several languages together, thus a single buffer might actually contain
818/// several nested scopes.
819#[derive(Clone, Debug)]
820pub struct LanguageScope {
821 language: Arc<Language>,
822 override_id: Option<u32>,
823}
824
825#[derive(Clone, Deserialize, Default, Debug, JsonSchema, Serialize)]
826pub struct LanguageConfigOverride {
827 #[serde(default)]
828 pub line_comments: Override<Vec<Arc<str>>>,
829 #[serde(default)]
830 pub block_comment: Override<(Arc<str>, Arc<str>)>,
831 #[serde(skip)]
832 pub disabled_bracket_ixs: Vec<u16>,
833 #[serde(default)]
834 pub word_characters: Override<HashSet<char>>,
835 #[serde(default)]
836 pub completion_query_characters: Override<HashSet<char>>,
837 #[serde(default)]
838 pub opt_into_language_servers: Vec<LanguageServerName>,
839}
840
841#[derive(Clone, Deserialize, Debug, Serialize, JsonSchema)]
842#[serde(untagged)]
843pub enum Override<T> {
844 Remove { remove: bool },
845 Set(T),
846}
847
848impl<T> Default for Override<T> {
849 fn default() -> Self {
850 Override::Remove { remove: false }
851 }
852}
853
854impl<T> Override<T> {
855 fn as_option<'a>(this: Option<&'a Self>, original: Option<&'a T>) -> Option<&'a T> {
856 match this {
857 Some(Self::Set(value)) => Some(value),
858 Some(Self::Remove { remove: true }) => None,
859 Some(Self::Remove { remove: false }) | None => original,
860 }
861 }
862}
863
864impl Default for LanguageConfig {
865 fn default() -> Self {
866 Self {
867 name: LanguageName::new(""),
868 code_fence_block_name: None,
869 grammar: None,
870 matcher: LanguageMatcher::default(),
871 brackets: Default::default(),
872 auto_indent_using_last_non_empty_line: auto_indent_using_last_non_empty_line_default(),
873 auto_indent_on_paste: None,
874 increase_indent_pattern: Default::default(),
875 decrease_indent_pattern: Default::default(),
876 autoclose_before: Default::default(),
877 line_comments: Default::default(),
878 block_comment: Default::default(),
879 scope_opt_in_language_servers: Default::default(),
880 overrides: Default::default(),
881 word_characters: Default::default(),
882 collapsed_placeholder: Default::default(),
883 hard_tabs: None,
884 tab_size: None,
885 soft_wrap: None,
886 prettier_parser_name: None,
887 hidden: false,
888 jsx_tag_auto_close: None,
889 completion_query_characters: Default::default(),
890 debuggers: Default::default(),
891 significant_indentation: Default::default(),
892 }
893 }
894}
895
896fn auto_indent_using_last_non_empty_line_default() -> bool {
897 true
898}
899
900fn deserialize_regex<'de, D: Deserializer<'de>>(d: D) -> Result<Option<Regex>, D::Error> {
901 let source = Option::<String>::deserialize(d)?;
902 if let Some(source) = source {
903 Ok(Some(regex::Regex::new(&source).map_err(de::Error::custom)?))
904 } else {
905 Ok(None)
906 }
907}
908
909fn regex_json_schema(_: &mut SchemaGenerator) -> Schema {
910 Schema::Object(SchemaObject {
911 instance_type: Some(InstanceType::String.into()),
912 ..Default::default()
913 })
914}
915
916fn serialize_regex<S>(regex: &Option<Regex>, serializer: S) -> Result<S::Ok, S::Error>
917where
918 S: Serializer,
919{
920 match regex {
921 Some(regex) => serializer.serialize_str(regex.as_str()),
922 None => serializer.serialize_none(),
923 }
924}
925
926#[doc(hidden)]
927#[cfg(any(test, feature = "test-support"))]
928pub struct FakeLspAdapter {
929 pub name: &'static str,
930 pub initialization_options: Option<Value>,
931 pub prettier_plugins: Vec<&'static str>,
932 pub disk_based_diagnostics_progress_token: Option<String>,
933 pub disk_based_diagnostics_sources: Vec<String>,
934 pub language_server_binary: LanguageServerBinary,
935
936 pub capabilities: lsp::ServerCapabilities,
937 pub initializer: Option<Box<dyn 'static + Send + Sync + Fn(&mut lsp::FakeLanguageServer)>>,
938 pub label_for_completion: Option<
939 Box<
940 dyn 'static
941 + Send
942 + Sync
943 + Fn(&lsp::CompletionItem, &Arc<Language>) -> Option<CodeLabel>,
944 >,
945 >,
946}
947
948/// Configuration of handling bracket pairs for a given language.
949///
950/// This struct includes settings for defining which pairs of characters are considered brackets and
951/// also specifies any language-specific scopes where these pairs should be ignored for bracket matching purposes.
952#[derive(Clone, Debug, Default, JsonSchema, Serialize)]
953pub struct BracketPairConfig {
954 /// A list of character pairs that should be treated as brackets in the context of a given language.
955 pub pairs: Vec<BracketPair>,
956 /// A list of tree-sitter scopes for which a given bracket should not be active.
957 /// N-th entry in `[Self::disabled_scopes_by_bracket_ix]` contains a list of disabled scopes for an n-th entry in `[Self::pairs]`
958 #[serde(skip)]
959 pub disabled_scopes_by_bracket_ix: Vec<Vec<String>>,
960}
961
962impl BracketPairConfig {
963 pub fn is_closing_brace(&self, c: char) -> bool {
964 self.pairs.iter().any(|pair| pair.end.starts_with(c))
965 }
966}
967
968fn bracket_pair_config_json_schema(r#gen: &mut SchemaGenerator) -> Schema {
969 Option::<Vec<BracketPairContent>>::json_schema(r#gen)
970}
971
972#[derive(Deserialize, JsonSchema)]
973pub struct BracketPairContent {
974 #[serde(flatten)]
975 pub bracket_pair: BracketPair,
976 #[serde(default)]
977 pub not_in: Vec<String>,
978}
979
980impl<'de> Deserialize<'de> for BracketPairConfig {
981 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
982 where
983 D: Deserializer<'de>,
984 {
985 let result = Vec::<BracketPairContent>::deserialize(deserializer)?;
986 let mut brackets = Vec::with_capacity(result.len());
987 let mut disabled_scopes_by_bracket_ix = Vec::with_capacity(result.len());
988 for entry in result {
989 brackets.push(entry.bracket_pair);
990 disabled_scopes_by_bracket_ix.push(entry.not_in);
991 }
992
993 Ok(BracketPairConfig {
994 pairs: brackets,
995 disabled_scopes_by_bracket_ix,
996 })
997 }
998}
999
1000/// Describes a single bracket pair and how an editor should react to e.g. inserting
1001/// an opening bracket or to a newline character insertion in between `start` and `end` characters.
1002#[derive(Clone, Debug, Default, Deserialize, PartialEq, JsonSchema, Serialize)]
1003pub struct BracketPair {
1004 /// Starting substring for a bracket.
1005 pub start: String,
1006 /// Ending substring for a bracket.
1007 pub end: String,
1008 /// True if `end` should be automatically inserted right after `start` characters.
1009 pub close: bool,
1010 /// True if selected text should be surrounded by `start` and `end` characters.
1011 #[serde(default = "default_true")]
1012 pub surround: bool,
1013 /// True if an extra newline should be inserted while the cursor is in the middle
1014 /// of that bracket pair.
1015 pub newline: bool,
1016}
1017
1018#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1019pub struct LanguageId(usize);
1020
1021impl LanguageId {
1022 pub(crate) fn new() -> Self {
1023 Self(NEXT_LANGUAGE_ID.fetch_add(1, SeqCst))
1024 }
1025}
1026
1027pub struct Language {
1028 pub(crate) id: LanguageId,
1029 pub(crate) config: LanguageConfig,
1030 pub(crate) grammar: Option<Arc<Grammar>>,
1031 pub(crate) context_provider: Option<Arc<dyn ContextProvider>>,
1032 pub(crate) toolchain: Option<Arc<dyn ToolchainLister>>,
1033}
1034
1035#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
1036pub struct GrammarId(pub usize);
1037
1038impl GrammarId {
1039 pub(crate) fn new() -> Self {
1040 Self(NEXT_GRAMMAR_ID.fetch_add(1, SeqCst))
1041 }
1042}
1043
1044pub struct Grammar {
1045 id: GrammarId,
1046 pub ts_language: tree_sitter::Language,
1047 pub(crate) error_query: Option<Query>,
1048 pub(crate) highlights_query: Option<Query>,
1049 pub(crate) brackets_config: Option<BracketsConfig>,
1050 pub(crate) redactions_config: Option<RedactionConfig>,
1051 pub(crate) runnable_config: Option<RunnableConfig>,
1052 pub(crate) indents_config: Option<IndentConfig>,
1053 pub outline_config: Option<OutlineConfig>,
1054 pub text_object_config: Option<TextObjectConfig>,
1055 pub embedding_config: Option<EmbeddingConfig>,
1056 pub(crate) injection_config: Option<InjectionConfig>,
1057 pub(crate) override_config: Option<OverrideConfig>,
1058 pub(crate) highlight_map: Mutex<HighlightMap>,
1059}
1060
1061struct IndentConfig {
1062 query: Query,
1063 indent_capture_ix: u32,
1064 start_capture_ix: Option<u32>,
1065 end_capture_ix: Option<u32>,
1066 outdent_capture_ix: Option<u32>,
1067}
1068
1069pub struct OutlineConfig {
1070 pub query: Query,
1071 pub item_capture_ix: u32,
1072 pub name_capture_ix: u32,
1073 pub context_capture_ix: Option<u32>,
1074 pub extra_context_capture_ix: Option<u32>,
1075 pub open_capture_ix: Option<u32>,
1076 pub close_capture_ix: Option<u32>,
1077 pub annotation_capture_ix: Option<u32>,
1078}
1079
1080#[derive(Debug, Clone, Copy, PartialEq)]
1081pub enum TextObject {
1082 InsideFunction,
1083 AroundFunction,
1084 InsideClass,
1085 AroundClass,
1086 InsideComment,
1087 AroundComment,
1088}
1089
1090impl TextObject {
1091 pub fn from_capture_name(name: &str) -> Option<TextObject> {
1092 match name {
1093 "function.inside" => Some(TextObject::InsideFunction),
1094 "function.around" => Some(TextObject::AroundFunction),
1095 "class.inside" => Some(TextObject::InsideClass),
1096 "class.around" => Some(TextObject::AroundClass),
1097 "comment.inside" => Some(TextObject::InsideComment),
1098 "comment.around" => Some(TextObject::AroundComment),
1099 _ => None,
1100 }
1101 }
1102
1103 pub fn around(&self) -> Option<Self> {
1104 match self {
1105 TextObject::InsideFunction => Some(TextObject::AroundFunction),
1106 TextObject::InsideClass => Some(TextObject::AroundClass),
1107 TextObject::InsideComment => Some(TextObject::AroundComment),
1108 _ => None,
1109 }
1110 }
1111}
1112
1113pub struct TextObjectConfig {
1114 pub query: Query,
1115 pub text_objects_by_capture_ix: Vec<(u32, TextObject)>,
1116}
1117
1118#[derive(Debug)]
1119pub struct EmbeddingConfig {
1120 pub query: Query,
1121 pub item_capture_ix: u32,
1122 pub name_capture_ix: Option<u32>,
1123 pub context_capture_ix: Option<u32>,
1124 pub collapse_capture_ix: Option<u32>,
1125 pub keep_capture_ix: Option<u32>,
1126}
1127
1128struct InjectionConfig {
1129 query: Query,
1130 content_capture_ix: u32,
1131 language_capture_ix: Option<u32>,
1132 patterns: Vec<InjectionPatternConfig>,
1133}
1134
1135struct RedactionConfig {
1136 pub query: Query,
1137 pub redaction_capture_ix: u32,
1138}
1139
1140#[derive(Clone, Debug, PartialEq)]
1141enum RunnableCapture {
1142 Named(SharedString),
1143 Run,
1144}
1145
1146struct RunnableConfig {
1147 pub query: Query,
1148 /// A mapping from capture indice to capture kind
1149 pub extra_captures: Vec<RunnableCapture>,
1150}
1151
1152struct OverrideConfig {
1153 query: Query,
1154 values: HashMap<u32, OverrideEntry>,
1155}
1156
1157#[derive(Debug)]
1158struct OverrideEntry {
1159 name: String,
1160 range_is_inclusive: bool,
1161 value: LanguageConfigOverride,
1162}
1163
1164#[derive(Default, Clone)]
1165struct InjectionPatternConfig {
1166 language: Option<Box<str>>,
1167 combined: bool,
1168}
1169
1170struct BracketsConfig {
1171 query: Query,
1172 open_capture_ix: u32,
1173 close_capture_ix: u32,
1174 patterns: Vec<BracketsPatternConfig>,
1175}
1176
1177#[derive(Clone, Debug, Default)]
1178struct BracketsPatternConfig {
1179 newline_only: bool,
1180}
1181
1182impl Language {
1183 pub fn new(config: LanguageConfig, ts_language: Option<tree_sitter::Language>) -> Self {
1184 Self::new_with_id(LanguageId::new(), config, ts_language)
1185 }
1186
1187 pub fn id(&self) -> LanguageId {
1188 self.id
1189 }
1190
1191 fn new_with_id(
1192 id: LanguageId,
1193 config: LanguageConfig,
1194 ts_language: Option<tree_sitter::Language>,
1195 ) -> Self {
1196 Self {
1197 id,
1198 config,
1199 grammar: ts_language.map(|ts_language| {
1200 Arc::new(Grammar {
1201 id: GrammarId::new(),
1202 highlights_query: None,
1203 brackets_config: None,
1204 outline_config: None,
1205 text_object_config: None,
1206 embedding_config: None,
1207 indents_config: None,
1208 injection_config: None,
1209 override_config: None,
1210 redactions_config: None,
1211 runnable_config: None,
1212 error_query: Query::new(&ts_language, "(ERROR) @error").ok(),
1213 ts_language,
1214 highlight_map: Default::default(),
1215 })
1216 }),
1217 context_provider: None,
1218 toolchain: None,
1219 }
1220 }
1221
1222 pub fn with_context_provider(mut self, provider: Option<Arc<dyn ContextProvider>>) -> Self {
1223 self.context_provider = provider;
1224 self
1225 }
1226
1227 pub fn with_toolchain_lister(mut self, provider: Option<Arc<dyn ToolchainLister>>) -> Self {
1228 self.toolchain = provider;
1229 self
1230 }
1231
1232 pub fn with_queries(mut self, queries: LanguageQueries) -> Result<Self> {
1233 if let Some(query) = queries.highlights {
1234 self = self
1235 .with_highlights_query(query.as_ref())
1236 .context("Error loading highlights query")?;
1237 }
1238 if let Some(query) = queries.brackets {
1239 self = self
1240 .with_brackets_query(query.as_ref())
1241 .context("Error loading brackets query")?;
1242 }
1243 if let Some(query) = queries.indents {
1244 self = self
1245 .with_indents_query(query.as_ref())
1246 .context("Error loading indents query")?;
1247 }
1248 if let Some(query) = queries.outline {
1249 self = self
1250 .with_outline_query(query.as_ref())
1251 .context("Error loading outline query")?;
1252 }
1253 if let Some(query) = queries.embedding {
1254 self = self
1255 .with_embedding_query(query.as_ref())
1256 .context("Error loading embedding query")?;
1257 }
1258 if let Some(query) = queries.injections {
1259 self = self
1260 .with_injection_query(query.as_ref())
1261 .context("Error loading injection query")?;
1262 }
1263 if let Some(query) = queries.overrides {
1264 self = self
1265 .with_override_query(query.as_ref())
1266 .context("Error loading override query")?;
1267 }
1268 if let Some(query) = queries.redactions {
1269 self = self
1270 .with_redaction_query(query.as_ref())
1271 .context("Error loading redaction query")?;
1272 }
1273 if let Some(query) = queries.runnables {
1274 self = self
1275 .with_runnable_query(query.as_ref())
1276 .context("Error loading runnables query")?;
1277 }
1278 if let Some(query) = queries.text_objects {
1279 self = self
1280 .with_text_object_query(query.as_ref())
1281 .context("Error loading textobject query")?;
1282 }
1283 Ok(self)
1284 }
1285
1286 pub fn with_highlights_query(mut self, source: &str) -> Result<Self> {
1287 let grammar = self
1288 .grammar_mut()
1289 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1290 grammar.highlights_query = Some(Query::new(&grammar.ts_language, source)?);
1291 Ok(self)
1292 }
1293
1294 pub fn with_runnable_query(mut self, source: &str) -> Result<Self> {
1295 let grammar = self
1296 .grammar_mut()
1297 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1298
1299 let query = Query::new(&grammar.ts_language, source)?;
1300 let mut extra_captures = Vec::with_capacity(query.capture_names().len());
1301
1302 for name in query.capture_names().iter() {
1303 let kind = if *name == "run" {
1304 RunnableCapture::Run
1305 } else {
1306 RunnableCapture::Named(name.to_string().into())
1307 };
1308 extra_captures.push(kind);
1309 }
1310
1311 grammar.runnable_config = Some(RunnableConfig {
1312 extra_captures,
1313 query,
1314 });
1315
1316 Ok(self)
1317 }
1318
1319 pub fn with_outline_query(mut self, source: &str) -> Result<Self> {
1320 let grammar = self
1321 .grammar_mut()
1322 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1323 let query = Query::new(&grammar.ts_language, source)?;
1324 let mut item_capture_ix = None;
1325 let mut name_capture_ix = None;
1326 let mut context_capture_ix = None;
1327 let mut extra_context_capture_ix = None;
1328 let mut open_capture_ix = None;
1329 let mut close_capture_ix = None;
1330 let mut annotation_capture_ix = None;
1331 get_capture_indices(
1332 &query,
1333 &mut [
1334 ("item", &mut item_capture_ix),
1335 ("name", &mut name_capture_ix),
1336 ("context", &mut context_capture_ix),
1337 ("context.extra", &mut extra_context_capture_ix),
1338 ("open", &mut open_capture_ix),
1339 ("close", &mut close_capture_ix),
1340 ("annotation", &mut annotation_capture_ix),
1341 ],
1342 );
1343 if let Some((item_capture_ix, name_capture_ix)) = item_capture_ix.zip(name_capture_ix) {
1344 grammar.outline_config = Some(OutlineConfig {
1345 query,
1346 item_capture_ix,
1347 name_capture_ix,
1348 context_capture_ix,
1349 extra_context_capture_ix,
1350 open_capture_ix,
1351 close_capture_ix,
1352 annotation_capture_ix,
1353 });
1354 }
1355 Ok(self)
1356 }
1357
1358 pub fn with_text_object_query(mut self, source: &str) -> Result<Self> {
1359 let grammar = self
1360 .grammar_mut()
1361 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1362 let query = Query::new(&grammar.ts_language, source)?;
1363
1364 let mut text_objects_by_capture_ix = Vec::new();
1365 for (ix, name) in query.capture_names().iter().enumerate() {
1366 if let Some(text_object) = TextObject::from_capture_name(name) {
1367 text_objects_by_capture_ix.push((ix as u32, text_object));
1368 }
1369 }
1370
1371 grammar.text_object_config = Some(TextObjectConfig {
1372 query,
1373 text_objects_by_capture_ix,
1374 });
1375 Ok(self)
1376 }
1377
1378 pub fn with_embedding_query(mut self, source: &str) -> Result<Self> {
1379 let grammar = self
1380 .grammar_mut()
1381 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1382 let query = Query::new(&grammar.ts_language, source)?;
1383 let mut item_capture_ix = None;
1384 let mut name_capture_ix = None;
1385 let mut context_capture_ix = None;
1386 let mut collapse_capture_ix = None;
1387 let mut keep_capture_ix = None;
1388 get_capture_indices(
1389 &query,
1390 &mut [
1391 ("item", &mut item_capture_ix),
1392 ("name", &mut name_capture_ix),
1393 ("context", &mut context_capture_ix),
1394 ("keep", &mut keep_capture_ix),
1395 ("collapse", &mut collapse_capture_ix),
1396 ],
1397 );
1398 if let Some(item_capture_ix) = item_capture_ix {
1399 grammar.embedding_config = Some(EmbeddingConfig {
1400 query,
1401 item_capture_ix,
1402 name_capture_ix,
1403 context_capture_ix,
1404 collapse_capture_ix,
1405 keep_capture_ix,
1406 });
1407 }
1408 Ok(self)
1409 }
1410
1411 pub fn with_brackets_query(mut self, source: &str) -> Result<Self> {
1412 let grammar = self
1413 .grammar_mut()
1414 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1415 let query = Query::new(&grammar.ts_language, source)?;
1416 let mut open_capture_ix = None;
1417 let mut close_capture_ix = None;
1418 get_capture_indices(
1419 &query,
1420 &mut [
1421 ("open", &mut open_capture_ix),
1422 ("close", &mut close_capture_ix),
1423 ],
1424 );
1425 let patterns = (0..query.pattern_count())
1426 .map(|ix| {
1427 let mut config = BracketsPatternConfig::default();
1428 for setting in query.property_settings(ix) {
1429 match setting.key.as_ref() {
1430 "newline.only" => config.newline_only = true,
1431 _ => {}
1432 }
1433 }
1434 config
1435 })
1436 .collect();
1437 if let Some((open_capture_ix, close_capture_ix)) = open_capture_ix.zip(close_capture_ix) {
1438 grammar.brackets_config = Some(BracketsConfig {
1439 query,
1440 open_capture_ix,
1441 close_capture_ix,
1442 patterns,
1443 });
1444 }
1445 Ok(self)
1446 }
1447
1448 pub fn with_indents_query(mut self, source: &str) -> Result<Self> {
1449 let grammar = self
1450 .grammar_mut()
1451 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1452 let query = Query::new(&grammar.ts_language, source)?;
1453 let mut indent_capture_ix = None;
1454 let mut start_capture_ix = None;
1455 let mut end_capture_ix = None;
1456 let mut outdent_capture_ix = None;
1457 get_capture_indices(
1458 &query,
1459 &mut [
1460 ("indent", &mut indent_capture_ix),
1461 ("start", &mut start_capture_ix),
1462 ("end", &mut end_capture_ix),
1463 ("outdent", &mut outdent_capture_ix),
1464 ],
1465 );
1466 if let Some(indent_capture_ix) = indent_capture_ix {
1467 grammar.indents_config = Some(IndentConfig {
1468 query,
1469 indent_capture_ix,
1470 start_capture_ix,
1471 end_capture_ix,
1472 outdent_capture_ix,
1473 });
1474 }
1475 Ok(self)
1476 }
1477
1478 pub fn with_injection_query(mut self, source: &str) -> Result<Self> {
1479 let grammar = self
1480 .grammar_mut()
1481 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1482 let query = Query::new(&grammar.ts_language, source)?;
1483 let mut language_capture_ix = None;
1484 let mut injection_language_capture_ix = None;
1485 let mut content_capture_ix = None;
1486 let mut injection_content_capture_ix = None;
1487 get_capture_indices(
1488 &query,
1489 &mut [
1490 ("language", &mut language_capture_ix),
1491 ("injection.language", &mut injection_language_capture_ix),
1492 ("content", &mut content_capture_ix),
1493 ("injection.content", &mut injection_content_capture_ix),
1494 ],
1495 );
1496 language_capture_ix = match (language_capture_ix, injection_language_capture_ix) {
1497 (None, Some(ix)) => Some(ix),
1498 (Some(_), Some(_)) => {
1499 return Err(anyhow!(
1500 "both language and injection.language captures are present"
1501 ));
1502 }
1503 _ => language_capture_ix,
1504 };
1505 content_capture_ix = match (content_capture_ix, injection_content_capture_ix) {
1506 (None, Some(ix)) => Some(ix),
1507 (Some(_), Some(_)) => {
1508 return Err(anyhow!(
1509 "both content and injection.content captures are present"
1510 ));
1511 }
1512 _ => content_capture_ix,
1513 };
1514 let patterns = (0..query.pattern_count())
1515 .map(|ix| {
1516 let mut config = InjectionPatternConfig::default();
1517 for setting in query.property_settings(ix) {
1518 match setting.key.as_ref() {
1519 "language" | "injection.language" => {
1520 config.language.clone_from(&setting.value);
1521 }
1522 "combined" | "injection.combined" => {
1523 config.combined = true;
1524 }
1525 _ => {}
1526 }
1527 }
1528 config
1529 })
1530 .collect();
1531 if let Some(content_capture_ix) = content_capture_ix {
1532 grammar.injection_config = Some(InjectionConfig {
1533 query,
1534 language_capture_ix,
1535 content_capture_ix,
1536 patterns,
1537 });
1538 }
1539 Ok(self)
1540 }
1541
1542 pub fn with_override_query(mut self, source: &str) -> anyhow::Result<Self> {
1543 let query = {
1544 let grammar = self
1545 .grammar
1546 .as_ref()
1547 .ok_or_else(|| anyhow!("no grammar for language"))?;
1548 Query::new(&grammar.ts_language, source)?
1549 };
1550
1551 let mut override_configs_by_id = HashMap::default();
1552 for (ix, mut name) in query.capture_names().iter().copied().enumerate() {
1553 let mut range_is_inclusive = false;
1554 if name.starts_with('_') {
1555 continue;
1556 }
1557 if let Some(prefix) = name.strip_suffix(".inclusive") {
1558 name = prefix;
1559 range_is_inclusive = true;
1560 }
1561
1562 let value = self.config.overrides.get(name).cloned().unwrap_or_default();
1563 for server_name in &value.opt_into_language_servers {
1564 if !self
1565 .config
1566 .scope_opt_in_language_servers
1567 .contains(server_name)
1568 {
1569 util::debug_panic!(
1570 "Server {server_name:?} has been opted-in by scope {name:?} but has not been marked as an opt-in server"
1571 );
1572 }
1573 }
1574
1575 override_configs_by_id.insert(
1576 ix as u32,
1577 OverrideEntry {
1578 name: name.to_string(),
1579 range_is_inclusive,
1580 value,
1581 },
1582 );
1583 }
1584
1585 let referenced_override_names = self.config.overrides.keys().chain(
1586 self.config
1587 .brackets
1588 .disabled_scopes_by_bracket_ix
1589 .iter()
1590 .flatten(),
1591 );
1592
1593 for referenced_name in referenced_override_names {
1594 if !override_configs_by_id
1595 .values()
1596 .any(|entry| entry.name == *referenced_name)
1597 {
1598 Err(anyhow!(
1599 "language {:?} has overrides in config not in query: {referenced_name:?}",
1600 self.config.name
1601 ))?;
1602 }
1603 }
1604
1605 for entry in override_configs_by_id.values_mut() {
1606 entry.value.disabled_bracket_ixs = self
1607 .config
1608 .brackets
1609 .disabled_scopes_by_bracket_ix
1610 .iter()
1611 .enumerate()
1612 .filter_map(|(ix, disabled_scope_names)| {
1613 if disabled_scope_names.contains(&entry.name) {
1614 Some(ix as u16)
1615 } else {
1616 None
1617 }
1618 })
1619 .collect();
1620 }
1621
1622 self.config.brackets.disabled_scopes_by_bracket_ix.clear();
1623
1624 let grammar = self
1625 .grammar_mut()
1626 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1627 grammar.override_config = Some(OverrideConfig {
1628 query,
1629 values: override_configs_by_id,
1630 });
1631 Ok(self)
1632 }
1633
1634 pub fn with_redaction_query(mut self, source: &str) -> anyhow::Result<Self> {
1635 let grammar = self
1636 .grammar_mut()
1637 .ok_or_else(|| anyhow!("cannot mutate grammar"))?;
1638
1639 let query = Query::new(&grammar.ts_language, source)?;
1640 let mut redaction_capture_ix = None;
1641 get_capture_indices(&query, &mut [("redact", &mut redaction_capture_ix)]);
1642
1643 if let Some(redaction_capture_ix) = redaction_capture_ix {
1644 grammar.redactions_config = Some(RedactionConfig {
1645 query,
1646 redaction_capture_ix,
1647 });
1648 }
1649
1650 Ok(self)
1651 }
1652
1653 fn grammar_mut(&mut self) -> Option<&mut Grammar> {
1654 Arc::get_mut(self.grammar.as_mut()?)
1655 }
1656
1657 pub fn name(&self) -> LanguageName {
1658 self.config.name.clone()
1659 }
1660
1661 pub fn code_fence_block_name(&self) -> Arc<str> {
1662 self.config
1663 .code_fence_block_name
1664 .clone()
1665 .unwrap_or_else(|| self.config.name.as_ref().to_lowercase().into())
1666 }
1667
1668 pub fn context_provider(&self) -> Option<Arc<dyn ContextProvider>> {
1669 self.context_provider.clone()
1670 }
1671
1672 pub fn toolchain_lister(&self) -> Option<Arc<dyn ToolchainLister>> {
1673 self.toolchain.clone()
1674 }
1675
1676 pub fn highlight_text<'a>(
1677 self: &'a Arc<Self>,
1678 text: &'a Rope,
1679 range: Range<usize>,
1680 ) -> Vec<(Range<usize>, HighlightId)> {
1681 let mut result = Vec::new();
1682 if let Some(grammar) = &self.grammar {
1683 let tree = grammar.parse_text(text, None);
1684 let captures =
1685 SyntaxSnapshot::single_tree_captures(range.clone(), text, &tree, self, |grammar| {
1686 grammar.highlights_query.as_ref()
1687 });
1688 let highlight_maps = vec![grammar.highlight_map()];
1689 let mut offset = 0;
1690 for chunk in
1691 BufferChunks::new(text, range, Some((captures, highlight_maps)), false, None)
1692 {
1693 let end_offset = offset + chunk.text.len();
1694 if let Some(highlight_id) = chunk.syntax_highlight_id {
1695 if !highlight_id.is_default() {
1696 result.push((offset..end_offset, highlight_id));
1697 }
1698 }
1699 offset = end_offset;
1700 }
1701 }
1702 result
1703 }
1704
1705 pub fn path_suffixes(&self) -> &[String] {
1706 &self.config.matcher.path_suffixes
1707 }
1708
1709 pub fn should_autoclose_before(&self, c: char) -> bool {
1710 c.is_whitespace() || self.config.autoclose_before.contains(c)
1711 }
1712
1713 pub fn set_theme(&self, theme: &SyntaxTheme) {
1714 if let Some(grammar) = self.grammar.as_ref() {
1715 if let Some(highlights_query) = &grammar.highlights_query {
1716 *grammar.highlight_map.lock() =
1717 HighlightMap::new(highlights_query.capture_names(), theme);
1718 }
1719 }
1720 }
1721
1722 pub fn grammar(&self) -> Option<&Arc<Grammar>> {
1723 self.grammar.as_ref()
1724 }
1725
1726 pub fn default_scope(self: &Arc<Self>) -> LanguageScope {
1727 LanguageScope {
1728 language: self.clone(),
1729 override_id: None,
1730 }
1731 }
1732
1733 pub fn lsp_id(&self) -> String {
1734 self.config.name.lsp_id()
1735 }
1736
1737 pub fn prettier_parser_name(&self) -> Option<&str> {
1738 self.config.prettier_parser_name.as_deref()
1739 }
1740
1741 pub fn config(&self) -> &LanguageConfig {
1742 &self.config
1743 }
1744}
1745
1746impl LanguageScope {
1747 pub fn path_suffixes(&self) -> &[String] {
1748 &self.language.path_suffixes()
1749 }
1750
1751 pub fn language_name(&self) -> LanguageName {
1752 self.language.config.name.clone()
1753 }
1754
1755 pub fn collapsed_placeholder(&self) -> &str {
1756 self.language.config.collapsed_placeholder.as_ref()
1757 }
1758
1759 /// Returns line prefix that is inserted in e.g. line continuations or
1760 /// in `toggle comments` action.
1761 pub fn line_comment_prefixes(&self) -> &[Arc<str>] {
1762 Override::as_option(
1763 self.config_override().map(|o| &o.line_comments),
1764 Some(&self.language.config.line_comments),
1765 )
1766 .map_or([].as_slice(), |e| e.as_slice())
1767 }
1768
1769 pub fn block_comment_delimiters(&self) -> Option<(&Arc<str>, &Arc<str>)> {
1770 Override::as_option(
1771 self.config_override().map(|o| &o.block_comment),
1772 self.language.config.block_comment.as_ref(),
1773 )
1774 .map(|e| (&e.0, &e.1))
1775 }
1776
1777 /// Returns a list of language-specific word characters.
1778 ///
1779 /// By default, Zed treats alphanumeric characters (and '_') as word characters for
1780 /// the purpose of actions like 'move to next word end` or whole-word search.
1781 /// It additionally accounts for language's additional word characters.
1782 pub fn word_characters(&self) -> Option<&HashSet<char>> {
1783 Override::as_option(
1784 self.config_override().map(|o| &o.word_characters),
1785 Some(&self.language.config.word_characters),
1786 )
1787 }
1788
1789 /// Returns a list of language-specific characters that are considered part of
1790 /// a completion query.
1791 pub fn completion_query_characters(&self) -> Option<&HashSet<char>> {
1792 Override::as_option(
1793 self.config_override()
1794 .map(|o| &o.completion_query_characters),
1795 Some(&self.language.config.completion_query_characters),
1796 )
1797 }
1798
1799 /// Returns a list of bracket pairs for a given language with an additional
1800 /// piece of information about whether the particular bracket pair is currently active for a given language.
1801 pub fn brackets(&self) -> impl Iterator<Item = (&BracketPair, bool)> {
1802 let mut disabled_ids = self
1803 .config_override()
1804 .map_or(&[] as _, |o| o.disabled_bracket_ixs.as_slice());
1805 self.language
1806 .config
1807 .brackets
1808 .pairs
1809 .iter()
1810 .enumerate()
1811 .map(move |(ix, bracket)| {
1812 let mut is_enabled = true;
1813 if let Some(next_disabled_ix) = disabled_ids.first() {
1814 if ix == *next_disabled_ix as usize {
1815 disabled_ids = &disabled_ids[1..];
1816 is_enabled = false;
1817 }
1818 }
1819 (bracket, is_enabled)
1820 })
1821 }
1822
1823 pub fn should_autoclose_before(&self, c: char) -> bool {
1824 c.is_whitespace() || self.language.config.autoclose_before.contains(c)
1825 }
1826
1827 pub fn language_allowed(&self, name: &LanguageServerName) -> bool {
1828 let config = &self.language.config;
1829 let opt_in_servers = &config.scope_opt_in_language_servers;
1830 if opt_in_servers.iter().any(|o| *o == *name) {
1831 if let Some(over) = self.config_override() {
1832 over.opt_into_language_servers.iter().any(|o| *o == *name)
1833 } else {
1834 false
1835 }
1836 } else {
1837 true
1838 }
1839 }
1840
1841 pub fn override_name(&self) -> Option<&str> {
1842 let id = self.override_id?;
1843 let grammar = self.language.grammar.as_ref()?;
1844 let override_config = grammar.override_config.as_ref()?;
1845 override_config.values.get(&id).map(|e| e.name.as_str())
1846 }
1847
1848 fn config_override(&self) -> Option<&LanguageConfigOverride> {
1849 let id = self.override_id?;
1850 let grammar = self.language.grammar.as_ref()?;
1851 let override_config = grammar.override_config.as_ref()?;
1852 override_config.values.get(&id).map(|e| &e.value)
1853 }
1854}
1855
1856impl Hash for Language {
1857 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
1858 self.id.hash(state)
1859 }
1860}
1861
1862impl PartialEq for Language {
1863 fn eq(&self, other: &Self) -> bool {
1864 self.id.eq(&other.id)
1865 }
1866}
1867
1868impl Eq for Language {}
1869
1870impl Debug for Language {
1871 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1872 f.debug_struct("Language")
1873 .field("name", &self.config.name)
1874 .finish()
1875 }
1876}
1877
1878impl Grammar {
1879 pub fn id(&self) -> GrammarId {
1880 self.id
1881 }
1882
1883 fn parse_text(&self, text: &Rope, old_tree: Option<Tree>) -> Tree {
1884 with_parser(|parser| {
1885 parser
1886 .set_language(&self.ts_language)
1887 .expect("incompatible grammar");
1888 let mut chunks = text.chunks_in_range(0..text.len());
1889 parser
1890 .parse_with_options(
1891 &mut move |offset, _| {
1892 chunks.seek(offset);
1893 chunks.next().unwrap_or("").as_bytes()
1894 },
1895 old_tree.as_ref(),
1896 None,
1897 )
1898 .unwrap()
1899 })
1900 }
1901
1902 pub fn highlight_map(&self) -> HighlightMap {
1903 self.highlight_map.lock().clone()
1904 }
1905
1906 pub fn highlight_id_for_name(&self, name: &str) -> Option<HighlightId> {
1907 let capture_id = self
1908 .highlights_query
1909 .as_ref()?
1910 .capture_index_for_name(name)?;
1911 Some(self.highlight_map.lock().get(capture_id))
1912 }
1913}
1914
1915impl CodeLabel {
1916 pub fn fallback_for_completion(
1917 item: &lsp::CompletionItem,
1918 language: Option<&Language>,
1919 ) -> Self {
1920 let highlight_id = item.kind.and_then(|kind| {
1921 let grammar = language?.grammar()?;
1922 use lsp::CompletionItemKind as Kind;
1923 match kind {
1924 Kind::CLASS => grammar.highlight_id_for_name("type"),
1925 Kind::CONSTANT => grammar.highlight_id_for_name("constant"),
1926 Kind::CONSTRUCTOR => grammar.highlight_id_for_name("constructor"),
1927 Kind::ENUM => grammar
1928 .highlight_id_for_name("enum")
1929 .or_else(|| grammar.highlight_id_for_name("type")),
1930 Kind::ENUM_MEMBER => grammar
1931 .highlight_id_for_name("variant")
1932 .or_else(|| grammar.highlight_id_for_name("property")),
1933 Kind::FIELD => grammar.highlight_id_for_name("property"),
1934 Kind::FUNCTION => grammar.highlight_id_for_name("function"),
1935 Kind::INTERFACE => grammar.highlight_id_for_name("type"),
1936 Kind::METHOD => grammar
1937 .highlight_id_for_name("function.method")
1938 .or_else(|| grammar.highlight_id_for_name("function")),
1939 Kind::OPERATOR => grammar.highlight_id_for_name("operator"),
1940 Kind::PROPERTY => grammar.highlight_id_for_name("property"),
1941 Kind::STRUCT => grammar.highlight_id_for_name("type"),
1942 Kind::VARIABLE => grammar.highlight_id_for_name("variable"),
1943 Kind::KEYWORD => grammar.highlight_id_for_name("keyword"),
1944 _ => None,
1945 }
1946 });
1947
1948 let label = &item.label;
1949 let label_length = label.len();
1950 let runs = highlight_id
1951 .map(|highlight_id| vec![(0..label_length, highlight_id)])
1952 .unwrap_or_default();
1953 let text = if let Some(detail) = item.detail.as_deref().filter(|detail| detail != label) {
1954 format!("{label} {detail}")
1955 } else if let Some(description) = item
1956 .label_details
1957 .as_ref()
1958 .and_then(|label_details| label_details.description.as_deref())
1959 .filter(|description| description != label)
1960 {
1961 format!("{label} {description}")
1962 } else {
1963 label.clone()
1964 };
1965 Self {
1966 text,
1967 runs,
1968 filter_range: 0..label_length,
1969 }
1970 }
1971
1972 pub fn plain(text: String, filter_text: Option<&str>) -> Self {
1973 let mut result = Self {
1974 runs: Vec::new(),
1975 filter_range: 0..text.len(),
1976 text,
1977 };
1978 if let Some(filter_text) = filter_text {
1979 if let Some(ix) = result.text.find(filter_text) {
1980 result.filter_range = ix..ix + filter_text.len();
1981 }
1982 }
1983 result
1984 }
1985
1986 pub fn push_str(&mut self, text: &str, highlight: Option<HighlightId>) {
1987 let start_ix = self.text.len();
1988 self.text.push_str(text);
1989 let end_ix = self.text.len();
1990 if let Some(highlight) = highlight {
1991 self.runs.push((start_ix..end_ix, highlight));
1992 }
1993 }
1994
1995 pub fn text(&self) -> &str {
1996 self.text.as_str()
1997 }
1998
1999 pub fn filter_text(&self) -> &str {
2000 &self.text[self.filter_range.clone()]
2001 }
2002}
2003
2004impl From<String> for CodeLabel {
2005 fn from(value: String) -> Self {
2006 Self::plain(value, None)
2007 }
2008}
2009
2010impl From<&str> for CodeLabel {
2011 fn from(value: &str) -> Self {
2012 Self::plain(value.to_string(), None)
2013 }
2014}
2015
2016impl Ord for LanguageMatcher {
2017 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
2018 self.path_suffixes.cmp(&other.path_suffixes).then_with(|| {
2019 self.first_line_pattern
2020 .as_ref()
2021 .map(Regex::as_str)
2022 .cmp(&other.first_line_pattern.as_ref().map(Regex::as_str))
2023 })
2024 }
2025}
2026
2027impl PartialOrd for LanguageMatcher {
2028 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
2029 Some(self.cmp(other))
2030 }
2031}
2032
2033impl Eq for LanguageMatcher {}
2034
2035impl PartialEq for LanguageMatcher {
2036 fn eq(&self, other: &Self) -> bool {
2037 self.path_suffixes == other.path_suffixes
2038 && self.first_line_pattern.as_ref().map(Regex::as_str)
2039 == other.first_line_pattern.as_ref().map(Regex::as_str)
2040 }
2041}
2042
2043#[cfg(any(test, feature = "test-support"))]
2044impl Default for FakeLspAdapter {
2045 fn default() -> Self {
2046 Self {
2047 name: "the-fake-language-server",
2048 capabilities: lsp::LanguageServer::full_capabilities(),
2049 initializer: None,
2050 disk_based_diagnostics_progress_token: None,
2051 initialization_options: None,
2052 disk_based_diagnostics_sources: Vec::new(),
2053 prettier_plugins: Vec::new(),
2054 language_server_binary: LanguageServerBinary {
2055 path: "/the/fake/lsp/path".into(),
2056 arguments: vec![],
2057 env: Default::default(),
2058 },
2059 label_for_completion: None,
2060 }
2061 }
2062}
2063
2064#[cfg(any(test, feature = "test-support"))]
2065#[async_trait(?Send)]
2066impl LspAdapter for FakeLspAdapter {
2067 fn name(&self) -> LanguageServerName {
2068 LanguageServerName(self.name.into())
2069 }
2070
2071 async fn check_if_user_installed(
2072 &self,
2073 _: &dyn LspAdapterDelegate,
2074 _: Arc<dyn LanguageToolchainStore>,
2075 _: &AsyncApp,
2076 ) -> Option<LanguageServerBinary> {
2077 Some(self.language_server_binary.clone())
2078 }
2079
2080 fn get_language_server_command<'a>(
2081 self: Arc<Self>,
2082 _: Arc<dyn LspAdapterDelegate>,
2083 _: Arc<dyn LanguageToolchainStore>,
2084 _: LanguageServerBinaryOptions,
2085 _: futures::lock::MutexGuard<'a, Option<LanguageServerBinary>>,
2086 _: &'a mut AsyncApp,
2087 ) -> Pin<Box<dyn 'a + Future<Output = Result<LanguageServerBinary>>>> {
2088 async move { Ok(self.language_server_binary.clone()) }.boxed_local()
2089 }
2090
2091 async fn fetch_latest_server_version(
2092 &self,
2093 _: &dyn LspAdapterDelegate,
2094 ) -> Result<Box<dyn 'static + Send + Any>> {
2095 unreachable!();
2096 }
2097
2098 async fn fetch_server_binary(
2099 &self,
2100 _: Box<dyn 'static + Send + Any>,
2101 _: PathBuf,
2102 _: &dyn LspAdapterDelegate,
2103 ) -> Result<LanguageServerBinary> {
2104 unreachable!();
2105 }
2106
2107 async fn cached_server_binary(
2108 &self,
2109 _: PathBuf,
2110 _: &dyn LspAdapterDelegate,
2111 ) -> Option<LanguageServerBinary> {
2112 unreachable!();
2113 }
2114
2115 fn disk_based_diagnostic_sources(&self) -> Vec<String> {
2116 self.disk_based_diagnostics_sources.clone()
2117 }
2118
2119 fn disk_based_diagnostics_progress_token(&self) -> Option<String> {
2120 self.disk_based_diagnostics_progress_token.clone()
2121 }
2122
2123 async fn initialization_options(
2124 self: Arc<Self>,
2125 _: &dyn Fs,
2126 _: &Arc<dyn LspAdapterDelegate>,
2127 ) -> Result<Option<Value>> {
2128 Ok(self.initialization_options.clone())
2129 }
2130
2131 async fn label_for_completion(
2132 &self,
2133 item: &lsp::CompletionItem,
2134 language: &Arc<Language>,
2135 ) -> Option<CodeLabel> {
2136 let label_for_completion = self.label_for_completion.as_ref()?;
2137 label_for_completion(item, language)
2138 }
2139}
2140
2141fn get_capture_indices(query: &Query, captures: &mut [(&str, &mut Option<u32>)]) {
2142 for (ix, name) in query.capture_names().iter().enumerate() {
2143 for (capture_name, index) in captures.iter_mut() {
2144 if capture_name == name {
2145 **index = Some(ix as u32);
2146 break;
2147 }
2148 }
2149 }
2150}
2151
2152pub fn point_to_lsp(point: PointUtf16) -> lsp::Position {
2153 lsp::Position::new(point.row, point.column)
2154}
2155
2156pub fn point_from_lsp(point: lsp::Position) -> Unclipped<PointUtf16> {
2157 Unclipped(PointUtf16::new(point.line, point.character))
2158}
2159
2160pub fn range_to_lsp(range: Range<PointUtf16>) -> Result<lsp::Range> {
2161 if range.start > range.end {
2162 Err(anyhow!(
2163 "Inverted range provided to an LSP request: {:?}-{:?}",
2164 range.start,
2165 range.end
2166 ))
2167 } else {
2168 Ok(lsp::Range {
2169 start: point_to_lsp(range.start),
2170 end: point_to_lsp(range.end),
2171 })
2172 }
2173}
2174
2175pub fn range_from_lsp(range: lsp::Range) -> Range<Unclipped<PointUtf16>> {
2176 let mut start = point_from_lsp(range.start);
2177 let mut end = point_from_lsp(range.end);
2178 if start > end {
2179 log::warn!("range_from_lsp called with inverted range {start:?}-{end:?}");
2180 mem::swap(&mut start, &mut end);
2181 }
2182 start..end
2183}
2184
2185#[cfg(test)]
2186mod tests {
2187 use super::*;
2188 use gpui::TestAppContext;
2189
2190 #[gpui::test(iterations = 10)]
2191 async fn test_language_loading(cx: &mut TestAppContext) {
2192 let languages = LanguageRegistry::test(cx.executor());
2193 let languages = Arc::new(languages);
2194 languages.register_native_grammars([
2195 ("json", tree_sitter_json::LANGUAGE),
2196 ("rust", tree_sitter_rust::LANGUAGE),
2197 ]);
2198 languages.register_test_language(LanguageConfig {
2199 name: "JSON".into(),
2200 grammar: Some("json".into()),
2201 matcher: LanguageMatcher {
2202 path_suffixes: vec!["json".into()],
2203 ..Default::default()
2204 },
2205 ..Default::default()
2206 });
2207 languages.register_test_language(LanguageConfig {
2208 name: "Rust".into(),
2209 grammar: Some("rust".into()),
2210 matcher: LanguageMatcher {
2211 path_suffixes: vec!["rs".into()],
2212 ..Default::default()
2213 },
2214 ..Default::default()
2215 });
2216 assert_eq!(
2217 languages.language_names(),
2218 &[
2219 "JSON".to_string(),
2220 "Plain Text".to_string(),
2221 "Rust".to_string(),
2222 ]
2223 );
2224
2225 let rust1 = languages.language_for_name("Rust");
2226 let rust2 = languages.language_for_name("Rust");
2227
2228 // Ensure language is still listed even if it's being loaded.
2229 assert_eq!(
2230 languages.language_names(),
2231 &[
2232 "JSON".to_string(),
2233 "Plain Text".to_string(),
2234 "Rust".to_string(),
2235 ]
2236 );
2237
2238 let (rust1, rust2) = futures::join!(rust1, rust2);
2239 assert!(Arc::ptr_eq(&rust1.unwrap(), &rust2.unwrap()));
2240
2241 // Ensure language is still listed even after loading it.
2242 assert_eq!(
2243 languages.language_names(),
2244 &[
2245 "JSON".to_string(),
2246 "Plain Text".to_string(),
2247 "Rust".to_string(),
2248 ]
2249 );
2250
2251 // Loading an unknown language returns an error.
2252 assert!(languages.language_for_name("Unknown").await.is_err());
2253 }
2254
2255 #[gpui::test]
2256 async fn test_completion_label_omits_duplicate_data() {
2257 let regular_completion_item_1 = lsp::CompletionItem {
2258 label: "regular1".to_string(),
2259 detail: Some("detail1".to_string()),
2260 label_details: Some(lsp::CompletionItemLabelDetails {
2261 detail: None,
2262 description: Some("description 1".to_string()),
2263 }),
2264 ..lsp::CompletionItem::default()
2265 };
2266
2267 let regular_completion_item_2 = lsp::CompletionItem {
2268 label: "regular2".to_string(),
2269 label_details: Some(lsp::CompletionItemLabelDetails {
2270 detail: None,
2271 description: Some("description 2".to_string()),
2272 }),
2273 ..lsp::CompletionItem::default()
2274 };
2275
2276 let completion_item_with_duplicate_detail_and_proper_description = lsp::CompletionItem {
2277 detail: Some(regular_completion_item_1.label.clone()),
2278 ..regular_completion_item_1.clone()
2279 };
2280
2281 let completion_item_with_duplicate_detail = lsp::CompletionItem {
2282 detail: Some(regular_completion_item_1.label.clone()),
2283 label_details: None,
2284 ..regular_completion_item_1.clone()
2285 };
2286
2287 let completion_item_with_duplicate_description = lsp::CompletionItem {
2288 label_details: Some(lsp::CompletionItemLabelDetails {
2289 detail: None,
2290 description: Some(regular_completion_item_2.label.clone()),
2291 }),
2292 ..regular_completion_item_2.clone()
2293 };
2294
2295 assert_eq!(
2296 CodeLabel::fallback_for_completion(®ular_completion_item_1, None).text,
2297 format!(
2298 "{} {}",
2299 regular_completion_item_1.label,
2300 regular_completion_item_1.detail.unwrap()
2301 ),
2302 "LSP completion items with both detail and label_details.description should prefer detail"
2303 );
2304 assert_eq!(
2305 CodeLabel::fallback_for_completion(®ular_completion_item_2, None).text,
2306 format!(
2307 "{} {}",
2308 regular_completion_item_2.label,
2309 regular_completion_item_2
2310 .label_details
2311 .as_ref()
2312 .unwrap()
2313 .description
2314 .as_ref()
2315 .unwrap()
2316 ),
2317 "LSP completion items without detail but with label_details.description should use that"
2318 );
2319 assert_eq!(
2320 CodeLabel::fallback_for_completion(
2321 &completion_item_with_duplicate_detail_and_proper_description,
2322 None
2323 )
2324 .text,
2325 format!(
2326 "{} {}",
2327 regular_completion_item_1.label,
2328 regular_completion_item_1
2329 .label_details
2330 .as_ref()
2331 .unwrap()
2332 .description
2333 .as_ref()
2334 .unwrap()
2335 ),
2336 "LSP completion items with both detail and label_details.description should prefer description only if the detail duplicates the completion label"
2337 );
2338 assert_eq!(
2339 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_detail, None).text,
2340 regular_completion_item_1.label,
2341 "LSP completion items with duplicate label and detail, should omit the detail"
2342 );
2343 assert_eq!(
2344 CodeLabel::fallback_for_completion(&completion_item_with_duplicate_description, None)
2345 .text,
2346 regular_completion_item_2.label,
2347 "LSP completion items with duplicate label and detail, should omit the detail"
2348 );
2349 }
2350}