From 6bc1ba6d62189919143c9964808f9a37dd3eb75b Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sun, 27 Aug 2023 16:32:18 -0400 Subject: [PATCH] Use stdin for formatter when `--stdin-filename` is provided (#6926) ## Summary Just making the formatter CLI more consistent with the linter -- e.g., we now use stdin on invocations like `cat foo.py | cargo run -p ruff_cli -- format -- --stdin-filename=foo.py`, instead of _only_ relying on the `-` file (and use the same helper as the linter to facilitate this). --- crates/ruff_cli/src/lib.rs | 45 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/crates/ruff_cli/src/lib.rs b/crates/ruff_cli/src/lib.rs index 0f104b3f28f89..5c76909249733 100644 --- a/crates/ruff_cli/src/lib.rs +++ b/crates/ruff_cli/src/lib.rs @@ -146,7 +146,7 @@ quoting the executed command, along with the relevant file contents and `pyproje Command::Linter { format } => commands::linter::linter(format)?, Command::Clean => commands::clean::clean(log_level)?, Command::GenerateShellCompletion { shell } => { - shell.generate(&mut Args::command(), &mut io::stdout()); + shell.generate(&mut Args::command(), &mut stdout()); } Command::Check(args) => return check(args, log_level), Command::Format { files } => return format(&files), @@ -161,27 +161,28 @@ fn format(paths: &[PathBuf]) -> Result { experimentation." ); - match &paths { - // Check if we should read from stdin - [path] if path == Path::new("-") => { - let unformatted = read_from_stdin()?; - let options = PyFormatOptions::from_extension(Path::new("stdin.py")); - let formatted = format_module(&unformatted, options)?; - stdout().lock().write_all(formatted.as_code().as_bytes())?; - Ok(ExitStatus::Success) - } - _ => { - // We want to use the same as `ruff check `, but we don't actually want to allow - // any of the linter settings. - // TODO(@konstin): Refactor this to allow getting config and resolver without going - // though clap. - let args_matches = CheckArgs::command() - .no_binary_name(true) - .get_matches_from(paths); - let check_args: CheckArgs = CheckArgs::from_arg_matches(&args_matches)?; - let (cli, overrides) = check_args.partition(); - commands::format::format(&cli, &overrides) - } + // We want to use the same as `ruff check `, but we don't actually want to allow + // any of the linter settings. + // TODO(konstin): Refactor this to allow getting config and resolver without going + // though clap. + let args_matches = CheckArgs::command() + .no_binary_name(true) + .get_matches_from(paths); + let check_args: CheckArgs = CheckArgs::from_arg_matches(&args_matches)?; + let (cli, overrides) = check_args.partition(); + + if is_stdin(&cli.files, cli.stdin_filename.as_deref()) { + let unformatted = read_from_stdin()?; + let options = cli + .stdin_filename + .as_deref() + .map(PyFormatOptions::from_extension) + .unwrap_or_default(); + let formatted = format_module(&unformatted, options)?; + stdout().lock().write_all(formatted.as_code().as_bytes())?; + Ok(ExitStatus::Success) + } else { + commands::format::format(&cli, &overrides) } }