Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Use proper NodeRuntime in the formatter interface
Kirill Bulatova8387b8b19e7
diff --git a/Cargo.lock b/Cargo.lockindex 9761e040d37..de339e15467 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -5526,6 +5526,7 @@ dependencies = ["futures 0.3.28","gpui","language",+ "node_runtime","serde","serde_derive","serde_json",@@ -5643,6 +5644,7 @@ dependencies = ["lazy_static","log","lsp",+ "node_runtime","parking_lot 0.11.2","postage","prettier",diff --git a/crates/prettier/Cargo.toml b/crates/prettier/Cargo.tomlindex 77a845b5b38..2b8bf99fc2c 100644--- a/crates/prettier/Cargo.toml+++ b/crates/prettier/Cargo.toml@@ -10,6 +10,7 @@ path = "src/prettier.rs"language = { path = "../language" }gpui = { path = "../gpui" }fs = { path = "../fs" }+node_runtime = { path = "../node_runtime"}serde.workspace = trueserde_derive.workspace = truediff --git a/crates/prettier/prettier_server/.gitignore b/crates/prettier/prettier_server/.gitignorenew file mode 100644index 00000000000..c2658d7d1b3--- /dev/null+++ b/crates/prettier/prettier_server/.gitignore@@ -0,0 +1 @@+node_modules/diff --git a/crates/prettier/prettier_server/package-lock.json b/crates/prettier/prettier_server/package-lock.jsonnew file mode 100644index 00000000000..6d495d72b47--- /dev/null+++ b/crates/prettier/prettier_server/package-lock.json@@ -0,0 +1,29 @@+{+ "name": "prettier_server",+ "version": "1.0.0",+ "lockfileVersion": 3,+ "requires": true,+ "packages": {+ "": {+ "name": "prettier_server",+ "version": "1.0.0",+ "dependencies": {+ "prettier": "^3.0.3"+ }+ },+ "node_modules/prettier": {+ "version": "3.0.3",+ "resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz",+ "integrity": "sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg==",+ "bin": {+ "prettier": "bin/prettier.cjs"+ },+ "engines": {+ "node": ">=14"+ },+ "funding": {+ "url": "https://github.com/prettier/prettier?sponsor=1"+ }+ }+ }+}diff --git a/crates/prettier/prettier_server/package.json b/crates/prettier/prettier_server/package.jsonindex 70c834b37e1..599f308e9f1 100644--- a/crates/prettier/prettier_server/package.json+++ b/crates/prettier/prettier_server/package.json@@ -1,11 +1,14 @@{- "name": "prettier_server",- "version": "1.0.0",- "description": "",- "main": "src/index.js",- "scripts": {- "start": "node src/index.js",- "test": "echo \"Error: no test specified\" && exit 1"- },- "author": "Zed Industries"+ "name": "prettier_server",+ "version": "1.0.0",+ "description": "",+ "main": "src/index.js",+ "scripts": {+ "start": "node src/index.js",+ "test": "echo \"Error: no test specified\" && exit 1"+ },+ "author": "Zed Industries",+ "dependencies": {+ "prettier": "^3.0"+ }}diff --git a/crates/prettier/prettier_server/src/index.js b/crates/prettier/prettier_server/src/index.jsindex c8f2f3bb37a..5ac35d7ef9c 100644--- a/crates/prettier/prettier_server/src/index.js+++ b/crates/prettier/prettier_server/src/index.js@@ -17,6 +17,7 @@ function handleData() {const length = buffer.readUInt32LE(0);console.log(length);+ console.log(buffer.toString());if (buffer.length < 4 + length) {return;}diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rsindex ca53bb7a03a..efb3a0fcf5e 100644--- a/crates/prettier/src/prettier.rs+++ b/crates/prettier/src/prettier.rs@@ -1,18 +1,17 @@use std::collections::{HashMap, VecDeque};-pub use std::path::{Path, PathBuf};-pub use std::sync::Arc;+use std::path::{Path, PathBuf};+use std::sync::Arc;use anyhow::Context;use fs::Fs;use gpui::ModelHandle;use language::{Buffer, Diff};+use node_runtime::NodeRuntime;pub struct Prettier {_private: (),}-pub struct NodeRuntime;-#[derive(Debug)]pub struct LocateStart {pub worktree_root_path: Arc<Path>,@@ -143,7 +142,7 @@ impl Prettier {}}- pub async fn start(prettier_path: &Path, node: Arc<NodeRuntime>) -> anyhow::Result<Self> {+ pub async fn start(prettier_path: &Path, node: Arc<dyn NodeRuntime>) -> anyhow::Result<Self> {todo!()}diff --git a/crates/project/Cargo.toml b/crates/project/Cargo.tomlindex c22193f1309..9f505c3fd2d 100644--- a/crates/project/Cargo.toml+++ b/crates/project/Cargo.toml@@ -31,6 +31,7 @@ git = { path = "../git" }gpui = { path = "../gpui" }language = { path = "../language" }lsp = { path = "../lsp" }+node_runtime = { path = "../node_runtime" }prettier = { path = "../prettier" }rpc = { path = "../rpc" }settings = { path = "../settings" }diff --git a/crates/project/src/project.rs b/crates/project/src/project.rsindex 6538a265400..b7fc1b8b34c 100644--- a/crates/project/src/project.rs+++ b/crates/project/src/project.rs@@ -49,8 +49,9 @@ use lsp::{DocumentHighlightKind, LanguageServer, LanguageServerBinary, LanguageServerId, OneOf,};use lsp_command::*;+use node_runtime::NodeRuntime;use postage::watch;-use prettier::{LocateStart, NodeRuntime, Prettier};+use prettier::{LocateStart, Prettier};use project_settings::{LspSettings, ProjectSettings};use rand::prelude::*;use search::SearchQuery;@@ -71,7 +72,7 @@ use std::{str,sync::{atomic::{AtomicUsize, Ordering::SeqCst},- Arc,+ Arc, OnceLock,},time::{Duration, Instant},};@@ -553,14 +554,27 @@ impl SearchMatchCandidate {}}+static NODE_RUNTIME: OnceLock<Arc<dyn NodeRuntime>> = OnceLock::new();+impl Project {pub fn init_settings(cx: &mut AppContext) {settings::register::<ProjectSettings>(cx);}- pub fn init(client: &Arc<Client>, cx: &mut AppContext) {+ pub fn init(+ client: &Arc<Client>,+ node_runtime: Option<Arc<dyn NodeRuntime>>,+ cx: &mut AppContext,+ ) {Self::init_settings(cx);+ // TODO kb move it to Project::local and other constructors?+ if let Some(node_runtime) = node_runtime {+ NODE_RUNTIME+ .set(node_runtime)+ .unwrap_or_else(|_| panic!("multiple init calls tried to set node runtime"));+ }+client.add_model_message_handler(Self::handle_add_collaborator);client.add_model_message_handler(Self::handle_update_project_collaborator);client.add_model_message_handler(Self::handle_remove_collaborator);@@ -8187,6 +8201,7 @@ impl Project {buffer: &ModelHandle<Buffer>,cx: &mut ModelContext<Self>,) -> Option<Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>> {+ let node_runtime = Arc::clone(NODE_RUNTIME.get()?);let buffer_file = File::from_dyn(buffer.read(cx).file());let buffer_path = buffer_file.map(|file| Arc::clone(file.path()));let worktree_path = buffer_file@@ -8253,13 +8268,12 @@ impl Project {return existing_prettier;}- let task_node_runtime = Arc::new(NodeRuntime);let task_prettier_path = prettier_path.clone();let new_prettier_task = cx.background().spawn(async move {Ok(Arc::new(- Prettier::start(&task_prettier_path, task_node_runtime)+ Prettier::start(&task_prettier_path, node_runtime).await.with_context(|| {format!("starting new prettier for path {task_prettier_path:?}")diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rsindex f89a880c715..8093632d3a4 100644--- a/crates/zed/src/main.rs+++ b/crates/zed/src/main.rs@@ -138,7 +138,7 @@ fn main() {theme::init(Assets, cx);context_menu::init(cx);- project::Project::init(&client, cx);+ project::Project::init(&client, Some(Arc::clone(&node_runtime)), cx);client::init(&client, cx);command_palette::init(cx);language::init(cx);