Autodetect parser name with prettier by default (#11558)

Closes https://github.com/zed-industries/zed/issues/11517 

* Removes forced prettier parser name for languages, making `auto`
command to run prettier on every file by default.
* Moves prettier configs away from plugin language declarations into
language settings

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2024-05-15 22:51:46 +03:00 committed by GitHub
parent 52c70c1082
commit cb430fc3e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 291 additions and 209 deletions

View file

@ -2,7 +2,7 @@ use anyhow::Context;
use collections::{HashMap, HashSet};
use fs::Fs;
use gpui::{AsyncAppContext, Model};
use language::{language_settings::language_settings, Buffer, Diff, LanguageRegistry};
use language::{language_settings::language_settings, Buffer, Diff};
use lsp::{LanguageServer, LanguageServerId};
use node_runtime::NodeRuntime;
use serde::{Deserialize, Serialize};
@ -25,7 +25,6 @@ pub struct RealPrettier {
default: bool,
prettier_dir: PathBuf,
server: Arc<LanguageServer>,
language_registry: Arc<LanguageRegistry>,
}
#[cfg(any(test, feature = "test-support"))]
@ -156,7 +155,6 @@ impl Prettier {
_: LanguageServerId,
prettier_dir: PathBuf,
_: Arc<dyn NodeRuntime>,
_: Arc<LanguageRegistry>,
_: AsyncAppContext,
) -> anyhow::Result<Self> {
Ok(Self::Test(TestPrettier {
@ -170,7 +168,6 @@ impl Prettier {
server_id: LanguageServerId,
prettier_dir: PathBuf,
node: Arc<dyn NodeRuntime>,
language_registry: Arc<LanguageRegistry>,
cx: AsyncAppContext,
) -> anyhow::Result<Self> {
use lsp::LanguageServerBinary;
@ -209,7 +206,6 @@ impl Prettier {
Ok(Self::Real(RealPrettier {
server,
default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(),
language_registry,
prettier_dir,
}))
}
@ -225,22 +221,19 @@ impl Prettier {
let params = buffer
.update(cx, |buffer, cx| {
let buffer_language = buffer.language();
let parser_with_plugins = buffer_language.and_then(|l| {
let prettier_parser = l.prettier_parser_name()?;
let mut prettier_plugins =
local.language_registry.all_prettier_plugins();
prettier_plugins.dedup();
Some((prettier_parser, prettier_plugins))
});
let language_settings = language_settings(buffer_language, buffer.file(), cx);
let prettier_settings = &language_settings.prettier;
anyhow::ensure!(
prettier_settings.allowed,
"Cannot format: prettier is not allowed for language {buffer_language:?}"
);
let prettier_node_modules = self.prettier_dir().join("node_modules");
anyhow::ensure!(
prettier_node_modules.is_dir(),
"Prettier node_modules dir does not exist: {prettier_node_modules:?}"
);
let plugin_name_into_path = |plugin_name: Arc<str>| {
let prettier_plugin_dir =
prettier_node_modules.join(plugin_name.as_ref());
let plugin_name_into_path = |plugin_name: &str| {
let prettier_plugin_dir = prettier_node_modules.join(plugin_name);
[
prettier_plugin_dir.join("dist").join("index.mjs"),
prettier_plugin_dir.join("dist").join("index.js"),
@ -255,45 +248,34 @@ impl Prettier {
.into_iter()
.find(|possible_plugin_path| possible_plugin_path.is_file())
};
let (parser, located_plugins) = match parser_with_plugins {
Some((parser, plugins)) => {
// Tailwind plugin requires being added last
// https://github.com/tailwindlabs/prettier-plugin-tailwindcss#compatibility-with-other-prettier-plugins
let mut add_tailwind_back = false;
let mut plugins = plugins
.into_iter()
.filter(|plugin_name| {
if plugin_name.as_ref()
== TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME
{
add_tailwind_back = true;
false
} else {
true
}
})
.map(|plugin_name| {
(plugin_name.clone(), plugin_name_into_path(plugin_name))
})
.collect::<Vec<_>>();
if add_tailwind_back {
plugins.push((
TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME.into(),
plugin_name_into_path(
TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME.into(),
),
));
// Tailwind plugin requires being added last
// https://github.com/tailwindlabs/prettier-plugin-tailwindcss#compatibility-with-other-prettier-plugins
let mut add_tailwind_back = false;
let mut located_plugins = prettier_settings.plugins.iter()
.filter(|plugin_name| {
if plugin_name.as_str() == TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME {
add_tailwind_back = true;
false
} else {
true
}
(Some(parser.to_string()), plugins)
}
None => (None, Vec::new()),
};
})
.map(|plugin_name| {
let plugin_path = plugin_name_into_path(plugin_name);
(plugin_name.clone(), plugin_path)
})
.collect::<Vec<_>>();
if add_tailwind_back {
located_plugins.push((
TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME.to_owned(),
plugin_name_into_path(TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME),
));
}
let prettier_options = if self.is_default() {
let language_settings =
language_settings(buffer_language, buffer.file(), cx);
let mut options = language_settings.prettier.clone();
let mut options = prettier_settings.options.clone();
if !options.contains_key("tabWidth") {
options.insert(
"tabWidth".to_string(),
@ -321,11 +303,7 @@ impl Prettier {
match located_plugin_path {
Some(path) => Some(path),
None => {
log::error!(
"Have not found plugin path for {:?} inside {:?}",
plugin_name,
prettier_node_modules
);
log::error!("Have not found plugin path for {plugin_name:?} inside {prettier_node_modules:?}");
None
}
}
@ -341,7 +319,7 @@ impl Prettier {
anyhow::Ok(FormatParams {
text: buffer.text(),
options: FormatOptions {
parser,
parser: prettier_settings.parser.clone(),
plugins,
path: buffer_path,
prettier_options,
@ -360,9 +338,19 @@ impl Prettier {
#[cfg(any(test, feature = "test-support"))]
Self::Test(_) => Ok(buffer
.update(cx, |buffer, cx| {
let formatted_text = buffer.text() + FORMAT_SUFFIX;
buffer.diff(formatted_text, cx)
})?
match buffer
.language()
.map(|language| language.lsp_id())
.as_deref()
{
Some("rust") => anyhow::bail!("prettier does not support Rust"),
Some(_other) => {
let formatted_text = buffer.text() + FORMAT_SUFFIX;
Ok(buffer.diff(formatted_text, cx))
}
None => panic!("Should not format buffer without a language with prettier"),
}
})??
.await),
}
}