suggested extensions (#9526)

Follow-up from #9138

Release Notes:

- Adds suggested extensions for some filetypes
([#7096](https://github.com/zed-industries/zed/issues/7096)).

---------

Co-authored-by: Felix Zeller <felixazeller@gmail.com>
This commit is contained in:
Conrad Irwin 2024-03-19 10:06:01 -06:00 committed by GitHub
parent 7573f35e8e
commit d6b7f14b51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 318 additions and 37 deletions

View file

@ -17,10 +17,14 @@ test-support = []
[dependencies]
anyhow.workspace = true
client.workspace = true
db.workspace = true
editor.workspace = true
extension.workspace = true
fuzzy.workspace = true
gpui.workspace = true
language.workspace = true
project.workspace = true
serde.workspace = true
settings.workspace = true
smallvec.workspace = true
theme.workspace = true

View file

@ -0,0 +1,115 @@
use std::{
collections::HashMap,
sync::{Arc, OnceLock},
};
use db::kvp::KEY_VALUE_STORE;
use editor::Editor;
use extension::ExtensionStore;
use gpui::{Entity, Model, VisualContext};
use language::Buffer;
use ui::ViewContext;
use workspace::{notifications::simple_message_notification, Workspace};
pub fn suggested_extension(file_extension_or_name: &str) -> Option<Arc<str>> {
static SUGGESTED: OnceLock<HashMap<&str, Arc<str>>> = OnceLock::new();
SUGGESTED
.get_or_init(|| {
[
("beancount", "beancount"),
("dockerfile", "Dockerfile"),
("elisp", "el"),
("fish", "fish"),
("git-firefly", ".gitconfig"),
("git-firefly", ".gitignore"),
("git-firefly", "COMMIT_EDITMSG"),
("git-firefly", "EDIT_DESCRIPTION"),
("git-firefly", "git-rebase-todo"),
("git-firefly", "MERGE_MSG"),
("git-firefly", "NOTES_EDITMSG"),
("git-firefly", "TAG_EDITMSG"),
("graphql", "gql"),
("graphql", "graphql"),
("java", "java"),
("kotlin", "kt"),
("latex", "tex"),
("make", "Makefile"),
("nix", "nix"),
("r", "r"),
("r", "R"),
("sql", "sql"),
("swift", "swift"),
("templ", "templ"),
("wgsl", "wgsl"),
]
.into_iter()
.map(|(name, file)| (file, name.into()))
.collect::<HashMap<&str, Arc<str>>>()
})
.get(file_extension_or_name)
.map(|str| str.clone())
}
fn language_extension_key(extension_id: &str) -> String {
format!("{}_extension_suggest", extension_id)
}
pub(crate) fn suggest(buffer: Model<Buffer>, cx: &mut ViewContext<Workspace>) {
let Some(file_name_or_extension) = buffer.read(cx).file().and_then(|file| {
Some(match file.path().extension() {
Some(extension) => extension.to_str()?.to_string(),
None => file.path().to_str()?.to_string(),
})
}) else {
return;
};
let Some(extension_id) = suggested_extension(&file_name_or_extension) else {
return;
};
let key = language_extension_key(&extension_id);
let value = KEY_VALUE_STORE.read_kvp(&key);
if value.is_err() || value.unwrap().is_some() {
return;
}
cx.on_next_frame(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;
}
workspace.show_notification(buffer.entity_id().as_u64() as usize, cx, |cx| {
cx.new_view(move |_cx| {
simple_message_notification::MessageNotification::new(format!(
"Do you want to install the recommended '{}' extension?",
file_name_or_extension
))
.with_click_message("Yes")
.on_click({
let extension_id = extension_id.clone();
move |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")
.on_secondary_click(move |cx| {
let key = language_extension_key(&extension_id);
db::write_and_log(cx, move || {
KEY_VALUE_STORE.write_kvp(key, "dismissed".to_string())
});
})
})
});
})
}

View file

@ -1,4 +1,5 @@
mod components;
mod extension_suggest;
use crate::components::ExtensionCard;
use client::telemetry::Telemetry;
@ -25,7 +26,7 @@ use workspace::{
actions!(zed, [Extensions, InstallDevExtension]);
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(move |workspace: &mut Workspace, _cx| {
cx.observe_new_views(move |workspace: &mut Workspace, cx| {
workspace
.register_action(move |workspace, _: &Extensions, cx| {
let extensions_page = ExtensionsPage::new(workspace, cx);
@ -53,6 +54,14 @@ pub fn init(cx: &mut AppContext) {
})
.detach();
});
cx.subscribe(workspace.project(), |_, _, event, cx| match event {
project::Event::LanguageNotFound(buffer) => {
extension_suggest::suggest(buffer.clone(), cx);
}
_ => {}
})
.detach();
})
.detach();
}