mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-26 06:15:39 +08:00
## Summary This is a first step towards #20411. After this, we would work to either upstream changes or adjust things within Ruff. Performance improvements have already been upstreamed (https://github.com/rust-lang/annotate-snippets-rs/pull/437, https://github.com/rust-lang/annotate-snippets-rs/pull/440) Re-implemented the following from the fork: - Hidden snippets (https://github.com/rust-lang/annotate-snippets-rs/issues/448) - Renderer-level disabling of hyperlinks - Jupyter cell indices (https://github.com/rust-lang/annotate-snippets-rs/issues/449) - Fixable mark (https://github.com/rust-lang/annotate-snippets-rs/issues/450) - `lineno_offset` (maybe not needed if #20648 is taken care of) - anonymized lines affects origin (https://github.com/rust-lang/annotate-snippets-rs/issues/451) - cut indicator (maybe not needed if we can switch to unicode renderer) Additional hacks to minimize behavior change include - Adjust upstream's hidden severity behavior to match Ruff's (https://github.com/rust-lang/annotate-snippets-rs/issues/452) - How newlines are annotated (https://github.com/rust-lang/annotate-snippets-rs/issues/454) - To not indent for line numbers when none will be present (https://github.com/rust-lang/annotate-snippets-rs/issues/453) - Enabling `simd` by default All of the above represents work that needs to be resolved to address #20411. `lib.name` hack is used to simplify maintenance with the assumption that we'll continue working towards #20411. Behavior changes that were included - Tweak when implicit padding lines are present - Fix Origin's line/column is now derived from the primary annotation - Make consistent the rendering of primary/context annotations - Fix snippets to show up for empty source (but may degrade #24528) - Fix handling of term width - Fix rendering of annotations between newline and first character (granted, maybe the caller is wrong) - Tweak multiline annotation markers New or upcoming features annotate-snippets features that we will be able to leverage by moving this forward - `AnnotationKind::Visible` to improve the quality of built-in code-folding if Ruff were to switch to it - `DecorStyle::Unicode` - *(upcoming)* No-graphiscs mode for screen readers (may also be useful for AIs) Deferred: - Moving related `Group`s into the same `Report` - Using `secondary_title` for any `Group` after the first in a `Report` - Using a `Snippet`, instead of titles, for "virtual code" like https://github.com/astral-sh/ruff/pull/27033#discussion_r3622727236 - Switching hidden snippets to `Origin` ## Test Plan Within cpython, I ran ```console $ hyperfine -i "../../ruff/target-baseline/release/ruff check" "../../ruff/target-update/release/ruff check" -N -m20 --output=null Benchmark 1: ../../ruff/target-baseline/release/ruff check Time (mean ± σ): 27.5 ms ± 1.1 ms [User: 33.0 ms, System: 108.4 ms] Range (min … max): 25.0 ms … 31.7 ms 94 runs Warning: Ignoring non-zero exit code. Benchmark 2: ../../ruff/target-update/release/ruff check Time (mean ± σ): 28.1 ms ± 2.0 ms [User: 33.4 ms, System: 105.7 ms] Range (min … max): 24.8 ms … 38.9 ms 102 runs Warning: Ignoring non-zero exit code. Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use t he '--warmup' or '--prepare' options. Summary ../../ruff/target-baseline/release/ruff check ran 1.03 ± 0.08 times faster than ../../ruff/target-update/release/ruff check ``` Before: <img width="486" height="583" alt="image" src="https://github.com/user-attachments/assets/2d94dabf-b28d-48c1-b17c-52aac59fd57e" /> After: <img width="486" height="583" alt="image" src="https://github.com/user-attachments/assets/b388ada3-e3d7-4ba0-b90b-1330eb81f4be" />
49 lines
1.6 KiB
Rust
49 lines
1.6 KiB
Rust
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
|
|
|
|
fn main() {
|
|
let source = r#") -> Option<String> {
|
|
for ann in annotations {
|
|
match (ann.range.0, ann.range.1) {
|
|
(None, None) => continue,
|
|
(Some(start), Some(end)) if start > end_index => continue,
|
|
(Some(start), Some(end)) if start >= start_index => {
|
|
let label = if let Some(ref label) = ann.label {
|
|
format!(" {}", label)
|
|
} else {
|
|
String::from("")
|
|
};
|
|
|
|
return Some(format!(
|
|
"{}{}{}",
|
|
" ".repeat(start - start_index),
|
|
"^".repeat(end - start),
|
|
label
|
|
));
|
|
}
|
|
_ => continue,
|
|
}
|
|
}"#;
|
|
let report = &[Level::ERROR
|
|
.primary_title("mismatched types")
|
|
.id("E0308")
|
|
.element(
|
|
Snippet::source(source)
|
|
.line_start(51)
|
|
.path("src/format.rs")
|
|
.fold(false)
|
|
.annotation(
|
|
AnnotationKind::Context
|
|
.span(5..19)
|
|
.label("expected `Option<String>` because of return type"),
|
|
)
|
|
.annotation(
|
|
AnnotationKind::Primary
|
|
.span(26..724)
|
|
.label("expected enum `std::option::Option`"),
|
|
),
|
|
)];
|
|
|
|
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
|
|
anstream::println!("{}", renderer.render(report));
|
|
}
|