From b0fb3c188b405bcf2efe667560521fdd4995e5dc Mon Sep 17 00:00:00 2001 From: magic-akari Date: Wed, 8 Nov 2023 22:50:09 +0800 Subject: [PATCH] feat: support Jupyter notebook --- crates/ruff_fmt/src/lib.rs | 8 ++++++-- crates/ruff_fmt/src/test.rs | 2 +- crates/ruff_fmt_config/src/lib.rs | 11 ++++++++++- crates/ruff_fmt_dprint/src/format_text.rs | 12 +++++++++--- crates/ruff_fmt_dprint/src/wasm_plugin.rs | 4 ++-- 5 files changed, 28 insertions(+), 9 deletions(-) diff --git a/crates/ruff_fmt/src/lib.rs b/crates/ruff_fmt/src/lib.rs index ab9ea04..20fabb9 100644 --- a/crates/ruff_fmt/src/lib.rs +++ b/crates/ruff_fmt/src/lib.rs @@ -5,13 +5,17 @@ use ruff_fmt_config::Config as InnerConfig; use ruff_python_formatter::format_module_source; #[wasm_bindgen] -pub fn format(input: &str, config: Option) -> Result { - let config: InnerConfig = if let Some(config) = config { +pub fn format(input: &str, path: Option, config: Option) -> Result { + let mut config: InnerConfig = if let Some(config) = config { serde_wasm_bindgen::from_value(config.clone()).map_err(|e| e.to_string())? } else { Default::default() }; + if let Some(path) = path { + config = config.with_path(path); + } + format_module_source(input, config.into()) .map(|result| result.into_code()) .map_err(|err| err.to_string()) diff --git a/crates/ruff_fmt/src/test.rs b/crates/ruff_fmt/src/test.rs index 1c53a40..bf7e2ba 100644 --- a/crates/ruff_fmt/src/test.rs +++ b/crates/ruff_fmt/src/test.rs @@ -16,7 +16,7 @@ mod tests { let mut expect = String::new(); File::open(extect_path).and_then(|mut file| file.read_to_string(&mut expect)).unwrap(); - let actual = format(&actual, None).unwrap(); + let actual = format(&actual, Some(input.to_string_lossy().to_string()), None).unwrap(); assert_eq!(actual, expect); } diff --git a/crates/ruff_fmt_config/src/lib.rs b/crates/ruff_fmt_config/src/lib.rs index 001ca21..62fc89c 100644 --- a/crates/ruff_fmt_config/src/lib.rs +++ b/crates/ruff_fmt_config/src/lib.rs @@ -1,5 +1,6 @@ use std::{ num::{NonZeroU16, NonZeroU8, ParseIntError, TryFromIntError}, + path::Path, str::FromStr, }; @@ -117,6 +118,9 @@ pub struct Config { pub line_width: Option, pub quote_style: Option, pub magic_trailing_comma: Option, + + #[serde(skip)] + path: String, } impl Config { @@ -144,11 +148,16 @@ impl Config { self.magic_trailing_comma = Some(magic_trailing_comma); self } + + pub fn with_path(mut self, path: String) -> Self { + self.path = path; + self + } } impl From for PyFormatOptions { fn from(value: Config) -> Self { - let mut config = Self::default(); + let mut config = Self::from_extension(Path::new(&value.path)); if let Some(indent_style) = value.indent_style { config = config.with_indent_style(indent_style.into()); diff --git a/crates/ruff_fmt_dprint/src/format_text.rs b/crates/ruff_fmt_dprint/src/format_text.rs index 109b389..8ee8db2 100644 --- a/crates/ruff_fmt_dprint/src/format_text.rs +++ b/crates/ruff_fmt_dprint/src/format_text.rs @@ -1,10 +1,13 @@ +use std::path::Path; + use dprint_core::plugins::FormatResult; use ruff_python_formatter::format_module_source; use super::configuration::Configuration; -pub fn format_text(text: &str, config: &Configuration) -> FormatResult { - format_module_source(text, config.clone().into()) +pub fn format_text(text: &str, file_path: &Path, config: &Configuration) -> FormatResult { + let config = config.clone().with_path(file_path.to_string_lossy().to_string()); + format_module_source(text, config.into()) .map_err(|err| err.into()) .map(|result| (result.as_code() != text).then_some(result.into_code())) } @@ -19,7 +22,10 @@ mod tests { fn should_error_on_syntax_diagnostic() { let global_config = GlobalConfiguration::default(); let config = resolve_config(ConfigKeyMap::new(), &global_config).config; - let message = format_text("for i range(10):", &config).err().unwrap().to_string(); + let message = format_text("for i range(10):", Path::new("main.py"), &config) + .err() + .unwrap() + .to_string(); assert!(message.contains("ParseError")); } } diff --git a/crates/ruff_fmt_dprint/src/wasm_plugin.rs b/crates/ruff_fmt_dprint/src/wasm_plugin.rs index ca0d40f..968c536 100644 --- a/crates/ruff_fmt_dprint/src/wasm_plugin.rs +++ b/crates/ruff_fmt_dprint/src/wasm_plugin.rs @@ -65,12 +65,12 @@ impl SyncPluginHandler for RuffFmtWasmPlugin { fn format( &mut self, - _file_path: &std::path::Path, + file_path: &std::path::Path, file_text: &str, config: &Configuration, mut _format_with_host: impl FnMut(&Path, String, &ConfigKeyMap) -> FormatResult, ) -> FormatResult { - format_text(file_text, config) + format_text(file_text, file_path, config) } }