Skip to content

Commit

Permalink
Add --skip-magic-trailing-comma to formatter dev comment (#8689)
Browse files Browse the repository at this point in the history
Testing the compatibility with the future stable black style, i realized
the `ruff_python_formatter` dev main was lacking the
`--skip-magic-trailing-comma` option. This does not affect `ruff
format`.

Usage:
```shell
cargo run --bin ruff_python_formatter -p ruff_python_formatter -- --skip-magic-trailing-comma --emit stdout scratch.py
```
  • Loading branch information
konstin committed Nov 15, 2023
1 parent 9d76e4e commit a783b14
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions crates/ruff_python_formatter/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use ruff_python_parser::{parse_ok_tokens, AsMode};
use ruff_text_size::Ranged;

use crate::comments::collect_comments;
use crate::{format_module_ast, PreviewMode, PyFormatOptions};
use crate::{format_module_ast, MagicTrailingComma, PreviewMode, PyFormatOptions};

#[derive(ValueEnum, Clone, Debug)]
pub enum Emit {
Expand Down Expand Up @@ -40,6 +40,8 @@ pub struct Cli {
pub print_ir: bool,
#[clap(long)]
pub print_comments: bool,
#[clap(long, short = 'C')]
pub skip_magic_trailing_comma: bool,
}

pub fn format_and_debug_print(source: &str, cli: &Cli, source_path: &Path) -> Result<String> {
Expand All @@ -51,11 +53,17 @@ pub fn format_and_debug_print(source: &str, cli: &Cli, source_path: &Path) -> Re
let module = parse_ok_tokens(tokens, source, source_type.as_mode(), "<filename>")
.context("Syntax error in input")?;

let options = PyFormatOptions::from_extension(source_path).with_preview(if cli.preview {
PreviewMode::Enabled
} else {
PreviewMode::Disabled
});
let options = PyFormatOptions::from_extension(source_path)
.with_preview(if cli.preview {
PreviewMode::Enabled
} else {
PreviewMode::Disabled
})
.with_magic_trailing_comma(if cli.skip_magic_trailing_comma {
MagicTrailingComma::Ignore
} else {
MagicTrailingComma::Respect
});

let source_code = SourceCode::new(source);
let formatted = format_module_ast(&module, &comment_ranges, source, options)
Expand Down

0 comments on commit a783b14

Please sign in to comment.