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