Draft prettier_server formatting
This commit is contained in:
parent
dca93fb177
commit
2a68f01402
5 changed files with 139 additions and 59 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -5527,6 +5527,7 @@ dependencies = [
|
||||||
"futures 0.3.28",
|
"futures 0.3.28",
|
||||||
"gpui",
|
"gpui",
|
||||||
"language",
|
"language",
|
||||||
|
"lsp",
|
||||||
"node_runtime",
|
"node_runtime",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
|
|
|
@ -10,6 +10,7 @@ path = "src/prettier.rs"
|
||||||
language = { path = "../language" }
|
language = { path = "../language" }
|
||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
fs = { path = "../fs" }
|
fs = { path = "../fs" }
|
||||||
|
lsp = { path = "../lsp" }
|
||||||
node_runtime = { path = "../node_runtime"}
|
node_runtime = { path = "../node_runtime"}
|
||||||
util = { path = "../util" }
|
util = { path = "../util" }
|
||||||
|
|
||||||
|
|
|
@ -4,12 +4,15 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use fs::Fs;
|
use fs::Fs;
|
||||||
use gpui::ModelHandle;
|
use gpui::{AsyncAppContext, ModelHandle, Task};
|
||||||
use language::{Buffer, Diff};
|
use language::{Buffer, Diff};
|
||||||
|
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use util::paths::DEFAULT_PRETTIER_DIR;
|
||||||
|
|
||||||
pub struct Prettier {
|
pub struct Prettier {
|
||||||
_private: (),
|
server: Arc<LanguageServer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -18,7 +21,9 @@ pub struct LocateStart {
|
||||||
pub starting_path: Arc<Path>,
|
pub starting_path: Arc<Path>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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";
|
||||||
|
|
||||||
impl Prettier {
|
impl Prettier {
|
||||||
// This was taken from the prettier-vscode extension.
|
// This was taken from the prettier-vscode extension.
|
||||||
|
@ -141,16 +146,55 @@ impl Prettier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn start(prettier_dir: &Path, node: Arc<dyn NodeRuntime>) -> anyhow::Result<Self> {
|
pub fn start(
|
||||||
|
prettier_dir: PathBuf,
|
||||||
|
node: Arc<dyn NodeRuntime>,
|
||||||
|
cx: AsyncAppContext,
|
||||||
|
) -> Task<anyhow::Result<Self>> {
|
||||||
|
cx.spawn(|cx| async move {
|
||||||
anyhow::ensure!(
|
anyhow::ensure!(
|
||||||
prettier_dir.is_dir(),
|
prettier_dir.is_dir(),
|
||||||
"Prettier dir {prettier_dir:?} is not a directory"
|
"Prettier dir {prettier_dir:?} is not a directory"
|
||||||
);
|
);
|
||||||
anyhow::bail!("TODO kb: start prettier server in {prettier_dir:?}")
|
let prettier_server = DEFAULT_PRETTIER_DIR.join(PRETTIER_SERVER_FILE);
|
||||||
|
anyhow::ensure!(
|
||||||
|
prettier_server.is_file(),
|
||||||
|
"no prettier server package found at {prettier_server:?}"
|
||||||
|
);
|
||||||
|
|
||||||
|
let node_path = node.binary_path().await?;
|
||||||
|
let server = LanguageServer::new(
|
||||||
|
LanguageServerId(0),
|
||||||
|
LanguageServerBinary {
|
||||||
|
path: node_path,
|
||||||
|
arguments: vec![prettier_server.into(), prettier_dir.into()],
|
||||||
|
},
|
||||||
|
Path::new("/"),
|
||||||
|
None,
|
||||||
|
cx,
|
||||||
|
)
|
||||||
|
.context("prettier server creation")?;
|
||||||
|
let server = server
|
||||||
|
.initialize(None)
|
||||||
|
.await
|
||||||
|
.context("prettier server initialization")?;
|
||||||
|
Ok(Self { server })
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn format(&self, buffer: &ModelHandle<Buffer>) -> anyhow::Result<Diff> {
|
pub async fn format(
|
||||||
todo!()
|
&self,
|
||||||
|
buffer: &ModelHandle<Buffer>,
|
||||||
|
cx: &AsyncAppContext,
|
||||||
|
) -> anyhow::Result<Diff> {
|
||||||
|
let buffer_text = buffer.read_with(cx, |buffer, _| buffer.text());
|
||||||
|
let response = self
|
||||||
|
.server
|
||||||
|
.request::<PrettierFormat>(PrettierFormatParams { text: buffer_text })
|
||||||
|
.await
|
||||||
|
.context("prettier format request")?;
|
||||||
|
dbg!("Formatted text", response.text);
|
||||||
|
anyhow::bail!("TODO kb calculate the diff")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn clear_cache(&self) -> anyhow::Result<()> {
|
pub async fn clear_cache(&self) -> anyhow::Result<()> {
|
||||||
|
@ -158,7 +202,6 @@ impl Prettier {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const PRETTIER_PACKAGE_NAME: &str = "prettier";
|
|
||||||
async fn find_closest_prettier_dir(
|
async fn find_closest_prettier_dir(
|
||||||
paths_to_check: Vec<PathBuf>,
|
paths_to_check: Vec<PathBuf>,
|
||||||
fs: &dyn Fs,
|
fs: &dyn Fs,
|
||||||
|
@ -206,3 +249,23 @@ async fn find_closest_prettier_dir(
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum PrettierFormat {}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct PrettierFormatParams {
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct PrettierFormatResult {
|
||||||
|
text: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl lsp::request::Request for PrettierFormat {
|
||||||
|
type Params = PrettierFormatParams;
|
||||||
|
type Result = PrettierFormatResult;
|
||||||
|
const METHOD: &'static str = "prettier/format";
|
||||||
|
}
|
||||||
|
|
|
@ -5,17 +5,17 @@ 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) {
|
||||||
console.error(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path`);
|
sendResponse(makeError(`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path`));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
fs.stat(prettierContainerPath, (err, stats) => {
|
fs.stat(prettierContainerPath, (err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(`Path '${prettierContainerPath}' does not exist.`);
|
sendResponse(makeError(`Path '${prettierContainerPath}' does not exist.`));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
console.log(`Path '${prettierContainerPath}' exists but is not a directory.`);
|
sendResponse(makeError(`Path '${prettierContainerPath}' exists but is not a directory.`));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -26,19 +26,19 @@ const prettierPath = path.join(prettierContainerPath, 'node_modules/prettier');
|
||||||
let prettier;
|
let prettier;
|
||||||
try {
|
try {
|
||||||
prettier = await loadPrettier(prettierPath);
|
prettier = await loadPrettier(prettierPath);
|
||||||
} catch (error) {
|
} catch (e) {
|
||||||
console.error("Failed to load prettier: ", error);
|
sendResponse(makeError(`Failed to load prettier: ${e}`));
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
console.log("Prettier loadded successfully.");
|
sendResponse(makeError("Prettier loadded successfully."));
|
||||||
process.stdin.resume();
|
process.stdin.resume();
|
||||||
handleBuffer(prettier);
|
handleBuffer(prettier);
|
||||||
})()
|
})()
|
||||||
|
|
||||||
async function handleBuffer(prettier) {
|
async function handleBuffer(prettier) {
|
||||||
for await (let messageText of readStdin()) {
|
for await (let messageText of readStdin()) {
|
||||||
handleData(messageText, prettier).catch(e => {
|
handleMessage(messageText, prettier).catch(e => {
|
||||||
console.error("Failed to handle formatter request", e);
|
sendResponse(makeError(`error during message handling: ${e}`));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,43 +107,63 @@ async function* readStdin() {
|
||||||
yield message.toString('utf8');
|
yield message.toString('utf8');
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(`Error reading stdin: ${e}`);
|
sendResponse(makeError(`Error reading stdin: ${e}`));
|
||||||
} finally {
|
} finally {
|
||||||
process.stdin.off('data', () => { });
|
process.stdin.off('data', () => { });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleData(messageText, prettier) {
|
// ?
|
||||||
try {
|
// shutdown
|
||||||
|
// error
|
||||||
|
async function handleMessage(messageText, prettier) {
|
||||||
const message = JSON.parse(messageText);
|
const message = JSON.parse(messageText);
|
||||||
await handleMessage(prettier, message);
|
const { method, id, params } = message;
|
||||||
} catch (e) {
|
if (method === undefined) {
|
||||||
sendResponse(makeError(`Request JSON parse error: ${e}`));
|
throw new Error(`Message method is undefined: ${messageText}`);
|
||||||
|
}
|
||||||
|
if (id === undefined) {
|
||||||
|
throw new Error(`Message id is undefined: ${messageText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method === 'prettier/format') {
|
||||||
|
if (params === undefined || params.text === undefined) {
|
||||||
|
throw new Error(`Message params.text is undefined: ${messageText}`);
|
||||||
|
}
|
||||||
|
let formattedText = await prettier.format(params.text);
|
||||||
|
sendResponse({ id, result: { text: formattedText } });
|
||||||
|
} else if (method === 'prettier/clear_cache') {
|
||||||
|
prettier.clearConfigCache();
|
||||||
|
sendResponse({ id, result: null });
|
||||||
|
} else if (method === 'initialize') {
|
||||||
|
sendResponse({
|
||||||
|
id,
|
||||||
|
result: {
|
||||||
|
"capabilities": {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error(`Unknown method: ${method}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// format
|
|
||||||
// clear_cache
|
|
||||||
//
|
|
||||||
// shutdown
|
|
||||||
// error
|
|
||||||
|
|
||||||
async function handleMessage(prettier, message) {
|
|
||||||
// TODO kb handle message.method, message.params and message.id
|
|
||||||
console.log(`message: ${JSON.stringify(message)}`);
|
|
||||||
sendResponse({ method: "hi", result: null });
|
|
||||||
}
|
|
||||||
|
|
||||||
function makeError(message) {
|
function makeError(message) {
|
||||||
return { method: "error", message };
|
return {
|
||||||
|
error: {
|
||||||
|
"code": -32600, // invalid request code
|
||||||
|
message,
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendResponse(response) {
|
function sendResponse(response) {
|
||||||
const message = Buffer.from(JSON.stringify(response));
|
let responsePayloadString = JSON.stringify({
|
||||||
const length = Buffer.alloc(4);
|
jsonrpc: "2.0",
|
||||||
length.writeUInt32LE(message.length);
|
...response
|
||||||
process.stdout.write(length);
|
});
|
||||||
process.stdout.write(message);
|
let headers = `Content-Length: ${Buffer.byteLength(responsePayloadString)}\r\n\r\n`;
|
||||||
|
let dataToSend = headers + responsePayloadString;
|
||||||
|
process.stdout.write(dataToSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadPrettier(prettierPath) {
|
function loadPrettier(prettierPath) {
|
||||||
|
|
|
@ -53,7 +53,7 @@ use lsp::{
|
||||||
use lsp_command::*;
|
use lsp_command::*;
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
use postage::watch;
|
use postage::watch;
|
||||||
use prettier::{LocateStart, Prettier};
|
use prettier::{LocateStart, Prettier, PRETTIER_SERVER_FILE, PRETTIER_SERVER_JS};
|
||||||
use project_settings::{LspSettings, ProjectSettings};
|
use project_settings::{LspSettings, ProjectSettings};
|
||||||
use rand::prelude::*;
|
use rand::prelude::*;
|
||||||
use search::SearchQuery;
|
use search::SearchQuery;
|
||||||
|
@ -4138,7 +4138,7 @@ impl Project {
|
||||||
Ok(prettier) => {
|
Ok(prettier) => {
|
||||||
format_operation = Some(FormatOperation::Prettier(
|
format_operation = Some(FormatOperation::Prettier(
|
||||||
prettier
|
prettier
|
||||||
.format(buffer)
|
.format(buffer, &cx)
|
||||||
.await
|
.await
|
||||||
.context("formatting via prettier")?,
|
.context("formatting via prettier")?,
|
||||||
));
|
));
|
||||||
|
@ -4176,7 +4176,7 @@ impl Project {
|
||||||
Ok(prettier) => {
|
Ok(prettier) => {
|
||||||
format_operation = Some(FormatOperation::Prettier(
|
format_operation = Some(FormatOperation::Prettier(
|
||||||
prettier
|
prettier
|
||||||
.format(buffer)
|
.format(buffer, &cx)
|
||||||
.await
|
.await
|
||||||
.context("formatting via prettier")?,
|
.context("formatting via prettier")?,
|
||||||
));
|
));
|
||||||
|
@ -8283,17 +8283,11 @@ impl Project {
|
||||||
return existing_prettier;
|
return existing_prettier;
|
||||||
}
|
}
|
||||||
|
|
||||||
let task_prettier_dir = prettier_dir.clone();
|
let start_task = Prettier::start(prettier_dir.clone(), node, cx.clone());
|
||||||
let new_prettier_task = cx
|
let new_prettier_task = cx
|
||||||
.background()
|
.background()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
Ok(Arc::new(
|
Ok(Arc::new(start_task.await.context("starting new prettier")?))
|
||||||
Prettier::start(&task_prettier_dir, node)
|
|
||||||
.await
|
|
||||||
.with_context(|| {
|
|
||||||
format!("starting new prettier for path {task_prettier_dir:?}")
|
|
||||||
})?,
|
|
||||||
))
|
|
||||||
.map_err(Arc::new)
|
.map_err(Arc::new)
|
||||||
})
|
})
|
||||||
.shared();
|
.shared();
|
||||||
|
@ -8344,16 +8338,17 @@ impl Project {
|
||||||
.get(&(worktree, default_prettier_dir.to_path_buf()))
|
.get(&(worktree, default_prettier_dir.to_path_buf()))
|
||||||
{
|
{
|
||||||
// TODO kb need to compare plugins, install missing and restart prettier
|
// TODO kb need to compare plugins, install missing and restart prettier
|
||||||
|
// TODO kb move the entire prettier init logic into prettier.rs
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let fs = Arc::clone(&self.fs);
|
let fs = Arc::clone(&self.fs);
|
||||||
cx.background()
|
cx.background()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
let prettier_wrapper_path = default_prettier_dir.join("prettier_server.js");
|
let prettier_wrapper_path = default_prettier_dir.join(PRETTIER_SERVER_FILE);
|
||||||
// method creates parent directory if it doesn't exist
|
// method creates parent directory if it doesn't exist
|
||||||
fs.save(&prettier_wrapper_path, &Rope::from(prettier::PRETTIER_SERVER_JS), LineEnding::Unix).await
|
fs.save(&prettier_wrapper_path, &Rope::from(PRETTIER_SERVER_JS), LineEnding::Unix).await
|
||||||
.with_context(|| format!("writing prettier_server.js file at {prettier_wrapper_path:?}"))?;
|
.with_context(|| format!("writing {PRETTIER_SERVER_FILE} file at {prettier_wrapper_path:?}"))?;
|
||||||
|
|
||||||
let packages_to_versions = future::try_join_all(
|
let packages_to_versions = future::try_join_all(
|
||||||
prettier_plugins
|
prettier_plugins
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue