mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-14 15:40:20 +00:00
Add test and basic implementation for formatter preview mode (#8044)
**Summary** Prepare for the black preview style becoming the black stable style at the end of the year. This adds a new test file to compare stable and preview on some relevant preview options in black, and makes `format_dev` understand the black preview flag. I've added poetry as a project that uses preview. I've implemented one specific deviation (collapsing of stub implementation in non-stub files) which showed up in poetry for testing. This also improves poetry compatibility from 0.99891 to 0.99919. Fixes #7440 New compatibility stats: | project | similarity index | total files | changed files | |----------------|------------------:|------------------:|------------------:| | cpython | 0.75803 | 1799 | 1647 | | django | 0.99983 | 2772 | 35 | | home-assistant | 0.99953 | 10596 | 189 | | poetry | 0.99919 | 317 | 12 | | transformers | 0.99963 | 2657 | 332 | | twine | 1.00000 | 33 | 0 | | typeshed | 0.99978 | 3669 | 20 | | warehouse | 0.99969 | 654 | 15 | | zulip | 0.99970 | 1459 | 22 |
This commit is contained in:
parent
f5e850745c
commit
317d3dd612
16 changed files with 790 additions and 32 deletions
|
@ -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, PyFormatOptions};
|
||||
use crate::{format_module_ast, PreviewMode, PyFormatOptions};
|
||||
|
||||
#[derive(ValueEnum, Clone, Debug)]
|
||||
pub enum Emit {
|
||||
|
@ -24,6 +24,7 @@ pub enum Emit {
|
|||
|
||||
#[derive(Parser)]
|
||||
#[command(author, version, about, long_about = None)]
|
||||
#[allow(clippy::struct_excessive_bools)] // It's only the dev cli anyways
|
||||
pub struct Cli {
|
||||
/// Python files to format. If there are none, stdin will be used. `-` as stdin is not supported
|
||||
pub files: Vec<PathBuf>,
|
||||
|
@ -34,6 +35,8 @@ pub struct Cli {
|
|||
#[clap(long)]
|
||||
pub check: bool,
|
||||
#[clap(long)]
|
||||
pub preview: bool,
|
||||
#[clap(long)]
|
||||
pub print_ir: bool,
|
||||
#[clap(long)]
|
||||
pub print_comments: bool,
|
||||
|
@ -48,7 +51,11 @@ 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);
|
||||
let options = PyFormatOptions::from_extension(source_path).with_preview(if cli.preview {
|
||||
PreviewMode::Enabled
|
||||
} else {
|
||||
PreviewMode::Disabled
|
||||
});
|
||||
|
||||
let source_code = SourceCode::new(source);
|
||||
let formatted = format_module_ast(&module, &comment_ranges, source, options)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue