mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
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).
This commit is contained in:
parent
cd47368ae4
commit
6bc1ba6d62
1 changed files with 23 additions and 22 deletions
|
@ -146,7 +146,7 @@ quoting the executed command, along with the relevant file contents and `pyproje
|
||||||
Command::Linter { format } => commands::linter::linter(format)?,
|
Command::Linter { format } => commands::linter::linter(format)?,
|
||||||
Command::Clean => commands::clean::clean(log_level)?,
|
Command::Clean => commands::clean::clean(log_level)?,
|
||||||
Command::GenerateShellCompletion { shell } => {
|
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::Check(args) => return check(args, log_level),
|
||||||
Command::Format { files } => return format(&files),
|
Command::Format { files } => return format(&files),
|
||||||
|
@ -161,27 +161,28 @@ fn format(paths: &[PathBuf]) -> Result<ExitStatus> {
|
||||||
experimentation."
|
experimentation."
|
||||||
);
|
);
|
||||||
|
|
||||||
match &paths {
|
// We want to use the same as `ruff check <files>`, but we don't actually want to allow
|
||||||
// Check if we should read from stdin
|
// any of the linter settings.
|
||||||
[path] if path == Path::new("-") => {
|
// TODO(konstin): Refactor this to allow getting config and resolver without going
|
||||||
let unformatted = read_from_stdin()?;
|
// though clap.
|
||||||
let options = PyFormatOptions::from_extension(Path::new("stdin.py"));
|
let args_matches = CheckArgs::command()
|
||||||
let formatted = format_module(&unformatted, options)?;
|
.no_binary_name(true)
|
||||||
stdout().lock().write_all(formatted.as_code().as_bytes())?;
|
.get_matches_from(paths);
|
||||||
Ok(ExitStatus::Success)
|
let check_args: CheckArgs = CheckArgs::from_arg_matches(&args_matches)?;
|
||||||
}
|
let (cli, overrides) = check_args.partition();
|
||||||
_ => {
|
|
||||||
// We want to use the same as `ruff check <files>`, but we don't actually want to allow
|
if is_stdin(&cli.files, cli.stdin_filename.as_deref()) {
|
||||||
// any of the linter settings.
|
let unformatted = read_from_stdin()?;
|
||||||
// TODO(@konstin): Refactor this to allow getting config and resolver without going
|
let options = cli
|
||||||
// though clap.
|
.stdin_filename
|
||||||
let args_matches = CheckArgs::command()
|
.as_deref()
|
||||||
.no_binary_name(true)
|
.map(PyFormatOptions::from_extension)
|
||||||
.get_matches_from(paths);
|
.unwrap_or_default();
|
||||||
let check_args: CheckArgs = CheckArgs::from_arg_matches(&args_matches)?;
|
let formatted = format_module(&unformatted, options)?;
|
||||||
let (cli, overrides) = check_args.partition();
|
stdout().lock().write_all(formatted.as_code().as_bytes())?;
|
||||||
commands::format::format(&cli, &overrides)
|
Ok(ExitStatus::Success)
|
||||||
}
|
} else {
|
||||||
|
commands::format::format(&cli, &overrides)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue