Move assets to their own crate, load keymaps in vim tests

Max Brunsfeld and Keith Simmons created

Also, move assets folder to the top-level.

Co-authored-by: Keith Simmons <keith@zed.dev>

Change summary

Cargo.lock                                            | 12 ++
assets/fonts/zed-mono/zed-mono-extended.ttf           |  0 
assets/fonts/zed-mono/zed-mono-extendedbold.ttf       |  0 
assets/fonts/zed-mono/zed-mono-extendedbolditalic.ttf |  0 
assets/fonts/zed-mono/zed-mono-extendeditalic.ttf     |  0 
assets/fonts/zed-sans/zed-sans-extended.ttf           |  0 
assets/fonts/zed-sans/zed-sans-extendedbold.ttf       |  0 
assets/fonts/zed-sans/zed-sans-extendedbolditalic.ttf |  0 
assets/fonts/zed-sans/zed-sans-extendeditalic.ttf     |  0 
assets/icons/broadcast-24.svg                         |  0 
assets/icons/comment-16.svg                           |  0 
assets/icons/diagnostic-error-10.svg                  |  0 
assets/icons/diagnostic-summary-error.svg             |  0 
assets/icons/diagnostic-summary-warning.svg           |  0 
assets/icons/diagnostic-warning-10.svg                |  0 
assets/icons/disclosure-closed.svg                    |  0 
assets/icons/disclosure-open.svg                      |  0 
assets/icons/file-16.svg                              |  0 
assets/icons/folder-tree-16.svg                       |  0 
assets/icons/magnifier.svg                            |  0 
assets/icons/offline-14.svg                           |  0 
assets/icons/signed-out-12.svg                        |  0 
assets/icons/user-16.svg                              |  0 
assets/icons/x.svg                                    |  0 
assets/icons/zap.svg                                  |  0 
assets/keymaps/default.json                           | 56 ------------
assets/keymaps/vim.json                               | 58 +++++++++++++
assets/themes/dark.json                               |  0 
assets/themes/light.json                              |  0 
crates/assets/Cargo.toml                              | 14 +++
crates/assets/src/assets.rs                           |  2 
crates/settings/Cargo.toml                            |  1 
crates/settings/src/keymap_file.rs                    | 11 ++
crates/vim/Cargo.toml                                 |  1 
crates/vim/src/vim_test_context.rs                    |  3 
crates/workspace/Cargo.toml                           |  2 
crates/zed/Cargo.toml                                 |  1 
crates/zed/src/main.rs                                |  2 
crates/zed/src/test.rs                                |  3 
crates/zed/src/zed.rs                                 | 10 -
styles/src/buildThemes.ts                             |  2 
41 files changed, 109 insertions(+), 69 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -168,6 +168,15 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bbf56136a5198c7b01a49e3afcbef6cf84597273d298f54432926024107b0109"
 
+[[package]]
+name = "assets"
+version = "0.1.0"
+dependencies = [
+ "anyhow",
+ "gpui",
+ "rust-embed",
+]
+
 [[package]]
 name = "async-attributes"
 version = "1.1.2"
