Skip to content

Commit

Permalink
feat: add new FullPath OutputFormat (astral-sh#14985)
Browse files Browse the repository at this point in the history
  • Loading branch information
BPL committed Dec 16, 2024
1 parent 425c248 commit f8e508e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 15 deletions.
12 changes: 12 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export RUFF_UPDATE_SCHEMA := "1"

alias b:=build

pre-commit *args:
cargo dev generate-all
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test {{args}}
uvx pre-commit run --all-files --show-diff-on-failure

build:
cargo build --release
4 changes: 4 additions & 0 deletions crates/ruff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ pub fn check(args: CheckCommand, global_options: GlobalConfigArgs) -> Result<Exi
printer_flags |= PrinterFlags::SHOW_FIX_SUMMARY;
}

// TODO: Discuss with ruff owner what'd be the proper way to expose this
// flag to the user from the cli args
printer_flags |= PrinterFlags::SHOW_FULL_PATH;

#[cfg(debug_assertions)]
if cache {
// `--no-cache` doesn't respect code changes, and so is often confusing during
Expand Down
52 changes: 45 additions & 7 deletions crates/ruff/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ bitflags! {
const SHOW_FIX_SUMMARY = 1 << 1;
/// Whether to show a diff of each fixed violation when emitting diagnostics.
const SHOW_FIX_DIFF = 1 << 2;
/// Whether to show full paths or not
const SHOW_FULL_PATH = 1 << 3;
}
}

Expand Down Expand Up @@ -249,7 +251,11 @@ impl Printer {
if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fix_summary(writer, &diagnostics.fixed)?;
print_fix_summary(
writer,
&diagnostics.fixed,
self.flags.intersects(Flags::SHOW_FULL_PATH),
)?;
writeln!(writer)?;
}
}
Expand All @@ -275,17 +281,25 @@ impl Printer {
JunitEmitter.emit(writer, &diagnostics.messages, &context)?;
}
OutputFormat::Concise | OutputFormat::Full => {
TextEmitter::default()
let emitter = TextEmitter::default()
.with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref()))
.with_show_fix_diff(self.flags.intersects(Flags::SHOW_FIX_DIFF))
.with_show_source(self.format == OutputFormat::Full)
.with_unsafe_fixes(self.unsafe_fixes)
.with_unsafe_fixes(self.unsafe_fixes);

// Use the show_full_path variable here
emitter
.with_show_full_path(self.flags.intersects(Flags::SHOW_FULL_PATH))
.emit(writer, &diagnostics.messages, &context)?;

if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fix_summary(writer, &diagnostics.fixed)?;
print_fix_summary(
writer,
&diagnostics.fixed,
self.flags.intersects(Flags::SHOW_FULL_PATH),
)?;
writeln!(writer)?;
}
}
Expand All @@ -296,12 +310,17 @@ impl Printer {
GroupedEmitter::default()
.with_show_fix_status(show_fix_status(self.fix_mode, fixables.as_ref()))
.with_unsafe_fixes(self.unsafe_fixes)
.with_show_full_path(self.flags.intersects(Flags::SHOW_FULL_PATH))
.emit(writer, &diagnostics.messages, &context)?;

if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fix_summary(writer, &diagnostics.fixed)?;
print_fix_summary(
writer,
&diagnostics.fixed,
self.flags.intersects(Flags::SHOW_FULL_PATH),
)?;
writeln!(writer)?;
}
}
Expand Down Expand Up @@ -468,6 +487,18 @@ impl Printer {
.with_show_source(preview)
.with_unsafe_fixes(self.unsafe_fixes)
.emit(writer, &diagnostics.messages, &context)?;

if self.flags.intersects(Flags::SHOW_FIX_SUMMARY) {
if !diagnostics.fixed.is_empty() {
writeln!(writer)?;
print_fix_summary(
writer,
&diagnostics.fixed,
self.flags.intersects(Flags::SHOW_FULL_PATH),
)?;
writeln!(writer)?;
}
}
}
writer.flush()?;

