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