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