mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-26 20:22:30 +00:00
Reload extensions more robustly when manually modifying installed extensions directory (#7749)
Release Notes: - N/A --------- Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
parent
c3176392c6
commit
c357e37dde
4 changed files with 335 additions and 260 deletions
|
@ -2,18 +2,19 @@ use anyhow::{anyhow, bail, Context as _, Result};
|
||||||
use async_compression::futures::bufread::GzipDecoder;
|
use async_compression::futures::bufread::GzipDecoder;
|
||||||
use async_tar::Archive;
|
use async_tar::Archive;
|
||||||
use client::ClientSettings;
|
use client::ClientSettings;
|
||||||
use collections::{HashMap, HashSet};
|
use collections::{BTreeMap, HashSet};
|
||||||
use fs::{Fs, RemoveOptions};
|
use fs::{Fs, RemoveOptions};
|
||||||
use futures::channel::mpsc::unbounded;
|
use futures::channel::mpsc::unbounded;
|
||||||
use futures::StreamExt as _;
|
use futures::StreamExt as _;
|
||||||
use futures::{io::BufReader, AsyncReadExt as _};
|
use futures::{io::BufReader, AsyncReadExt as _};
|
||||||
use gpui::{actions, AppContext, Context, Global, Model, ModelContext, SharedString, Task};
|
use gpui::{actions, AppContext, Context, Global, Model, ModelContext, Task};
|
||||||
use language::{
|
use language::{
|
||||||
LanguageConfig, LanguageMatcher, LanguageQueries, LanguageRegistry, QUERY_FILENAME_PREFIXES,
|
LanguageConfig, LanguageMatcher, LanguageQueries, LanguageRegistry, QUERY_FILENAME_PREFIXES,
|
||||||
};
|
};
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use settings::Settings as _;
|
use settings::Settings as _;
|
||||||
|
use std::cmp::Ordering;
|
||||||
use std::{
|
use std::{
|
||||||
ffi::OsStr,
|
ffi::OsStr,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
|
@ -22,6 +23,7 @@ use std::{
|
||||||
};
|
};
|
||||||
use theme::{ThemeRegistry, ThemeSettings};
|
use theme::{ThemeRegistry, ThemeSettings};
|
||||||
use util::http::AsyncBody;
|
use util::http::AsyncBody;
|
||||||
|
use util::TryFutureExt;
|
||||||
use util::{http::HttpClient, paths::EXTENSIONS_DIR, ResultExt};
|
use util::{http::HttpClient, paths::EXTENSIONS_DIR, ResultExt};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -61,6 +63,9 @@ pub struct ExtensionStore {
|
||||||
manifest_path: PathBuf,
|
manifest_path: PathBuf,
|
||||||
language_registry: Arc<LanguageRegistry>,
|
language_registry: Arc<LanguageRegistry>,
|
||||||
theme_registry: Arc<ThemeRegistry>,
|
theme_registry: Arc<ThemeRegistry>,
|
||||||
|
extension_changes: ExtensionChanges,
|
||||||
|
reload_task: Option<Task<Option<()>>>,
|
||||||
|
needs_reload: bool,
|
||||||
_watch_extensions_dir: [Task<()>; 2],
|
_watch_extensions_dir: [Task<()>; 2],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,12 +73,12 @@ struct GlobalExtensionStore(Model<ExtensionStore>);
|
||||||
|
|
||||||
impl Global for GlobalExtensionStore {}
|
impl Global for GlobalExtensionStore {}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Default)]
|
#[derive(Debug, Deserialize, Serialize, Default)]
|
||||||
pub struct Manifest {
|
pub struct Manifest {
|
||||||
pub extensions: HashMap<Arc<str>, Arc<str>>,
|
pub extensions: BTreeMap<Arc<str>, Arc<str>>,
|
||||||
pub grammars: HashMap<Arc<str>, GrammarManifestEntry>,
|
pub grammars: BTreeMap<Arc<str>, GrammarManifestEntry>,
|
||||||
pub languages: HashMap<Arc<str>, LanguageManifestEntry>,
|
pub languages: BTreeMap<Arc<str>, LanguageManifestEntry>,
|
||||||
pub themes: HashMap<String, ThemeManifestEntry>,
|
pub themes: BTreeMap<Arc<str>, ThemeManifestEntry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Deserialize, Serialize)]
|
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Deserialize, Serialize)]
|
||||||
|
@ -96,6 +101,13 @@ pub struct ThemeManifestEntry {
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ExtensionChanges {
|
||||||
|
languages: HashSet<Arc<str>>,
|
||||||
|
grammars: HashSet<Arc<str>>,
|
||||||
|
themes: HashSet<Arc<str>>,
|
||||||
|
}
|
||||||
|
|
||||||
actions!(zed, [ReloadExtensions]);
|
actions!(zed, [ReloadExtensions]);
|
||||||
|
|
||||||
pub fn init(
|
pub fn init(
|
||||||
|
@ -118,9 +130,7 @@ pub fn init(
|
||||||
|
|
||||||
cx.on_action(|_: &ReloadExtensions, cx| {
|
cx.on_action(|_: &ReloadExtensions, cx| {
|
||||||
let store = cx.global::<GlobalExtensionStore>().0.clone();
|
let store = cx.global::<GlobalExtensionStore>().0.clone();
|
||||||
store
|
store.update(cx, |store, cx| store.reload(cx))
|
||||||
.update(cx, |store, cx| store.reload(cx))
|
|
||||||
.detach_and_log_err(cx);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.set_global(GlobalExtensionStore(store));
|
cx.set_global(GlobalExtensionStore(store));
|
||||||
|
@ -145,6 +155,9 @@ impl ExtensionStore {
|
||||||
manifest_path: extensions_dir.join("manifest.json"),
|
manifest_path: extensions_dir.join("manifest.json"),
|
||||||
extensions_being_installed: Default::default(),
|
extensions_being_installed: Default::default(),
|
||||||
extensions_being_uninstalled: Default::default(),
|
extensions_being_uninstalled: Default::default(),
|
||||||
|
reload_task: None,
|
||||||
|
needs_reload: false,
|
||||||
|
extension_changes: ExtensionChanges::default(),
|
||||||
fs,
|
fs,
|
||||||
http_client,
|
http_client,
|
||||||
language_registry,
|
language_registry,
|
||||||
|
@ -181,7 +194,7 @@ impl ExtensionStore {
|
||||||
};
|
};
|
||||||
|
|
||||||
if should_reload {
|
if should_reload {
|
||||||
self.reload(cx).detach_and_log_err(cx);
|
self.reload(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +261,7 @@ impl ExtensionStore {
|
||||||
extension_id: Arc<str>,
|
extension_id: Arc<str>,
|
||||||
version: Arc<str>,
|
version: Arc<str>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Task<Result<()>> {
|
) {
|
||||||
log::info!("installing extension {extension_id} {version}");
|
log::info!("installing extension {extension_id} {version}");
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/api/extensions/{extension_id}/{version}/download",
|
"{}/api/extensions/{extension_id}/{version}/download",
|
||||||
|
@ -271,21 +284,16 @@ impl ExtensionStore {
|
||||||
.unpack(extensions_dir.join(extension_id.as_ref()))
|
.unpack(extensions_dir.join(extension_id.as_ref()))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
this.update(&mut cx, |store, cx| {
|
this.update(&mut cx, |this, cx| {
|
||||||
store
|
this.extensions_being_installed
|
||||||
.extensions_being_installed
|
|
||||||
.remove(extension_id.as_ref());
|
.remove(extension_id.as_ref());
|
||||||
store.reload(cx)
|
this.reload(cx)
|
||||||
})?
|
})
|
||||||
.await
|
|
||||||
})
|
})
|
||||||
|
.detach_and_log_err(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uninstall_extension(
|
pub fn uninstall_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
|
||||||
&mut self,
|
|
||||||
extension_id: Arc<str>,
|
|
||||||
cx: &mut ModelContext<Self>,
|
|
||||||
) -> Task<Result<()>> {
|
|
||||||
let extensions_dir = self.extensions_dir();
|
let extensions_dir = self.extensions_dir();
|
||||||
let fs = self.fs.clone();
|
let fs = self.fs.clone();
|
||||||
|
|
||||||
|
@ -306,34 +314,98 @@ impl ExtensionStore {
|
||||||
this.extensions_being_uninstalled
|
this.extensions_being_uninstalled
|
||||||
.remove(extension_id.as_ref());
|
.remove(extension_id.as_ref());
|
||||||
this.reload(cx)
|
this.reload(cx)
|
||||||
})?
|
})
|
||||||
.await
|
|
||||||
})
|
})
|
||||||
|
.detach_and_log_err(cx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates the set of installed extensions.
|
||||||
|
///
|
||||||
|
/// First, this unloads any themes, languages, or grammars that are
|
||||||
|
/// no longer in the manifest, or whose files have changed on disk.
|
||||||
|
/// Then it loads any themes, languages, or grammars that are newly
|
||||||
|
/// added to the manifest, or whose files have changed on disk.
|
||||||
fn manifest_updated(&mut self, manifest: Manifest, cx: &mut ModelContext<Self>) {
|
fn manifest_updated(&mut self, manifest: Manifest, cx: &mut ModelContext<Self>) {
|
||||||
|
fn diff<'a, T, I1, I2>(
|
||||||
|
old_keys: I1,
|
||||||
|
new_keys: I2,
|
||||||
|
modified_keys: &HashSet<Arc<str>>,
|
||||||
|
) -> (Vec<Arc<str>>, Vec<Arc<str>>)
|
||||||
|
where
|
||||||
|
T: PartialEq,
|
||||||
|
I1: Iterator<Item = (&'a Arc<str>, T)>,
|
||||||
|
I2: Iterator<Item = (&'a Arc<str>, T)>,
|
||||||
|
{
|
||||||
|
let mut removed_keys = Vec::default();
|
||||||
|
let mut added_keys = Vec::default();
|
||||||
|
let mut old_keys = old_keys.peekable();
|
||||||
|
let mut new_keys = new_keys.peekable();
|
||||||
|
loop {
|
||||||
|
match (old_keys.peek(), new_keys.peek()) {
|
||||||
|
(None, None) => return (removed_keys, added_keys),
|
||||||
|
(None, Some(_)) => {
|
||||||
|
added_keys.push(new_keys.next().unwrap().0.clone());
|
||||||
|
}
|
||||||
|
(Some(_), None) => {
|
||||||
|
removed_keys.push(old_keys.next().unwrap().0.clone());
|
||||||
|
}
|
||||||
|
(Some((old_key, _)), Some((new_key, _))) => match old_key.cmp(&new_key) {
|
||||||
|
Ordering::Equal => {
|
||||||
|
let (old_key, old_value) = old_keys.next().unwrap();
|
||||||
|
let (new_key, new_value) = new_keys.next().unwrap();
|
||||||
|
if old_value != new_value || modified_keys.contains(old_key) {
|
||||||
|
removed_keys.push(old_key.clone());
|
||||||
|
added_keys.push(new_key.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ordering::Less => {
|
||||||
|
removed_keys.push(old_keys.next().unwrap().0.clone());
|
||||||
|
}
|
||||||
|
Ordering::Greater => {
|
||||||
|
added_keys.push(new_keys.next().unwrap().0.clone());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let old_manifest = self.manifest.read();
|
let old_manifest = self.manifest.read();
|
||||||
let language_names = old_manifest.languages.keys().cloned().collect::<Vec<_>>();
|
let (languages_to_remove, languages_to_add) = diff(
|
||||||
let grammar_names = old_manifest.grammars.keys().cloned().collect::<Vec<_>>();
|
old_manifest.languages.iter(),
|
||||||
let theme_names = old_manifest
|
manifest.languages.iter(),
|
||||||
.themes
|
&self.extension_changes.languages,
|
||||||
.keys()
|
);
|
||||||
.cloned()
|
let (grammars_to_remove, grammars_to_add) = diff(
|
||||||
.map(SharedString::from)
|
old_manifest.grammars.iter(),
|
||||||
.collect::<Vec<_>>();
|
manifest.grammars.iter(),
|
||||||
|
&self.extension_changes.grammars,
|
||||||
|
);
|
||||||
|
let (themes_to_remove, themes_to_add) = diff(
|
||||||
|
old_manifest.themes.iter(),
|
||||||
|
manifest.themes.iter(),
|
||||||
|
&self.extension_changes.themes,
|
||||||
|
);
|
||||||
|
self.extension_changes.clear();
|
||||||
drop(old_manifest);
|
drop(old_manifest);
|
||||||
|
|
||||||
|
let themes_to_remove = &themes_to_remove
|
||||||
|
.into_iter()
|
||||||
|
.map(|theme| theme.into())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
self.theme_registry.remove_user_themes(&themes_to_remove);
|
||||||
self.language_registry
|
self.language_registry
|
||||||
.remove_languages(&language_names, &grammar_names);
|
.remove_languages(&languages_to_remove, &grammars_to_remove);
|
||||||
self.theme_registry.remove_user_themes(&theme_names);
|
|
||||||
|
|
||||||
self.language_registry
|
self.language_registry
|
||||||
.register_wasm_grammars(manifest.grammars.iter().map(|(grammar_name, grammar)| {
|
.register_wasm_grammars(grammars_to_add.iter().map(|grammar_name| {
|
||||||
|
let grammar = manifest.grammars.get(grammar_name).unwrap();
|
||||||
let mut grammar_path = self.extensions_dir.clone();
|
let mut grammar_path = self.extensions_dir.clone();
|
||||||
grammar_path.extend([grammar.extension.as_ref(), grammar.path.as_path()]);
|
grammar_path.extend([grammar.extension.as_ref(), grammar.path.as_path()]);
|
||||||
(grammar_name.clone(), grammar_path)
|
(grammar_name.clone(), grammar_path)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
for (language_name, language) in &manifest.languages {
|
for language_name in &languages_to_add {
|
||||||
|
let language = manifest.languages.get(language_name.as_ref()).unwrap();
|
||||||
let mut language_path = self.extensions_dir.clone();
|
let mut language_path = self.extensions_dir.clone();
|
||||||
language_path.extend([language.extension.as_ref(), language.path.as_path()]);
|
language_path.extend([language.extension.as_ref(), language.path.as_path()]);
|
||||||
self.language_registry.register_language(
|
self.language_registry.register_language(
|
||||||
|
@ -354,10 +426,13 @@ impl ExtensionStore {
|
||||||
let fs = self.fs.clone();
|
let fs = self.fs.clone();
|
||||||
let root_dir = self.extensions_dir.clone();
|
let root_dir = self.extensions_dir.clone();
|
||||||
let theme_registry = self.theme_registry.clone();
|
let theme_registry = self.theme_registry.clone();
|
||||||
let themes = manifest.themes.clone();
|
let themes = themes_to_add
|
||||||
|
.iter()
|
||||||
|
.filter_map(|name| manifest.themes.get(name).cloned())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
cx.background_executor()
|
cx.background_executor()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
for theme in themes.values() {
|
for theme in &themes {
|
||||||
let mut theme_path = root_dir.clone();
|
let mut theme_path = root_dir.clone();
|
||||||
theme_path.extend([theme.extension.as_ref(), theme.path.as_path()]);
|
theme_path.extend([theme.extension.as_ref(), theme.path.as_path()]);
|
||||||
|
|
||||||
|
@ -384,23 +459,22 @@ impl ExtensionStore {
|
||||||
.detach();
|
.detach();
|
||||||
|
|
||||||
*self.manifest.write() = manifest;
|
*self.manifest.write() = manifest;
|
||||||
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn watch_extensions_dir(&self, cx: &mut ModelContext<Self>) -> [Task<()>; 2] {
|
fn watch_extensions_dir(&self, cx: &mut ModelContext<Self>) -> [Task<()>; 2] {
|
||||||
let manifest = self.manifest.clone();
|
let manifest = self.manifest.clone();
|
||||||
let fs = self.fs.clone();
|
let fs = self.fs.clone();
|
||||||
let language_registry = self.language_registry.clone();
|
|
||||||
let theme_registry = self.theme_registry.clone();
|
|
||||||
let extensions_dir = self.extensions_dir.clone();
|
let extensions_dir = self.extensions_dir.clone();
|
||||||
|
|
||||||
let (reload_theme_tx, mut reload_theme_rx) = unbounded();
|
let (changes_tx, mut changes_rx) = unbounded();
|
||||||
|
|
||||||
let events_task = cx.background_executor().spawn(async move {
|
let events_task = cx.background_executor().spawn(async move {
|
||||||
let mut events = fs.watch(&extensions_dir, Duration::from_millis(250)).await;
|
let mut events = fs.watch(&extensions_dir, Duration::from_millis(250)).await;
|
||||||
while let Some(events) = events.next().await {
|
while let Some(events) = events.next().await {
|
||||||
let mut changed_grammars = Vec::default();
|
let mut changed_grammars = HashSet::default();
|
||||||
let mut changed_languages = Vec::default();
|
let mut changed_languages = HashSet::default();
|
||||||
let mut changed_themes = Vec::default();
|
let mut changed_themes = HashSet::default();
|
||||||
|
|
||||||
{
|
{
|
||||||
let manifest = manifest.read();
|
let manifest = manifest.read();
|
||||||
|
@ -410,7 +484,7 @@ impl ExtensionStore {
|
||||||
grammar_path
|
grammar_path
|
||||||
.extend([grammar.extension.as_ref(), grammar.path.as_path()]);
|
.extend([grammar.extension.as_ref(), grammar.path.as_path()]);
|
||||||
if event.path.starts_with(&grammar_path) || event.path == grammar_path {
|
if event.path.starts_with(&grammar_path) || event.path == grammar_path {
|
||||||
changed_grammars.push(grammar_name.clone());
|
changed_grammars.insert(grammar_name.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -420,42 +494,37 @@ impl ExtensionStore {
|
||||||
.extend([language.extension.as_ref(), language.path.as_path()]);
|
.extend([language.extension.as_ref(), language.path.as_path()]);
|
||||||
if event.path.starts_with(&language_path) || event.path == language_path
|
if event.path.starts_with(&language_path) || event.path == language_path
|
||||||
{
|
{
|
||||||
changed_languages.push(language_name.clone());
|
changed_languages.insert(language_name.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (_theme_name, theme) in &manifest.themes {
|
for (theme_name, theme) in &manifest.themes {
|
||||||
let mut theme_path = extensions_dir.clone();
|
let mut theme_path = extensions_dir.clone();
|
||||||
theme_path.extend([theme.extension.as_ref(), theme.path.as_path()]);
|
theme_path.extend([theme.extension.as_ref(), theme.path.as_path()]);
|
||||||
if event.path.starts_with(&theme_path) || event.path == theme_path {
|
if event.path.starts_with(&theme_path) || event.path == theme_path {
|
||||||
changed_themes.push(theme_path.clone());
|
changed_themes.insert(theme_name.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
language_registry.reload_languages(&changed_languages, &changed_grammars);
|
changes_tx
|
||||||
|
.unbounded_send(ExtensionChanges {
|
||||||
for theme_path in &changed_themes {
|
languages: changed_languages,
|
||||||
if fs.is_file(&theme_path).await {
|
grammars: changed_grammars,
|
||||||
theme_registry
|
themes: changed_themes,
|
||||||
.load_user_theme(&theme_path, fs.clone())
|
})
|
||||||
.await
|
.ok();
|
||||||
.context("failed to load user theme")
|
|
||||||
.log_err();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !changed_themes.is_empty() {
|
|
||||||
reload_theme_tx.unbounded_send(()).ok();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let reload_theme_task = cx.spawn(|_, cx| async move {
|
let reload_task = cx.spawn(|this, mut cx| async move {
|
||||||
while let Some(_) = reload_theme_rx.next().await {
|
while let Some(changes) = changes_rx.next().await {
|
||||||
if cx
|
if this
|
||||||
.update(|cx| ThemeSettings::reload_current_theme(cx))
|
.update(&mut cx, |this, cx| {
|
||||||
|
this.extension_changes.merge(changes);
|
||||||
|
this.reload(cx);
|
||||||
|
})
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
|
@ -463,136 +532,179 @@ impl ExtensionStore {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
[events_task, reload_theme_task]
|
[events_task, reload_task]
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
|
fn reload(&mut self, cx: &mut ModelContext<Self>) {
|
||||||
|
if self.reload_task.is_some() {
|
||||||
|
self.needs_reload = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let fs = self.fs.clone();
|
let fs = self.fs.clone();
|
||||||
let extensions_dir = self.extensions_dir.clone();
|
let extensions_dir = self.extensions_dir.clone();
|
||||||
let manifest_path = self.manifest_path.clone();
|
let manifest_path = self.manifest_path.clone();
|
||||||
cx.spawn(|this, mut cx| async move {
|
self.needs_reload = false;
|
||||||
let manifest = cx
|
self.reload_task = Some(cx.spawn(|this, mut cx| {
|
||||||
.background_executor()
|
async move {
|
||||||
.spawn(async move {
|
let manifest = cx
|
||||||
let mut manifest = Manifest::default();
|
.background_executor()
|
||||||
|
.spawn(async move {
|
||||||
|
let mut manifest = Manifest::default();
|
||||||
|
|
||||||
let mut extension_paths = fs
|
fs.create_dir(&extensions_dir).await.log_err();
|
||||||
.read_dir(&extensions_dir)
|
|
||||||
.await
|
|
||||||
.context("failed to read extensions directory")?;
|
|
||||||
while let Some(extension_dir) = extension_paths.next().await {
|
|
||||||
let extension_dir = extension_dir?;
|
|
||||||
let Some(extension_name) =
|
|
||||||
extension_dir.file_name().and_then(OsStr::to_str)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
let extension_paths = fs.read_dir(&extensions_dir).await;
|
||||||
struct ExtensionJson {
|
if let Ok(mut extension_paths) = extension_paths {
|
||||||
pub version: String,
|
while let Some(extension_dir) = extension_paths.next().await {
|
||||||
|
let Ok(extension_dir) = extension_dir else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
Self::add_extension_to_manifest(
|
||||||
|
fs.clone(),
|
||||||
|
extension_dir,
|
||||||
|
&mut manifest,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.log_err();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let extension_json_path = extension_dir.join("extension.json");
|
if let Ok(manifest_json) = serde_json::to_string_pretty(&manifest) {
|
||||||
let extension_json: ExtensionJson =
|
fs.save(
|
||||||
serde_json::from_str(&fs.load(&extension_json_path).await?)?;
|
&manifest_path,
|
||||||
|
&manifest_json.as_str().into(),
|
||||||
|
Default::default(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.context("failed to save extension manifest")
|
||||||
|
.log_err();
|
||||||
|
}
|
||||||
|
|
||||||
manifest
|
manifest
|
||||||
.extensions
|
})
|
||||||
.insert(extension_name.into(), extension_json.version.into());
|
.await;
|
||||||
|
|
||||||
if let Ok(mut grammar_paths) =
|
this.update(&mut cx, |this, cx| {
|
||||||
fs.read_dir(&extension_dir.join("grammars")).await
|
this.manifest_updated(manifest, cx);
|
||||||
{
|
this.reload_task.take();
|
||||||
while let Some(grammar_path) = grammar_paths.next().await {
|
if this.needs_reload {
|
||||||
let grammar_path = grammar_path?;
|
this.reload(cx);
|
||||||
let Ok(relative_path) = grammar_path.strip_prefix(&extension_dir)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let Some(grammar_name) =
|
|
||||||
grammar_path.file_stem().and_then(OsStr::to_str)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
manifest.grammars.insert(
|
|
||||||
grammar_name.into(),
|
|
||||||
GrammarManifestEntry {
|
|
||||||
extension: extension_name.into(),
|
|
||||||
path: relative_path.into(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(mut language_paths) =
|
|
||||||
fs.read_dir(&extension_dir.join("languages")).await
|
|
||||||
{
|
|
||||||
while let Some(language_path) = language_paths.next().await {
|
|
||||||
let language_path = language_path?;
|
|
||||||
let Ok(relative_path) = language_path.strip_prefix(&extension_dir)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let config = fs.load(&language_path.join("config.toml")).await?;
|
|
||||||
let config = ::toml::from_str::<LanguageConfig>(&config)?;
|
|
||||||
|
|
||||||
manifest.languages.insert(
|
|
||||||
config.name.clone(),
|
|
||||||
LanguageManifestEntry {
|
|
||||||
extension: extension_name.into(),
|
|
||||||
path: relative_path.into(),
|
|
||||||
matcher: config.matcher,
|
|
||||||
grammar: config.grammar,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Ok(mut theme_paths) =
|
|
||||||
fs.read_dir(&extension_dir.join("themes")).await
|
|
||||||
{
|
|
||||||
while let Some(theme_path) = theme_paths.next().await {
|
|
||||||
let theme_path = theme_path?;
|
|
||||||
let Ok(relative_path) = theme_path.strip_prefix(&extension_dir)
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
let Some(theme_family) =
|
|
||||||
ThemeRegistry::read_user_theme(&theme_path, fs.clone())
|
|
||||||
.await
|
|
||||||
.log_err()
|
|
||||||
else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
|
|
||||||
for theme in theme_family.themes {
|
|
||||||
let location = ThemeManifestEntry {
|
|
||||||
extension: extension_name.into(),
|
|
||||||
path: relative_path.into(),
|
|
||||||
};
|
|
||||||
|
|
||||||
manifest.themes.insert(theme.name, location);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.save(
|
|
||||||
&manifest_path,
|
|
||||||
&serde_json::to_string_pretty(&manifest)?.as_str().into(),
|
|
||||||
Default::default(),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.context("failed to save extension manifest")?;
|
|
||||||
|
|
||||||
anyhow::Ok(manifest)
|
|
||||||
})
|
})
|
||||||
.await?;
|
}
|
||||||
this.update(&mut cx, |this, cx| this.manifest_updated(manifest, cx))
|
.log_err()
|
||||||
})
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_extension_to_manifest(
|
||||||
|
fs: Arc<dyn Fs>,
|
||||||
|
extension_dir: PathBuf,
|
||||||
|
manifest: &mut Manifest,
|
||||||
|
) -> Result<()> {
|
||||||
|
let extension_name = extension_dir
|
||||||
|
.file_name()
|
||||||
|
.and_then(OsStr::to_str)
|
||||||
|
.ok_or_else(|| anyhow!("invalid extension name"))?;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct ExtensionJson {
|
||||||
|
pub version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
let extension_json_path = extension_dir.join("extension.json");
|
||||||
|
let extension_json = fs
|
||||||
|
.load(&extension_json_path)
|
||||||
|
.await
|
||||||
|
.context("failed to load extension.json")?;
|
||||||
|
let extension_json: ExtensionJson =
|
||||||
|
serde_json::from_str(&extension_json).context("invalid extension.json")?;
|
||||||
|
|
||||||
|
manifest
|
||||||
|
.extensions
|
||||||
|
.insert(extension_name.into(), extension_json.version.into());
|
||||||
|
|
||||||
|
if let Ok(mut grammar_paths) = fs.read_dir(&extension_dir.join("grammars")).await {
|
||||||
|
while let Some(grammar_path) = grammar_paths.next().await {
|
||||||
|
let grammar_path = grammar_path?;
|
||||||
|
let Ok(relative_path) = grammar_path.strip_prefix(&extension_dir) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Some(grammar_name) = grammar_path.file_stem().and_then(OsStr::to_str) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
manifest.grammars.insert(
|
||||||
|
grammar_name.into(),
|
||||||
|
GrammarManifestEntry {
|
||||||
|
extension: extension_name.into(),
|
||||||
|
path: relative_path.into(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(mut language_paths) = fs.read_dir(&extension_dir.join("languages")).await {
|
||||||
|
while let Some(language_path) = language_paths.next().await {
|
||||||
|
let language_path = language_path?;
|
||||||
|
let Ok(relative_path) = language_path.strip_prefix(&extension_dir) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let config = fs.load(&language_path.join("config.toml")).await?;
|
||||||
|
let config = ::toml::from_str::<LanguageConfig>(&config)?;
|
||||||
|
|
||||||
|
manifest.languages.insert(
|
||||||
|
config.name.clone(),
|
||||||
|
LanguageManifestEntry {
|
||||||
|
extension: extension_name.into(),
|
||||||
|
path: relative_path.into(),
|
||||||
|
matcher: config.matcher,
|
||||||
|
grammar: config.grammar,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(mut theme_paths) = fs.read_dir(&extension_dir.join("themes")).await {
|
||||||
|
while let Some(theme_path) = theme_paths.next().await {
|
||||||
|
let theme_path = theme_path?;
|
||||||
|
let Ok(relative_path) = theme_path.strip_prefix(&extension_dir) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(theme_family) = ThemeRegistry::read_user_theme(&theme_path, fs.clone())
|
||||||
|
.await
|
||||||
|
.log_err()
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
|
for theme in theme_family.themes {
|
||||||
|
let location = ThemeManifestEntry {
|
||||||
|
extension: extension_name.into(),
|
||||||
|
path: relative_path.into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
manifest.themes.insert(theme.name.into(), location);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ExtensionChanges {
|
||||||
|
fn clear(&mut self) {
|
||||||
|
self.grammars.clear();
|
||||||
|
self.languages.clear();
|
||||||
|
self.themes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn merge(&mut self, other: Self) {
|
||||||
|
self.grammars.extend(other.grammars);
|
||||||
|
self.languages.extend(other.languages);
|
||||||
|
self.themes.extend(other.themes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -257,10 +257,7 @@ async fn test_extension_store(cx: &mut TestAppContext) {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
store
|
store.update(cx, |store, cx| store.reload(cx));
|
||||||
.update(cx, |store, cx| store.reload(cx))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
cx.executor().run_until_parked();
|
cx.executor().run_until_parked();
|
||||||
store.read_with(cx, |store, _| {
|
store.read_with(cx, |store, _| {
|
||||||
|
@ -331,13 +328,11 @@ async fn test_extension_store(cx: &mut TestAppContext) {
|
||||||
assert_eq!(fs.metadata_call_count(), prev_fs_metadata_call_count + 2);
|
assert_eq!(fs.metadata_call_count(), prev_fs_metadata_call_count + 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
store
|
store.update(cx, |store, cx| {
|
||||||
.update(cx, |store, cx| {
|
store.uninstall_extension("zed-ruby".into(), cx)
|
||||||
store.uninstall_extension("zed-ruby".into(), cx)
|
});
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
|
cx.executor().run_until_parked();
|
||||||
expected_manifest.extensions.remove("zed-ruby");
|
expected_manifest.extensions.remove("zed-ruby");
|
||||||
expected_manifest.languages.remove("Ruby");
|
expected_manifest.languages.remove("Ruby");
|
||||||
expected_manifest.languages.remove("ERB");
|
expected_manifest.languages.remove("ERB");
|
||||||
|
|
|
@ -38,6 +38,7 @@ pub struct ExtensionsPage {
|
||||||
extensions_entries: Vec<Extension>,
|
extensions_entries: Vec<Extension>,
|
||||||
query_editor: View<Editor>,
|
query_editor: View<Editor>,
|
||||||
query_contains_error: bool,
|
query_contains_error: bool,
|
||||||
|
_subscription: gpui::Subscription,
|
||||||
extension_fetch_task: Option<Task<()>>,
|
extension_fetch_task: Option<Task<()>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +76,9 @@ impl Render for ExtensionsPage {
|
||||||
impl ExtensionsPage {
|
impl ExtensionsPage {
|
||||||
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
|
pub fn new(workspace: &Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
|
||||||
let extensions_panel = cx.new_view(|cx: &mut ViewContext<Self>| {
|
let extensions_panel = cx.new_view(|cx: &mut ViewContext<Self>| {
|
||||||
|
let store = ExtensionStore::global(cx);
|
||||||
|
let subscription = cx.observe(&store, |_, _, cx| cx.notify());
|
||||||
|
|
||||||
let query_editor = cx.new_view(|cx| Editor::single_line(cx));
|
let query_editor = cx.new_view(|cx| Editor::single_line(cx));
|
||||||
cx.subscribe(&query_editor, Self::on_query_change).detach();
|
cx.subscribe(&query_editor, Self::on_query_change).detach();
|
||||||
|
|
||||||
|
@ -86,6 +90,7 @@ impl ExtensionsPage {
|
||||||
extensions_entries: Vec::new(),
|
extensions_entries: Vec::new(),
|
||||||
query_contains_error: false,
|
query_contains_error: false,
|
||||||
extension_fetch_task: None,
|
extension_fetch_task: None,
|
||||||
|
_subscription: subscription,
|
||||||
query_editor,
|
query_editor,
|
||||||
};
|
};
|
||||||
this.fetch_extensions(None, cx);
|
this.fetch_extensions(None, cx);
|
||||||
|
@ -100,25 +105,15 @@ impl ExtensionsPage {
|
||||||
version: Arc<str>,
|
version: Arc<str>,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) {
|
) {
|
||||||
let install = ExtensionStore::global(cx).update(cx, |store, cx| {
|
ExtensionStore::global(cx).update(cx, |store, cx| {
|
||||||
store.install_extension(extension_id, version, cx)
|
store.install_extension(extension_id, version, cx)
|
||||||
});
|
});
|
||||||
cx.spawn(move |this, mut cx| async move {
|
|
||||||
install.await?;
|
|
||||||
this.update(&mut cx, |_, cx| cx.notify())
|
|
||||||
})
|
|
||||||
.detach_and_log_err(cx);
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn uninstall_extension(&self, extension_id: Arc<str>, cx: &mut ViewContext<Self>) {
|
fn uninstall_extension(&self, extension_id: Arc<str>, cx: &mut ViewContext<Self>) {
|
||||||
let install = ExtensionStore::global(cx)
|
ExtensionStore::global(cx)
|
||||||
.update(cx, |store, cx| store.uninstall_extension(extension_id, cx));
|
.update(cx, |store, cx| store.uninstall_extension(extension_id, cx));
|
||||||
cx.spawn(move |this, mut cx| async move {
|
|
||||||
install.await?;
|
|
||||||
this.update(&mut cx, |_, cx| cx.notify())
|
|
||||||
})
|
|
||||||
.detach_and_log_err(cx);
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,15 +399,21 @@ impl Item for ExtensionsPage {
|
||||||
_workspace_id: WorkspaceId,
|
_workspace_id: WorkspaceId,
|
||||||
cx: &mut ViewContext<Self>,
|
cx: &mut ViewContext<Self>,
|
||||||
) -> Option<View<Self>> {
|
) -> Option<View<Self>> {
|
||||||
Some(cx.new_view(|_| ExtensionsPage {
|
Some(cx.new_view(|cx| {
|
||||||
fs: self.fs.clone(),
|
let store = ExtensionStore::global(cx);
|
||||||
workspace: self.workspace.clone(),
|
let subscription = cx.observe(&store, |_, _, cx| cx.notify());
|
||||||
list: UniformListScrollHandle::new(),
|
|
||||||
telemetry: self.telemetry.clone(),
|
ExtensionsPage {
|
||||||
extensions_entries: Default::default(),
|
fs: self.fs.clone(),
|
||||||
query_editor: self.query_editor.clone(),
|
workspace: self.workspace.clone(),
|
||||||
query_contains_error: false,
|
list: UniformListScrollHandle::new(),
|
||||||
extension_fetch_task: None,
|
telemetry: self.telemetry.clone(),
|
||||||
|
extensions_entries: Default::default(),
|
||||||
|
query_editor: self.query_editor.clone(),
|
||||||
|
_subscription: subscription,
|
||||||
|
query_contains_error: false,
|
||||||
|
extension_fetch_task: None,
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -151,11 +151,6 @@ impl LanguageRegistry {
|
||||||
self.state.write().reload();
|
self.state.write().reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears out the given languages and reload them from scratch.
|
|
||||||
pub fn reload_languages(&self, languages: &[Arc<str>], grammars: &[Arc<str>]) {
|
|
||||||
self.state.write().reload_languages(languages, grammars);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes the specified languages and grammars from the registry.
|
/// Removes the specified languages and grammars from the registry.
|
||||||
pub fn remove_languages(
|
pub fn remove_languages(
|
||||||
&self,
|
&self,
|
||||||
|
@ -209,6 +204,9 @@ impl LanguageRegistry {
|
||||||
lsp_adapters,
|
lsp_adapters,
|
||||||
loaded: false,
|
loaded: false,
|
||||||
});
|
});
|
||||||
|
state.version += 1;
|
||||||
|
state.reload_count += 1;
|
||||||
|
*state.subscription.0.borrow_mut() = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds grammars to the registry. Language configurations reference a grammar by name. The
|
/// Adds grammars to the registry. Language configurations reference a grammar by name. The
|
||||||
|
@ -229,11 +227,15 @@ impl LanguageRegistry {
|
||||||
&self,
|
&self,
|
||||||
grammars: impl IntoIterator<Item = (impl Into<Arc<str>>, PathBuf)>,
|
grammars: impl IntoIterator<Item = (impl Into<Arc<str>>, PathBuf)>,
|
||||||
) {
|
) {
|
||||||
self.state.write().grammars.extend(
|
let mut state = self.state.write();
|
||||||
|
state.grammars.extend(
|
||||||
grammars
|
grammars
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(name, path)| (name.into(), AvailableGrammar::Unloaded(path))),
|
.map(|(name, path)| (name.into(), AvailableGrammar::Unloaded(path))),
|
||||||
);
|
);
|
||||||
|
state.version += 1;
|
||||||
|
state.reload_count += 1;
|
||||||
|
*state.subscription.0.borrow_mut() = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn language_names(&self) -> Vec<String> {
|
pub fn language_names(&self) -> Vec<String> {
|
||||||
|
@ -679,6 +681,10 @@ impl LanguageRegistryState {
|
||||||
languages_to_remove: &[Arc<str>],
|
languages_to_remove: &[Arc<str>],
|
||||||
grammars_to_remove: &[Arc<str>],
|
grammars_to_remove: &[Arc<str>],
|
||||||
) {
|
) {
|
||||||
|
if languages_to_remove.is_empty() && grammars_to_remove.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
self.languages
|
self.languages
|
||||||
.retain(|language| !languages_to_remove.contains(&language.name()));
|
.retain(|language| !languages_to_remove.contains(&language.name()));
|
||||||
self.available_languages
|
self.available_languages
|
||||||
|
@ -690,45 +696,6 @@ impl LanguageRegistryState {
|
||||||
*self.subscription.0.borrow_mut() = ();
|
*self.subscription.0.borrow_mut() = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reload_languages(
|
|
||||||
&mut self,
|
|
||||||
languages_to_reload: &[Arc<str>],
|
|
||||||
grammars_to_reload: &[Arc<str>],
|
|
||||||
) {
|
|
||||||
for (name, grammar) in self.grammars.iter_mut() {
|
|
||||||
if grammars_to_reload.contains(name) {
|
|
||||||
if let AvailableGrammar::Loaded(path, _) = grammar {
|
|
||||||
*grammar = AvailableGrammar::Unloaded(path.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.languages.retain(|language| {
|
|
||||||
let should_reload = languages_to_reload.contains(&language.config.name)
|
|
||||||
|| language
|
|
||||||
.config
|
|
||||||
.grammar
|
|
||||||
.as_ref()
|
|
||||||
.map_or(false, |grammar| grammars_to_reload.contains(&grammar));
|
|
||||||
!should_reload
|
|
||||||
});
|
|
||||||
|
|
||||||
for language in &mut self.available_languages {
|
|
||||||
if languages_to_reload.contains(&language.name)
|
|
||||||
|| language
|
|
||||||
.grammar
|
|
||||||
.as_ref()
|
|
||||||
.map_or(false, |grammar| grammars_to_reload.contains(grammar))
|
|
||||||
{
|
|
||||||
language.loaded = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.version += 1;
|
|
||||||
self.reload_count += 1;
|
|
||||||
*self.subscription.0.borrow_mut() = ();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Mark the given language as having been loaded, so that the
|
/// Mark the given language as having been loaded, so that the
|
||||||
/// language registry won't try to load it again.
|
/// language registry won't try to load it again.
|
||||||
fn mark_language_loaded(&mut self, id: LanguageId) {
|
fn mark_language_loaded(&mut self, id: LanguageId) {
|
||||||
|
|
Loading…
Reference in a new issue