mirror of
https://github.com/zed-industries/zed.git
synced 2024-09-19 18:41:56 +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"
|
||||||
|
@ -17,7 +17,9 @@ fs.stat(prettierContainerPath, (err, stats) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!stats.isDirectory()) {
|
if (!stats.isDirectory()) {
|
||||||
process.stderr.write(`Path '${prettierContainerPath}' exists but is not a directory\n`);
|
process.stderr.write(
|
||||||
|
`Path '${prettierContainerPath}' exists but is not a directory\n`,
|
||||||
|
);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -41,7 +43,9 @@ class Prettier {
|
|||||||
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(
|
||||||
|
`Prettier at path '${prettierPath}' loaded successfully, config: ${JSON.stringify(config)}\n`,
|
||||||
|
);
|
||||||
process.stdin.resume();
|
process.stdin.resume();
|
||||||
handleBuffer(new Prettier(prettierPath, prettier, config));
|
handleBuffer(new Prettier(prettierPath, prettier, config));
|
||||||
})();
|
})();
|
||||||
@ -63,7 +67,9 @@ async function handleBuffer(prettier) {
|
|||||||
}
|
}
|
||||||
sendResponse({
|
sendResponse({
|
||||||
id: message.id,
|
id: message.id,
|
||||||
...makeError(`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`),
|
...makeError(
|
||||||
|
`error during message '${JSON.stringify(errorMessage)}' handling: ${e}`,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -97,7 +103,9 @@ async function* readStdin() {
|
|||||||
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(
|
||||||
|
"Unexpected end of stream: headers not found",
|
||||||
|
);
|
||||||
continue main_loop;
|
continue main_loop;
|
||||||
} else if (buffer.length > contentLengthHeaderName.length * 10) {
|
} else if (buffer.length > contentLengthHeaderName.length * 10) {
|
||||||
await handleStreamEnded(
|
await handleStreamEnded(
|
||||||
@ -115,10 +123,14 @@ async function* readStdin() {
|
|||||||
.map((header) => header.split(":"))
|
.map((header) => header.split(":"))
|
||||||
.filter((header) => header[2] === undefined)
|
.filter((header) => header[2] === undefined)
|
||||||
.filter((header) => (header[1] || "").length > 0)
|
.filter((header) => (header[1] || "").length > 0)
|
||||||
.find((header) => (header[0] || "").trim() === contentLengthHeaderName);
|
.find(
|
||||||
|
(header) => (header[0] || "").trim() === contentLengthHeaderName,
|
||||||
|
);
|
||||||
const contentLength = (contentLengthHeader || [])[1];
|
const contentLength = (contentLengthHeader || [])[1];
|
||||||
if (contentLength === undefined) {
|
if (contentLength === undefined) {
|
||||||
await handleStreamEnded(`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`);
|
await handleStreamEnded(
|
||||||
|
`Missing or incorrect ${contentLengthHeaderName} header: ${headers}`,
|
||||||
|
);
|
||||||
continue main_loop;
|
continue main_loop;
|
||||||
}
|
}
|
||||||
headersLength = headers.length + headerSeparator.length * 2;
|
headersLength = headers.length + headerSeparator.length * 2;
|
||||||
@ -163,20 +175,27 @@ async function handleMessage(message, prettier) {
|
|||||||
|
|
||||||
if (method === "prettier/format") {
|
if (method === "prettier/format") {
|
||||||
if (params === undefined || params.text === undefined) {
|
if (params === undefined || params.text === undefined) {
|
||||||
throw new Error(`Message params.text is undefined: ${JSON.stringify(message)}`);
|
throw new Error(
|
||||||
|
`Message params.text is undefined: ${JSON.stringify(message)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
if (params.options === undefined) {
|
if (params.options === undefined) {
|
||||||
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
|
throw new Error(
|
||||||
|
`Message params.options is undefined: ${JSON.stringify(message)}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let resolvedConfig = {};
|
let resolvedConfig = {};
|
||||||
if (params.options.filepath !== undefined) {
|
if (params.options.filepath !== undefined) {
|
||||||
resolvedConfig = (await prettier.prettier.resolveConfig(params.options.filepath)) || {};
|
resolvedConfig =
|
||||||
|
(await prettier.prettier.resolveConfig(params.options.filepath)) || {};
|
||||||
}
|
}
|
||||||
|
|
||||||
const plugins = Array.isArray(resolvedConfig?.plugins) && resolvedConfig.plugins.length > 0 ?
|
const plugins =
|
||||||
resolvedConfig.plugins :
|
Array.isArray(resolvedConfig?.plugins) &&
|
||||||
params.options.plugins;
|
resolvedConfig.plugins.length > 0
|
||||||
|
? resolvedConfig.plugins
|
||||||
|
: params.options.plugins;
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
...(params.options.prettierOptions || prettier.config),
|
...(params.options.prettierOptions || prettier.config),
|
||||||
@ -194,7 +213,8 @@ async function handleMessage(message, prettier) {
|
|||||||
sendResponse({ id, result: { text: formattedText } });
|
sendResponse({ id, result: { text: formattedText } });
|
||||||
} else if (method === "prettier/clear_cache") {
|
} else if (method === "prettier/clear_cache") {
|
||||||
prettier.prettier.clearConfigCache();
|
prettier.prettier.clearConfigCache();
|
||||||
prettier.config = (await prettier.prettier.resolveConfig(prettier.path)) || {};
|
prettier.config =
|
||||||
|
(await prettier.prettier.resolveConfig(prettier.path)) || {};
|
||||||
sendResponse({ id, result: null });
|
sendResponse({ id, result: null });
|
||||||
} else if (method === "initialize") {
|
} else if (method === "initialize") {
|
||||||
sendResponse({
|
sendResponse({
|
||||||
@ -237,7 +257,9 @@ function loadPrettier(prettierPath) {
|
|||||||
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