mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Move stdin formatting to its own command file (#6981)
## Summary This is similar to `commands::check` vs. `commands::check_stdin`, and gets the logic out of the parent file (`lib.rs`). It also ensures that we avoid formatting files that should be excluded when `--force-exclude` is provided.
This commit is contained in:
parent
34221346c1
commit
25c374856a
5 changed files with 51 additions and 20 deletions
|
@ -1,4 +1,3 @@
|
|||
use std::io::{self, Read};
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Result;
|
||||
|
@ -9,13 +8,7 @@ use ruff_workspace::resolver::{python_file_at_path, PyprojectConfig};
|
|||
|
||||
use crate::args::Overrides;
|
||||
use crate::diagnostics::{lint_stdin, Diagnostics};
|
||||
|
||||
/// Read a `String` from `stdin`.
|
||||
pub(crate) fn read_from_stdin() -> Result<String> {
|
||||
let mut buffer = String::new();
|
||||
io::stdin().lock().read_to_string(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
use crate::stdin::read_from_stdin;
|
||||
|
||||
/// Run the linter over a single file, read from `stdin`.
|
||||
pub(crate) fn check_stdin(
|
||||
|
|
37
crates/ruff_cli/src/commands/format_stdin.rs
Normal file
37
crates/ruff_cli/src/commands/format_stdin.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use std::io::{stdout, Write};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
use ruff_python_formatter::{format_module, PyFormatOptions};
|
||||
use ruff_workspace::resolver::python_file_at_path;
|
||||
|
||||
use crate::args::{FormatArguments, Overrides};
|
||||
use crate::resolve::resolve;
|
||||
use crate::stdin::read_from_stdin;
|
||||
use crate::ExitStatus;
|
||||
|
||||
/// Run the formatter over a single file, read from `stdin`.
|
||||
pub(crate) fn format_stdin(cli: &FormatArguments, overrides: &Overrides) -> Result<ExitStatus> {
|
||||
let pyproject_config = resolve(
|
||||
cli.isolated,
|
||||
cli.config.as_deref(),
|
||||
overrides,
|
||||
cli.stdin_filename.as_deref(),
|
||||
)?;
|
||||
|
||||
if let Some(filename) = cli.stdin_filename.as_deref() {
|
||||
if !python_file_at_path(filename, &pyproject_config, overrides)? {
|
||||
return Ok(ExitStatus::Success);
|
||||
}
|
||||
}
|
||||
|
||||
let stdin = read_from_stdin()?;
|
||||
let options = cli
|
||||
.stdin_filename
|
||||
.as_deref()
|
||||
.map(PyFormatOptions::from_extension)
|
||||
.unwrap_or_default();
|
||||
let formatted = format_module(&stdin, options)?;
|
||||
stdout().lock().write_all(formatted.as_code().as_bytes())?;
|
||||
Ok(ExitStatus::Success)
|
||||
}
|
|
@ -4,6 +4,7 @@ pub(crate) mod check_stdin;
|
|||
pub(crate) mod clean;
|
||||
pub(crate) mod config;
|
||||
pub(crate) mod format;
|
||||
pub(crate) mod format_stdin;
|
||||
pub(crate) mod linter;
|
||||
pub(crate) mod rule;
|
||||
pub(crate) mod show_files;
|
||||
|
|
|
@ -13,10 +13,8 @@ use ruff::logging::{set_up_logging, LogLevel};
|
|||
use ruff::settings::types::SerializationFormat;
|
||||
use ruff::settings::{flags, CliSettings};
|
||||
use ruff::{fs, warn_user_once};
|
||||
use ruff_python_formatter::{format_module, PyFormatOptions};
|
||||
|
||||
use crate::args::{Args, CheckCommand, Command, FormatCommand};
|
||||
use crate::commands::check_stdin::read_from_stdin;
|
||||
use crate::printer::{Flags as PrinterFlags, Printer};
|
||||
|
||||
pub mod args;
|
||||
|
@ -26,6 +24,7 @@ mod diagnostics;
|
|||
mod panic;
|
||||
mod printer;
|
||||
pub mod resolve;
|
||||
mod stdin;
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub enum ExitStatus {
|
||||
|
@ -77,7 +76,7 @@ fn change_detected(paths: &[PathBuf]) -> Option<ChangeKind> {
|
|||
None
|
||||
}
|
||||
|
||||
/// Returns true if the linter should read from standard input.
|
||||
/// Returns true if the command should read from standard input.
|
||||
fn is_stdin(files: &[PathBuf], stdin_filename: Option<&Path>) -> bool {
|
||||
// If the user provided a `--stdin-filename`, always read from standard input.
|
||||
if stdin_filename.is_some() {
|
||||
|
@ -163,15 +162,7 @@ fn format(args: FormatCommand, log_level: LogLevel) -> Result<ExitStatus> {
|
|||
let (cli, overrides) = 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)
|
||||
commands::format_stdin::format_stdin(&cli, &overrides)
|
||||
} else {
|
||||
commands::format::format(&cli, &overrides, log_level)
|
||||
}
|
||||
|
|
9
crates/ruff_cli/src/stdin.rs
Normal file
9
crates/ruff_cli/src/stdin.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
/// Read a string from `stdin`.
|
||||
pub(crate) fn read_from_stdin() -> Result<String, io::Error> {
|
||||
let mut buffer = String::new();
|
||||
io::stdin().lock().read_to_string(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue