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