Expand tilde paths in edit prediction settings (#31235)

clauses3 created

Release Notes:

- edit_prediction: Handle `~` in paths in `disabled_globs` setting

Change summary

Cargo.lock                               |  1 +
crates/language/Cargo.toml               |  1 +
crates/language/src/language_settings.rs | 20 +++++++++++++++-----
docs/src/ai/edit-prediction.md           | 12 ++++++++++++
4 files changed, 29 insertions(+), 5 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -8762,6 +8762,7 @@ dependencies = [
  "serde",
  "serde_json",
  "settings",
+ "shellexpand 2.1.2",
  "smallvec",
  "smol",
  "streaming-iterator",

crates/language/Cargo.toml 🔗

@@ -51,6 +51,7 @@ schemars.workspace = true
 serde.workspace = true
 serde_json.workspace = true
 settings.workspace = true
+shellexpand.workspace = true
 smallvec.workspace = true
 smol.workspace = true
 streaming-iterator.workspace = true

crates/language/src/language_settings.rs 🔗

@@ -23,6 +23,7 @@ use serde_json::Value;
 use settings::{
     Settings, SettingsLocation, SettingsSources, SettingsStore, add_references_to_properties,
 };
+use shellexpand;
 use std::{borrow::Cow, num::NonZeroU32, path::Path, sync::Arc};
 use util::serde::default_true;
 
@@ -1331,9 +1332,10 @@ impl settings::Settings for AllLanguageSettings {
                 disabled_globs: completion_globs
                     .iter()
                     .filter_map(|g| {
+                        let expanded_g = shellexpand::tilde(g).into_owned();
                         Some(DisabledGlob {
-                            matcher: globset::Glob::new(g).ok()?.compile_matcher(),
-                            is_absolute: Path::new(g).is_absolute(),
+                            matcher: globset::Glob::new(&expanded_g).ok()?.compile_matcher(),
+                            is_absolute: Path::new(&expanded_g).is_absolute(),
                         })
                     })
                     .collect(),
@@ -1712,10 +1714,12 @@ mod tests {
                         };
                         #[cfg(windows)]
                         let glob_str = glob_str.as_str();
-
+                        let expanded_glob_str = shellexpand::tilde(glob_str).into_owned();
                         DisabledGlob {
-                            matcher: globset::Glob::new(glob_str).unwrap().compile_matcher(),
-                            is_absolute: Path::new(glob_str).is_absolute(),
+                            matcher: globset::Glob::new(&expanded_glob_str)
+                                .unwrap()
+                                .compile_matcher(),
+                            is_absolute: Path::new(&expanded_glob_str).is_absolute(),
                         }
                     })
                     .collect(),
@@ -1811,6 +1815,12 @@ mod tests {
         let dot_env_file = make_test_file(&[".env"]);
         let settings = build_settings(&[".env"]);
         assert!(!settings.enabled_for_file(&dot_env_file, &cx));
+
+        // Test tilde expansion
+        let home = shellexpand::tilde("~").into_owned().to_string();
+        let home_file = make_test_file(&[&home, "test.rs"]);
+        let settings = build_settings(&["~/test.rs"]);
+        assert!(!settings.enabled_for_file(&home_file, &cx));
     }
 
     #[test]

docs/src/ai/edit-prediction.md 🔗

@@ -231,6 +231,18 @@ To not have predictions appear automatically as you type when working with a spe
 }
 ```
 
+### In Specific Directories
+
+To disable edit predictions for specific directories or files, set this within `settings.json`:
+
+```json
+{
+  "edit_predictions": {
+    "disabled_globs": ["~/.config/zed/settings.json"]
+  }
+}
+```
+
 ### Turning Off Completely
 
 To completely turn off edit prediction across all providers, explicitly set the settings to `none`, like so: