mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
refactor: Reduce code duplication
This commit is contained in:
parent
c32441e4ab
commit
682d206992
2 changed files with 25 additions and 16 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::{BufWriter, Write};
|
use std::io::{BufWriter, Write};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -496,21 +497,34 @@ fn num_digits(n: usize) -> usize {
|
||||||
.max(1)
|
.max(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Print a single `Message` with full details.
|
struct CodeAndBody<'a>(&'a Message);
|
||||||
fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
|
|
||||||
let label = format!(
|
impl Display for CodeAndBody<'_> {
|
||||||
"{path}{sep}{row}{sep}{col}{sep} {code} {autofix}{body}",
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
path = relativize_path(Path::new(&message.filename)).bold(),
|
write!(
|
||||||
sep = ":".cyan(),
|
f,
|
||||||
row = message.location.row(),
|
"{code} {autofix}{body}",
|
||||||
col = message.location.column(),
|
code = self.0.kind.rule().code().red().bold(),
|
||||||
code = message.kind.rule().code().red().bold(),
|
autofix = self
|
||||||
autofix = message
|
.0
|
||||||
.kind
|
.kind
|
||||||
.fixable()
|
.fixable()
|
||||||
.then_some(format_args!("[{}] ", "*".cyan()))
|
.then_some(format_args!("[{}] ", "*".cyan()))
|
||||||
.unwrap_or(format_args!("")),
|
.unwrap_or(format_args!("")),
|
||||||
body = message.kind.body(),
|
body = self.0.kind.body(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Print a single `Message` with full details.
|
||||||
|
fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
|
||||||
|
let label = format!(
|
||||||
|
"{path}{sep}{row}{sep}{col}{sep} {code_and_body}",
|
||||||
|
path = relativize_path(Path::new(&message.filename)).bold(),
|
||||||
|
sep = ":".cyan(),
|
||||||
|
row = message.location.row(),
|
||||||
|
col = message.location.column(),
|
||||||
|
code_and_body = CodeAndBody(message)
|
||||||
);
|
);
|
||||||
writeln!(stdout, "{label}")?;
|
writeln!(stdout, "{label}")?;
|
||||||
if let Some(source) = &message.source {
|
if let Some(source) = &message.source {
|
||||||
|
@ -567,19 +581,13 @@ fn print_grouped_message<T: Write>(
|
||||||
column_length: usize,
|
column_length: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let label = format!(
|
let label = format!(
|
||||||
" {row_padding}{row}{sep}{col}{col_padding} {code} {autofix}{body}",
|
" {row_padding}{row}{sep}{col}{col_padding} {code_and_body}",
|
||||||
row_padding = " ".repeat(row_length - num_digits(message.location.row())),
|
row_padding = " ".repeat(row_length - num_digits(message.location.row())),
|
||||||
row = message.location.row(),
|
row = message.location.row(),
|
||||||
sep = ":".cyan(),
|
sep = ":".cyan(),
|
||||||
col = message.location.column(),
|
col = message.location.column(),
|
||||||
col_padding = " ".repeat(column_length - num_digits(message.location.column())),
|
col_padding = " ".repeat(column_length - num_digits(message.location.column())),
|
||||||
code = message.kind.rule().code().red().bold(),
|
code_and_body = CodeAndBody(message),
|
||||||
autofix = message
|
|
||||||
.kind
|
|
||||||
.fixable()
|
|
||||||
.then_some(format_args!("[{}] ", "*".cyan()))
|
|
||||||
.unwrap_or(format_args!("")),
|
|
||||||
body = message.kind.body(),
|
|
||||||
);
|
);
|
||||||
writeln!(stdout, "{label}")?;
|
writeln!(stdout, "{label}")?;
|
||||||
if let Some(source) = &message.source {
|
if let Some(source) = &message.source {
|
||||||
|
|
|
@ -36,6 +36,7 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>)
|
||||||
|
|
||||||
let rule_name = rule.as_ref();
|
let rule_name = rule.as_ref();
|
||||||
|
|
||||||
|
#[allow(clippy::or_fun_call)]
|
||||||
table_out.push_str(&format!(
|
table_out.push_str(&format!(
|
||||||
"| {} | {} | {} | {} |",
|
"| {} | {} | {} | {} |",
|
||||||
rule.code(),
|
rule.code(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue