Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Allow formatting of unsaved buffers with prettier (#12095)

Thorsten Ballc290d924f1d3
diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rs
index e28fc5e86d0..37765a3c4c5 100644
--- a/crates/prettier/src/prettier.rs
+++ b/crates/prettier/src/prettier.rs
@@ -1,4 +1,4 @@
-use anyhow::Context;
+use anyhow::{anyhow, Context};
use collections::{HashMap, HashSet};
use fs::Fs;
use gpui::{AsyncAppContext, Model};
@@ -315,6 +315,12 @@ impl Prettier {
}
})
.collect();
+
+ if prettier_settings.parser.is_none() && buffer_path.is_none() {
+ log::error!("Formatting unsaved file with prettier failed. No prettier parser configured for language");
+ return Err(anyhow!("Cannot determine prettier parser for unsaved file"));
+ }
+
log::debug!(
"Formatting file {:?} with prettier, plugins :{:?}, options: {:?}",
buffer.file().map(|f| f.full_path(cx)),
@@ -333,6 +339,7 @@ impl Prettier {
})
})?
.context("prettier params calculation")?;
+
let response = local
.server
.request::<Format>(params)
diff --git a/crates/prettier/src/prettier_server.js b/crates/prettier/src/prettier_server.js
index 7e4ced773eb..d19c557f8e0 100644
--- a/crates/prettier/src/prettier_server.js
+++ b/crates/prettier/src/prettier_server.js
@@ -186,11 +186,17 @@ async function handleMessage(message, prettier) {
}
let resolvedConfig = {};
- if (params.options.filepath !== undefined) {
+ if (params.options.filepath) {
resolvedConfig =
(await prettier.prettier.resolveConfig(params.options.filepath)) || {};
}
+ // Marking the params.options.filepath as undefined makes
+ // prettier.format() work even if no filepath is set.
+ if (params.options.filepath === null) {
+ params.options.filepath = undefined;
+ }
+
const plugins =
Array.isArray(resolvedConfig?.plugins) &&
resolvedConfig.plugins.length > 0
diff --git a/crates/project/src/prettier_support.rs b/crates/project/src/prettier_support.rs
index c81a703a372..706c4372bb9 100644
--- a/crates/project/src/prettier_support.rs
+++ b/crates/project/src/prettier_support.rs
@@ -61,7 +61,8 @@ pub(super) async fn format_with_prettier(
.update(cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
})
- .ok()?;
+ .ok()
+ .flatten();
let format_result = prettier
.format(buffer, buffer_path, cx)
diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs
index c13426e8f12..1b8e1cac4ec 100644
--- a/crates/project/src/project.rs
+++ b/crates/project/src/project.rs
@@ -4702,11 +4702,11 @@ impl Project {
if self.is_local() {
let buffers_with_paths = buffers
.into_iter()
- .filter_map(|buffer_handle| {
+ .map(|buffer_handle| {
let buffer = buffer_handle.read(cx);
- let file = File::from_dyn(buffer.file())?;
- let buffer_abs_path = file.as_local().map(|f| f.abs_path(cx));
- Some((buffer_handle, buffer_abs_path))
+ let buffer_abs_path = File::from_dyn(buffer.file())
+ .and_then(|file| file.as_local().map(|f| f.abs_path(cx)));
+ (buffer_handle, buffer_abs_path)
})
.collect::<Vec<_>>();