mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +00:00
refactor: Reduce code duplication
This commit is contained in:
parent
9011456aa1
commit
6f16f1c39b
2 changed files with 43 additions and 69 deletions
|
@ -498,9 +498,8 @@ fn num_digits(n: usize) -> usize {
|
||||||
|
|
||||||
/// Print a single `Message` with full details.
|
/// Print a single `Message` with full details.
|
||||||
fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
|
fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
|
||||||
let label = if message.kind.fixable() {
|
let label = format!(
|
||||||
format!(
|
"{}{}{}{}{}{} {} {}{}",
|
||||||
"{}{}{}{}{}{} {} [{}] {}",
|
|
||||||
relativize_path(Path::new(&message.filename)).bold(),
|
relativize_path(Path::new(&message.filename)).bold(),
|
||||||
":".cyan(),
|
":".cyan(),
|
||||||
message.location.row(),
|
message.location.row(),
|
||||||
|
@ -508,22 +507,13 @@ fn print_message<T: Write>(stdout: &mut T, message: &Message) -> Result<()> {
|
||||||
message.location.column(),
|
message.location.column(),
|
||||||
":".cyan(),
|
":".cyan(),
|
||||||
message.kind.rule().code().red().bold(),
|
message.kind.rule().code().red().bold(),
|
||||||
"*".cyan(),
|
message
|
||||||
|
.kind
|
||||||
|
.fixable()
|
||||||
|
.then_some(format_args!("[{}] ", "*".cyan()))
|
||||||
|
.unwrap_or(format_args!("")),
|
||||||
message.kind.body(),
|
message.kind.body(),
|
||||||
)
|
);
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"{}{}{}{}{}{} {} {}",
|
|
||||||
relativize_path(Path::new(&message.filename)).bold(),
|
|
||||||
":".cyan(),
|
|
||||||
message.location.row(),
|
|
||||||
":".cyan(),
|
|
||||||
message.location.column(),
|
|
||||||
":".cyan(),
|
|
||||||
message.kind.rule().code().red().bold(),
|
|
||||||
message.kind.body(),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
writeln!(stdout, "{label}")?;
|
writeln!(stdout, "{label}")?;
|
||||||
if let Some(source) = &message.source {
|
if let Some(source) = &message.source {
|
||||||
let commit = message.kind.commit();
|
let commit = message.kind.commit();
|
||||||
|
@ -578,30 +568,21 @@ fn print_grouped_message<T: Write>(
|
||||||
row_length: usize,
|
row_length: usize,
|
||||||
column_length: usize,
|
column_length: usize,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let label = if message.kind.fixable() {
|
let label = format!(
|
||||||
format!(
|
" {}{}{}{}{} {} {}{}",
|
||||||
" {}{}{}{}{} {} [{}] {}",
|
|
||||||
" ".repeat(row_length - num_digits(message.location.row())),
|
" ".repeat(row_length - num_digits(message.location.row())),
|
||||||
message.location.row(),
|
message.location.row(),
|
||||||
":".cyan(),
|
":".cyan(),
|
||||||
message.location.column(),
|
message.location.column(),
|
||||||
" ".repeat(column_length - num_digits(message.location.column())),
|
" ".repeat(column_length - num_digits(message.location.column())),
|
||||||
message.kind.rule().code().red().bold(),
|
message.kind.rule().code().red().bold(),
|
||||||
"*".cyan(),
|
message
|
||||||
|
.kind
|
||||||
|
.fixable()
|
||||||
|
.then_some(format_args!("[{}] ", "*".cyan()))
|
||||||
|
.unwrap_or(format_args!("")),
|
||||||
message.kind.body(),
|
message.kind.body(),
|
||||||
)
|
);
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
" {}{}{}{}{} {} {}",
|
|
||||||
" ".repeat(row_length - num_digits(message.location.row())),
|
|
||||||
message.location.row(),
|
|
||||||
":".cyan(),
|
|
||||||
message.location.column(),
|
|
||||||
" ".repeat(column_length - num_digits(message.location.column())),
|
|
||||||
message.kind.rule().code().red().bold(),
|
|
||||||
message.kind.body(),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
writeln!(stdout, "{label}")?;
|
writeln!(stdout, "{label}")?;
|
||||||
if let Some(source) = &message.source {
|
if let Some(source) = &message.source {
|
||||||
let commit = message.kind.commit();
|
let commit = message.kind.commit();
|
||||||
|
|
|
@ -34,25 +34,18 @@ fn generate_table(table_out: &mut String, rules: impl IntoIterator<Item = Rule>)
|
||||||
Some(_) => "🛠",
|
Some(_) => "🛠",
|
||||||
};
|
};
|
||||||
|
|
||||||
if rule.explanation().is_some() {
|
let rule_name = rule.as_ref();
|
||||||
table_out.push_str(&format!(
|
|
||||||
"| {} | [{}]({}/{}.md) | {} | {} |",
|
|
||||||
rule.code(),
|
|
||||||
rule.as_ref(),
|
|
||||||
URL_PREFIX,
|
|
||||||
rule.as_ref(),
|
|
||||||
rule.message_formats()[0].replace('|', r"\|"),
|
|
||||||
fix_token
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
table_out.push_str(&format!(
|
table_out.push_str(&format!(
|
||||||
"| {} | {} | {} | {} |",
|
"| {} | {} | {} | {} |",
|
||||||
rule.code(),
|
rule.code(),
|
||||||
rule.as_ref(),
|
rule.explanation()
|
||||||
|
.is_some()
|
||||||
|
.then_some(format_args!("[{rule_name}]({URL_PREFIX}/{rule_name}.md)",))
|
||||||
|
.unwrap_or(format_args!("{rule_name}")),
|
||||||
rule.message_formats()[0].replace('|', r"\|"),
|
rule.message_formats()[0].replace('|', r"\|"),
|
||||||
fix_token
|
fix_token
|
||||||
));
|
));
|
||||||
}
|
|
||||||
table_out.push('\n');
|
table_out.push('\n');
|
||||||
}
|
}
|
||||||
table_out.push('\n');
|
table_out.push('\n');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue