Port changes to zed2

This commit is contained in:
Kirill Bulatov 2023-11-03 11:02:38 +02:00
parent 24dd1c5812
commit 09346fb9f1
8 changed files with 1059 additions and 501 deletions

View file

@ -61,7 +61,7 @@ impl Prettier {
) -> anyhow::Result<Option<PathBuf>> { ) -> anyhow::Result<Option<PathBuf>> {
let mut path_to_check = locate_from let mut path_to_check = locate_from
.components() .components()
.take_while(|component| !is_node_modules(component)) .take_while(|component| component.as_os_str().to_string_lossy() != "node_modules")
.collect::<PathBuf>(); .collect::<PathBuf>();
let path_to_check_metadata = fs let path_to_check_metadata = fs
.metadata(&path_to_check) .metadata(&path_to_check)
@ -763,7 +763,3 @@ mod tests {
}; };
} }
} }
fn is_node_modules(path_component: &std::path::Component<'_>) -> bool {
path_component.as_os_str().to_string_lossy() == "node_modules"
}

View file

@ -1,11 +1,13 @@
const { Buffer } = require('buffer'); const { Buffer } = require("buffer");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const { once } = require('events'); const { once } = require("events");
const prettierContainerPath = process.argv[2]; const prettierContainerPath = process.argv[2];
if (prettierContainerPath == null || prettierContainerPath.length == 0) { if (prettierContainerPath == null || prettierContainerPath.length == 0) {
process.stderr.write(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`); process.stderr.write(
`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`,
);
process.exit(1); process.exit(1);
} }
fs.stat(prettierContainerPath, (err, stats) => { fs.stat(prettierContainerPath, (err, stats) => {
@ -19,7 +21,7 @@ fs.stat(prettierContainerPath, (err, stats) => {
process.exit(1); process.exit(1);
} }
}); });
const prettierPath = path.join(prettierContainerPath, 'node_modules/prettier'); const prettierPath = path.join(prettierContainerPath, "node_modules/prettier");
class Prettier { class Prettier {
constructor(path, prettier, config) { constructor(path, prettier, config) {
@ -34,7 +36,7 @@ class Prettier {
let config; let config;
try { try {
prettier = await loadPrettier(prettierPath); prettier = await loadPrettier(prettierPath);
config = await prettier.resolveConfig(prettierPath) || {}; config = (await prettier.resolveConfig(prettierPath)) || {};
} catch (e) { } catch (e) {
process.stderr.write(`Failed to load prettier: ${e}\n`); process.stderr.write(`Failed to load prettier: ${e}\n`);
process.exit(1); process.exit(1);
@ -42,7 +44,7 @@ class Prettier {
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`); process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
process.stdin.resume(); process.stdin.resume();
handleBuffer(new Prettier(prettierPath, prettier, config)); handleBuffer(new Prettier(prettierPath, prettier, config));
})() })();
async function handleBuffer(prettier) { async function handleBuffer(prettier) {
for await (const messageText of readStdin()) { for await (const messageText of readStdin()) {
@ -54,25 +56,29 @@ async function handleBuffer(prettier) {
continue; continue;
} }
// allow concurrent request handling by not `await`ing the message handling promise (async function) // allow concurrent request handling by not `await`ing the message handling promise (async function)
handleMessage(message, prettier).catch(e => { handleMessage(message, prettier).catch((e) => {
const errorMessage = message; const errorMessage = message;
if ((errorMessage.params || {}).text !== undefined) { if ((errorMessage.params || {}).text !== undefined) {
errorMessage.params.text = "..snip.."; errorMessage.params.text = "..snip..";
} }
sendResponse({ id: message.id, ...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`) }); }); sendResponse({
id: message.id,
...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
});
});
} }
} }
const headerSeparator = "\r\n"; const headerSeparator = "\r\n";
const contentLengthHeaderName = 'Content-Length'; const contentLengthHeaderName = "Content-Length";
async function* readStdin() { async function* readStdin() {
let buffer = Buffer.alloc(0); let buffer = Buffer.alloc(0);
let streamEnded = false; let streamEnded = false;
process.stdin.on('end', () => { process.stdin.on("end", () => {
streamEnded = true; streamEnded = true;
}); });
process.stdin.on('data', (data) => { process.stdin.on("data", (data) => {
buffer = Buffer.concat([buffer, data]); buffer = Buffer.concat([buffer, data]);
}); });
@ -80,7 +86,7 @@ async function* readStdin() {
sendResponse(makeError(errorMessage)); sendResponse(makeError(errorMessage));
buffer = Buffer.alloc(0); buffer = Buffer.alloc(0);
messageLength = null; messageLength = null;
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
streamEnded = false; streamEnded = false;
} }
@ -91,20 +97,25 @@ async function* readStdin() {
if (messageLength === null) { if (messageLength === null) {
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) { while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
if (streamEnded) { if (streamEnded) {
await handleStreamEnded('Unexpected end of stream: headers not found'); await handleStreamEnded("Unexpected end of stream: headers not found");
continue main_loop; continue main_loop;
} else if (buffer.length > contentLengthHeaderName.length * 10) { } else if (buffer.length > contentLengthHeaderName.length * 10) {
await handleStreamEnded(`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`); await handleStreamEnded(
`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
);
continue main_loop; continue main_loop;
} }
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
} }
const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString('ascii'); const headers = buffer
const contentLengthHeader = headers.split(headerSeparator) .subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
.map(header => header.split(':')) .toString("ascii");
.filter(header => header[2] === undefined) const contentLengthHeader = headers
.filter(header => (header[1] || '').length > 0) .split(headerSeparator)
.find(header => (header[0] || '').trim() === contentLengthHeaderName); .map((header) => header.split(":"))
.filter((header) => header[2] === undefined)
.filter((header) => (header[1] || "").length > 0)
.find((header) => (header[0] || "").trim() === contentLengthHeaderName);
const contentLength = (contentLengthHeader || [])[1]; const contentLength = (contentLengthHeader || [])[1];
if (contentLength === undefined) { if (contentLength === undefined) {
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`); await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
@ -114,13 +125,14 @@ async function* readStdin() {
messageLength = parseInt(contentLength, 10); messageLength = parseInt(contentLength, 10);
} }
while (buffer.length < (headersLength + messageLength)) { while (buffer.length < headersLength + messageLength) {
if (streamEnded) { if (streamEnded) {
await handleStreamEnded( await handleStreamEnded(
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`); `Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
);
continue main_loop; continue main_loop;
} }
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
} }
const messageEnd = headersLength + messageLength; const messageEnd = headersLength + messageLength;
@ -128,12 +140,12 @@ async function* readStdin() {
buffer = buffer.subarray(messageEnd); buffer = buffer.subarray(messageEnd);
headersLength = null; headersLength = null;
messageLength = null; messageLength = null;
yield message.toString('utf8'); yield message.toString("utf8");
} }
} catch (e) { } catch (e) {
sendResponse(makeError(`Error reading stdin: ${e}`)); sendResponse(makeError(`Error reading stdin: ${e}`));
} finally { } finally {
process.stdin.off('data', () => { }); process.stdin.off("data", () => {});
} }
} }
@ -146,7 +158,7 @@ async function handleMessage(message, prettier) {
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`); throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
} }
if (method === 'prettier/format') { if (method === "prettier/format") {
if (params === undefined || params.text === undefined) { if (params === undefined || params.text === undefined) {
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`); throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
} }
@ -156,7 +168,7 @@ async function handleMessage(message, prettier) {
let resolvedConfig = {}; let resolvedConfig = {};
if (params.options.filepath !== undefined) { if (params.options.filepath !== undefined) {
resolvedConfig = await prettier.prettier.resolveConfig(params.options.filepath) || {}; resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
} }
const options = { const options = {
@ -164,21 +176,25 @@ async function handleMessage(message, prettier) {
...resolvedConfig, ...resolvedConfig,
parser: params.options.parser, parser: params.options.parser,
plugins: params.options.plugins, plugins: params.options.plugins,
path: params.options.filepath path: params.options.filepath,
}; };
process.stderr.write(`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${params.options.filepath || ''}' with options: ${JSON.stringify(options)}\n`); process.stderr.write(
`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
params.options.filepath || ""
}' with options: ${JSON.stringify(options)}\n`,
);
const formattedText = await prettier.prettier.format(params.text, options); const formattedText = await prettier.prettier.format(params.text, options);
sendResponse({ id, result: { text: formattedText } }); sendResponse({ id, result: { text: formattedText } });
} else if (method === 'prettier/clear_cache') { } else if (method === "prettier/clear_cache") {
prettier.prettier.clearConfigCache(); prettier.prettier.clearConfigCache();
prettier.config = await prettier.prettier.resolveConfig(prettier.path) || {}; prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
sendResponse({ id, result: null }); sendResponse({ id, result: null });
} else if (method === 'initialize') { } else if (method === "initialize") {
sendResponse({ sendResponse({
id: id || 0, id,
result: { result: {
"capabilities": {} capabilities: {},
} },
}); });
} else { } else {
throw new Error(`Unknown method: ${method}`); throw new Error(`Unknown method: ${method}`);
@ -188,18 +204,20 @@ async function handleMessage(message, prettier) {
function makeError(message) { function makeError(message) {
return { return {
error: { error: {
"code": -32600, // invalid request code code: -32600, // invalid request code
message, message,
} },
}; };
} }
function sendResponse(response) { function sendResponse(response) {
const responsePayloadString = JSON.stringify({ const responsePayloadString = JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
...response ...response,
}); });
const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(responsePayloadString)}${headerSeparator}${headerSeparator}`; const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(
responsePayloadString,
)}${headerSeparator}${headerSeparator}`;
process.stdout.write(headers + responsePayloadString); process.stdout.write(headers + responsePayloadString);
} }

View file

@ -1,5 +1,5 @@
use anyhow::Context; use anyhow::Context;
use collections::HashMap; use collections::{HashMap, HashSet};
use fs::Fs; use fs::Fs;
use gpui::{AsyncAppContext, Model}; use gpui::{AsyncAppContext, Model};
use language::{language_settings::language_settings, Buffer, Diff}; use language::{language_settings::language_settings, Buffer, Diff};
@ -7,11 +7,10 @@ use lsp::{LanguageServer, LanguageServerId};
use node_runtime::NodeRuntime; use node_runtime::NodeRuntime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::VecDeque,
path::{Path, PathBuf}, path::{Path, PathBuf},
sync::Arc, sync::Arc,
}; };
use util::paths::DEFAULT_PRETTIER_DIR; use util::paths::{PathMatcher, DEFAULT_PRETTIER_DIR};
pub enum Prettier { pub enum Prettier {
Real(RealPrettier), Real(RealPrettier),
@ -20,7 +19,6 @@ pub enum Prettier {
} }
pub struct RealPrettier { pub struct RealPrettier {
worktree_id: Option<usize>,
default: bool, default: bool,
prettier_dir: PathBuf, prettier_dir: PathBuf,
server: Arc<LanguageServer>, server: Arc<LanguageServer>,
@ -28,17 +26,10 @@ pub struct RealPrettier {
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub struct TestPrettier { pub struct TestPrettier {
worktree_id: Option<usize>,
prettier_dir: PathBuf, prettier_dir: PathBuf,
default: bool, default: bool,
} }
#[derive(Debug)]
pub struct LocateStart {
pub worktree_root_path: Arc<Path>,
pub starting_path: Arc<Path>,
}
pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js"; pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js";
pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js");
const PRETTIER_PACKAGE_NAME: &str = "prettier"; const PRETTIER_PACKAGE_NAME: &str = "prettier";
@ -63,79 +54,106 @@ impl Prettier {
".editorconfig", ".editorconfig",
]; ];
pub async fn locate( pub async fn locate_prettier_installation(
starting_path: Option<LocateStart>, fs: &dyn Fs,
fs: Arc<dyn Fs>, installed_prettiers: &HashSet<PathBuf>,
) -> anyhow::Result<PathBuf> { locate_from: &Path,
fn is_node_modules(path_component: &std::path::Component<'_>) -> bool { ) -> anyhow::Result<Option<PathBuf>> {
path_component.as_os_str().to_string_lossy() == "node_modules" let mut path_to_check = locate_from
}
let paths_to_check = match starting_path.as_ref() {
Some(starting_path) => {
let worktree_root = starting_path
.worktree_root_path
.components() .components()
.into_iter() .take_while(|component| component.as_os_str().to_string_lossy() != "node_modules")
.take_while(|path_component| !is_node_modules(path_component))
.collect::<PathBuf>(); .collect::<PathBuf>();
if worktree_root != starting_path.worktree_root_path.as_ref() { let path_to_check_metadata = fs
vec![worktree_root] .metadata(&path_to_check)
} else {
if starting_path.starting_path.as_ref() == Path::new("") {
worktree_root
.parent()
.map(|path| vec![path.to_path_buf()])
.unwrap_or_default()
} else {
let file_to_format = starting_path.starting_path.as_ref();
let mut paths_to_check = VecDeque::new();
let mut current_path = worktree_root;
for path_component in file_to_format.components().into_iter() {
let new_path = current_path.join(path_component);
let old_path = std::mem::replace(&mut current_path, new_path);
paths_to_check.push_front(old_path);
if is_node_modules(&path_component) {
break;
}
}
Vec::from(paths_to_check)
}
}
}
None => Vec::new(),
};
match find_closest_prettier_dir(paths_to_check, fs.as_ref())
.await .await
.with_context(|| format!("finding prettier starting with {starting_path:?}"))? .with_context(|| format!("failed to get metadata for initial path {path_to_check:?}"))?
.with_context(|| format!("empty metadata for initial path {path_to_check:?}"))?;
if !path_to_check_metadata.is_dir {
path_to_check.pop();
}
let mut project_path_with_prettier_dependency = None;
loop {
if installed_prettiers.contains(&path_to_check) {
log::debug!("Found prettier path {path_to_check:?} in installed prettiers");
return Ok(Some(path_to_check));
} else if let Some(package_json_contents) =
read_package_json(fs, &path_to_check).await?
{ {
Some(prettier_dir) => Ok(prettier_dir), if has_prettier_in_package_json(&package_json_contents) {
None => Ok(DEFAULT_PRETTIER_DIR.to_path_buf()), if has_prettier_in_node_modules(fs, &path_to_check).await? {
log::debug!("Found prettier path {path_to_check:?} in both package.json and node_modules");
return Ok(Some(path_to_check));
} else if project_path_with_prettier_dependency.is_none() {
project_path_with_prettier_dependency = Some(path_to_check.clone());
}
} else {
match package_json_contents.get("workspaces") {
Some(serde_json::Value::Array(workspaces)) => {
match &project_path_with_prettier_dependency {
Some(project_path_with_prettier_dependency) => {
let subproject_path = project_path_with_prettier_dependency.strip_prefix(&path_to_check).expect("traversing path parents, should be able to strip prefix");
if workspaces.iter().filter_map(|value| {
if let serde_json::Value::String(s) = value {
Some(s.clone())
} else {
log::warn!("Skipping non-string 'workspaces' value: {value:?}");
None
}
}).any(|workspace_definition| {
if let Some(path_matcher) = PathMatcher::new(&workspace_definition).ok() {
path_matcher.is_match(subproject_path)
} else {
workspace_definition == subproject_path.to_string_lossy()
}
}) {
anyhow::ensure!(has_prettier_in_node_modules(fs, &path_to_check).await?, "Found prettier path {path_to_check:?} in the workspace root for project in {project_path_with_prettier_dependency:?}, but it's not installed into workspace root's node_modules");
log::info!("Found prettier path {path_to_check:?} in the workspace root for project in {project_path_with_prettier_dependency:?}");
return Ok(Some(path_to_check));
} else {
log::warn!("Skipping path {path_to_check:?} that has prettier in its 'node_modules' subdirectory, but is not included in its package.json workspaces {workspaces:?}");
}
}
None => {
log::warn!("Skipping path {path_to_check:?} that has prettier in its 'node_modules' subdirectory, but has no prettier in its package.json");
}
}
},
Some(unknown) => log::error!("Failed to parse workspaces for {path_to_check:?} from package.json, got {unknown:?}. Skipping."),
None => log::warn!("Skipping path {path_to_check:?} that has no prettier dependency and no workspaces section in its package.json"),
}
}
}
if !path_to_check.pop() {
match project_path_with_prettier_dependency {
Some(closest_prettier_discovered) => {
anyhow::bail!("No prettier found in node_modules for ancestors of {locate_from:?}, but discovered prettier package.json dependency in {closest_prettier_discovered:?}")
}
None => {
log::debug!("Found no prettier in ancestors of {locate_from:?}");
return Ok(None);
}
}
}
} }
} }
#[cfg(any(test, feature = "test-support"))] #[cfg(any(test, feature = "test-support"))]
pub async fn start( pub async fn start(
worktree_id: Option<usize>,
_: LanguageServerId, _: LanguageServerId,
prettier_dir: PathBuf, prettier_dir: PathBuf,
_: Arc<dyn NodeRuntime>, _: Arc<dyn NodeRuntime>,
_: AsyncAppContext, _: AsyncAppContext,
) -> anyhow::Result<Self> { ) -> anyhow::Result<Self> {
Ok( Ok(Self::Test(TestPrettier {
#[cfg(any(test, feature = "test-support"))]
Self::Test(TestPrettier {
worktree_id,
default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(), default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(),
prettier_dir, prettier_dir,
}), }))
)
} }
#[cfg(not(any(test, feature = "test-support")))] #[cfg(not(any(test, feature = "test-support")))]
pub async fn start( pub async fn start(
worktree_id: Option<usize>,
server_id: LanguageServerId, server_id: LanguageServerId,
prettier_dir: PathBuf, prettier_dir: PathBuf,
node: Arc<dyn NodeRuntime>, node: Arc<dyn NodeRuntime>,
@ -174,7 +192,6 @@ impl Prettier {
.await .await
.context("prettier server initialization")?; .context("prettier server initialization")?;
Ok(Self::Real(RealPrettier { Ok(Self::Real(RealPrettier {
worktree_id,
server, server,
default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(), default: prettier_dir == DEFAULT_PRETTIER_DIR.as_path(),
prettier_dir, prettier_dir,
@ -370,64 +387,61 @@ impl Prettier {
Self::Test(test_prettier) => &test_prettier.prettier_dir, Self::Test(test_prettier) => &test_prettier.prettier_dir,
} }
} }
pub fn worktree_id(&self) -> Option<usize> {
match self {
Self::Real(local) => local.worktree_id,
#[cfg(any(test, feature = "test-support"))]
Self::Test(test_prettier) => test_prettier.worktree_id,
}
}
}
async fn find_closest_prettier_dir(
paths_to_check: Vec<PathBuf>,
fs: &dyn Fs,
) -> anyhow::Result<Option<PathBuf>> {
for path in paths_to_check {
let possible_package_json = path.join("package.json");
if let Some(package_json_metadata) = fs
.metadata(&possible_package_json)
.await
.with_context(|| format!("Fetching metadata for {possible_package_json:?}"))?
{
if !package_json_metadata.is_dir && !package_json_metadata.is_symlink {
let package_json_contents = fs
.load(&possible_package_json)
.await
.with_context(|| format!("reading {possible_package_json:?} file contents"))?;
if let Ok(json_contents) = serde_json::from_str::<HashMap<String, serde_json::Value>>(
&package_json_contents,
) {
if let Some(serde_json::Value::Object(o)) = json_contents.get("dependencies") {
if o.contains_key(PRETTIER_PACKAGE_NAME) {
return Ok(Some(path));
}
}
if let Some(serde_json::Value::Object(o)) = json_contents.get("devDependencies")
{
if o.contains_key(PRETTIER_PACKAGE_NAME) {
return Ok(Some(path));
}
}
}
}
} }
async fn has_prettier_in_node_modules(fs: &dyn Fs, path: &Path) -> anyhow::Result<bool> {
let possible_node_modules_location = path.join("node_modules").join(PRETTIER_PACKAGE_NAME); let possible_node_modules_location = path.join("node_modules").join(PRETTIER_PACKAGE_NAME);
if let Some(node_modules_location_metadata) = fs if let Some(node_modules_location_metadata) = fs
.metadata(&possible_node_modules_location) .metadata(&possible_node_modules_location)
.await .await
.with_context(|| format!("fetching metadata for {possible_node_modules_location:?}"))? .with_context(|| format!("fetching metadata for {possible_node_modules_location:?}"))?
{ {
if node_modules_location_metadata.is_dir { return Ok(node_modules_location_metadata.is_dir);
return Ok(Some(path));
} }
Ok(false)
}
async fn read_package_json(
fs: &dyn Fs,
path: &Path,
) -> anyhow::Result<Option<HashMap<String, serde_json::Value>>> {
let possible_package_json = path.join("package.json");
if let Some(package_json_metadata) = fs
.metadata(&possible_package_json)
.await
.with_context(|| format!("fetching metadata for package json {possible_package_json:?}"))?
{
if !package_json_metadata.is_dir && !package_json_metadata.is_symlink {
let package_json_contents = fs
.load(&possible_package_json)
.await
.with_context(|| format!("reading {possible_package_json:?} file contents"))?;
return serde_json::from_str::<HashMap<String, serde_json::Value>>(
&package_json_contents,
)
.map(Some)
.with_context(|| format!("parsing {possible_package_json:?} file contents"));
} }
} }
Ok(None) Ok(None)
} }
fn has_prettier_in_package_json(
package_json_contents: &HashMap<String, serde_json::Value>,
) -> bool {
if let Some(serde_json::Value::Object(o)) = package_json_contents.get("dependencies") {
if o.contains_key(PRETTIER_PACKAGE_NAME) {
return true;
}
}
if let Some(serde_json::Value::Object(o)) = package_json_contents.get("devDependencies") {
if o.contains_key(PRETTIER_PACKAGE_NAME) {
return true;
}
}
false
}
enum Format {} enum Format {}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
@ -466,3 +480,316 @@ impl lsp::request::Request for ClearCache {
type Result = (); type Result = ();
const METHOD: &'static str = "prettier/clear_cache"; const METHOD: &'static str = "prettier/clear_cache";
} }
#[cfg(test)]
mod tests {
use fs::FakeFs;
use serde_json::json;
use super::*;
#[gpui::test]
async fn test_prettier_lookup_finds_nothing(cx: &mut gpui::TestAppContext) {
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/root",
json!({
".config": {
"zed": {
"settings.json": r#"{ "formatter": "auto" }"#,
},
},
"work": {
"project": {
"src": {
"index.js": "// index.js file contents",
},
"node_modules": {
"expect": {
"build": {
"print.js": "// print.js file contents",
},
"package.json": r#"{
"devDependencies": {
"prettier": "2.5.1"
}
}"#,
},
"prettier": {
"index.js": "// Dummy prettier package file",
},
},
"package.json": r#"{}"#
},
}
}),
)
.await;
assert!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/.config/zed/settings.json"),
)
.await
.unwrap()
.is_none(),
"Should successfully find no prettier for path hierarchy without it"
);
assert!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/work/project/src/index.js")
)
.await
.unwrap()
.is_none(),
"Should successfully find no prettier for path hierarchy that has node_modules with prettier, but no package.json mentions of it"
);
assert!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/work/project/node_modules/expect/build/print.js")
)
.await
.unwrap()
.is_none(),
"Even though it has package.json with prettier in it and no prettier on node_modules along the path, nothing should fail since declared inside node_modules"
);
}
#[gpui::test]
async fn test_prettier_lookup_in_simple_npm_projects(cx: &mut gpui::TestAppContext) {
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/root",
json!({
"web_blog": {
"node_modules": {
"prettier": {
"index.js": "// Dummy prettier package file",
},
"expect": {
"build": {
"print.js": "// print.js file contents",
},
"package.json": r#"{
"devDependencies": {
"prettier": "2.5.1"
}
}"#,
},
},
"pages": {
"[slug].tsx": "// [slug].tsx file contents",
},
"package.json": r#"{
"devDependencies": {
"prettier": "2.3.0"
},
"prettier": {
"semi": false,
"printWidth": 80,
"htmlWhitespaceSensitivity": "strict",
"tabWidth": 4
}
}"#
}
}),
)
.await;
assert_eq!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/web_blog/pages/[slug].tsx")
)
.await
.unwrap(),
Some(PathBuf::from("/root/web_blog")),
"Should find a preinstalled prettier in the project root"
);
assert_eq!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/web_blog/node_modules/expect/build/print.js")
)
.await
.unwrap(),
Some(PathBuf::from("/root/web_blog")),
"Should find a preinstalled prettier in the project root even for node_modules files"
);
}
#[gpui::test]
async fn test_prettier_lookup_for_not_installed(cx: &mut gpui::TestAppContext) {
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/root",
json!({
"work": {
"web_blog": {
"pages": {
"[slug].tsx": "// [slug].tsx file contents",
},
"package.json": r#"{
"devDependencies": {
"prettier": "2.3.0"
},
"prettier": {
"semi": false,
"printWidth": 80,
"htmlWhitespaceSensitivity": "strict",
"tabWidth": 4
}
}"#
}
}
}),
)
.await;
let path = "/root/work/web_blog/node_modules/pages/[slug].tsx";
match Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new(path)
)
.await {
Ok(path) => panic!("Expected to fail for prettier in package.json but not in node_modules found, but got path {path:?}"),
Err(e) => {
let message = e.to_string();
assert!(message.contains(path), "Error message should mention which start file was used for location");
assert!(message.contains("/root/work/web_blog"), "Error message should mention potential candidates without prettier node_modules contents");
},
};
assert_eq!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::from_iter(
[PathBuf::from("/root"), PathBuf::from("/root/work")].into_iter()
),
Path::new("/root/work/web_blog/node_modules/pages/[slug].tsx")
)
.await
.unwrap(),
Some(PathBuf::from("/root/work")),
"Should return first cached value found without path checks"
);
}
#[gpui::test]
async fn test_prettier_lookup_in_npm_workspaces(cx: &mut gpui::TestAppContext) {
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/root",
json!({
"work": {
"full-stack-foundations": {
"exercises": {
"03.loading": {
"01.problem.loader": {
"app": {
"routes": {
"users+": {
"$username_+": {
"notes.tsx": "// notes.tsx file contents",
},
},
},
},
"node_modules": {},
"package.json": r#"{
"devDependencies": {
"prettier": "^3.0.3"
}
}"#
},
},
},
"package.json": r#"{
"workspaces": ["exercises/*/*", "examples/*"]
}"#,
"node_modules": {
"prettier": {
"index.js": "// Dummy prettier package file",
},
},
},
}
}),
)
.await;
assert_eq!(
Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/work/full-stack-foundations/exercises/03.loading/01.problem.loader/app/routes/users+/$username_+/notes.tsx"),
).await.unwrap(),
Some(PathBuf::from("/root/work/full-stack-foundations")),
"Should ascend to the multi-workspace root and find the prettier there",
);
}
#[gpui::test]
async fn test_prettier_lookup_in_npm_workspaces_for_not_installed(
cx: &mut gpui::TestAppContext,
) {
let fs = FakeFs::new(cx.executor().clone());
fs.insert_tree(
"/root",
json!({
"work": {
"full-stack-foundations": {
"exercises": {
"03.loading": {
"01.problem.loader": {
"app": {
"routes": {
"users+": {
"$username_+": {
"notes.tsx": "// notes.tsx file contents",
},
},
},
},
"node_modules": {},
"package.json": r#"{
"devDependencies": {
"prettier": "^3.0.3"
}
}"#
},
},
},
"package.json": r#"{
"workspaces": ["exercises/*/*", "examples/*"]
}"#,
},
}
}),
)
.await;
match Prettier::locate_prettier_installation(
fs.as_ref(),
&HashSet::default(),
Path::new("/root/work/full-stack-foundations/exercises/03.loading/01.problem.loader/app/routes/users+/$username_+/notes.tsx")
)
.await {
Ok(path) => panic!("Expected to fail for prettier in package.json but not in node_modules found, but got path {path:?}"),
Err(e) => {
let message = e.to_string();
assert!(message.contains("/root/work/full-stack-foundations/exercises/03.loading/01.problem.loader"), "Error message should mention which project had prettier defined");
assert!(message.contains("/root/work/full-stack-foundations"), "Error message should mention potential candidates without prettier node_modules contents");
},
};
}
}

