Skip to content

Commit

Permalink
feat: support Jupyter notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari committed Nov 8, 2023
1 parent 03243d2 commit b0fb3c1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
8 changes: 6 additions & 2 deletions crates/ruff_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config>) -> Result<String, String> {
let config: InnerConfig = if let Some(config) = config {
pub fn format(input: &str, path: Option<String>, config: Option<Config>) -> Result<String, String> {
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())
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_fmt/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 10 additions & 1 deletion crates/ruff_fmt_config/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
num::{NonZeroU16, NonZeroU8, ParseIntError, TryFromIntError},
path::Path,
str::FromStr,
};

Expand Down Expand Up @@ -117,6 +118,9 @@ pub struct Config {
pub line_width: Option<LineWidth>,
pub quote_style: Option<QuoteStyle>,
pub magic_trailing_comma: Option<MagicTrailingComma>,

#[serde(skip)]
path: String,
}

impl Config {
Expand Down Expand Up @@ -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<Config> 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());
Expand Down
12 changes: 9 additions & 3 deletions crates/ruff_fmt_dprint/src/format_text.rs
Original file line number Diff line number Diff line change
@@ -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()))
}
Expand All @@ -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"));
}
}
4 changes: 2 additions & 2 deletions crates/ruff_fmt_dprint/src/wasm_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ impl SyncPluginHandler<Configuration> 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)
}
}

Expand Down

0 comments on commit b0fb3c1

Please sign in to comment.