Handle non-printable characters in diff view (#11687)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
aditya pillai 2024-06-07 23:22:03 -07:00 committed by GitHub
parent ee1621b2f9
commit ed947792cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 82 additions and 59 deletions

View file

@ -10,6 +10,7 @@ use ruff_diagnostics::{Applicability, Fix};
use ruff_source_file::{OneIndexed, SourceFile};
use crate::message::Message;
use crate::text_helpers::ShowNonprinting;
/// Renders a diff that shows the code fixes.
///
@ -101,6 +102,7 @@ impl Display for Diff<'_> {
)?;
for (emphasized, value) in change.iter_strings_lossy() {
let value = value.show_nonprinting();
if emphasized {
write!(f, "{}", line_style.apply_to(&value).underline().on_black())?;
} else {

View file

@ -17,6 +17,7 @@ use crate::message::diff::Diff;
use crate::message::{Emitter, EmitterContext, Message};
use crate::registry::AsRule;
use crate::settings::types::UnsafeFixes;
use crate::text_helpers::ShowNonprinting;
bitflags! {
#[derive(Default)]
@ -251,6 +252,8 @@ impl Display for MessageCodeFrame<'_> {
range - start_offset,
);
let source_text = source.text.show_nonprinting();
let start_char = source.text[TextRange::up_to(source.annotation_range.start())]
.chars()
.count();
@ -262,7 +265,7 @@ impl Display for MessageCodeFrame<'_> {
let snippet = Snippet {
title: None,
slices: vec![Slice {
source: &source.text,
source: &source_text,
line_start: self.notebook_index.map_or_else(
|| start_index.get(),
|notebook_index| {