mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-29 04:20:46 +00:00
Simplify default prettier installation function
This commit is contained in:
parent
96f6b89508
commit
f1314afe35
2 changed files with 33 additions and 33 deletions
|
@ -25,6 +25,26 @@ use crate::{
|
|||
Event, File, FormatOperation, PathChange, Project, ProjectEntryId, Worktree, WorktreeId,
|
||||
};
|
||||
|
||||
pub fn prettier_plugins_for_language(language: &Language, language_settings: &LanguageSettings) -> Option<HashSet<&'static str>> {
|
||||
match &language_settings.formatter {
|
||||
Formatter::Prettier { .. } | Formatter::Auto => {}
|
||||
Formatter::LanguageServer | Formatter::External { .. } => return None,
|
||||
};
|
||||
let mut prettier_plugins = None;
|
||||
if language.prettier_parser_name().is_some() {
|
||||
prettier_plugins
|
||||
.get_or_insert_with(|| HashSet::default())
|
||||
.extend(
|
||||
language
|
||||
.lsp_adapters()
|
||||
.iter()
|
||||
.flat_map(|adapter| adapter.prettier_plugins()),
|
||||
)
|
||||
}
|
||||
|
||||
prettier_plugins
|
||||
}
|
||||
|
||||
pub(super) async fn format_with_prettier(
|
||||
project: &ModelHandle<Project>,
|
||||
buffer: &ModelHandle<Buffer>,
|
||||
|
@ -578,15 +598,10 @@ impl Project {
|
|||
pub fn install_default_prettier(
|
||||
&mut self,
|
||||
_worktree: Option<WorktreeId>,
|
||||
_new_language: &Language,
|
||||
language_settings: &LanguageSettings,
|
||||
_plugins: HashSet<&'static str>,
|
||||
_cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
// suppress unused code warnings
|
||||
match &language_settings.formatter {
|
||||
Formatter::Prettier { .. } | Formatter::Auto => {}
|
||||
Formatter::LanguageServer | Formatter::External { .. } => return,
|
||||
};
|
||||
let _ = &self.default_prettier.installed_plugins;
|
||||
}
|
||||
|
||||
|
@ -594,33 +609,12 @@ impl Project {
|
|||
pub fn install_default_prettier(
|
||||
&mut self,
|
||||
worktree: Option<WorktreeId>,
|
||||
new_language: &Language,
|
||||
language_settings: &LanguageSettings,
|
||||
mut new_plugins: HashSet<&'static str>,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) {
|
||||
match &language_settings.formatter {
|
||||
Formatter::Prettier { .. } | Formatter::Auto => {}
|
||||
Formatter::LanguageServer | Formatter::External { .. } => return,
|
||||
};
|
||||
let Some(node) = self.node.as_ref().cloned() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut prettier_plugins = None;
|
||||
if new_language.prettier_parser_name().is_some() {
|
||||
prettier_plugins
|
||||
.get_or_insert_with(|| HashSet::<&'static str>::default())
|
||||
.extend(
|
||||
new_language
|
||||
.lsp_adapters()
|
||||
.iter()
|
||||
.flat_map(|adapter| adapter.prettier_plugins()),
|
||||
)
|
||||
}
|
||||
let Some(prettier_plugins) = prettier_plugins else {
|
||||
return;
|
||||
};
|
||||
|
||||
let fs = Arc::clone(&self.fs);
|
||||
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
|
||||
self.worktree_for_id(worktree_id, cx)
|
||||
|
@ -637,9 +631,8 @@ impl Project {
|
|||
.await
|
||||
})
|
||||
}
|
||||
None => Task::ready(Ok(ControlFlow::Break(()))),
|
||||
None => Task::ready(Ok(ControlFlow::Continue(None))),
|
||||
};
|
||||
let mut new_plugins = prettier_plugins;
|
||||
new_plugins
|
||||
.retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin));
|
||||
let mut installation_attempt = 0;
|
||||
|
|
|
@ -925,8 +925,14 @@ impl Project {
|
|||
.detach();
|
||||
}
|
||||
|
||||
let mut prettier_plugins_by_worktree = HashMap::default();
|
||||
for (worktree, language, settings) in language_formatters_to_check {
|
||||
self.install_default_prettier(worktree, &language, &settings, cx);
|
||||
if let Some(plugins) = prettier_support::prettier_plugins_for_language(&language, &settings) {
|
||||
prettier_plugins_by_worktree.entry(worktree).or_insert_with(|| HashSet::default()).extend(plugins);
|
||||
}
|
||||
}
|
||||
for (worktree, prettier_plugins) in prettier_plugins_by_worktree {
|
||||
self.install_default_prettier(worktree, prettier_plugins, cx);
|
||||
}
|
||||
|
||||
// Start all the newly-enabled language servers.
|
||||
|
@ -2682,8 +2688,9 @@ impl Project {
|
|||
let settings = language_settings(Some(&new_language), buffer_file.as_ref(), cx).clone();
|
||||
let buffer_file = File::from_dyn(buffer_file.as_ref());
|
||||
let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx));
|
||||
|
||||
self.install_default_prettier(worktree, &new_language, &settings, cx);
|
||||
if let Some(prettier_plugins) = prettier_support::prettier_plugins_for_language(&new_language, &settings) {
|
||||
self.install_default_prettier(worktree, prettier_plugins, cx);
|
||||
};
|
||||
if let Some(file) = buffer_file {
|
||||
let worktree = file.worktree.clone();
|
||||
if let Some(tree) = worktree.read(cx).as_local() {
|
||||
|
|
Loading…
Reference in a new issue