View file

@ -1,11 +1,13 @@
const { Buffer } = require('buffer'); const { Buffer } = require("buffer");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const { once } = require('events'); const { once } = require("events");
const prettierContainerPath = process.argv[2]; const prettierContainerPath = process.argv[2];
if (prettierContainerPath == null || prettierContainerPath.length == 0) { if (prettierContainerPath == null || prettierContainerPath.length == 0) {
process.stderr.write(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`); process.stderr.write(
`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`,
);
process.exit(1); process.exit(1);
} }
fs.stat(prettierContainerPath, (err, stats) => { fs.stat(prettierContainerPath, (err, stats) => {
@ -19,7 +21,7 @@ fs.stat(prettierContainerPath, (err, stats) => {
process.exit(1); process.exit(1);
} }
}); });
const prettierPath = path.join(prettierContainerPath, 'node_modules/prettier'); const prettierPath = path.join(prettierContainerPath, "node_modules/prettier");
class Prettier { class Prettier {
constructor(path, prettier, config) { constructor(path, prettier, config) {
@ -34,7 +36,7 @@ class Prettier {
let config; let config;
try { try {
prettier = await loadPrettier(prettierPath); prettier = await loadPrettier(prettierPath);
config = await prettier.resolveConfig(prettierPath) || {}; config = (await prettier.resolveConfig(prettierPath)) || {};
} catch (e) { } catch (e) {
process.stderr.write(`Failed to load prettier: ${e}\n`); process.stderr.write(`Failed to load prettier: ${e}\n`);
process.exit(1); process.exit(1);
@ -42,7 +44,7 @@ class Prettier {
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`); process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
process.stdin.resume(); process.stdin.resume();
handleBuffer(new Prettier(prettierPath, prettier, config)); handleBuffer(new Prettier(prettierPath, prettier, config));
})() })();
async function handleBuffer(prettier) { async function handleBuffer(prettier) {
for await (const messageText of readStdin()) { for await (const messageText of readStdin()) {
@ -54,22 +56,29 @@ async function handleBuffer(prettier) {
continue; continue;
} }
// allow concurrent request handling by not `await`ing the message handling promise (async function) // allow concurrent request handling by not `await`ing the message handling promise (async function)
handleMessage(message, prettier).catch(e => { handleMessage(message, prettier).catch((e) => {
sendResponse({ id: message.id, ...makeError(`error during message handling: ${e}`) }); const errorMessage = message;
if ((errorMessage.params || {}).text !== undefined) {
errorMessage.params.text = "..snip..";
}
sendResponse({
id: message.id,
...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
});
}); });
} }
} }
const headerSeparator = "\r\n"; const headerSeparator = "\r\n";
const contentLengthHeaderName = 'Content-Length'; const contentLengthHeaderName = "Content-Length";
async function* readStdin() { async function* readStdin() {
let buffer = Buffer.alloc(0); let buffer = Buffer.alloc(0);
let streamEnded = false; let streamEnded = false;
process.stdin.on('end', () => { process.stdin.on("end", () => {
streamEnded = true; streamEnded = true;
}); });
process.stdin.on('data', (data) => { process.stdin.on("data", (data) => {
buffer = Buffer.concat([buffer, data]); buffer = Buffer.concat([buffer, data]);
}); });
@ -77,7 +86,7 @@ async function* readStdin() {
sendResponse(makeError(errorMessage)); sendResponse(makeError(errorMessage));
buffer = Buffer.alloc(0); buffer = Buffer.alloc(0);
messageLength = null; messageLength = null;
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
streamEnded = false; streamEnded = false;
} }
@ -88,20 +97,25 @@ async function* readStdin() {
if (messageLength === null) { if (messageLength === null) {
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) { while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
if (streamEnded) { if (streamEnded) {
await handleStreamEnded('Unexpected end of stream: headers not found'); await handleStreamEnded("Unexpected end of stream: headers not found");
continue main_loop; continue main_loop;
} else if (buffer.length > contentLengthHeaderName.length * 10) { } else if (buffer.length > contentLengthHeaderName.length * 10) {
await handleStreamEnded(`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`); await handleStreamEnded(
`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
);
continue main_loop; continue main_loop;
} }
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
} }
const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString('ascii'); const headers = buffer
const contentLengthHeader = headers.split(headerSeparator) .subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
.map(header => header.split(':')) .toString("ascii");
.filter(header => header[2] === undefined) const contentLengthHeader = headers
.filter(header => (header[1] || '').length > 0) .split(headerSeparator)
.find(header => (header[0] || '').trim() === contentLengthHeaderName); .map((header) => header.split(":"))
.filter((header) => header[2] === undefined)
.filter((header) => (header[1] || "").length > 0)
.find((header) => (header[0] || "").trim() === contentLengthHeaderName);
const contentLength = (contentLengthHeader || [])[1]; const contentLength = (contentLengthHeader || [])[1];
if (contentLength === undefined) { if (contentLength === undefined) {
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`); await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
@ -111,13 +125,14 @@ async function* readStdin() {
messageLength = parseInt(contentLength, 10); messageLength = parseInt(contentLength, 10);
} }
while (buffer.length < (headersLength + messageLength)) { while (buffer.length < headersLength + messageLength) {
if (streamEnded) { if (streamEnded) {
await handleStreamEnded( await handleStreamEnded(
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`); `Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
);
continue main_loop; continue main_loop;
} }
await once(process.stdin, 'readable'); await once(process.stdin, "readable");
} }
const messageEnd = headersLength + messageLength; const messageEnd = headersLength + messageLength;
@ -125,12 +140,12 @@ async function* readStdin() {
buffer = buffer.subarray(messageEnd); buffer = buffer.subarray(messageEnd);
headersLength = null; headersLength = null;
messageLength = null; messageLength = null;
yield message.toString('utf8'); yield message.toString("utf8");
} }
} catch (e) { } catch (e) {
sendResponse(makeError(`Error reading stdin: ${e}`)); sendResponse(makeError(`Error reading stdin: ${e}`));
} finally { } finally {
process.stdin.off('data', () => { }); process.stdin.off("data", () => {});
} }
} }
@ -143,7 +158,7 @@ async function handleMessage(message, prettier) {
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`); throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
} }
if (method === 'prettier/format') { if (method === "prettier/format") {
if (params === undefined || params.text === undefined) { if (params === undefined || params.text === undefined) {
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`); throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
} }
@ -153,7 +168,7 @@ async function handleMessage(message, prettier) {
let resolvedConfig = {}; let resolvedConfig = {};
if (params.options.filepath !== undefined) { if (params.options.filepath !== undefined) {
resolvedConfig = await prettier.prettier.resolveConfig(params.options.filepath) || {}; resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
} }
const options = { const options = {
@ -161,21 +176,25 @@ async function handleMessage(message, prettier) {
...resolvedConfig, ...resolvedConfig,
parser: params.options.parser, parser: params.options.parser,
plugins: params.options.plugins, plugins: params.options.plugins,
path: params.options.filepath path: params.options.filepath,
}; };
process.stderr.write(`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${params.options.filepath || ''}' with options: ${JSON.stringify(options)}\n`); process.stderr.write(
`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
params.options.filepath || ""
}' with options: ${JSON.stringify(options)}\n`,
);
const formattedText = await prettier.prettier.format(params.text, options); const formattedText = await prettier.prettier.format(params.text, options);
sendResponse({ id, result: { text: formattedText } }); sendResponse({ id, result: { text: formattedText } });
} else if (method === 'prettier/clear_cache') { } else if (method === "prettier/clear_cache") {
prettier.prettier.clearConfigCache(); prettier.prettier.clearConfigCache();
prettier.config = await prettier.prettier.resolveConfig(prettier.path) || {}; prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
sendResponse({ id, result: null }); sendResponse({ id, result: null });
} else if (method === 'initialize') { } else if (method === "initialize") {
sendResponse({ sendResponse({
id, id,
result: { result: {
"capabilities": {} capabilities: {},
} },
}); });
} else { } else {
throw new Error(`Unknown method: ${method}`); throw new Error(`Unknown method: ${method}`);
@ -185,18 +204,20 @@ async function handleMessage(message, prettier) {
function makeError(message) { function makeError(message) {
return { return {
error: { error: {
"code": -32600, // invalid request code code: -32600, // invalid request code
message, message,
} },
}; };
} }
function sendResponse(response) { function sendResponse(response) {
const responsePayloadString = JSON.stringify({ const responsePayloadString = JSON.stringify({
jsonrpc: "2.0", jsonrpc: "2.0",
...response ...response,
}); });
const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(responsePayloadString)}${headerSeparator}${headerSeparator}`; const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(
responsePayloadString,
)}${headerSeparator}${headerSeparator}`;
process.stdout.write(headers + responsePayloadString); process.stdout.write(headers + responsePayloadString);
} }

View file

@ -4157,7 +4157,9 @@ impl Project {
match prettier_task.await match prettier_task.await
{ {
Ok(prettier) => { Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| File::from_dyn(buffer.file()).map(|f| f.abs_path(cx))); let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
});
format_operation = Some(FormatOperation::Prettier( format_operation = Some(FormatOperation::Prettier(
prettier prettier
.format(buffer, buffer_path, &cx) .format(buffer, buffer_path, &cx)
@ -4213,7 +4215,9 @@ impl Project {
match prettier_task.await match prettier_task.await
{ {
Ok(prettier) => { Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| File::from_dyn(buffer.file()).map(|f| f.abs_path(cx))); let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
});
format_operation = Some(FormatOperation::Prettier( format_operation = Some(FormatOperation::Prettier(
prettier prettier
.format(buffer, buffer_path, &cx) .format(buffer, buffer_path, &cx)

View file

@ -54,7 +54,7 @@ use lsp_command::*;
use node_runtime::NodeRuntime; use node_runtime::NodeRuntime;
use parking_lot::Mutex; use parking_lot::Mutex;
use postage::watch; use postage::watch;
use prettier::{LocateStart, Prettier}; use prettier::Prettier;
use project_settings::{LspSettings, ProjectSettings}; use project_settings::{LspSettings, ProjectSettings};
use rand::prelude::*; use rand::prelude::*;
use search::SearchQuery; use search::SearchQuery;
@ -82,8 +82,11 @@ use std::{
use terminals::Terminals; use terminals::Terminals;
use text::Anchor; use text::Anchor;
use util::{ use util::{
debug_panic, defer, http::HttpClient, merge_json_value_into, debug_panic, defer,
paths::LOCAL_SETTINGS_RELATIVE_PATH, post_inc, ResultExt, TryFutureExt as _, http::HttpClient,
merge_json_value_into,
paths::{DEFAULT_PRETTIER_DIR, LOCAL_SETTINGS_RELATIVE_PATH},
post_inc, ResultExt, TryFutureExt as _,
}; };
pub use fs::*; pub use fs::*;
@ -162,17 +165,15 @@ pub struct Project {
copilot_log_subscription: Option<lsp::Subscription>, copilot_log_subscription: Option<lsp::Subscription>,
current_lsp_settings: HashMap<Arc<str>, LspSettings>, current_lsp_settings: HashMap<Arc<str>, LspSettings>,
node: Option<Arc<dyn NodeRuntime>>, node: Option<Arc<dyn NodeRuntime>>,
#[cfg(not(any(test, feature = "test-support")))]
default_prettier: Option<DefaultPrettier>, default_prettier: Option<DefaultPrettier>,
prettier_instances: HashMap< prettiers_per_worktree: HashMap<WorktreeId, HashSet<Option<PathBuf>>>,
(Option<WorktreeId>, PathBuf), prettier_instances: HashMap<PathBuf, Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>,
>,
} }
#[cfg(not(any(test, feature = "test-support")))]
struct DefaultPrettier { struct DefaultPrettier {
installation_process: Option<Shared<Task<()>>>, instance: Option<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
installation_process: Option<Shared<Task<Result<(), Arc<anyhow::Error>>>>>,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet<&'static str>, installed_plugins: HashSet<&'static str>,
} }
@ -686,8 +687,8 @@ impl Project {
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(), current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(),
node: Some(node), node: Some(node),
#[cfg(not(any(test, feature = "test-support")))]
default_prettier: None, default_prettier: None,
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(), prettier_instances: HashMap::default(),
} }
}) })
@ -789,8 +790,8 @@ impl Project {
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(), current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(),
node: None, node: None,
#[cfg(not(any(test, feature = "test-support")))]
default_prettier: None, default_prettier: None,
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(), prettier_instances: HashMap::default(),
}; };
for worktree in worktrees { for worktree in worktrees {
@ -963,8 +964,7 @@ impl Project {
} }
for (worktree, language, settings) in language_formatters_to_check { for (worktree, language, settings) in language_formatters_to_check {
self.install_default_formatters(worktree, &language, &settings, cx) self.install_default_formatters(worktree, &language, &settings, cx);
.detach_and_log_err(cx);
} }
// Start all the newly-enabled language servers. // Start all the newly-enabled language servers.
@ -2720,20 +2720,7 @@ impl Project {
let buffer_file = File::from_dyn(buffer_file.as_ref()); let buffer_file = File::from_dyn(buffer_file.as_ref());
let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx)); let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx));
let task_buffer = buffer.clone();
let prettier_installation_task =
self.install_default_formatters(worktree, &new_language, &settings, cx); self.install_default_formatters(worktree, &new_language, &settings, cx);
cx.spawn(move |project, mut cx| async move {
prettier_installation_task.await?;
let _ = project
.update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(&task_buffer, cx)
})?
.await;
anyhow::Ok(())
})
.detach_and_log_err(cx);
if let Some(file) = buffer_file { if let Some(file) = buffer_file {
let worktree = file.worktree.clone(); let worktree = file.worktree.clone();
if let Some(tree) = worktree.read(cx).as_local() { if let Some(tree) = worktree.read(cx).as_local() {
@ -4096,7 +4083,7 @@ impl Project {
} }
pub fn format( pub fn format(
&self, &mut self,
buffers: HashSet<Model<Buffer>>, buffers: HashSet<Model<Buffer>>,
push_to_history: bool, push_to_history: bool,
trigger: FormatTrigger, trigger: FormatTrigger,
@ -4116,10 +4103,10 @@ impl Project {
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
cx.spawn(move |this, mut cx| async move { cx.spawn(move |project, mut cx| async move {
// Do not allow multiple concurrent formatting requests for the // Do not allow multiple concurrent formatting requests for the
// same buffer. // same buffer.
this.update(&mut cx, |this, cx| { project.update(&mut cx, |this, cx| {
buffers_with_paths_and_servers.retain(|(buffer, _, _)| { buffers_with_paths_and_servers.retain(|(buffer, _, _)| {
this.buffers_being_formatted this.buffers_being_formatted
.insert(buffer.read(cx).remote_id()) .insert(buffer.read(cx).remote_id())
@ -4127,7 +4114,7 @@ impl Project {
})?; })?;
let _cleanup = defer({ let _cleanup = defer({
let this = this.clone(); let this = project.clone();
let mut cx = cx.clone(); let mut cx = cx.clone();
let buffers = &buffers_with_paths_and_servers; let buffers = &buffers_with_paths_and_servers;
move || { move || {
@ -4195,7 +4182,7 @@ impl Project {
{ {
format_operation = Some(FormatOperation::Lsp( format_operation = Some(FormatOperation::Lsp(
Self::format_via_lsp( Self::format_via_lsp(
&this, &project,
&buffer, &buffer,
buffer_abs_path, buffer_abs_path,
&language_server, &language_server,
@ -4230,7 +4217,7 @@ impl Project {
} }
} }
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => { (Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(prettier_task) = this if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| { .update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx) project.prettier_instance_for_buffer(buffer, cx)
})?.await { })?.await {
@ -4247,16 +4234,35 @@ impl Project {
.context("formatting via prettier")?, .context("formatting via prettier")?,
)); ));
} }
Err(e) => anyhow::bail!( Err(e) => {
"Failed to create prettier instance for buffer during autoformatting: {e:#}" project.update(&mut cx, |project, _| {
), match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
})?;
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
} }
} else if let Some((language_server, buffer_abs_path)) = } else if let Some((language_server, buffer_abs_path)) =
language_server.as_ref().zip(buffer_abs_path.as_ref()) language_server.as_ref().zip(buffer_abs_path.as_ref())
{ {
format_operation = Some(FormatOperation::Lsp( format_operation = Some(FormatOperation::Lsp(
Self::format_via_lsp( Self::format_via_lsp(
&this, &project,
&buffer, &buffer,
buffer_abs_path, buffer_abs_path,
&language_server, &language_server,
@ -4269,7 +4275,7 @@ impl Project {
} }
} }
(Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => { (Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(prettier_task) = this if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| { .update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx) project.prettier_instance_for_buffer(buffer, cx)
})?.await { })?.await {
@ -4286,9 +4292,28 @@ impl Project {
.context("formatting via prettier")?, .context("formatting via prettier")?,
)); ));
} }
Err(e) => anyhow::bail!( Err(e) => {
"Failed to create prettier instance for buffer during formatting: {e:#}" project.update(&mut cx, |project, _| {
), match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
})?;
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
} }
} }
} }
@ -6506,15 +6531,25 @@ impl Project {
"Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}" "Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}"
); );
let prettiers_to_reload = self let prettiers_to_reload = self
.prettier_instances .prettiers_per_worktree
.get(&current_worktree_id)
.iter() .iter()
.filter_map(|((worktree_id, prettier_path), prettier_task)| { .flat_map(|prettier_paths| prettier_paths.iter())
if worktree_id.is_none() || worktree_id == &Some(current_worktree_id) { .flatten()
Some((*worktree_id, prettier_path.clone(), prettier_task.clone())) .filter_map(|prettier_path| {
} else { Some((
None current_worktree_id,
} Some(prettier_path.clone()),
self.prettier_instances.get(prettier_path)?.clone(),
))
}) })
.chain(self.default_prettier.iter().filter_map(|default_prettier| {
Some((
current_worktree_id,
None,
default_prettier.instance.clone()?,
))
}))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
cx.background_executor() cx.background_executor()
@ -6525,9 +6560,14 @@ impl Project {
.clear_cache() .clear_cache()
.await .await
.with_context(|| { .with_context(|| {
format!( match prettier_path {
Some(prettier_path) => format!(
"clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update" "clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update"
) ),
None => format!(
"clearing default prettier cache for worktree {worktree_id:?} on prettier settings update"
),
}
}) })
.map_err(Arc::new) .map_err(Arc::new)
} }
@ -8411,7 +8451,12 @@ impl Project {
&mut self, &mut self,
buffer: &Model<Buffer>, buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<Option<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>> { ) -> Task<
Option<(
Option<PathBuf>,
Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>,
)>,
> {
let buffer = buffer.read(cx); let buffer = buffer.read(cx);
let buffer_file = buffer.file(); let buffer_file = buffer.file();
let Some(buffer_language) = buffer.language() else { let Some(buffer_language) = buffer.language() else {
@ -8421,142 +8466,142 @@ impl Project {
return Task::ready(None); return Task::ready(None);
} }
let buffer_file = File::from_dyn(buffer_file); if self.is_local() {
let buffer_path = buffer_file.map(|file| Arc::clone(file.path()));
let worktree_path = buffer_file
.as_ref()
.and_then(|file| Some(file.worktree.read(cx).abs_path()));
let worktree_id = buffer_file.map(|file| file.worktree_id(cx));
if self.is_local() || worktree_id.is_none() || worktree_path.is_none() {
let Some(node) = self.node.as_ref().map(Arc::clone) else { let Some(node) = self.node.as_ref().map(Arc::clone) else {
return Task::ready(None); return Task::ready(None);
}; };
let fs = self.fs.clone(); match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx)))
cx.spawn(move |this, mut cx| async move { {
let prettier_dir = match cx Some((worktree_id, buffer_path)) => {
let fs = Arc::clone(&self.fs);
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
return cx.spawn(|project, mut cx| async move {
match cx
.background_executor() .background_executor()
.spawn(Prettier::locate( .spawn(async move {
worktree_path.zip(buffer_path).map( Prettier::locate_prettier_installation(
|(worktree_root_path, starting_path)| LocateStart { fs.as_ref(),
worktree_root_path, &installed_prettiers,
starting_path, &buffer_path,
}, )
), .await
fs, })
))
.await .await
{ {
Ok(path) => path, Ok(None) => {
Err(e) => { match project.update(&mut cx, |project, _| {
return Some(
Task::ready(Err(Arc::new(e.context(
"determining prettier path for worktree {worktree_path:?}",
))))
.shared(),
);
}
};
if let Some(existing_prettier) = this
.update(&mut cx, |project, _| {
project project
.prettier_instances .prettiers_per_worktree
.get(&(worktree_id, prettier_dir.clone())) .entry(worktree_id)
.cloned() .or_default()
}) .insert(None);
.ok() project.default_prettier.as_ref().and_then(
.flatten() |default_prettier| default_prettier.instance.clone(),
{ )
return Some(existing_prettier); }) {
Ok(Some(old_task)) => Some((None, old_task)),
Ok(None) => {
match project.update(&mut cx, |_, cx| {
start_default_prettier(node, Some(worktree_id), cx)
}) {
Ok(new_default_prettier) => {
return Some((None, new_default_prettier.await))
}
Err(e) => {
Some((
None,
Task::ready(Err(Arc::new(e.context("project is gone during default prettier startup"))))
.shared(),
))
}
}
}
Err(e) => Some((None, Task::ready(Err(Arc::new(e.context("project is gone during default prettier checks"))))
.shared())),
}
}
Ok(Some(prettier_dir)) => {
match project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(Some(prettier_dir.clone()));
project.prettier_instances.get(&prettier_dir).cloned()
}) {
Ok(Some(existing_prettier)) => {
log::debug!(
"Found already started prettier in {prettier_dir:?}"
);
return Some((Some(prettier_dir), existing_prettier));
}
Err(e) => {
return Some((
Some(prettier_dir),
Task::ready(Err(Arc::new(e.context("project is gone during custom prettier checks"))))
.shared(),
))
}
_ => {},
} }
log::info!("Found prettier in {prettier_dir:?}, starting."); log::info!("Found prettier in {prettier_dir:?}, starting.");
let task_prettier_dir = prettier_dir.clone(); let new_prettier_task =
let new_prettier_task = cx match project.update(&mut cx, |project, cx| {
.spawn({ let new_prettier_task = start_prettier(
let this = this.clone();
move |mut cx| async move {
let new_server_id = this.update(&mut cx, |this, _| {
this.languages.next_language_server_id()
})?;
let prettier = Prettier::start(
worktree_id.map(|id| id.to_usize()),
new_server_id,
task_prettier_dir,
node, node,
cx.clone(), prettier_dir.clone(),
) Some(worktree_id),
.await cx,
.context("prettier start") );
.map_err(Arc::new)?; project.prettier_instances.insert(
log::info!("Started prettier in {:?}", prettier.prettier_dir()); prettier_dir.clone(),
new_prettier_task.clone(),
if let Some(prettier_server) = prettier.server() { );
this.update(&mut cx, |project, cx| { new_prettier_task
let name = if prettier.is_default() { }) {
LanguageServerName(Arc::from("prettier (default)")) Ok(task) => task,
} else { Err(e) => return Some((
let prettier_dir = prettier.prettier_dir(); Some(prettier_dir),
let worktree_path = prettier Task::ready(Err(Arc::new(e.context("project is gone during custom prettier startup"))))
.worktree_id() .shared()
.map(WorktreeId::from_usize) )),
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.read(cx).abs_path());
match worktree_path {
Some(worktree_path) => {
if worktree_path.as_ref() == prettier_dir {
LanguageServerName(Arc::from(format!(
"prettier ({})",
prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default()
)))
} else {
let dir_to_display = match prettier_dir
.strip_prefix(&worktree_path)
.ok()
{
Some(relative_path) => relative_path,
None => prettier_dir,
}; };
LanguageServerName(Arc::from(format!( Some((Some(prettier_dir), new_prettier_task))
"prettier ({})", }
dir_to_display.display(), Err(e) => {
return Some((
None,
Task::ready(Err(Arc::new(
e.context("determining prettier path"),
))) )))
.shared(),
));
} }
} }
None => LanguageServerName(Arc::from(format!( });
"prettier ({})", }
prettier_dir.display(), None => {
))), let started_default_prettier = self
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.instance.clone());
match started_default_prettier {
Some(old_task) => return Task::ready(Some((None, old_task))),
None => {
let new_task = start_default_prettier(node, None, cx);
return cx.spawn(|_, _| async move { Some((None, new_task.await)) });
}
} }
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
})?;
} }
Ok(Arc::new(prettier)).map_err(Arc::new)
} }
})
.shared();
this.update(&mut cx, |project, _| {
project
.prettier_instances
.insert((worktree_id, prettier_dir), new_prettier_task.clone());
})
.ok();
Some(new_prettier_task)
})
} else if self.remote_id().is_some() { } else if self.remote_id().is_some() {
return Task::ready(None); return Task::ready(None);
} else { } else {
Task::ready(Some( Task::ready(Some((
None,
Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(), Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(),
)) )))
} }
} }
@ -8567,8 +8612,7 @@ impl Project {
_: &Language, _: &Language,
_: &LanguageSettings, _: &LanguageSettings,
_: &mut ModelContext<Self>, _: &mut ModelContext<Self>,
) -> Task<anyhow::Result<()>> { ) {
Task::ready(Ok(()))
} }
#[cfg(not(any(test, feature = "test-support")))] #[cfg(not(any(test, feature = "test-support")))]
@ -8578,19 +8622,19 @@ impl Project {
new_language: &Language, new_language: &Language,
language_settings: &LanguageSettings, language_settings: &LanguageSettings,
cx: &mut ModelContext<Self>, cx: &mut ModelContext<Self>,
) -> Task<anyhow::Result<()>> { ) {
match &language_settings.formatter { match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => {} Formatter::Prettier { .. } | Formatter::Auto => {}
Formatter::LanguageServer | Formatter::External { .. } => return Task::ready(Ok(())), Formatter::LanguageServer | Formatter::External { .. } => return,
}; };
let Some(node) = self.node.as_ref().cloned() else { let Some(node) = self.node.as_ref().cloned() else {
return Task::ready(Ok(())); return;
}; };
let mut prettier_plugins = None; let mut prettier_plugins = None;
if new_language.prettier_parser_name().is_some() { if new_language.prettier_parser_name().is_some() {
prettier_plugins prettier_plugins
.get_or_insert_with(|| HashSet::default()) .get_or_insert_with(|| HashSet::<&'static str>::default())
.extend( .extend(
new_language new_language
.lsp_adapters() .lsp_adapters()
@ -8599,112 +8643,285 @@ impl Project {
) )
} }
let Some(prettier_plugins) = prettier_plugins else { let Some(prettier_plugins) = prettier_plugins else {
return Task::ready(Ok(())); return;
}; };
let fs = Arc::clone(&self.fs);
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
self.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
}) {
Some(locate_from) => {
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
cx.background_executor().spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
locate_from.as_ref(),
)
.await
})
}
None => Task::ready(Ok(None)),
};
let mut plugins_to_install = prettier_plugins; let mut plugins_to_install = prettier_plugins;
let (mut install_success_tx, mut install_success_rx) =
futures::channel::mpsc::channel::<HashSet<&'static str>>(1);
let new_installation_process = cx
.spawn(|this, mut cx| async move {
if let Some(installed_plugins) = install_success_rx.next().await {
this.update(&mut cx, |this, _| {
let default_prettier =
this.default_prettier
.get_or_insert_with(|| DefaultPrettier {
installation_process: None,
installed_plugins: HashSet::default(),
});
if !installed_plugins.is_empty() {
log::info!("Installed new prettier plugins: {installed_plugins:?}");
default_prettier.installed_plugins.extend(installed_plugins);
}
})
.ok();
}
})
.shared();
let previous_installation_process = let previous_installation_process =
if let Some(default_prettier) = &mut self.default_prettier { if let Some(default_prettier) = &mut self.default_prettier {
plugins_to_install plugins_to_install
.retain(|plugin| !default_prettier.installed_plugins.contains(plugin)); .retain(|plugin| !default_prettier.installed_plugins.contains(plugin));
if plugins_to_install.is_empty() { if plugins_to_install.is_empty() {
return Task::ready(Ok(())); return;
} }
std::mem::replace( default_prettier.installation_process.clone()
&mut default_prettier.installation_process,
Some(new_installation_process.clone()),
)
} else { } else {
None None
}; };
let default_prettier_dir = util::paths::DEFAULT_PRETTIER_DIR.as_path();
let already_running_prettier = self
.prettier_instances
.get(&(worktree, default_prettier_dir.to_path_buf()))
.cloned();
let fs = Arc::clone(&self.fs); let fs = Arc::clone(&self.fs);
cx.spawn(move |this, mut cx| async move { let default_prettier = self
if let Some(previous_installation_process) = previous_installation_process { .default_prettier
previous_installation_process.await; .get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
installed_plugins: HashSet::default(),
});
default_prettier.installation_process = Some(
cx.spawn(|this, mut cx| async move {
match locate_prettier_installation
.await
.context("locate prettier installation")
.map_err(Arc::new)?
{
Some(_non_default_prettier) => return Ok(()),
None => {
let mut needs_install = match previous_installation_process {
Some(previous_installation_process) => {
previous_installation_process.await.is_err()
} }
let mut everything_was_installed = false; None => true,
};
this.update(&mut cx, |this, _| { this.update(&mut cx, |this, _| {
match &mut this.default_prettier { if let Some(default_prettier) = &mut this.default_prettier {
Some(default_prettier) => { plugins_to_install.retain(|plugin| {
plugins_to_install !default_prettier.installed_plugins.contains(plugin)
.retain(|plugin| !default_prettier.installed_plugins.contains(plugin)); });
everything_was_installed = plugins_to_install.is_empty(); needs_install |= !plugins_to_install.is_empty();
},
None => this.default_prettier = Some(DefaultPrettier { installation_process: Some(new_installation_process), installed_plugins: HashSet::default() }),
} }
})?; })?;
if everything_was_installed { if needs_install {
return Ok(()); let installed_plugins = plugins_to_install.clone();
cx.background_executor()
.spawn(async move {
install_default_prettier(plugins_to_install, node, fs).await
})
.await
.context("prettier & plugins install")
.map_err(Arc::new)?;
this.update(&mut cx, |this, _| {
let default_prettier =
this.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: Some(
Task::ready(Ok(())).shared(),
),
installed_plugins: HashSet::default(),
});
default_prettier.instance = None;
default_prettier.installed_plugins.extend(installed_plugins);
})?;
}
}
}
Ok(())
})
.shared(),
);
}
} }
cx.spawn(move |_| async move { fn start_default_prettier(
let prettier_wrapper_path = default_prettier_dir.join(prettier::PRETTIER_SERVER_FILE); node: Arc<dyn NodeRuntime>,
// method creates parent directory if it doesn't exist worktree_id: Option<WorktreeId>,
fs.save(&prettier_wrapper_path, &text::Rope::from(prettier::PRETTIER_SERVER_JS), text::LineEnding::Unix).await cx: &mut ModelContext<'_, Project>,
.with_context(|| format!("writing {} file at {prettier_wrapper_path:?}", prettier::PRETTIER_SERVER_FILE))?; ) -> Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>> {
cx.spawn(|project, mut cx| async move {
loop {
let default_prettier_installing = match project.update(&mut cx, |project, _| {
project
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.installation_process.clone())
}) {
Ok(installation) => installation,
Err(e) => {
return Task::ready(Err(Arc::new(
e.context("project is gone during default prettier installation"),
)))
.shared()
}
};
match default_prettier_installing {
Some(installation_task) => {
if installation_task.await.is_ok() {
break;
}
}
None => break,
}
}
let packages_to_versions = future::try_join_all( match project.update(&mut cx, |project, cx| {
plugins_to_install match project
.iter() .default_prettier
.chain(Some(&"prettier")) .as_mut()
.map(|package_name| async { .and_then(|default_prettier| default_prettier.instance.as_mut())
{
Some(default_prettier) => default_prettier.clone(),
None => {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project
.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet::default(),
})
.instance = Some(new_default_prettier.clone());
new_default_prettier
}
}
}) {
Ok(task) => task,
Err(e) => Task::ready(Err(Arc::new(
e.context("project is gone during default prettier startup"),
)))
.shared(),
}
})
}
fn start_prettier(
node: Arc<dyn NodeRuntime>,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>> {
cx.spawn(|project, mut cx| async move {
let new_server_id = project.update(&mut cx, |project, _| {
project.languages.next_language_server_id()
})?;
let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone())
.await
.context("default prettier spawn")
.map(Arc::new)
.map_err(Arc::new)?;
register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx);
Ok(new_prettier)
})
.shared()
}
fn register_new_prettier(
project: &WeakModel<Project>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) {
let prettier_dir = prettier.prettier_dir();
let is_default = prettier.is_default();
if is_default {
log::info!("Started default prettier in {prettier_dir:?}");
} else {
log::info!("Started prettier in {prettier_dir:?}");
}
if let Some(prettier_server) = prettier.server() {
project
.update(cx, |project, cx| {
let name = if is_default {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let worktree_path = worktree_id
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path()));
let name = match worktree_path {
Some(worktree_path) => {
if prettier_dir == worktree_path.as_ref() {
let name = prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
format!("prettier ({name})")
} else {
let dir_to_display = prettier_dir
.strip_prefix(worktree_path.as_ref())
.ok()
.unwrap_or(prettier_dir);
format!("prettier ({})", dir_to_display.display())
}
}
None => format!("prettier ({})", prettier_dir.display()),
};
LanguageServerName(Arc::from(name))
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
})
.ok();
}
}
#[cfg(not(any(test, feature = "test-support")))]
async fn install_default_prettier(
plugins_to_install: HashSet<&'static str>,
node: Arc<dyn NodeRuntime>,
fs: Arc<dyn Fs>,
) -> anyhow::Result<()> {
let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE);
// method creates parent directory if it doesn't exist
fs.save(
&prettier_wrapper_path,
&text::Rope::from(prettier::PRETTIER_SERVER_JS),
text::LineEnding::Unix,
)
.await
.with_context(|| {
format!(
"writing {} file at {prettier_wrapper_path:?}",
prettier::PRETTIER_SERVER_FILE
)
})?;
let packages_to_versions =
future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map(
|package_name| async {
let returned_package_name = package_name.to_string(); let returned_package_name = package_name.to_string();
let latest_version = node.npm_package_latest_version(package_name) let latest_version = node
.npm_package_latest_version(package_name)
.await .await
.with_context(|| { .with_context(|| {
format!("fetching latest npm version for package {returned_package_name}") format!("fetching latest npm version for package {returned_package_name}")
})?; })?;
anyhow::Ok((returned_package_name, latest_version)) anyhow::Ok((returned_package_name, latest_version))
}), },
) ))
.await .await
.context("fetching latest npm versions")?; .context("fetching latest npm versions")?;
log::info!("Fetching default prettier and plugins: {packages_to_versions:?}"); log::info!("Fetching default prettier and plugins: {packages_to_versions:?}");
let borrowed_packages = packages_to_versions.iter().map(|(package, version)| { let borrowed_packages = packages_to_versions
(package.as_str(), version.as_str()) .iter()
}).collect::<Vec<_>>(); .map(|(package, version)| (package.as_str(), version.as_str()))
node.npm_install_packages(default_prettier_dir, &borrowed_packages).await.context("fetching formatter packages")?; .collect::<Vec<_>>();
let installed_packages = !plugins_to_install.is_empty(); node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages)
install_success_tx.try_send(plugins_to_install).ok(); .await
.context("fetching formatter packages")?;
if !installed_packages {
if let Some(prettier) = already_running_prettier {
prettier.await.map_err(|e| anyhow::anyhow!("Default prettier startup await failure: {e:#}"))?.clear_cache().await.context("clearing default prettier cache after plugins install")?;
}
}
anyhow::Ok(()) anyhow::Ok(())
}).await
})
}
} }
fn subscribe_for_copilot_events( fn subscribe_for_copilot_events(

View file

@ -1,4 +1,4 @@
use crate::{search::PathMatcher, Event, *}; use crate::{Event, *};
use fs::FakeFs; use fs::FakeFs;
use futures::{future, StreamExt}; use futures::{future, StreamExt};
use gpui::AppContext; use gpui::AppContext;
@ -13,7 +13,7 @@ use pretty_assertions::assert_eq;
use serde_json::json; use serde_json::json;
use std::{os, task::Poll}; use std::{os, task::Poll};
use unindent::Unindent as _; use unindent::Unindent as _;
use util::{assert_set_eq, test::temp_tree}; use util::{assert_set_eq, paths::PathMatcher, test::temp_tree};
#[gpui::test] #[gpui::test]
async fn test_block_via_channel(cx: &mut gpui::TestAppContext) { async fn test_block_via_channel(cx: &mut gpui::TestAppContext) {

View file

@ -1,7 +1,6 @@
use aho_corasick::{AhoCorasick, AhoCorasickBuilder}; use aho_corasick::{AhoCorasick, AhoCorasickBuilder};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use client::proto; use client::proto;
use globset::{Glob, GlobMatcher};
use itertools::Itertools; use itertools::Itertools;
use language::{char_kind, BufferSnapshot}; use language::{char_kind, BufferSnapshot};
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
@ -10,9 +9,10 @@ use std::{
borrow::Cow, borrow::Cow,
io::{BufRead, BufReader, Read}, io::{BufRead, BufReader, Read},
ops::Range, ops::Range,
path::{Path, PathBuf}, path::Path,
sync::Arc, sync::Arc,
}; };
use util::paths::PathMatcher;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SearchInputs { pub struct SearchInputs {
@ -52,31 +52,6 @@ pub enum SearchQuery {
}, },
} }
#[derive(Clone, Debug)]
pub struct PathMatcher {
maybe_path: PathBuf,
glob: GlobMatcher,
}
impl std::fmt::Display for PathMatcher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.maybe_path.to_string_lossy().fmt(f)
}
}
impl PathMatcher {
pub fn new(maybe_glob: &str) -> Result<Self, globset::Error> {
Ok(PathMatcher {
glob: Glob::new(&maybe_glob)?.compile_matcher(),
maybe_path: PathBuf::from(maybe_glob),
})
}
pub fn is_match<P: AsRef<Path>>(&self, other: P) -> bool {
other.as_ref().starts_with(&self.maybe_path) || self.glob.is_match(other)
}
}
impl SearchQuery { impl SearchQuery {
pub fn text( pub fn text(
query: impl ToString, query: impl ToString,