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