Expand Down Expand Up @@ -498,7 +529,7 @@ fn show_fix_status(fix_mode: flags::FixMode, fixables: Option<&FixableStatistics
(!fix_mode.is_apply()) && fixables.is_some_and(FixableStatistics::any_applicable_fixes)
}

fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap) -> Result<()> {
fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap, show_full_path: bool) -> Result<()> {
let total = fixed
.values()
.map(|table| table.values().sum::<usize>())
Expand All @@ -520,13 +551,20 @@ fn print_fix_summary(writer: &mut dyn Write, fixed: &FixMap) -> Result<()> {
.iter()
.sorted_by_key(|(filename, ..)| filename.as_str())
{
let display_path = if show_full_path {
filename.to_string()
} else {
relativize_path(filename).to_string()
};

writeln!(
writer,
"{} {}{}",
"-".cyan(),
relativize_path(filename).bold(),
display_path.bold(),
":".cyan()
)?;

for (rule, count) in table.iter().sorted_by_key(|(.., count)| Reverse(*count)) {
writeln!(
writer,
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/message/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def f()
g()
16 changes: 14 additions & 2 deletions crates/ruff_linter/src/message/grouped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::settings::types::UnsafeFixes;
pub struct GroupedEmitter {
show_fix_status: bool,
show_source: bool,
show_full_path: bool,
unsafe_fixes: UnsafeFixes,
}

Expand All @@ -35,6 +36,12 @@ impl GroupedEmitter {
self
}

#[must_use]
pub fn with_show_full_path(mut self, show_full_path: bool) -> Self {
self.show_full_path = show_full_path; // Set flag
self
}

#[must_use]
pub fn with_unsafe_fixes(mut self, unsafe_fixes: UnsafeFixes) -> Self {
self.unsafe_fixes = unsafe_fixes;
Expand Down Expand Up @@ -64,8 +71,13 @@ impl Emitter for GroupedEmitter {
let row_length = calculate_print_width(max_row_length);
let column_length = calculate_print_width(max_column_length);

// Print the filename.
writeln!(writer, "{}:", relativize_path(filename).underline())?;
// Print the filename, checking the `show_full_path` flag
if self.show_full_path {
writeln!(writer, "{}:", filename.cyan())?; // Print full path
} else {
writeln!(writer, "{}:", relativize_path(filename).underline())?;
// Print relative path
}

// Print each message.
for message in messages {
Expand Down
31 changes: 25 additions & 6 deletions crates/ruff_linter/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ bitflags! {
const SHOW_FIX_DIFF = 1 << 1;
/// Whether to show the source code of a diagnostic.
const SHOW_SOURCE = 1 << 2;
/// Whether to show the full path in diagnostics.
const SHOW_FULL_PATH = 1 << 3;
}
}

Expand Down Expand Up @@ -61,6 +63,12 @@ impl TextEmitter {
self.unsafe_fixes = unsafe_fixes;
self
}

#[must_use]
pub fn with_show_full_path(mut self, full_path: bool) -> Self {
self.flags.set(EmitterFlags::SHOW_FULL_PATH, full_path);
self
}
}

impl Emitter for TextEmitter {
Expand All @@ -71,12 +79,23 @@ impl Emitter for TextEmitter {
context: &EmitterContext,
) -> anyhow::Result<()> {
for message in messages {
write!(
writer,
"{path}{sep}",
path = relativize_path(message.filename()).bold(),
sep = ":".cyan(),
)?;
let full_path = self.flags.contains(EmitterFlags::SHOW_FULL_PATH);

if full_path {
write!(
writer,
"{path}{sep}",
path = message.filename(),
sep = ":".cyan(),
)?;
} else {
write!(
writer,
"{path}{sep}",
path = relativize_path(message.filename()).bold(),
sep = ":".cyan(),
)?;
}

let start_location = message.compute_start_location();
let notebook_index = context.notebook_index(message.filename());
Expand Down

0 comments on commit f8e508e

Please sign in to comment.