mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Better formatter CLI verbose output (#7129)
This commit is contained in:
parent
154fe7bdcc
commit
0465b03282
3 changed files with 41 additions and 34 deletions
|
@ -64,7 +64,7 @@ shellexpand = { workspace = true }
|
||||||
similar = { workspace = true }
|
similar = { workspace = true }
|
||||||
strum = { workspace = true, features = [] }
|
strum = { workspace = true, features = [] }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
tracing = { workspace = true }
|
tracing = { workspace = true, features = ["log"] }
|
||||||
walkdir = { version = "2.3.2" }
|
walkdir = { version = "2.3.2" }
|
||||||
wild = { version = "2" }
|
wild = { version = "2" }
|
||||||
|
|
||||||
|
|
|
@ -8,10 +8,10 @@ use std::time::Instant;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use log::{debug, warn};
|
|
||||||
use rayon::iter::Either::{Left, Right};
|
use rayon::iter::Either::{Left, Right};
|
||||||
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
use rayon::iter::{IntoParallelIterator, ParallelIterator};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use tracing::{debug, warn};
|
||||||
|
|
||||||
use ruff::fs;
|
use ruff::fs;
|
||||||
use ruff::logging::LogLevel;
|
use ruff::logging::LogLevel;
|
||||||
|
@ -61,30 +61,38 @@ pub(crate) fn format(
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let (results, errors): (Vec<_>, Vec<_>) = paths
|
let (results, errors): (Vec<_>, Vec<_>) = paths
|
||||||
.into_par_iter()
|
.into_par_iter()
|
||||||
.filter_map(|entry| match entry {
|
.filter_map(|entry| {
|
||||||
Ok(entry) => {
|
match entry {
|
||||||
let path = entry.path();
|
Ok(entry) => {
|
||||||
|
let path = entry.path();
|
||||||
|
|
||||||
let SourceType::Python(source_type @ (PySourceType::Python | PySourceType::Stub)) =
|
let SourceType::Python(
|
||||||
SourceType::from(path)
|
source_type @ (PySourceType::Python | PySourceType::Stub),
|
||||||
else {
|
) = SourceType::from(path)
|
||||||
// Ignore any non-Python files.
|
else {
|
||||||
return None;
|
// Ignore any non-Python files.
|
||||||
};
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
let line_length = resolver.resolve(path, &pyproject_config).line_length;
|
let line_length = resolver.resolve(path, &pyproject_config).line_length;
|
||||||
let options = PyFormatOptions::from_source_type(source_type)
|
let options = PyFormatOptions::from_source_type(source_type)
|
||||||
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)));
|
.with_line_width(LineWidth::from(NonZeroU16::from(line_length)));
|
||||||
Some(format_path(path, options, mode))
|
debug!("Formatting {} with {:?}", path.display(), options);
|
||||||
|
Some(format_path(path, options, mode))
|
||||||
|
}
|
||||||
|
Err(err) => Some(Err(FormatCommandError::Ignore(err))),
|
||||||
}
|
}
|
||||||
Err(err) => Some(Err(FormatCommandError::Ignore(err))),
|
|
||||||
})
|
})
|
||||||
.partition_map(|result| match result {
|
.partition_map(|result| match result {
|
||||||
Ok(diagnostic) => Left(diagnostic),
|
Ok(diagnostic) => Left(diagnostic),
|
||||||
Err(err) => Right(err),
|
Err(err) => Right(err),
|
||||||
});
|
});
|
||||||
let duration = start.elapsed();
|
let duration = start.elapsed();
|
||||||
debug!("Formatted files in: {:?}", duration);
|
debug!(
|
||||||
|
"Formatted {} files in {:.2?}",
|
||||||
|
results.len() + errors.len(),
|
||||||
|
duration
|
||||||
|
);
|
||||||
|
|
||||||
let summary = FormatResultSummary::new(results, mode);
|
let summary = FormatResultSummary::new(results, mode);
|
||||||
|
|
||||||
|
|
|
@ -54,32 +54,31 @@ impl<'a> Printer<'a> {
|
||||||
|
|
||||||
/// Prints the passed in element as well as all its content,
|
/// Prints the passed in element as well as all its content,
|
||||||
/// starting at the specified indentation level
|
/// starting at the specified indentation level
|
||||||
|
#[tracing::instrument(name = "Printer::print", skip_all)]
|
||||||
pub fn print_with_indent(
|
pub fn print_with_indent(
|
||||||
mut self,
|
mut self,
|
||||||
document: &'a Document,
|
document: &'a Document,
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) -> PrintResult<Printed> {
|
) -> PrintResult<Printed> {
|
||||||
tracing::debug_span!("Printer::print").in_scope(move || {
|
let mut stack = PrintCallStack::new(PrintElementArgs::new(Indention::Level(indent)));
|
||||||
let mut stack = PrintCallStack::new(PrintElementArgs::new(Indention::Level(indent)));
|
let mut queue: PrintQueue<'a> = PrintQueue::new(document.as_ref());
|
||||||
let mut queue: PrintQueue<'a> = PrintQueue::new(document.as_ref());
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(element) = queue.pop() {
|
if let Some(element) = queue.pop() {
|
||||||
self.print_element(&mut stack, &mut queue, element)?;
|
self.print_element(&mut stack, &mut queue, element)?;
|
||||||
} else {
|
} else {
|
||||||
if !self.flush_line_suffixes(&mut queue, &mut stack, None) {
|
if !self.flush_line_suffixes(&mut queue, &mut stack, None) {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Printed::new(
|
Ok(Printed::new(
|
||||||
self.state.buffer,
|
self.state.buffer,
|
||||||
None,
|
None,
|
||||||
self.state.source_markers,
|
self.state.source_markers,
|
||||||
self.state.verbatim_markers,
|
self.state.verbatim_markers,
|
||||||
))
|
))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prints a single element and push the following elements to queue
|
/// Prints a single element and push the following elements to queue
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue