diff --git a/Cargo.lock b/Cargo.lock index cd36221de0..8b1278b3dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5517,6 +5517,10 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "prettier" +version = "0.1.0" + [[package]] name = "pretty_assertions" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index 7dae3bd81f..25aec39cdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,6 +52,7 @@ members = [ "crates/plugin", "crates/plugin_macros", "crates/plugin_runtime", + "crates/prettier", "crates/project", "crates/project_panel", "crates/project_symbols", diff --git a/crates/prettier/Cargo.toml b/crates/prettier/Cargo.toml new file mode 100644 index 0000000000..798899df0e --- /dev/null +++ b/crates/prettier/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "prettier" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[lib] +path = "src/prettier.rs" diff --git a/crates/prettier/prettier_server/.zed/settings.json b/crates/prettier/prettier_server/.zed/settings.json new file mode 100644 index 0000000000..856536c62d --- /dev/null +++ b/crates/prettier/prettier_server/.zed/settings.json @@ -0,0 +1,7 @@ +{ + "languages": { + "JavaScript": { + "tab_size": 4 + } + } +} diff --git a/crates/prettier/prettier_server/package.json b/crates/prettier/prettier_server/package.json new file mode 100644 index 0000000000..70c834b37e --- /dev/null +++ b/crates/prettier/prettier_server/package.json @@ -0,0 +1,11 @@ +{ + "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" +} diff --git a/crates/prettier/prettier_server/src/index.js b/crates/prettier/prettier_server/src/index.js new file mode 100644 index 0000000000..c8f2f3bb37 --- /dev/null +++ b/crates/prettier/prettier_server/src/index.js @@ -0,0 +1,56 @@ +const { Buffer } = require('buffer'); + +let buffer = Buffer.alloc(0); +process.stdin.resume(); +process.stdin.on('data', (data) => { + buffer = Buffer.concat([buffer, data]); + handleData(); +}); +process.stdin.on('end', () => { + handleData(); +}); + +function handleData() { + if (buffer.length < 4) { + return; + } + + const length = buffer.readUInt32LE(0); + console.log(length); + if (buffer.length < 4 + length) { + return; + } + + const bytes = buffer.subarray(4, 4 + length); + buffer = buffer.subarray(4 + length); + + try { + const message = JSON.parse(bytes); + handleMessage(message); + } catch (_) { + sendResponse(makeError("Request JSON parse error")); + return; + } +} + +// format +// clear_cache +// shutdown +// error + +function handleMessage(message) { + console.log(message); + sendResponse({ method: "hi", result: null }); +} + +function makeError(message) { + return { method: "error", message }; +} + +function sendResponse(response) { + let message = Buffer.from(JSON.stringify(response)); + let length = Buffer.alloc(4); + length.writeUInt32LE(message.length); + process.stdout.write(length); + process.stdout.write(message); +} diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rs new file mode 100644 index 0000000000..7d12d9af81 --- /dev/null +++ b/crates/prettier/src/prettier.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +}