diff --git a/crates/prettier/src/prettier.rs b/crates/prettier/src/prettier.rs index 68374d8a03..a02b139510 100644 --- a/crates/prettier/src/prettier.rs +++ b/crates/prettier/src/prettier.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use anyhow::Context; use fs::Fs; use gpui::{AsyncAppContext, ModelHandle, Task}; +use language::language_settings::language_settings; use language::{Buffer, BundledFormatter, Diff}; use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId}; use node_runtime::NodeRuntime; @@ -188,11 +189,13 @@ impl Prettier { cx: &AsyncAppContext, ) -> anyhow::Result { let params = buffer.read_with(cx, |buffer, cx| { - let path = buffer - .file() + 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()); - let parser = buffer.language().and_then(|language| { + let parser = buffer_language.and_then(|language| { language .lsp_adapters() .iter() @@ -203,9 +206,14 @@ impl Prettier { } }) }); + let tab_width = Some(language_settings.tab_size.get()); PrettierFormatParams { text: buffer.text(), - options: FormatOptions { parser, path }, + options: FormatOptions { + parser, + path, + tab_width, + }, } }); let response = self @@ -283,7 +291,9 @@ struct PrettierFormatParams { #[serde(rename_all = "camelCase")] struct FormatOptions { parser: Option, + #[serde(rename = "filepath")] path: Option, + tab_width: Option, } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] diff --git a/crates/prettier/src/prettier_server.js b/crates/prettier/src/prettier_server.js index 5e86720cd8..2725914334 100644 --- a/crates/prettier/src/prettier_server.js +++ b/crates/prettier/src/prettier_server.js @@ -134,15 +134,7 @@ async function handleMessage(messageText, prettier) { if (params.options === undefined) { throw new Error(`Message params.options is undefined: ${messageText}`); } - - 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); + const formattedText = await prettier.format(params.text, params.options); sendResponse({ id, result: { text: formattedText } }); } else if (method === 'prettier/clear_cache') { prettier.clearConfigCache();