mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Format prettier_server.js
(#9583)
This PURELY formats the file by opening it in Zed and hitting save with save-on-format on. It's been bugging me that I can't change the file without the whole thing getting reformatted, so here we are. Release Notes: - N/A
This commit is contained in:
parent
3a2eb12f68
commit
3853991c20
@ -15,6 +15,10 @@
|
|||||||
"JSON": {
|
"JSON": {
|
||||||
"tab_size": 2,
|
"tab_size": 2,
|
||||||
"formatter": "prettier"
|
"formatter": "prettier"
|
||||||
|
},
|
||||||
|
"JavaScript": {
|
||||||
|
"tab_size": 2,
|
||||||
|
"formatter": "prettier"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"formatter": "auto"
|
"formatter": "auto"
|
||||||
|
@ -5,241 +5,263 @@ 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(
|
process.stderr.write(
|
||||||
`Prettier path argument was not specified or empty.\nUsage: ${process.argv[0]} ${process.argv[1]} prettier/path\n`,
|
`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) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
process.stderr.write(`Path '${prettierContainerPath}' does not exist\n`);
|
process.stderr.write(`Path '${prettierContainerPath}' does not exist\n`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
|
process.stderr.write(
|
||||||
process.exit(1);
|
`Path '${prettierContainerPath}' exists but is not a directory\n`,
|
||||||
}
|
);
|
||||||
|
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) {
|
||||||
this.path = path;
|
this.path = path;
|
||||||
this.prettier = prettier;
|
this.prettier = prettier;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
let prettier;
|
let 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);
|
||||||
}
|
}
|
||||||
process.stderr.write(`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`);
|
process.stderr.write(
|
||||||
process.stdin.resume();
|
`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`,
|
||||||
handleBuffer(new Prettier(prettierPath, prettier, config));
|
);
|
||||||
|
process.stdin.resume();
|
||||||
|
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()) {
|
||||||
let message;
|
let message;
|
||||||
try {
|
try {
|
||||||
message = JSON.parse(messageText);
|
message = JSON.parse(messageText);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
sendResponse(makeError(`Failed to parse message '${messageText}': ${e}`));
|
sendResponse(makeError(`Failed to parse message '${messageText}': ${e}`));
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
// allow concurrent request handling by not `await`ing the message handling promise (async function)
|
|
||||||
handleMessage(message, prettier).catch((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}`),
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
// allow concurrent request handling by not `await`ing the message handling promise (async function)
|
||||||
|
handleMessage(message, prettier).catch((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]);
|
||||||
});
|
});
|
||||||
|
|
||||||
async function handleStreamEnded(errorMessage) {
|
async function handleStreamEnded(errorMessage) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let headersLength = null;
|
let headersLength = null;
|
||||||
let messageLength = null;
|
let messageLength = null;
|
||||||
main_loop: while (true) {
|
main_loop: while (true) {
|
||||||
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(
|
||||||
continue main_loop;
|
"Unexpected end of stream: headers not found",
|
||||||
} else if (buffer.length > contentLengthHeaderName.length * 10) {
|
);
|
||||||
await handleStreamEnded(
|
continue main_loop;
|
||||||
`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
|
} else if (buffer.length > contentLengthHeaderName.length * 10) {
|
||||||
);
|
await handleStreamEnded(
|
||||||
continue main_loop;
|
`Unexpected stream of bytes: no headers end found after ${buffer.length} bytes of input`,
|
||||||
}
|
);
|
||||||
await once(process.stdin, "readable");
|
continue main_loop;
|
||||||
}
|
}
|
||||||
const headers = buffer
|
await once(process.stdin, "readable");
|
||||||
.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);
|
|
||||||
const contentLength = (contentLengthHeader || [])[1];
|
|
||||||
if (contentLength === undefined) {
|
|
||||||
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
|
|
||||||
continue main_loop;
|
|
||||||
}
|
|
||||||
headersLength = headers.length + headerSeparator.length * 2;
|
|
||||||
messageLength = parseInt(contentLength, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (buffer.length < headersLength + messageLength) {
|
|
||||||
if (streamEnded) {
|
|
||||||
await handleStreamEnded(
|
|
||||||
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
|
|
||||||
);
|
|
||||||
continue main_loop;
|
|
||||||
}
|
|
||||||
await once(process.stdin, "readable");
|
|
||||||
}
|
|
||||||
|
|
||||||
const messageEnd = headersLength + messageLength;
|
|
||||||
const message = buffer.subarray(headersLength, messageEnd);
|
|
||||||
buffer = buffer.subarray(messageEnd);
|
|
||||||
headersLength = null;
|
|
||||||
messageLength = null;
|
|
||||||
yield message.toString("utf8");
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
const headers = buffer
|
||||||
sendResponse(makeError(`Error reading stdin: ${e}`));
|
.subarray(0, buffer.indexOf(`${headerSeparator}${headerSeparator}`))
|
||||||
} finally {
|
.toString("ascii");
|
||||||
process.stdin.off("data", () => {});
|
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,
|
||||||
|
);
|
||||||
|
const contentLength = (contentLengthHeader || [])[1];
|
||||||
|
if (contentLength === undefined) {
|
||||||
|
await handleStreamEnded(
|
||||||
|
`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`,
|
||||||
|
);
|
||||||
|
continue main_loop;
|
||||||
|
}
|
||||||
|
headersLength = headers.length + headerSeparator.length * 2;
|
||||||
|
messageLength = parseInt(contentLength, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (buffer.length < headersLength + messageLength) {
|
||||||
|
if (streamEnded) {
|
||||||
|
await handleStreamEnded(
|
||||||
|
`Unexpected end of stream: buffer length ${buffer.length} does not match expected header length ${headersLength} + body length ${messageLength}`,
|
||||||
|
);
|
||||||
|
continue main_loop;
|
||||||
|
}
|
||||||
|
await once(process.stdin, "readable");
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageEnd = headersLength + messageLength;
|
||||||
|
const message = buffer.subarray(headersLength, messageEnd);
|
||||||
|
buffer = buffer.subarray(messageEnd);
|
||||||
|
headersLength = null;
|
||||||
|
messageLength = null;
|
||||||
|
yield message.toString("utf8");
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
sendResponse(makeError(`Error reading stdin: ${e}`));
|
||||||
|
} finally {
|
||||||
|
process.stdin.off("data", () => {});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleMessage(message, prettier) {
|
async function handleMessage(message, prettier) {
|
||||||
const { method, id, params } = message;
|
const { method, id, params } = message;
|
||||||
if (method === undefined) {
|
if (method === undefined) {
|
||||||
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
|
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
|
||||||
} else if (method == "initialized") {
|
} else if (method == "initialized") {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id === undefined) {
|
||||||
|
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (method === "prettier/format") {
|
||||||
|
if (params === undefined || params.text === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`Message params.text is undefined: ${JSON.stringify(message)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (params.options === undefined) {
|
||||||
|
throw new Error(
|
||||||
|
`Message params.options is undefined: ${JSON.stringify(message)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id === undefined) {
|
let resolvedConfig = {};
|
||||||
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
|
if (params.options.filepath !== undefined) {
|
||||||
|
resolvedConfig =
|
||||||
|
(await prettier.prettier.resolveConfig(params.options.filepath)) || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method === "prettier/format") {
|
const plugins =
|
||||||
if (params === undefined || params.text === undefined) {
|
Array.isArray(resolvedConfig?.plugins) &&
|
||||||
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
|
resolvedConfig.plugins.length > 0
|
||||||
}
|
? resolvedConfig.plugins
|
||||||
if (params.options === undefined) {
|
: params.options.plugins;
|
||||||
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let resolvedConfig = {};
|
const options = {
|
||||||
if (params.options.filepath !== undefined) {
|
...(params.options.prettierOptions || prettier.config),
|
||||||
resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
|
...resolvedConfig,
|
||||||
}
|
plugins,
|
||||||
|
parser: params.options.parser,
|
||||||
const plugins = Array.isArray(resolvedConfig?.plugins) && resolvedConfig.plugins.length > 0 ?
|
path: params.options.filepath,
|
||||||
resolvedConfig.plugins :
|
};
|
||||||
params.options.plugins;
|
process.stderr.write(
|
||||||
|
`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
|
||||||
const options = {
|
params.options.filepath || ""
|
||||||
...(params.options.prettierOptions || prettier.config),
|
}' with options: ${JSON.stringify(options)}\n`,
|
||||||
...resolvedConfig,
|
);
|
||||||
plugins,
|
const formattedText = await prettier.prettier.format(params.text, options);
|
||||||
parser: params.options.parser,
|
sendResponse({ id, result: { text: formattedText } });
|
||||||
path: params.options.filepath,
|
} else if (method === "prettier/clear_cache") {
|
||||||
};
|
prettier.prettier.clearConfigCache();
|
||||||
process.stderr.write(
|
prettier.config =
|
||||||
`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${
|
(await prettier.prettier.resolveConfig(prettier.path)) || {};
|
||||||
params.options.filepath || ""
|
sendResponse({ id, result: null });
|
||||||
}' with options: ${JSON.stringify(options)}\n`,
|
} else if (method === "initialize") {
|
||||||
);
|
sendResponse({
|
||||||
const formattedText = await prettier.prettier.format(params.text, options);
|
id,
|
||||||
sendResponse({ id, result: { text: formattedText } });
|
result: {
|
||||||
} else if (method === "prettier/clear_cache") {
|
capabilities: {},
|
||||||
prettier.prettier.clearConfigCache();
|
},
|
||||||
prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
|
});
|
||||||
sendResponse({ id, result: null });
|
} else {
|
||||||
} else if (method === "initialize") {
|
throw new Error(`Unknown method: ${method}`);
|
||||||
sendResponse({
|
}
|
||||||
id,
|
|
||||||
result: {
|
|
||||||
capabilities: {},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
throw new Error(`Unknown method: ${method}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
const headers = `${contentLengthHeaderName}: ${Buffer.byteLength(
|
||||||
responsePayloadString,
|
responsePayloadString,
|
||||||
)}${headerSeparator}${headerSeparator}`;
|
)}${headerSeparator}${headerSeparator}`;
|
||||||
process.stdout.write(headers + responsePayloadString);
|
process.stdout.write(headers + responsePayloadString);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadPrettier(prettierPath) {
|
function loadPrettier(prettierPath) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
fs.access(prettierPath, fs.constants.F_OK, (err) => {
|
fs.access(prettierPath, fs.constants.F_OK, (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
|
reject(`Path '${prettierPath}' does not exist.Error: ${err}`);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
resolve(require(prettierPath));
|
resolve(require(prettierPath));
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
reject(`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`);
|
reject(
|
||||||
}
|
`Error requiring prettier module from path '${prettierPath}'.Error: ${err}`,
|
||||||
}
|
);
|
||||||
});
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user