1use anyhow::Context;
2use collections::HashMap;
3use fs::Fs;
4use gpui::{AppContext, AsyncAppContext, BorrowAppContext, EventEmitter, Model, ModelContext};
5use language::LanguageServerName;
6use paths::{
7 local_settings_file_relative_path, local_tasks_file_relative_path,
8 local_vscode_tasks_file_relative_path, EDITORCONFIG_NAME,
9};
10use rpc::{proto, AnyProtoClient, TypedEnvelope};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use settings::{
14 parse_json_with_comments, InvalidSettingsError, LocalSettingsKind, Settings, SettingsLocation,
15 SettingsSources, SettingsStore,
16};
17use std::{
18 path::{Path, PathBuf},
19 sync::Arc,
20 time::Duration,
21};
22use task::{TaskTemplates, VsCodeTaskFile};
23use util::ResultExt;
24use worktree::{PathChange, UpdatedEntriesSet, Worktree, WorktreeId};
25
26use crate::{
27 task_store::TaskStore,
28 worktree_store::{WorktreeStore, WorktreeStoreEvent},
29};
30
31#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
32pub struct ProjectSettings {
33 /// Configuration for language servers.
34 ///
35 /// The following settings can be overridden for specific language servers:
36 /// - initialization_options
37 ///
38 /// To override settings for a language, add an entry for that language server's
39 /// name to the lsp value.
40 /// Default: null
41 #[serde(default)]
42 pub lsp: HashMap<LanguageServerName, LspSettings>,
43
44 /// Configuration for Git-related features
45 #[serde(default)]
46 pub git: GitSettings,
47
48 /// Configuration for Node-related features
49 #[serde(default)]
50 pub node: NodeBinarySettings,
51
52 /// Configuration for how direnv configuration should be loaded
53 #[serde(default)]
54 pub load_direnv: DirenvSettings,
55
56 /// Configuration for session-related features
57 #[serde(default)]
58 pub session: SessionSettings,
59}
60
61#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
62pub struct NodeBinarySettings {
63 /// The path to the node binary
64 pub path: Option<String>,
65 /// The path to the npm binary Zed should use (defaults to .path/../npm)
66 pub npm_path: Option<String>,
67 /// If disabled, zed will download its own copy of node.
68 #[serde(default)]
69 pub ignore_system_version: Option<bool>,
70}
71
72#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
73#[serde(rename_all = "snake_case")]
74pub enum DirenvSettings {
75 /// Load direnv configuration through a shell hook
76 ShellHook,
77 /// Load direnv configuration directly using `direnv export json`
78 #[default]
79 Direct,
80}
81
82#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
83pub struct GitSettings {
84 /// Whether or not to show the git gutter.
85 ///
86 /// Default: tracked_files
87 pub git_gutter: Option<GitGutterSetting>,
88 pub gutter_debounce: Option<u64>,
89 /// Whether or not to show git blame data inline in
90 /// the currently focused line.
91 ///
92 /// Default: on
93 pub inline_blame: Option<InlineBlameSettings>,
94}
95
96impl GitSettings {
97 pub fn inline_blame_enabled(&self) -> bool {
98 #[allow(unknown_lints, clippy::manual_unwrap_or_default)]
99 match self.inline_blame {
100 Some(InlineBlameSettings { enabled, .. }) => enabled,
101 _ => false,
102 }
103 }
104
105 pub fn inline_blame_delay(&self) -> Option<Duration> {
106 match self.inline_blame {
107 Some(InlineBlameSettings {
108 delay_ms: Some(delay_ms),
109 ..
110 }) if delay_ms > 0 => Some(Duration::from_millis(delay_ms)),
111 _ => None,
112 }
113 }
114
115 pub fn show_inline_commit_summary(&self) -> bool {
116 match self.inline_blame {
117 Some(InlineBlameSettings {
118 show_commit_summary,
119 ..
120 }) => show_commit_summary,
121 _ => false,
122 }
123 }
124}
125
126#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
127#[serde(rename_all = "snake_case")]
128pub enum GitGutterSetting {
129 /// Show git gutter in tracked files.
130 #[default]
131 TrackedFiles,
132 /// Hide git gutter
133 Hide,
134}
135
136#[derive(Clone, Copy, Debug, Default, Serialize, Deserialize, JsonSchema)]
137#[serde(rename_all = "snake_case")]
138pub struct InlineBlameSettings {
139 /// Whether or not to show git blame data inline in
140 /// the currently focused line.
141 ///
142 /// Default: true
143 #[serde(default = "true_value")]
144 pub enabled: bool,
145 /// Whether to only show the inline blame information
146 /// after a delay once the cursor stops moving.
147 ///
148 /// Default: 0
149 pub delay_ms: Option<u64>,
150 /// The minimum column number to show the inline blame information at
151 ///
152 /// Default: 0
153 pub min_column: Option<u32>,
154 /// Whether to show commit summary as part of the inline blame.
155 ///
156 /// Default: false
157 #[serde(default = "false_value")]
158 pub show_commit_summary: bool,
159}
160
161const fn true_value() -> bool {
162 true
163}
164const fn false_value() -> bool {
165 true
166}
167
168#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
169pub struct BinarySettings {
170 pub path: Option<String>,
171 pub arguments: Option<Vec<String>>,
172 pub ignore_system_version: Option<bool>,
173}
174
175#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
176#[serde(rename_all = "snake_case")]
177pub struct LspSettings {
178 pub binary: Option<BinarySettings>,
179 pub initialization_options: Option<serde_json::Value>,
180 pub settings: Option<serde_json::Value>,
181}
182
183#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
184pub struct SessionSettings {
185 /// Whether or not to restore unsaved buffers on restart.
186 ///
187 /// If this is true, user won't be prompted whether to save/discard
188 /// dirty files when closing the application.
189 ///
190 /// Default: true
191 pub restore_unsaved_buffers: bool,
192}
193
194impl Default for SessionSettings {
195 fn default() -> Self {
196 Self {
197 restore_unsaved_buffers: true,
198 }
199 }
200}
201
202impl Settings for ProjectSettings {
203 const KEY: Option<&'static str> = None;
204
205 type FileContent = Self;
206
207 fn load(
208 sources: SettingsSources<Self::FileContent>,
209 _: &mut AppContext,
210 ) -> anyhow::Result<Self> {
211 sources.json_merge()
212 }
213}
214
215pub enum SettingsObserverMode {
216 Local(Arc<dyn Fs>),
217 Remote,
218}
219
220#[derive(Clone, Debug, PartialEq)]
221pub enum SettingsObserverEvent {
222 LocalSettingsUpdated(Result<(), InvalidSettingsError>),
223}
224
225impl EventEmitter<SettingsObserverEvent> for SettingsObserver {}
226
227pub struct SettingsObserver {
228 mode: SettingsObserverMode,
229 downstream_client: Option<AnyProtoClient>,
230 worktree_store: Model<WorktreeStore>,
231 project_id: u64,
232 task_store: Model<TaskStore>,
233}
234
235/// SettingsObserver observers changes to .zed/{settings, task}.json files in local worktrees
236/// (or the equivalent protobuf messages from upstream) and updates local settings
237/// and sends notifications downstream.
238/// In ssh mode it also monitors ~/.config/zed/{settings, task}.json and sends the content
239/// upstream.
240impl SettingsObserver {
241 pub fn init(client: &AnyProtoClient) {
242 client.add_model_message_handler(Self::handle_update_worktree_settings);
243 }
244
245 pub fn new_local(
246 fs: Arc<dyn Fs>,
247 worktree_store: Model<WorktreeStore>,
248 task_store: Model<TaskStore>,
249 cx: &mut ModelContext<Self>,
250 ) -> Self {
251 cx.subscribe(&worktree_store, Self::on_worktree_store_event)
252 .detach();
253
254 Self {
255 worktree_store,
256 task_store,
257 mode: SettingsObserverMode::Local(fs),
258 downstream_client: None,
259 project_id: 0,
260 }
261 }
262
263 pub fn new_remote(
264 worktree_store: Model<WorktreeStore>,
265 task_store: Model<TaskStore>,
266 _: &mut ModelContext<Self>,
267 ) -> Self {
268 Self {
269 worktree_store,
270 task_store,
271 mode: SettingsObserverMode::Remote,
272 downstream_client: None,
273 project_id: 0,
274 }
275 }
276
277 pub fn shared(
278 &mut self,
279 project_id: u64,
280 downstream_client: AnyProtoClient,
281 cx: &mut ModelContext<Self>,
282 ) {
283 self.project_id = project_id;
284 self.downstream_client = Some(downstream_client.clone());
285
286 let store = cx.global::<SettingsStore>();
287 for worktree in self.worktree_store.read(cx).worktrees() {
288 let worktree_id = worktree.read(cx).id().to_proto();
289 for (path, content) in store.local_settings(worktree.read(cx).id()) {
290 downstream_client
291 .send(proto::UpdateWorktreeSettings {
292 project_id,
293 worktree_id,
294 path: path.to_string_lossy().into(),
295 content: Some(content),
296 kind: Some(
297 local_settings_kind_to_proto(LocalSettingsKind::Settings).into(),
298 ),
299 })
300 .log_err();
301 }
302 for (path, content, _) in store.local_editorconfig_settings(worktree.read(cx).id()) {
303 downstream_client
304 .send(proto::UpdateWorktreeSettings {
305 project_id,
306 worktree_id,
307 path: path.to_string_lossy().into(),
308 content: Some(content),
309 kind: Some(
310 local_settings_kind_to_proto(LocalSettingsKind::Editorconfig).into(),
311 ),
312 })
313 .log_err();
314 }
315 }
316 }
317
318 pub fn unshared(&mut self, _: &mut ModelContext<Self>) {
319 self.downstream_client = None;
320 }
321
322 async fn handle_update_worktree_settings(
323 this: Model<Self>,
324 envelope: TypedEnvelope<proto::UpdateWorktreeSettings>,
325 mut cx: AsyncAppContext,
326 ) -> anyhow::Result<()> {
327 let kind = match envelope.payload.kind {
328 Some(kind) => proto::LocalSettingsKind::from_i32(kind)
329 .with_context(|| format!("unknown kind {kind}"))?,
330 None => proto::LocalSettingsKind::Settings,
331 };
332 this.update(&mut cx, |this, cx| {
333 let worktree_id = WorktreeId::from_proto(envelope.payload.worktree_id);
334 let Some(worktree) = this
335 .worktree_store
336 .read(cx)
337 .worktree_for_id(worktree_id, cx)
338 else {
339 return;
340 };
341
342 this.update_settings(
343 worktree,
344 [(
345 PathBuf::from(&envelope.payload.path).into(),
346 local_settings_kind_from_proto(kind),
347 envelope.payload.content,
348 )],
349 cx,
350 );
351 })?;
352 Ok(())
353 }
354
355 fn on_worktree_store_event(
356 &mut self,
357 _: Model<WorktreeStore>,
358 event: &WorktreeStoreEvent,
359 cx: &mut ModelContext<Self>,
360 ) {
361 if let WorktreeStoreEvent::WorktreeAdded(worktree) = event {
362 cx.subscribe(worktree, |this, worktree, event, cx| {
363 if let worktree::Event::UpdatedEntries(changes) = event {
364 this.update_local_worktree_settings(&worktree, changes, cx)
365 }
366 })
367 .detach()
368 }
369 }
370
371 fn update_local_worktree_settings(
372 &mut self,
373 worktree: &Model<Worktree>,
374 changes: &UpdatedEntriesSet,
375 cx: &mut ModelContext<Self>,
376 ) {
377 let SettingsObserverMode::Local(fs) = &self.mode else {
378 return;
379 };
380
381 let mut settings_contents = Vec::new();
382 for (path, _, change) in changes.iter() {
383 let (settings_dir, kind) = if path.ends_with(local_settings_file_relative_path()) {
384 let settings_dir = Arc::<Path>::from(
385 path.ancestors()
386 .nth(local_settings_file_relative_path().components().count())
387 .unwrap(),
388 );
389 (settings_dir, LocalSettingsKind::Settings)
390 } else if path.ends_with(local_tasks_file_relative_path()) {
391 let settings_dir = Arc::<Path>::from(
392 path.ancestors()
393 .nth(
394 local_tasks_file_relative_path()
395 .components()
396 .count()
397 .saturating_sub(1),
398 )
399 .unwrap(),
400 );
401 (settings_dir, LocalSettingsKind::Tasks)
402 } else if path.ends_with(local_vscode_tasks_file_relative_path()) {
403 let settings_dir = Arc::<Path>::from(
404 path.ancestors()
405 .nth(
406 local_vscode_tasks_file_relative_path()
407 .components()
408 .count()
409 .saturating_sub(1),
410 )
411 .unwrap(),
412 );
413 (settings_dir, LocalSettingsKind::Tasks)
414 } else if path.ends_with(EDITORCONFIG_NAME) {
415 let Some(settings_dir) = path.parent().map(Arc::from) else {
416 continue;
417 };
418 (settings_dir, LocalSettingsKind::Editorconfig)
419 } else {
420 continue;
421 };
422
423 let removed = change == &PathChange::Removed;
424 let fs = fs.clone();
425 let abs_path = match worktree.read(cx).absolutize(path) {
426 Ok(abs_path) => abs_path,
427 Err(e) => {
428 log::warn!("Cannot absolutize {path:?} received as {change:?} FS change: {e}");
429 continue;
430 }
431 };
432 settings_contents.push(async move {
433 (
434 settings_dir,
435 kind,
436 if removed {
437 None
438 } else {
439 Some(
440 async move {
441 let content = fs.load(&abs_path).await?;
442 if abs_path.ends_with(local_vscode_tasks_file_relative_path()) {
443 let vscode_tasks =
444 parse_json_with_comments::<VsCodeTaskFile>(&content)
445 .with_context(|| {
446 format!("parsing VSCode tasks, file {abs_path:?}")
447 })?;
448 let zed_tasks = TaskTemplates::try_from(vscode_tasks)
449 .with_context(|| {
450 format!(
451 "converting VSCode tasks into Zed ones, file {abs_path:?}"
452 )
453 })?;
454 serde_json::to_string(&zed_tasks).with_context(|| {
455 format!(
456 "serializing Zed tasks into JSON, file {abs_path:?}"
457 )
458 })
459 } else {
460 Ok(content)
461 }
462 }
463 .await,
464 )
465 },
466 )
467 });
468 }
469
470 if settings_contents.is_empty() {
471 return;
472 }
473
474 let worktree = worktree.clone();
475 cx.spawn(move |this, cx| async move {
476 let settings_contents: Vec<(Arc<Path>, _, _)> =
477 futures::future::join_all(settings_contents).await;
478 cx.update(|cx| {
479 this.update(cx, |this, cx| {
480 this.update_settings(
481 worktree,
482 settings_contents.into_iter().map(|(path, kind, content)| {
483 (path, kind, content.and_then(|c| c.log_err()))
484 }),
485 cx,
486 )
487 })
488 })
489 })
490 .detach();
491 }
492
493 fn update_settings(
494 &mut self,
495 worktree: Model<Worktree>,
496 settings_contents: impl IntoIterator<Item = (Arc<Path>, LocalSettingsKind, Option<String>)>,
497 cx: &mut ModelContext<Self>,
498 ) {
499 let worktree_id = worktree.read(cx).id();
500 let remote_worktree_id = worktree.read(cx).id();
501 let task_store = self.task_store.clone();
502
503 for (directory, kind, file_content) in settings_contents {
504 match kind {
505 LocalSettingsKind::Settings | LocalSettingsKind::Editorconfig => cx
506 .update_global::<SettingsStore, _>(|store, cx| {
507 let result = store.set_local_settings(
508 worktree_id,
509 directory.clone(),
510 kind,
511 file_content.as_deref(),
512 cx,
513 );
514
515 match result {
516 Err(InvalidSettingsError::LocalSettings { path, message }) => {
517 log::error!(
518 "Failed to set local settings in {:?}: {:?}",
519 path,
520 message
521 );
522 cx.emit(SettingsObserverEvent::LocalSettingsUpdated(Err(
523 InvalidSettingsError::LocalSettings { path, message },
524 )));
525 }
526 Err(e) => {
527 log::error!("Failed to set local settings: {e}");
528 }
529 Ok(_) => {
530 cx.emit(SettingsObserverEvent::LocalSettingsUpdated(Ok(())));
531 }
532 }
533 }),
534 LocalSettingsKind::Tasks => task_store.update(cx, |task_store, cx| {
535 task_store
536 .update_user_tasks(
537 Some(SettingsLocation {
538 worktree_id,
539 path: directory.as_ref(),
540 }),
541 file_content.as_deref(),
542 cx,
543 )
544 .log_err();
545 }),
546 };
547
548 if let Some(downstream_client) = &self.downstream_client {
549 downstream_client
550 .send(proto::UpdateWorktreeSettings {
551 project_id: self.project_id,
552 worktree_id: remote_worktree_id.to_proto(),
553 path: directory.to_string_lossy().into_owned(),
554 content: file_content,
555 kind: Some(local_settings_kind_to_proto(kind).into()),
556 })
557 .log_err();
558 }
559 }
560 }
561}
562
563pub fn local_settings_kind_from_proto(kind: proto::LocalSettingsKind) -> LocalSettingsKind {
564 match kind {
565 proto::LocalSettingsKind::Settings => LocalSettingsKind::Settings,
566 proto::LocalSettingsKind::Tasks => LocalSettingsKind::Tasks,
567 proto::LocalSettingsKind::Editorconfig => LocalSettingsKind::Editorconfig,
568 }
569}
570
571pub fn local_settings_kind_to_proto(kind: LocalSettingsKind) -> proto::LocalSettingsKind {
572 match kind {
573 LocalSettingsKind::Settings => proto::LocalSettingsKind::Settings,
574 LocalSettingsKind::Tasks => proto::LocalSettingsKind::Tasks,
575 LocalSettingsKind::Editorconfig => proto::LocalSettingsKind::Editorconfig,
576 }
577}