mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-24 17:28:40 +00:00
Add more parameters into prettier invocations
This commit is contained in:
parent
2a68f01402
commit
6a8e3fd02d
2 changed files with 26 additions and 10 deletions
|
@ -187,10 +187,15 @@ impl Prettier {
|
|||
buffer: &ModelHandle<Buffer>,
|
||||
cx: &AsyncAppContext,
|
||||
) -> anyhow::Result<Diff> {
|
||||
let buffer_text = buffer.read_with(cx, |buffer, _| buffer.text());
|
||||
let (buffer_text, buffer_language) =
|
||||
buffer.read_with(cx, |buffer, _| (buffer.text(), buffer.language().cloned()));
|
||||
let response = self
|
||||
.server
|
||||
.request::<PrettierFormat>(PrettierFormatParams { text: buffer_text })
|
||||
.request::<PrettierFormat>(PrettierFormatParams {
|
||||
text: buffer_text,
|
||||
path: None,
|
||||
parser: None,
|
||||
})
|
||||
.await
|
||||
.context("prettier format request")?;
|
||||
dbg!("Formatted text", response.text);
|
||||
|
@ -256,6 +261,9 @@ enum PrettierFormat {}
|
|||
#[serde(rename_all = "camelCase")]
|
||||
struct PrettierFormatParams {
|
||||
text: String,
|
||||
// TODO kb have "options" or something more generic instead?
|
||||
parser: Option<String>,
|
||||
path: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
|
|
|
@ -43,6 +43,8 @@ async function handleBuffer(prettier) {
|
|||
}
|
||||
}
|
||||
|
||||
const headerSeparator = "\r\n";
|
||||
|
||||
async function* readStdin() {
|
||||
let buffer = Buffer.alloc(0);
|
||||
let streamEnded = false;
|
||||
|
@ -62,13 +64,12 @@ async function* readStdin() {
|
|||
}
|
||||
|
||||
try {
|
||||
const headersSeparator = "\r\n\r\n";
|
||||
let contentLengthHeaderName = 'Content-Length';
|
||||
let headersLength = null;
|
||||
let messageLength = null;
|
||||
main_loop: while (true) {
|
||||
if (messageLength === null) {
|
||||
while (buffer.indexOf(headersSeparator) === -1) {
|
||||
while (buffer.indexOf(`${headerSeparator}${headerSeparator}`) === -1) {
|
||||
if (streamEnded) {
|
||||
await handleStreamEnded('Unexpected end of stream: headers not found');
|
||||
continue main_loop;
|
||||
|
@ -78,16 +79,16 @@ async function* readStdin() {
|
|||
}
|
||||
await once(process.stdin, 'readable');
|
||||
}
|
||||
const headers = buffer.subarray(0, buffer.indexOf(headersSeparator)).toString('ascii');
|
||||
const contentLengthHeader = headers.split('\r\n').map(header => header.split(': '))
|
||||
const headers = buffer.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`)).toString('ascii');
|
||||
const contentLengthHeader = headers.split(headerSeparator).map(header => header.split(':'))
|
||||
.filter(header => header[2] === undefined)
|
||||
.filter(header => (header[1] || '').length > 0)
|
||||
.find(header => header[0].trim() === contentLengthHeaderName);
|
||||
if (contentLengthHeader === undefined) {
|
||||
await handleStreamEnded(`Missing or incorrect Content-Length header: ${headers}`);
|
||||
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
|
||||
continue main_loop;
|
||||
}
|
||||
headersLength = headers.length + headersSeparator.length;
|
||||
headersLength = headers.length + headerSeparator.length * 2;
|
||||
messageLength = parseInt(contentLengthHeader[1], 10);
|
||||
}
|
||||
|
||||
|
@ -130,7 +131,14 @@ async function handleMessage(messageText, prettier) {
|
|||
if (params === undefined || params.text === undefined) {
|
||||
throw new Error(`Message params.text is undefined: ${messageText}`);
|
||||
}
|
||||
let formattedText = await prettier.format(params.text);
|
||||
|
||||
let options = {};
|
||||
if (message.path !== undefined) {
|
||||
options.filepath = message.path;
|
||||
} else {
|
||||
options.parser = message.parser || 'babel';
|
||||
}
|
||||
const formattedText = await prettier.format(params.text, options);
|
||||
sendResponse({ id, result: { text: formattedText } });
|
||||
} else if (method === 'prettier/clear_cache') {
|
||||
prettier.clearConfigCache();
|
||||
|
@ -161,7 +169,7 @@ function sendResponse(response) {
|
|||
jsonrpc: "2.0",
|
||||
...response
|
||||
});
|
||||
let headers = `Content-Length: ${Buffer.byteLength(responsePayloadString)}\r\n\r\n`;
|
||||
let headers = `Content-Length: ${Buffer.byteLength(responsePayloadString)}${headerSeparator}${headerSeparator}`;
|
||||
let dataToSend = headers + responsePayloadString;
|
||||
process.stdout.write(dataToSend);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue