1//! Provides `language`-related settings.
2
3use crate::{File, Language};
4use anyhow::Result;
5use collections::{HashMap, HashSet};
6use globset::GlobMatcher;
7use gpui::AppContext;
8use schemars::{
9 schema::{InstanceType, ObjectValidation, Schema, SchemaObject},
10 JsonSchema,
11};
12use serde::{Deserialize, Serialize};
13use settings::Settings;
14use std::{num::NonZeroU32, path::Path, sync::Arc};
15
16/// Initializes the language settings.
17pub fn init(cx: &mut AppContext) {
18 AllLanguageSettings::register(cx);
19}
20
21/// Returns the settings for the specified language from the provided file.
22pub fn language_settings<'a>(
23 language: Option<&Arc<Language>>,
24 file: Option<&Arc<dyn File>>,
25 cx: &'a AppContext,
26) -> &'a LanguageSettings {
27 let language_name = language.map(|l| l.name());
28 all_language_settings(file, cx).language(language_name.as_deref())
29}
30
31/// Returns the settings for all languages from the provided file.
32pub fn all_language_settings<'a>(
33 file: Option<&Arc<dyn File>>,
34 cx: &'a AppContext,
35) -> &'a AllLanguageSettings {
36 let location = file.map(|f| (f.worktree_id(), f.path().as_ref()));
37 AllLanguageSettings::get(location, cx)
38}
39
40/// The settings for all languages.
41#[derive(Debug, Clone)]
42pub struct AllLanguageSettings {
43 /// The settings for GitHub Copilot.
44 pub copilot: CopilotSettings,
45 defaults: LanguageSettings,
46 languages: HashMap<Arc<str>, LanguageSettings>,
47}
48
49/// The settings for a particular language.
50#[derive(Debug, Clone, Deserialize)]
51pub struct LanguageSettings {
52 /// How many columns a tab should occupy.
53 pub tab_size: NonZeroU32,
54 /// Whether to indent lines using tab characters, as opposed to multiple
55 /// spaces.
56 pub hard_tabs: bool,
57 /// How to soft-wrap long lines of text.
58 pub soft_wrap: SoftWrap,
59 /// The column at which to soft-wrap lines, for buffers where soft-wrap
60 /// is enabled.
61 pub preferred_line_length: u32,
62 /// Whether to show wrap guides in the editor. Setting this to true will
63 /// show a guide at the 'preferred_line_length' value if softwrap is set to
64 /// 'preferred_line_length', and will show any additional guides as specified
65 /// by the 'wrap_guides' setting.
66 pub show_wrap_guides: bool,
67 /// Character counts at which to show wrap guides in the editor.
68 pub wrap_guides: Vec<usize>,
69 /// Whether or not to perform a buffer format before saving.
70 pub format_on_save: FormatOnSave,
71 /// Whether or not to remove any trailing whitespace from lines of a buffer
72 /// before saving it.
73 pub remove_trailing_whitespace_on_save: bool,
74 /// Whether or not to ensure there's a single newline at the end of a buffer
75 /// when saving it.
76 pub ensure_final_newline_on_save: bool,
77 /// How to perform a buffer format.
78 pub formatter: Formatter,
79 /// Zed's Prettier integration settings.
80 /// If Prettier is enabled, Zed will use this its Prettier instance for any applicable file, if
81 /// the project has no other Prettier installed.
82 pub prettier: HashMap<String, serde_json::Value>,
83 /// Whether to use language servers to provide code intelligence.
84 pub enable_language_server: bool,
85 /// Controls whether Copilot provides suggestion immediately (true)
86 /// or waits for a `copilot::Toggle` (false).
87 pub show_copilot_suggestions: bool,
88 /// Whether to show tabs and spaces in the editor.
89 pub show_whitespaces: ShowWhitespaceSetting,
90 /// Whether to start a new line with a comment when a previous line is a comment as well.
91 pub extend_comment_on_newline: bool,
92 /// Inlay hint related settings.
93 pub inlay_hints: InlayHintSettings,
94 /// Whether to automatically close brackets.
95 pub use_autoclose: bool,
96 /// Which code actions to run on save
97 pub code_actions_on_format: HashMap<String, bool>,
98}
99
100/// The settings for [GitHub Copilot](https://github.com/features/copilot).
101#[derive(Clone, Debug, Default)]
102pub struct CopilotSettings {
103 /// Whether Copilot is enabled.
104 pub feature_enabled: bool,
105 /// A list of globs representing files that Copilot should be disabled for.
106 pub disabled_globs: Vec<GlobMatcher>,
107}
108
109/// The settings for all languages.
110#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
111pub struct AllLanguageSettingsContent {
112 /// The settings for enabling/disabling features.
113 #[serde(default)]
114 pub features: Option<FeaturesContent>,
115 /// The settings for GitHub Copilot.
116 #[serde(default)]
117 pub copilot: Option<CopilotSettingsContent>,
118 /// The default language settings.
119 #[serde(flatten)]
120 pub defaults: LanguageSettingsContent,
121 /// The settings for individual languages.
122 #[serde(default, alias = "language_overrides")]
123 pub languages: HashMap<Arc<str>, LanguageSettingsContent>,
124}
125
126/// The settings for a particular language.
127#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
128pub struct LanguageSettingsContent {
129 /// How many columns a tab should occupy.
130 ///
131 /// Default: 4
132 #[serde(default)]
133 pub tab_size: Option<NonZeroU32>,
134 /// Whether to indent lines using tab characters, as opposed to multiple
135 /// spaces.
136 ///
137 /// Default: false
138 #[serde(default)]
139 pub hard_tabs: Option<bool>,
140 /// How to soft-wrap long lines of text.
141 ///
142 /// Default: none
143 #[serde(default)]
144 pub soft_wrap: Option<SoftWrap>,
145 /// The column at which to soft-wrap lines, for buffers where soft-wrap
146 /// is enabled.
147 ///
148 /// Default: 80
149 #[serde(default)]
150 pub preferred_line_length: Option<u32>,
151 /// Whether to show wrap guides in the editor. Setting this to true will
152 /// show a guide at the 'preferred_line_length' value if softwrap is set to
153 /// 'preferred_line_length', and will show any additional guides as specified
154 /// by the 'wrap_guides' setting.
155 ///
156 /// Default: true
157 #[serde(default)]
158 pub show_wrap_guides: Option<bool>,
159 /// Character counts at which to show wrap guides in the editor.
160 ///
161 /// Default: []
162 #[serde(default)]
163 pub wrap_guides: Option<Vec<usize>>,
164 /// Whether or not to perform a buffer format before saving.
165 ///
166 /// Default: on
167 #[serde(default)]
168 pub format_on_save: Option<FormatOnSave>,
169 /// Whether or not to remove any trailing whitespace from lines of a buffer
170 /// before saving it.
171 ///
172 /// Default: true
173 #[serde(default)]
174 pub remove_trailing_whitespace_on_save: Option<bool>,
175 /// Whether or not to ensure there's a single newline at the end of a buffer
176 /// when saving it.
177 ///
178 /// Default: true
179 #[serde(default)]
180 pub ensure_final_newline_on_save: Option<bool>,
181 /// How to perform a buffer format.
182 ///
183 /// Default: auto
184 #[serde(default)]
185 pub formatter: Option<Formatter>,
186 /// Zed's Prettier integration settings.
187 /// If Prettier is enabled, Zed will use this its Prettier instance for any applicable file, if
188 /// the project has no other Prettier installed.
189 ///
190 /// Default: {}
191 #[serde(default)]
192 pub prettier: Option<HashMap<String, serde_json::Value>>,
193 /// Whether to use language servers to provide code intelligence.
194 ///
195 /// Default: true
196 #[serde(default)]
197 pub enable_language_server: Option<bool>,
198 /// Controls whether Copilot provides suggestion immediately (true)
199 /// or waits for a `copilot::Toggle` (false).
200 ///
201 /// Default: true
202 #[serde(default)]
203 pub show_copilot_suggestions: Option<bool>,
204 /// Whether to show tabs and spaces in the editor.
205 #[serde(default)]
206 pub show_whitespaces: Option<ShowWhitespaceSetting>,
207 /// Whether to start a new line with a comment when a previous line is a comment as well.
208 ///
209 /// Default: true
210 #[serde(default)]
211 pub extend_comment_on_newline: Option<bool>,
212 /// Inlay hint related settings.
213 #[serde(default)]
214 pub inlay_hints: Option<InlayHintSettings>,
215 /// Whether to automatically type closing characters for you. For example,
216 /// when you type (, Zed will automatically add a closing ) at the correct position.
217 ///
218 /// Default: true
219 pub use_autoclose: Option<bool>,
220
221 /// Which code actions to run on save
222 ///
223 /// Default: {} (or {"source.organizeImports": true} for Go).
224 pub code_actions_on_format: Option<HashMap<String, bool>>,
225}
226
227/// The contents of the GitHub Copilot settings.
228#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
229pub struct CopilotSettingsContent {
230 /// A list of globs representing files that Copilot should be disabled for.
231 #[serde(default)]
232 pub disabled_globs: Option<Vec<String>>,
233}
234
235/// The settings for enabling/disabling features.
236#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
237#[serde(rename_all = "snake_case")]
238pub struct FeaturesContent {
239 /// Whether the GitHub Copilot feature is enabled.
240 pub copilot: Option<bool>,
241}
242
243/// Controls the soft-wrapping behavior in the editor.
244#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
245#[serde(rename_all = "snake_case")]
246pub enum SoftWrap {
247 /// Do not soft wrap.
248 None,
249 /// Soft wrap lines that overflow the editor
250 EditorWidth,
251 /// Soft wrap lines at the preferred line length
252 PreferredLineLength,
253}
254
255/// Controls the behavior of formatting files when they are saved.
256#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
257#[serde(rename_all = "snake_case")]
258pub enum FormatOnSave {
259 /// Files should be formatted on save.
260 On,
261 /// Files should not be formatted on save.
262 Off,
263 /// Files should be formatted using the current language server.
264 LanguageServer,
265 /// The external program to use to format the files on save.
266 External {
267 /// The external program to run.
268 command: Arc<str>,
269 /// The arguments to pass to the program.
270 arguments: Arc<[String]>,
271 },
272}
273
274/// Controls how whitespace should be displayedin the editor.
275#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
276#[serde(rename_all = "snake_case")]
277pub enum ShowWhitespaceSetting {
278 /// Draw whitespace only for the selected text.
279 Selection,
280 /// Do not draw any tabs or spaces.
281 None,
282 /// Draw all invisible symbols.
283 All,
284}
285
286/// Controls which formatter should be used when formatting code.
287#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
288#[serde(rename_all = "snake_case")]
289pub enum Formatter {
290 /// Format files using Zed's Prettier integration (if applicable),
291 /// or falling back to formatting via language server.
292 #[default]
293 Auto,
294 /// Format code using the current language server.
295 LanguageServer,
296 /// Format code using Zed's Prettier integration.
297 Prettier,
298 /// Format code using an external command.
299 External {
300 /// The external program to run.
301 command: Arc<str>,
302 /// The arguments to pass to the program.
303 arguments: Arc<[String]>,
304 },
305}
306
307/// The settings for inlay hints.
308#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
309pub struct InlayHintSettings {
310 /// Global switch to toggle hints on and off.
311 ///
312 /// Default: false
313 #[serde(default)]
314 pub enabled: bool,
315 /// Whether type hints should be shown.
316 ///
317 /// Default: true
318 #[serde(default = "default_true")]
319 pub show_type_hints: bool,
320 /// Whether parameter hints should be shown.
321 ///
322 /// Default: true
323 #[serde(default = "default_true")]
324 pub show_parameter_hints: bool,
325 /// Whether other hints should be shown.
326 ///
327 /// Default: true
328 #[serde(default = "default_true")]
329 pub show_other_hints: bool,
330 /// Whether or not to debounce inlay hints updates after buffer edits.
331 ///
332 /// Set to 0 to disable debouncing.
333 ///
334 /// Default: 700
335 #[serde(default = "edit_debounce_ms")]
336 pub edit_debounce_ms: u64,
337 /// Whether or not to debounce inlay hints updates after buffer scrolls.
338 ///
339 /// Set to 0 to disable debouncing.
340 ///
341 /// Default: 50
342 #[serde(default = "scroll_debounce_ms")]
343 pub scroll_debounce_ms: u64,
344}
345
346fn default_true() -> bool {
347 true
348}
349
350fn edit_debounce_ms() -> u64 {
351 700
352}
353
354fn scroll_debounce_ms() -> u64 {
355 50
356}
357
358impl InlayHintSettings {
359 /// Returns the kinds of inlay hints that are enabled based on the settings.
360 pub fn enabled_inlay_hint_kinds(&self) -> HashSet<Option<InlayHintKind>> {
361 let mut kinds = HashSet::default();
362 if self.show_type_hints {
363 kinds.insert(Some(InlayHintKind::Type));
364 }
365 if self.show_parameter_hints {
366 kinds.insert(Some(InlayHintKind::Parameter));
367 }
368 if self.show_other_hints {
369 kinds.insert(None);
370 }
371 kinds
372 }
373}
374
375impl AllLanguageSettings {
376 /// Returns the [`LanguageSettings`] for the language with the specified name.
377 pub fn language<'a>(&'a self, language_name: Option<&str>) -> &'a LanguageSettings {
378 if let Some(name) = language_name {
379 if let Some(overrides) = self.languages.get(name) {
380 return overrides;
381 }
382 }
383 &self.defaults
384 }
385
386 /// Returns whether GitHub Copilot is enabled for the given path.
387 pub fn copilot_enabled_for_path(&self, path: &Path) -> bool {
388 !self
389 .copilot
390 .disabled_globs
391 .iter()
392 .any(|glob| glob.is_match(path))
393 }
394
395 /// Returns whether GitHub Copilot is enabled for the given language and path.
396 pub fn copilot_enabled(&self, language: Option<&Arc<Language>>, path: Option<&Path>) -> bool {
397 if !self.copilot.feature_enabled {
398 return false;
399 }
400
401 if let Some(path) = path {
402 if !self.copilot_enabled_for_path(path) {
403 return false;
404 }
405 }
406
407 self.language(language.map(|l| l.name()).as_deref())
408 .show_copilot_suggestions
409 }
410}
411
412/// The kind of an inlay hint.
413#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
414pub enum InlayHintKind {
415 /// An inlay hint for a type.
416 Type,
417 /// An inlay hint for a parameter.
418 Parameter,
419}
420
421impl InlayHintKind {
422 /// Returns the [`InlayHintKind`] from the given name.
423 ///
424 /// Returns `None` if `name` does not match any of the expected
425 /// string representations.
426 pub fn from_name(name: &str) -> Option<Self> {
427 match name {
428 "type" => Some(InlayHintKind::Type),
429 "parameter" => Some(InlayHintKind::Parameter),
430 _ => None,
431 }
432 }
433
434 /// Returns the name of this [`InlayHintKind`].
435 pub fn name(&self) -> &'static str {
436 match self {
437 InlayHintKind::Type => "type",
438 InlayHintKind::Parameter => "parameter",
439 }
440 }
441}
442
443impl settings::Settings for AllLanguageSettings {
444 const KEY: Option<&'static str> = None;
445
446 type FileContent = AllLanguageSettingsContent;
447
448 fn load(
449 default_value: &Self::FileContent,
450 user_settings: &[&Self::FileContent],
451 _: &mut AppContext,
452 ) -> Result<Self> {
453 // A default is provided for all settings.
454 let mut defaults: LanguageSettings =
455 serde_json::from_value(serde_json::to_value(&default_value.defaults)?)?;
456
457 let mut languages = HashMap::default();
458 for (language_name, settings) in &default_value.languages {
459 let mut language_settings = defaults.clone();
460 merge_settings(&mut language_settings, settings);
461 languages.insert(language_name.clone(), language_settings);
462 }
463
464 let mut copilot_enabled = default_value
465 .features
466 .as_ref()
467 .and_then(|f| f.copilot)
468 .ok_or_else(Self::missing_default)?;
469 let mut copilot_globs = default_value
470 .copilot
471 .as_ref()
472 .and_then(|c| c.disabled_globs.as_ref())
473 .ok_or_else(Self::missing_default)?;
474
475 for user_settings in user_settings {
476 if let Some(copilot) = user_settings.features.as_ref().and_then(|f| f.copilot) {
477 copilot_enabled = copilot;
478 }
479 if let Some(globs) = user_settings
480 .copilot
481 .as_ref()
482 .and_then(|f| f.disabled_globs.as_ref())
483 {
484 copilot_globs = globs;
485 }
486
487 // A user's global settings override the default global settings and
488 // all default language-specific settings.
489 merge_settings(&mut defaults, &user_settings.defaults);
490 for language_settings in languages.values_mut() {
491 merge_settings(language_settings, &user_settings.defaults);
492 }
493
494 // A user's language-specific settings override default language-specific settings.
495 for (language_name, user_language_settings) in &user_settings.languages {
496 merge_settings(
497 languages
498 .entry(language_name.clone())
499 .or_insert_with(|| defaults.clone()),
500 user_language_settings,
501 );
502 }
503 }
504
505 Ok(Self {
506 copilot: CopilotSettings {
507 feature_enabled: copilot_enabled,
508 disabled_globs: copilot_globs
509 .iter()
510 .filter_map(|g| Some(globset::Glob::new(g).ok()?.compile_matcher()))
511 .collect(),
512 },
513 defaults,
514 languages,
515 })
516 }
517
518 fn json_schema(
519 generator: &mut schemars::gen::SchemaGenerator,
520 params: &settings::SettingsJsonSchemaParams,
521 _: &AppContext,
522 ) -> schemars::schema::RootSchema {
523 let mut root_schema = generator.root_schema_for::<Self::FileContent>();
524
525 // Create a schema for a 'languages overrides' object, associating editor
526 // settings with specific languages.
527 assert!(root_schema
528 .definitions
529 .contains_key("LanguageSettingsContent"));
530
531 let languages_object_schema = SchemaObject {
532 instance_type: Some(InstanceType::Object.into()),
533 object: Some(Box::new(ObjectValidation {
534 properties: params
535 .language_names
536 .iter()
537 .map(|name| {
538 (
539 name.clone(),
540 Schema::new_ref("#/definitions/LanguageSettingsContent".into()),
541 )
542 })
543 .collect(),
544 ..Default::default()
545 })),
546 ..Default::default()
547 };
548
549 root_schema
550 .definitions
551 .extend([("Languages".into(), languages_object_schema.into())]);
552
553 root_schema
554 .schema
555 .object
556 .as_mut()
557 .unwrap()
558 .properties
559 .extend([
560 (
561 "languages".to_owned(),
562 Schema::new_ref("#/definitions/Languages".into()),
563 ),
564 // For backward compatibility
565 (
566 "language_overrides".to_owned(),
567 Schema::new_ref("#/definitions/Languages".into()),
568 ),
569 ]);
570
571 root_schema
572 }
573}
574
575fn merge_settings(settings: &mut LanguageSettings, src: &LanguageSettingsContent) {
576 merge(&mut settings.tab_size, src.tab_size);
577 merge(&mut settings.hard_tabs, src.hard_tabs);
578 merge(&mut settings.soft_wrap, src.soft_wrap);
579 merge(&mut settings.use_autoclose, src.use_autoclose);
580 merge(&mut settings.show_wrap_guides, src.show_wrap_guides);
581 merge(&mut settings.wrap_guides, src.wrap_guides.clone());
582 merge(
583 &mut settings.code_actions_on_format,
584 src.code_actions_on_format.clone(),
585 );
586
587 merge(
588 &mut settings.preferred_line_length,
589 src.preferred_line_length,
590 );
591 merge(&mut settings.formatter, src.formatter.clone());
592 merge(&mut settings.prettier, src.prettier.clone());
593 merge(&mut settings.format_on_save, src.format_on_save.clone());
594 merge(
595 &mut settings.remove_trailing_whitespace_on_save,
596 src.remove_trailing_whitespace_on_save,
597 );
598 merge(
599 &mut settings.ensure_final_newline_on_save,
600 src.ensure_final_newline_on_save,
601 );
602 merge(
603 &mut settings.enable_language_server,
604 src.enable_language_server,
605 );
606 merge(
607 &mut settings.show_copilot_suggestions,
608 src.show_copilot_suggestions,
609 );
610 merge(&mut settings.show_whitespaces, src.show_whitespaces);
611 merge(
612 &mut settings.extend_comment_on_newline,
613 src.extend_comment_on_newline,
614 );
615 merge(&mut settings.inlay_hints, src.inlay_hints);
616 fn merge<T>(target: &mut T, value: Option<T>) {
617 if let Some(value) = value {
618 *target = value;
619 }
620 }
621}