diff --git a/Justfile b/Justfile new file mode 100644 index 00000000000000..be1e35acc7b226 --- /dev/null +++ b/Justfile @@ -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 \ No newline at end of file diff --git a/crates/ruff/src/lib.rs b/crates/ruff/src/lib.rs index b45e588416106b..322da7304b8e1f 100644 --- a/crates/ruff/src/lib.rs +++ b/crates/ruff/src/lib.rs @@ -308,6 +308,10 @@ pub fn check(args: CheckCommand, global_options: GlobalConfigArgs) -> Result { - 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)?; } } @@ -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)?; } } @@ -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()?; @@ -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::()) @@ -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, diff --git a/crates/ruff_linter/src/message/a.py b/crates/ruff_linter/src/message/a.py new file mode 100644 index 00000000000000..7a432d38b37e34 --- /dev/null +++ b/crates/ruff_linter/src/message/a.py @@ -0,0 +1,2 @@ +def f() + g() \ No newline at end of file diff --git a/crates/ruff_linter/src/message/grouped.rs b/crates/ruff_linter/src/message/grouped.rs index 1dfa5d15e6b2b9..bdad86bafd6431 100644 --- a/crates/ruff_linter/src/message/grouped.rs +++ b/crates/ruff_linter/src/message/grouped.rs @@ -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, } @@ -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; @@ -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 { diff --git a/crates/ruff_linter/src/message/text.rs b/crates/ruff_linter/src/message/text.rs index 7b3c417e48e10e..a9ddd0e6e9d649 100644 --- a/crates/ruff_linter/src/message/text.rs +++ b/crates/ruff_linter/src/message/text.rs @@ -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; } } @@ -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 { @@ -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());