Resolve prettier config before every formatting
This commit is contained in:
parent
658b58378e
commit
a9f80a603c
3 changed files with 19 additions and 15 deletions
|
@ -199,14 +199,11 @@ impl Prettier {
|
|||
pub async fn format(
|
||||
&self,
|
||||
buffer: &ModelHandle<Buffer>,
|
||||
buffer_path: Option<PathBuf>,
|
||||
cx: &AsyncAppContext,
|
||||
) -> anyhow::Result<Diff> {
|
||||
let params = buffer.read_with(cx, |buffer, cx| {
|
||||
let buffer_file = buffer.file();
|
||||
let buffer_language = buffer.language();
|
||||
let path = buffer_file
|
||||
.map(|file| file.full_path(cx))
|
||||
.map(|path| path.to_path_buf());
|
||||
let parsers_with_plugins = buffer_language
|
||||
.into_iter()
|
||||
.flat_map(|language| {
|
||||
|
@ -242,7 +239,6 @@ impl Prettier {
|
|||
log::warn!("Found multiple parsers with plugins {parsers_with_plugins:?}, will select only one: {selected_parser_with_plugins:?}");
|
||||
}
|
||||
|
||||
// TODO kb move the entire prettier server js file into *.mjs one instead?
|
||||
let plugin_name_into_path = |plugin_name: &str| self.prettier_dir.join("node_modules").join(plugin_name).join("dist").join("index.mjs");
|
||||
let (parser, plugins) = match selected_parser_with_plugins {
|
||||
Some((parser, plugins)) => {
|
||||
|
@ -267,7 +263,7 @@ impl Prettier {
|
|||
};
|
||||
|
||||
let prettier_options = if self.default {
|
||||
let language_settings = language_settings(buffer_language, buffer_file, cx);
|
||||
let language_settings = language_settings(buffer_language, buffer.file(), cx);
|
||||
let mut options = language_settings.prettier.clone();
|
||||
if !options.contains_key("tabWidth") {
|
||||
options.insert(
|
||||
|
@ -295,8 +291,7 @@ impl Prettier {
|
|||
options: FormatOptions {
|
||||
parser,
|
||||
plugins,
|
||||
// TODO kb is not absolute now
|
||||
path,
|
||||
path: buffer_path,
|
||||
prettier_options,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -150,15 +150,19 @@ async function handleMessage(message, prettier) {
|
|||
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
|
||||
}
|
||||
|
||||
let resolvedConfig = {};
|
||||
if (params.options.filepath !== undefined) {
|
||||
resolvedConfig = await prettier.prettier.resolveConfig(params.options.filepath) || {};
|
||||
}
|
||||
|
||||
const options = {
|
||||
...(params.options.prettierOptions || prettier.config),
|
||||
...resolvedConfig,
|
||||
parser: params.options.parser,
|
||||
plugins: params.options.plugins,
|
||||
path: params.options.path
|
||||
path: params.options.filepath
|
||||
};
|
||||
// TODO kb always resolve prettier config for each file.
|
||||
// need to understand if default prettier can be affected by other configs in the project
|
||||
// (restart default prettiers on config changes too then)
|
||||
process.stderr.write(`Resolved config: ${JSON.stringify(resolvedConfig)}, will format file '${params.options.filepath || ''}' with options: ${JSON.stringify(options)}\n`);
|
||||
const formattedText = await prettier.prettier.format(params.text, options);
|
||||
sendResponse({ id, result: { text: formattedText } });
|
||||
} else if (method === 'prettier/clear_cache') {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue