mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-25 01:34:02 +00:00
Generify prettier properties, add tabWidth
This commit is contained in:
parent
2a5b9b635b
commit
39ad3a625c
2 changed files with 15 additions and 13 deletions
|
@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use fs::Fs;
|
use fs::Fs;
|
||||||
use gpui::{AsyncAppContext, ModelHandle, Task};
|
use gpui::{AsyncAppContext, ModelHandle, Task};
|
||||||
|
use language::language_settings::language_settings;
|
||||||
use language::{Buffer, BundledFormatter, Diff};
|
use language::{Buffer, BundledFormatter, Diff};
|
||||||
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
|
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
|
@ -188,11 +189,13 @@ impl Prettier {
|
||||||
cx: &AsyncAppContext,
|
cx: &AsyncAppContext,
|
||||||
) -> anyhow::Result<Diff> {
|
) -> anyhow::Result<Diff> {
|
||||||
let params = buffer.read_with(cx, |buffer, cx| {
|
let params = buffer.read_with(cx, |buffer, cx| {
|
||||||
let path = buffer
|
let buffer_file = buffer.file();
|
||||||
.file()
|
let buffer_language = buffer.language();
|
||||||
|
let language_settings = language_settings(buffer_language, buffer_file, cx);
|
||||||
|
let path = buffer_file
|
||||||
.map(|file| file.full_path(cx))
|
.map(|file| file.full_path(cx))
|
||||||
.map(|path| path.to_path_buf());
|
.map(|path| path.to_path_buf());
|
||||||
let parser = buffer.language().and_then(|language| {
|
let parser = buffer_language.and_then(|language| {
|
||||||
language
|
language
|
||||||
.lsp_adapters()
|
.lsp_adapters()
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -203,9 +206,14 @@ impl Prettier {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
let tab_width = Some(language_settings.tab_size.get());
|
||||||
PrettierFormatParams {
|
PrettierFormatParams {
|
||||||
text: buffer.text(),
|
text: buffer.text(),
|
||||||
options: FormatOptions { parser, path },
|
options: FormatOptions {
|
||||||
|
parser,
|
||||||
|
path,
|
||||||
|
tab_width,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let response = self
|
let response = self
|
||||||
|
@ -283,7 +291,9 @@ struct PrettierFormatParams {
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct FormatOptions {
|
struct FormatOptions {
|
||||||
parser: Option<String>,
|
parser: Option<String>,
|
||||||
|
#[serde(rename = "filepath")]
|
||||||
path: Option<PathBuf>,
|
path: Option<PathBuf>,
|
||||||
|
tab_width: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
|
|
@ -134,15 +134,7 @@ async function handleMessage(messageText, prettier) {
|
||||||
if (params.options === undefined) {
|
if (params.options === undefined) {
|
||||||
throw new Error(`Message params.options is undefined: ${messageText}`);
|
throw new Error(`Message params.options is undefined: ${messageText}`);
|
||||||
}
|
}
|
||||||
|
const formattedText = await prettier.format(params.text, params.options);
|
||||||
let options = {};
|
|
||||||
if (params.options.path !== undefined) {
|
|
||||||
options.filepath = params.options.path;
|
|
||||||
}
|
|
||||||
if (params.options.parser !== undefined) {
|
|
||||||
options.parser = params.options.parser;
|
|
||||||
}
|
|
||||||
const formattedText = await prettier.format(params.text, options);
|
|
||||||
sendResponse({ id, result: { text: formattedText } });
|
sendResponse({ id, result: { text: formattedText } });
|
||||||
} else if (method === 'prettier/clear_cache') {
|
} else if (method === 'prettier/clear_cache') {
|
||||||
prettier.clearConfigCache();
|
prettier.clearConfigCache();
|
||||||
|
|
Loading…
Reference in a new issue