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