Copy setting and keymap files from legacy config locations

Antonio Scandurra created

Change summary

crates/zed/src/main.rs  | 26 +++++++++++++++++++++++---
crates/zed/src/paths.rs | 28 +++++++++++++++-------------
crates/zed/src/zed.rs   | 10 +++++++---
3 files changed, 45 insertions(+), 19 deletions(-)

Detailed changes

crates/zed/src/main.rs 🔗

@@ -42,9 +42,7 @@ use zed::{
 
 fn main() {
     let http = http::client();
-    fs::create_dir_all(&*zed::paths::LANGUAGES_DIR).expect("could not create languages path");
-    fs::create_dir_all(&*zed::paths::DB_DIR).expect("could not create database path");
-    fs::create_dir_all(&*zed::paths::LOGS_DIR).expect("could not create logs path");
+    init_paths();
     init_logger();
 
     log::info!("========== starting zed ==========");
@@ -195,6 +193,28 @@ fn main() {
     });
 }
 
+fn init_paths() {
+    fs::create_dir_all(&*zed::paths::CONFIG_DIR).expect("could not create config path");
+    fs::create_dir_all(&*zed::paths::LANGUAGES_DIR).expect("could not create languages path");
+    fs::create_dir_all(&*zed::paths::DB_DIR).expect("could not create database path");
+    fs::create_dir_all(&*zed::paths::LOGS_DIR).expect("could not create logs path");
+
+    // Copy setting files from legacy locations. TODO: remove this after a few releases.
+    thread::spawn(|| {
+        if fs::metadata(&*zed::paths::legacy::SETTINGS).is_ok()
+            && fs::metadata(&*zed::paths::SETTINGS).is_err()
+        {
+            fs::copy(&*zed::paths::legacy::SETTINGS, &*zed::paths::SETTINGS).log_err();
+        }
+
+        if fs::metadata(&*zed::paths::legacy::KEYMAP).is_ok()
+            && fs::metadata(&*zed::paths::KEYMAP).is_err()
+        {
+            fs::copy(&*zed::paths::legacy::KEYMAP, &*zed::paths::KEYMAP).log_err();
+        }
+    });
+}
+
 fn init_logger() {
     if stdout_is_a_pty() {
         env_logger::init();

crates/zed/src/paths.rs 🔗

@@ -1,22 +1,24 @@
-use std::{env, path::PathBuf};
+use std::path::PathBuf;
 
-use lazy_static::lazy_static;
-
-lazy_static! {
+lazy_static::lazy_static! {
     static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
-    static ref CACHE_DIR: PathBuf = dirs::cache_dir()
-        .expect("failed to determine cache directory")
-        .join("Zed");
-    pub static ref CONFIG_DIR: PathBuf = env::var_os("XDG_CONFIG_HOME")
-        .map(|home| home.into())
-        .unwrap_or_else(|| HOME.join(".config"))
-        .join("zed");
+    pub static ref CONFIG_DIR: PathBuf = HOME.join(".config").join("zed");
     pub static ref LOGS_DIR: PathBuf = HOME.join("Library/Logs/Zed");
-    pub static ref LANGUAGES_DIR: PathBuf = CACHE_DIR.join("languages");
-    pub static ref DB_DIR: PathBuf = CACHE_DIR.join("db");
+    pub static ref LANGUAGES_DIR: PathBuf = HOME.join("Library/Application Support/Zed/languages");
+    pub static ref DB_DIR: PathBuf = HOME.join("Library/Application Support/Zed/db");
     pub static ref DB: PathBuf = DB_DIR.join("zed.db");
     pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
     pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
     pub static ref LOG: PathBuf = LOGS_DIR.join("Zed.log");
     pub static ref OLD_LOG: PathBuf = LOGS_DIR.join("Zed.log.old");
 }
+
+pub mod legacy {
+    use std::path::PathBuf;
+
+    lazy_static::lazy_static! {
+        static ref CONFIG_DIR: PathBuf = super::HOME.join(".zed");
+        pub static ref SETTINGS: PathBuf = CONFIG_DIR.join("settings.json");
+        pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
+    }
+}

crates/zed/src/zed.rs 🔗

@@ -223,11 +223,11 @@ pub fn initialize_workspace(
                 },
                 "schemas": [
                     {
-                        "fileMatch": [".zed/settings.json"],
+                        "fileMatch": [schema_file_match(&*paths::SETTINGS)],
                         "schema": settings_file_json_schema(theme_names, language_names),
                     },
                     {
-                        "fileMatch": [".zed/keymap.json"],
+                        "fileMatch": [schema_file_match(&*paths::KEYMAP)],
                         "schema": keymap_file_json_schema(&action_names),
                     }
                 ]
@@ -385,7 +385,6 @@ fn open_config_file(
     cx.spawn(|workspace, mut cx| async move {
         let fs = &app_state.fs;
         if !fs.is_file(path).await {
-            fs.create_dir(&paths::CONFIG_DIR).await?;
             fs.create_file(path, Default::default()).await?;
             fs.save(path, &default_content(), Default::default())
                 .await?;
@@ -481,6 +480,11 @@ fn open_bundled_config_file(
     });
 }
 
+fn schema_file_match(path: &Path) -> &Path {
+    path.strip_prefix(path.parent().unwrap().parent().unwrap())
+        .unwrap()
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;