Skip to content

Commit

Permalink
ruff_linter,ruff_python_parser: migrate to updated annotate-snippets
Browse files Browse the repository at this point in the history
This is pretty much just moving to the new API and taking care to use
byte offsets. This is *almost* enough. The next commit will fix a bug
involving the handling of unprintable characters as a result of
switching to byte offsets.
  • Loading branch information
BurntSushi committed Jan 15, 2025
1 parent 1b97677 commit 84179aa
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 99 deletions.
25 changes: 3 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ red_knot_test = { path = "crates/red_knot_test" }
red_knot_workspace = { path = "crates/red_knot_workspace", default-features = false }

aho-corasick = { version = "1.1.3" }
annotate-snippets = { version = "0.9.2", features = ["color"] }
anstream = { version = "0.6.18" }
anstyle = { version = "1.0.10" }
anyhow = { version = "1.0.80" }
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license = { workspace = true }
[lib]

[dependencies]
ruff_annotate_snippets = { workspace = true }
ruff_cache = { workspace = true }
ruff_diagnostics = { workspace = true, features = ["serde"] }
ruff_index = { workspace = true }
Expand All @@ -30,7 +31,6 @@ ruff_source_file = { workspace = true, features = ["serde"] }
ruff_text_size = { workspace = true }

aho-corasick = { workspace = true }
annotate-snippets = { workspace = true, features = ["color"] }
anyhow = { workspace = true }
bitflags = { workspace = true }
chrono = { workspace = true }
Expand Down
73 changes: 27 additions & 46 deletions crates/ruff_linter/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::io::Write;

use annotate_snippets::display_list::{DisplayList, FormatOptions};
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
use bitflags::bitflags;
use colored::Colorize;
use ruff_annotate_snippets::{Level, Renderer, Snippet};

use ruff_notebook::NotebookIndex;
use ruff_source_file::{OneIndexed, SourceLocation};
Expand Down Expand Up @@ -186,12 +185,8 @@ pub(super) struct MessageCodeFrame<'a> {
impl Display for MessageCodeFrame<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let suggestion = self.message.suggestion();
let footer = if suggestion.is_some() {
vec![Annotation {
id: None,
label: suggestion,
annotation_type: AnnotationType::Help,
}]
let footers = if let Some(suggestion) = suggestion {
vec![Level::Help.title(suggestion)]
} else {
Vec::new()
};
Expand Down Expand Up @@ -257,51 +252,37 @@ impl Display for MessageCodeFrame<'_> {

let source_text = source.text.show_nonprinting();

let start_char = source.text[TextRange::up_to(source.annotation_range.start())]
.chars()
.count();

let char_length = source.text[source.annotation_range].chars().count();

let label = self
.message
.rule()
.map_or_else(String::new, |rule| rule.noqa_code().to_string());

let snippet = Snippet {
title: None,
slices: vec![Slice {
source: &source_text,
line_start: self.notebook_index.map_or_else(
|| start_index.get(),
|notebook_index| {
notebook_index
.cell_row(start_index)
.unwrap_or(OneIndexed::MIN)
.get()
},
),
annotations: vec![SourceAnnotation {
label: &label,
annotation_type: AnnotationType::Error,
range: (start_char, start_char + char_length),
}],
// The origin (file name, line number, and column number) is already encoded
// in the `label`.
origin: None,
fold: false,
}],
footer,
opt: FormatOptions {
#[cfg(test)]
color: false,
#[cfg(not(test))]
color: colored::control::SHOULD_COLORIZE.should_colorize(),
..FormatOptions::default()
let line_start = self.notebook_index.map_or_else(
|| start_index.get(),
|notebook_index| {
notebook_index
.cell_row(start_index)
.unwrap_or(OneIndexed::MIN)
.get()
},
};
);

writeln!(f, "{message}", message = DisplayList::from(snippet))
let span = usize::from(source.annotation_range.start())
..usize::from(source.annotation_range.end());
let annotation = Level::Error.span(span).label(&label);
let snippet = Snippet::source(&source_text)
.line_start(line_start)
.annotation(annotation)
.fold(false);
let message = Level::None.title("").snippet(snippet).footers(footers);

let renderer = if !cfg!(test) && colored::control::SHOULD_COLORIZE.should_colorize() {
Renderer::styled()
} else {
Renderer::plain()
};
let rendered = renderer.render(message);
writeln!(f, "{rendered}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ unicode_names2 = { workspace = true }
unicode-normalization = { workspace = true }

[dev-dependencies]
ruff_annotate_snippets = { workspace = true }
ruff_source_file = { workspace = true }

annotate-snippets = { workspace = true }
anyhow = { workspace = true }
insta = { workspace = true, features = ["glob"] }
walkdir = { workspace = true }
Expand Down
39 changes: 11 additions & 28 deletions crates/ruff_python_parser/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::fmt::{Formatter, Write};
use std::fs;
use std::path::Path;

use annotate_snippets::display_list::{DisplayList, FormatOptions};
use annotate_snippets::snippet::{AnnotationType, Slice, Snippet, SourceAnnotation};

use ruff_annotate_snippets::{Level, Renderer, Snippet};
use ruff_python_ast::visitor::source_order::{walk_module, SourceOrderVisitor, TraversalSignal};
use ruff_python_ast::{AnyNodeRef, Mod};
use ruff_python_parser::{parse_unchecked, Mode, ParseErrorType, Token};
Expand Down Expand Up @@ -203,33 +201,18 @@ impl std::fmt::Display for CodeFrame<'_> {
.source_code
.slice(TextRange::new(start_offset, end_offset));

let start_char = source[TextRange::up_to(annotation_range.start())]
.chars()
.count();

let char_length = source[annotation_range].chars().count();
let label = format!("Syntax Error: {error}", error = self.error);

let snippet = Snippet {
title: None,
slices: vec![Slice {
source,
line_start: start_index.get(),
annotations: vec![SourceAnnotation {
label: &label,
annotation_type: AnnotationType::Error,
range: (start_char, start_char + char_length),
}],
// The origin (file name, line number, and column number) is already encoded
// in the `label`.
origin: None,
fold: false,
}],
footer: Vec::new(),
opt: FormatOptions::default(),
};

writeln!(f, "{message}", message = DisplayList::from(snippet))
let span = usize::from(annotation_range.start())..usize::from(annotation_range.end());
let annotation = Level::Error.span(span).label(&label);
let snippet = Snippet::source(source)
.line_start(start_index.get())
.annotation(annotation)
.fold(false);
let message = Level::None.title("").snippet(snippet);
let renderer = Renderer::plain();
let rendered = renderer.render(message);
writeln!(f, "{rendered}")
}
}

Expand Down

0 comments on commit 84179aa

Please sign in to comment.