Skip to content

Commit

Permalink
Use different symbols for fix messages depending on applicability
Browse files Browse the repository at this point in the history
Refactors `violation_string` as well
  • Loading branch information
zanieb committed Oct 3, 2023
1 parent 96b8ce1 commit 9949fa0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
44 changes: 21 additions & 23 deletions crates/ruff_cli/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,29 +478,27 @@ impl<'a> FixableStatistics<'a> {

/// Build the displayed fix status message depending on the types of the remaining fixes.
fn violation_string(&self) -> String {
let prefix = format!("[{}]", "*".cyan());
let mut fix_status = prefix;

if self.automatic > 0 {
fix_status = format!(
"{fix_status} {} potentially fixable with the --fix option.",
self.automatic
);
}

if self.suggested > 0 {
let (line_break, extra_prefix) = if self.automatic > 0 {
("\n", format!("[{}]", "*".cyan()))
} else {
("", String::new())
};

let total = self.automatic + self.suggested;
fix_status = format!(
"{fix_status}{line_break}{extra_prefix} {total} potentially fixable with the --fix-suggested option."
);
let automatic_prefix = format!("[{}]", Applicability::Automatic.symbol().cyan());
let suggested_prefix = format!("[{}]", Applicability::Suggested.symbol().cyan());

if self.automatic > 0 && self.suggested > 0 {
format!(
"{automatic_prefix} {} fixable with the --fix option.\n\
{suggested_prefix} {} potentially fixable with the --fix-suggested option.",
self.automatic, self.suggested
)
} else if self.automatic > 0 {
format!(
"{automatic_prefix} {} fixable with the --fix option.",
self.automatic,
)
} else if self.suggested > 0 {
format!(
"{suggested_prefix} {} potentially fixable with the --fix-suggested option.",
self.suggested
)
} else {
String::new()
}

fix_status
}
}
28 changes: 14 additions & 14 deletions crates/ruff_cli/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ fn stdin_success() {
fn stdin_error() {
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
.args(STDIN_BASE_OPTIONS)
.pass_stdin("import os\n"), @r#"
.pass_stdin("import os\n"), @r###"
success: false
exit_code: 1
----- stdout -----
-:1:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 potentially fixable with the --fix option.
[*] 1 fixable with the --fix option.
----- stderr -----
"#);
"###);
}

#[test]
Expand All @@ -69,7 +69,7 @@ fn stdin_filename() {
----- stdout -----
F401.py:1:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 potentially fixable with the --fix option.
[*] 1 fixable with the --fix option.
----- stderr -----
"###);
Expand All @@ -87,7 +87,7 @@ fn stdin_source_type_py() {
----- stdout -----
TCH.py:1:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 potentially fixable with the --fix option.
[*] 1 fixable with the --fix option.
----- stderr -----
"###);
Expand Down Expand Up @@ -789,7 +789,7 @@ fn check_input_from_argfile() -> Result<()> {
----- stdout -----
/path/to/a.py:1:8: F401 [*] `os` imported but unused
Found 1 error.
[*] 1 potentially fixable with the --fix option.
[*] 1 fixable with the --fix option.
----- stderr -----
"###);
Expand All @@ -816,11 +816,11 @@ fn displays_fix_applicability_levels() {
success: false
exit_code: 1
----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated
-:1:14: F601 [**] Dictionary key literal `'a'` repeated
-:2:7: UP034 [*] Avoid extraneous parentheses
Found 2 errors.
[*] 1 potentially fixable with the --fix option.
[*] 2 potentially fixable with the --fix-suggested option.
[*] 1 fixable with the --fix option.
[**] 1 potentially fixable with the --fix-suggested option.
----- stderr -----
"###);
Expand All @@ -835,9 +835,9 @@ fn displays_remaining_suggested_fixes() {
success: false
exit_code: 1
----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated
-:1:14: F601 [**] Dictionary key literal `'a'` repeated
Found 1 error.
[*] 1 potentially fixable with the --fix-suggested option.
[**] 1 potentially fixable with the --fix-suggested option.
----- stderr -----
"###);
Expand Down Expand Up @@ -889,11 +889,11 @@ fn fix_applies_automatic_and_suggested_fixes_with_fix_suggested() {
success: false
exit_code: 1
----- stdout -----
-:1:14: F601 [*] Dictionary key literal `'a'` repeated
-:1:14: F601 [**] Dictionary key literal `'a'` repeated
-:2:7: UP034 [*] Avoid extraneous parentheses
Found 2 errors.
[*] 1 potentially fixable with the --fix option.
[*] 2 potentially fixable with the --fix-suggested option.
[*] 1 fixable with the --fix option.
[**] 1 potentially fixable with the --fix-suggested option.
----- stderr -----
"###);
Expand Down
10 changes: 10 additions & 0 deletions crates/ruff_diagnostics/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ pub enum Applicability {
Automatic,
}

impl Applicability {
pub fn symbol(&self) -> &'static str {
match self {
Self::Automatic => "*",
Self::Suggested => "**",
_ => "*",
}
}
}

/// Indicates the level of isolation required to apply a fix.
#[derive(Default, Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
4 changes: 3 additions & 1 deletion crates/ruff_linter/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,13 @@ impl Display for RuleCodeAndBody<'_> {
let kind = &self.message.kind;

if self.show_fix_status && self.message.fix.is_some() {
let indicator = self.message.fix.as_ref().unwrap().applicability().symbol();

write!(
f,
"{code} {fix}{body}",
code = kind.rule().noqa_code().to_string().red().bold(),
fix = format_args!("[{}] ", "*".cyan()),
fix = format_args!("[{}] ", indicator.cyan()),
body = kind.body,
)
} else {
Expand Down

0 comments on commit 9949fa0

Please sign in to comment.