mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:56 +00:00
ruff_linter,ruff_python_parser: migrate to updated annotate-snippets
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.
This commit is contained in:
parent
1b97677779
commit
84179aaa96
6 changed files with 43 additions and 99 deletions
|
@ -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};
|
||||
|
@ -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}")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue