Allow to configure default prettier

This commit is contained in:
Kirill Bulatov 2023-09-22 18:40:12 +03:00
parent b109075bf2
commit 6ec3927dd3
7 changed files with 60 additions and 13 deletions

View file

@ -7,6 +7,7 @@ edition = "2021"
path = "src/prettier.rs"
[dependencies]
collections = { path = "../collections"}
language = { path = "../language" }
gpui = { path = "../gpui" }
fs = { path = "../fs" }

View file

@ -1,8 +1,9 @@
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use anyhow::Context;
use collections::HashMap;
use fs::Fs;
use gpui::{AsyncAppContext, ModelHandle};
use language::language_settings::language_settings;
@ -202,7 +203,6 @@ impl Prettier {
let params = buffer.read_with(cx, |buffer, cx| {
let buffer_file = buffer.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(|path| path.to_path_buf());
@ -217,14 +217,38 @@ impl Prettier {
}
})
});
let tab_width = Some(language_settings.tab_size.get());
let prettier_options = if self.default {
let language_settings = language_settings(buffer_language, buffer_file, cx);
let mut options = language_settings.prettier.clone();
if !options.contains_key("tabWidth") {
options.insert(
"tabWidth".to_string(),
serde_json::Value::Number(serde_json::Number::from(
language_settings.tab_size.get(),
)),
);
}
if !options.contains_key("printWidth") {
options.insert(
"printWidth".to_string(),
serde_json::Value::Number(serde_json::Number::from(
language_settings.preferred_line_length,
)),
);
}
Some(options)
} else {
None
};
FormatParams {
text: buffer.text(),
options: FormatOptions {
parser,
// TODO kb is not absolute now
path,
tab_width,
prettier_options,
},
}
});
@ -318,13 +342,13 @@ struct FormatParams {
options: FormatOptions,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct FormatOptions {
parser: Option<String>,
#[serde(rename = "filepath")]
path: Option<PathBuf>,
tab_width: Option<u32>,
prettier_options: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]

View file

@ -149,7 +149,13 @@ async function handleMessage(message, prettier) {
if (params.options === undefined) {
throw new Error(`Message params.options is undefined: ${JSON.stringify(message)}`);
}
const formattedText = await prettier.prettier.format(params.text, { ...prettier.config, ...params.options });
const options = {
...(params.options.prettierOptions || prettier.config),
parser: params.options.parser,
path: params.options.path
};
const formattedText = await prettier.prettier.format(params.text, options);
sendResponse({ id, result: { text: formattedText } });
} else if (method === 'prettier/clear_cache') {
prettier.prettier.clearConfigCache();