@@ -4425,6 +4434,7 @@ name = "settings"
 version = "0.1.0"
 dependencies = [
  "anyhow",
+ "assets",
  "collections",
  "gpui",
  "schemars",
@@ -5743,6 +5753,7 @@ checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
 name = "vim"
 version = "0.1.0"
 dependencies = [
+ "assets",
  "collections",
  "editor",
  "gpui",
@@ -6018,6 +6029,7 @@ name = "zed"
 version = "0.25.0"
 dependencies = [
  "anyhow",
+ "assets",
  "async-compression",
  "async-recursion",
  "async-trait",

crates/zed/assets/keymaps/default.json → assets/keymaps/default.json 🔗

@@ -255,61 +255,5 @@
     "ProjectPanel": {
         "left": "project_panel::CollapseSelectedEntry",
         "right": "project_panel::ExpandSelectedEntry"
-    },
-    "Editor && vim_mode == insert": {
-        "escape": "vim::NormalBefore",
-        "ctrl-c": "vim::NormalBefore"
-    },
-    "Editor && vim_mode == normal && vim_submode == g": {
-        "g": "vim::MoveToStart",
-        "escape": [
-            "vim::SwitchMode",
-            {
-                "Normal": "None"
-            }
-        ]
-    },
-    "Editor && vim_mode == normal": {
-        "i": [
-            "vim::SwitchMode",
-            "Insert"
-        ],
-        "g": [
-            "vim::SwitchMode",
-            {
-                "Normal": "GPrefix"
-            }
-        ],
-        "h": "vim::MoveLeft",
-        "j": "vim::MoveDown",
-        "k": "vim::MoveUp",
-        "l": "vim::MoveRight",
-        "0": "vim::MoveToStartOfLine",
-        "shift-$": "vim::MoveToEndOfLine",
-        "shift-G": "vim::MoveToEnd",
-        "w": [
-            "vim::MoveToNextWordStart",
-            false
-        ],
-        "shift-W": [
-            "vim::MoveToNextWordStart",
-            true
-        ],
-        "e": [
-            "vim::MoveToNextWordEnd",
-            false
-        ],
-        "shift-E": [
-            "vim::MoveToNextWordEnd",
-            true
-        ],
-        "b": [
-            "vim::MoveToPreviousWordStart",
-            false
-        ],
-        "shift-B": [
-            "vim::MoveToPreviousWordStart",
-            true
-        ]
     }
 }

assets/keymaps/vim.json 🔗

@@ -0,0 +1,58 @@
+{
+    "Editor && vim_mode == insert": {
+        "escape": "vim::NormalBefore",
+        "ctrl-c": "vim::NormalBefore"
+    },
+    "Editor && vim_mode == normal && vim_submode == g": {
+        "g": "vim::MoveToStart",
+        "escape": [
+            "vim::SwitchMode",
+            {
+                "Normal": "None"
+            }
+        ]
+    },
+    "Editor && vim_mode == normal": {
+        "i": [
+            "vim::SwitchMode",
+            "Insert"
+        ],
+        "g": [
+            "vim::SwitchMode",
+            {
+                "Normal": "GPrefix"
+            }
+        ],
+        "h": "vim::MoveLeft",
+        "j": "vim::MoveDown",
+        "k": "vim::MoveUp",
+        "l": "vim::MoveRight",
+        "0": "vim::MoveToStartOfLine",
+        "shift-$": "vim::MoveToEndOfLine",
+        "shift-G": "vim::MoveToEnd",
+        "w": [
+            "vim::MoveToNextWordStart",
+            false
+        ],
+        "shift-W": [
+            "vim::MoveToNextWordStart",
+            true
+        ],
+        "e": [
+            "vim::MoveToNextWordEnd",
+            false
+        ],
+        "shift-E": [
+            "vim::MoveToNextWordEnd",
+            true
+        ],
+        "b": [
+            "vim::MoveToPreviousWordStart",
+            false
+        ],
+        "shift-B": [
+            "vim::MoveToPreviousWordStart",
+            true
+        ]
+    }
+}

crates/assets/Cargo.toml 🔗

@@ -0,0 +1,14 @@
+[package]
+name = "assets"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+path = "src/assets.rs"
+doctest = false
+
+[dependencies]
+gpui = { path = "../gpui" }
+anyhow = "1.0.38"
+rust-embed = { version = "6.3", features = ["include-exclude"] }
+

crates/zed/src/assets.rs → crates/assets/src/assets.rs 🔗

@@ -3,7 +3,7 @@ use gpui::AssetSource;
 use rust_embed::RustEmbed;
 
 #[derive(RustEmbed)]
-#[folder = "assets"]
+#[folder = "../../assets"]
 #[exclude = "*.DS_Store"]
 pub struct Assets;
 

crates/settings/Cargo.toml 🔗

@@ -11,6 +11,7 @@ doctest = false
 test-support = []
 
 [dependencies]
+assets = { path = "../assets" }
 collections = { path = "../collections" }
 gpui = { path = "../gpui" }
 theme = { path = "../theme" }

crates/settings/src/keymap_file.rs 🔗

@@ -1,4 +1,5 @@
 use anyhow::{Context, Result};
+use assets::Assets;
 use collections::BTreeMap;
 use gpui::{keymap::Binding, MutableAppContext};
 use serde::Deserialize;
@@ -9,6 +10,16 @@ struct ActionWithData<'a>(#[serde(borrow)] &'a str, #[serde(borrow)] &'a RawValu
 type ActionSetsByContext<'a> = BTreeMap<&'a str, ActionsByKeystroke<'a>>;
 type ActionsByKeystroke<'a> = BTreeMap<&'a str, &'a RawValue>;
 
+pub fn load_built_in_keymaps(cx: &mut MutableAppContext) {
+    for path in ["keymaps/default.json", "keymaps/vim.json"] {
+        load_keymap(
+            cx,
+            std::str::from_utf8(Assets::get(path).unwrap().data.as_ref()).unwrap(),
+        )
+        .unwrap();
+    }
+}
+
 pub fn load_keymap(cx: &mut MutableAppContext, content: &str) -> Result<()> {
     let actions: ActionSetsByContext = serde_json::from_str(content)?;
     for (context, actions) in actions {

crates/vim/Cargo.toml 🔗

@@ -8,6 +8,7 @@ path = "src/vim.rs"
 doctest = false
 
 [dependencies]
+assets = { path = "../assets" }
 collections = { path = "../collections" }
 editor = { path = "../editor" }
 gpui = { path = "../gpui" }

crates/vim/src/vim_test_context.rs 🔗

@@ -23,7 +23,10 @@ impl<'a> VimTestContext<'a> {
         cx.update(|cx| {
             editor::init(cx);
             crate::init(cx);
+
+            settings::keymap_file::load_built_in_keymaps(cx);
         });
+
         let params = cx.update(WorkspaceParams::test);
 
         cx.update(|cx| {

crates/workspace/Cargo.toml 🔗

@@ -8,7 +8,7 @@ path = "src/workspace.rs"
 doctest = false
 
 [features]
-test-support = ["client/test-support", "project/test-support"]
+test-support = ["client/test-support", "project/test-support", "settings/test-support"]
 
 [dependencies]
 client = { path = "../client" }

crates/zed/Cargo.toml 🔗

@@ -29,6 +29,7 @@ test-support = [
 ]
 
 [dependencies]
+assets = { path = "../assets" }
 breadcrumbs = { path = "../breadcrumbs" }
 chat_panel = { path = "../chat_panel" }
 collections = { path = "../collections" }

crates/zed/src/main.rs 🔗

@@ -15,9 +15,9 @@ use std::{env, fs, path::PathBuf, sync::Arc};
 use theme::{ThemeRegistry, DEFAULT_THEME_NAME};
 use util::ResultExt;
 use workspace::{self, AppState, OpenNew, OpenPaths};
+use assets::Assets;
 use zed::{
     self,
-    assets::Assets,
     build_window_options, build_workspace,
     fs::RealFs,
     languages, menus,

crates/zed/src/test.rs 🔗

@@ -1,4 +1,5 @@
-use crate::{assets::Assets, build_window_options, build_workspace, AppState};
+use crate::{build_window_options, build_workspace, AppState};
+use assets::Assets;
 use client::{test::FakeHttpClient, ChannelList, Client, UserStore};
 use gpui::MutableAppContext;
 use language::LanguageRegistry;

crates/zed/src/zed.rs 🔗

@@ -1,11 +1,9 @@
-pub mod assets;
 pub mod languages;
 pub mod menus;
 pub mod settings_file;
 #[cfg(any(test, feature = "test-support"))]
 pub mod test;
 
-use assets::Assets;
 use breadcrumbs::Breadcrumbs;
 use chat_panel::ChatPanel;
 pub use client;
@@ -104,11 +102,7 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
 
     workspace::lsp_status::init(cx);
 
-    settings::keymap_file::load_keymap(
-        cx,
-        std::str::from_utf8(Assets::get("keymaps/default.json").unwrap().data.as_ref()).unwrap(),
-    )
-    .unwrap();
+    settings::keymap_file::load_built_in_keymaps(cx);
 }
 
 pub fn build_workspace(
@@ -209,7 +203,7 @@ fn quit(_: &Quit, cx: &mut gpui::MutableAppContext) {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use crate::assets::Assets;
+    use assets::Assets;
     use editor::{DisplayPoint, Editor};
     use gpui::{AssetSource, MutableAppContext, TestAppContext, ViewHandle};
     use project::{Fs, ProjectPath};

styles/src/buildThemes.ts 🔗

@@ -10,7 +10,7 @@ for (let theme of themes) {
   let styleTree = snakeCase(app(theme));
   let styleTreeJSON = JSON.stringify(styleTree, null, 2);
   let outPath = path.resolve(
-    `${__dirname}/../../crates/zed/assets/themes/${theme.name}.json`
+    `${__dirname}/../assets/themes/${theme.name}.json`
   );
   fs.writeFileSync(outPath, styleTreeJSON);
   console.log(`- ${outPath} created`);