There's still a bit more work to do on this, but this PR is compiling (with warnings) after eliminating the key types. When the tasks below are complete, this will be the new narrative for GPUI: - `Entity<T>` - This replaces `View<T>`/`Model<T>`. It represents a unit of state, and if `T` implements `Render`, then `Entity<T>` implements `Element`. - `&mut App` This replaces `AppContext` and represents the app. - `&mut Context<T>` This replaces `ModelContext` and derefs to `App`. It is provided by the framework when updating an entity. - `&mut Window` Broken out of `&mut WindowContext` which no longer exists. Every method that once took `&mut WindowContext` now takes `&mut Window, &mut App` and every method that took `&mut ViewContext<T>` now takes `&mut Window, &mut Context<T>` Not pictured here are the two other failed attempts. It's been quite a month! Tasks: - [x] Remove `View`, `ViewContext`, `WindowContext` and thread through `Window` - [x] [@cole-miller @mikayla-maki] Redraw window when entities change - [x] [@cole-miller @mikayla-maki] Get examples and Zed running - [x] [@cole-miller @mikayla-maki] Fix Zed rendering - [x] [@mikayla-maki] Fix todo! macros and comments - [x] Fix a bug where the editor would not be redrawn because of view caching - [x] remove publicness window.notify() and replace with `AppContext::notify` - [x] remove `observe_new_window_models`, replace with `observe_new_models` with an optional window - [x] Fix a bug where the project panel would not be redrawn because of the wrong refresh() call being used - [x] Fix the tests - [x] Fix warnings by eliminating `Window` params or using `_` - [x] Fix conflicts - [x] Simplify generic code where possible - [x] Rename types - [ ] Update docs ### issues post merge - [x] Issues switching between normal and insert mode - [x] Assistant re-rendering failure - [x] Vim test failures - [x] Mac build issue Release Notes: - N/A --------- Co-authored-by: Antonio Scandurra <me@as-cii.com> Co-authored-by: Cole Miller <cole@zed.dev> Co-authored-by: Mikayla <mikayla@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: max <max@zed.dev> Co-authored-by: Michael Sloan <michael@zed.dev> Co-authored-by: Mikayla Maki <mikaylamaki@Mikaylas-MacBook-Pro.local> Co-authored-by: Mikayla <mikayla.c.maki@gmail.com> Co-authored-by: joão <joao@zed.dev>
241 lines
7.5 KiB
Rust
241 lines
7.5 KiB
Rust
use std::collections::HashMap;
|
|
use std::path::Path;
|
|
use std::sync::{Arc, OnceLock};
|
|
|
|
use db::kvp::KEY_VALUE_STORE;
|
|
use editor::Editor;
|
|
use extension_host::ExtensionStore;
|
|
use gpui::{AppContext as _, Context, Entity, SharedString, Window};
|
|
use language::Buffer;
|
|
use workspace::notifications::simple_message_notification::MessageNotification;
|
|
use workspace::{notifications::NotificationId, Workspace};
|
|
|
|
const SUGGESTIONS_BY_EXTENSION_ID: &[(&str, &[&str])] = &[
|
|
("astro", &["astro"]),
|
|
("beancount", &["beancount"]),
|
|
("clojure", &["bb", "clj", "cljc", "cljs", "edn"]),
|
|
("neocmake", &["CMakeLists.txt", "cmake"]),
|
|
("csharp", &["cs"]),
|
|
("cython", &["pyx", "pxd", "pxi"]),
|
|
("dart", &["dart"]),
|
|
("dockerfile", &["Dockerfile"]),
|
|
("elisp", &["el"]),
|
|
("elixir", &["ex", "exs", "heex"]),
|
|
("elm", &["elm"]),
|
|
("erlang", &["erl", "hrl"]),
|
|
("fish", &["fish"]),
|
|
(
|
|
"git-firefly",
|
|
&[
|
|
".gitconfig",
|
|
".gitignore",
|
|
"COMMIT_EDITMSG",
|
|
"EDIT_DESCRIPTION",
|
|
"MERGE_MSG",
|
|
"NOTES_EDITMSG",
|
|
"TAG_EDITMSG",
|
|
"git-rebase-todo",
|
|
],
|
|
),
|
|
("gleam", &["gleam"]),
|
|
("glsl", &["vert", "frag"]),
|
|
("graphql", &["gql", "graphql"]),
|
|
("haskell", &["hs"]),
|
|
("html", &["htm", "html", "shtml"]),
|
|
("java", &["java"]),
|
|
("kotlin", &["kt"]),
|
|
("latex", &["tex"]),
|
|
("log", &["log"]),
|
|
("lua", &["lua"]),
|
|
("make", &["Makefile"]),
|
|
("nix", &["nix"]),
|
|
("nu", &["nu"]),
|
|
("ocaml", &["ml", "mli"]),
|
|
("php", &["php"]),
|
|
("prisma", &["prisma"]),
|
|
("proto", &["proto"]),
|
|
("purescript", &["purs"]),
|
|
("r", &["r", "R"]),
|
|
("racket", &["rkt"]),
|
|
("rescript", &["res", "resi"]),
|
|
("ruby", &["rb", "erb"]),
|
|
("scheme", &["scm"]),
|
|
("scss", &["scss"]),
|
|
("sql", &["sql"]),
|
|
("svelte", &["svelte"]),
|
|
("swift", &["swift"]),
|
|
("templ", &["templ"]),
|
|
("terraform", &["tf", "tfvars", "hcl"]),
|
|
("toml", &["Cargo.lock", "toml"]),
|
|
("vue", &["vue"]),
|
|
("wgsl", &["wgsl"]),
|
|
("wit", &["wit"]),
|
|
("zig", &["zig"]),
|
|
];
|
|
|
|
fn suggested_extensions() -> &'static HashMap<&'static str, Arc<str>> {
|
|
static SUGGESTIONS_BY_PATH_SUFFIX: OnceLock<HashMap<&str, Arc<str>>> = OnceLock::new();
|
|
SUGGESTIONS_BY_PATH_SUFFIX.get_or_init(|| {
|
|
SUGGESTIONS_BY_EXTENSION_ID
|
|
.iter()
|
|
.flat_map(|(name, path_suffixes)| {
|
|
let name = Arc::<str>::from(*name);
|
|
path_suffixes
|
|
.iter()
|
|
.map(move |suffix| (*suffix, name.clone()))
|
|
})
|
|
.collect()
|
|
})
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
struct SuggestedExtension {
|
|
pub extension_id: Arc<str>,
|
|
pub file_name_or_extension: Arc<str>,
|
|
}
|
|
|
|
/// Returns the suggested extension for the given [`Path`].
|
|
fn suggested_extension(path: impl AsRef<Path>) -> Option<SuggestedExtension> {
|
|
let path = path.as_ref();
|
|
|
|
let file_extension: Option<Arc<str>> = path
|
|
.extension()
|
|
.and_then(|extension| Some(extension.to_str()?.into()));
|
|
let file_name: Option<Arc<str>> = path
|
|
.file_name()
|
|
.and_then(|file_name| Some(file_name.to_str()?.into()));
|
|
|
|
let (file_name_or_extension, extension_id) = None
|
|
// We suggest against file names first, as these suggestions will be more
|
|
// specific than ones based on the file extension.
|
|
.or_else(|| {
|
|
file_name.clone().zip(
|
|
file_name
|
|
.as_deref()
|
|
.and_then(|file_name| suggested_extensions().get(file_name)),
|
|
)
|
|
})
|
|
.or_else(|| {
|
|
file_extension.clone().zip(
|
|
file_extension
|
|
.as_deref()
|
|
.and_then(|file_extension| suggested_extensions().get(file_extension)),
|
|
)
|
|
})?;
|
|
|
|
Some(SuggestedExtension {
|
|
extension_id: extension_id.clone(),
|
|
file_name_or_extension,
|
|
})
|
|
}
|
|
|
|
fn language_extension_key(extension_id: &str) -> String {
|
|
format!("{}_extension_suggest", extension_id)
|
|
}
|
|
|
|
pub(crate) fn suggest(buffer: Entity<Buffer>, window: &mut Window, cx: &mut Context<Workspace>) {
|
|
let Some(file) = buffer.read(cx).file().cloned() else {
|
|
return;
|
|
};
|
|
|
|
let Some(SuggestedExtension {
|
|
extension_id,
|
|
file_name_or_extension,
|
|
}) = suggested_extension(file.path())
|
|
else {
|
|
return;
|
|
};
|
|
|
|
let key = language_extension_key(&extension_id);
|
|
let Ok(None) = KEY_VALUE_STORE.read_kvp(&key) else {
|
|
return;
|
|
};
|
|
|
|
cx.on_next_frame(window, move |workspace, _, cx| {
|
|
let Some(editor) = workspace.active_item_as::<Editor>(cx) else {
|
|
return;
|
|
};
|
|
|
|
if editor.read(cx).buffer().read(cx).as_singleton().as_ref() != Some(&buffer) {
|
|
return;
|
|
}
|
|
|
|
struct ExtensionSuggestionNotification;
|
|
|
|
let notification_id = NotificationId::composite::<ExtensionSuggestionNotification>(
|
|
SharedString::from(extension_id.clone()),
|
|
);
|
|
|
|
workspace.show_notification(notification_id, cx, |cx| {
|
|
cx.new(move |_cx| {
|
|
MessageNotification::new(format!(
|
|
"Do you want to install the recommended '{}' extension for '{}' files?",
|
|
extension_id, file_name_or_extension
|
|
))
|
|
.with_click_message("Yes, install extension")
|
|
.on_click({
|
|
let extension_id = extension_id.clone();
|
|
move |_window, cx| {
|
|
let extension_id = extension_id.clone();
|
|
let extension_store = ExtensionStore::global(cx);
|
|
extension_store.update(cx, move |store, cx| {
|
|
store.install_latest_extension(extension_id, cx);
|
|
});
|
|
}
|
|
})
|
|
.with_secondary_click_message("No, don't install it")
|
|
.on_secondary_click(move |_window, cx| {
|
|
let key = language_extension_key(&extension_id);
|
|
db::write_and_log(cx, move || {
|
|
KEY_VALUE_STORE.write_kvp(key, "dismissed".to_string())
|
|
});
|
|
})
|
|
})
|
|
});
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
pub fn test_suggested_extension() {
|
|
assert_eq!(
|
|
suggested_extension("Cargo.toml"),
|
|
Some(SuggestedExtension {
|
|
extension_id: "toml".into(),
|
|
file_name_or_extension: "toml".into()
|
|
})
|
|
);
|
|
assert_eq!(
|
|
suggested_extension("Cargo.lock"),
|
|
Some(SuggestedExtension {
|
|
extension_id: "toml".into(),
|
|
file_name_or_extension: "Cargo.lock".into()
|
|
})
|
|
);
|
|
assert_eq!(
|
|
suggested_extension("Dockerfile"),
|
|
Some(SuggestedExtension {
|
|
extension_id: "dockerfile".into(),
|
|
file_name_or_extension: "Dockerfile".into()
|
|
})
|
|
);
|
|
assert_eq!(
|
|
suggested_extension("a/b/c/d/.gitignore"),
|
|
Some(SuggestedExtension {
|
|
extension_id: "git-firefly".into(),
|
|
file_name_or_extension: ".gitignore".into()
|
|
})
|
|
);
|
|
assert_eq!(
|
|
suggested_extension("a/b/c/d/test.gleam"),
|
|
Some(SuggestedExtension {
|
|
extension_id: "gleam".into(),
|
|
file_name_or_extension: "gleam".into()
|
|
})
|
|
);
|
|
}
|
|
}
|