Skip to content

Commit

Permalink
fix displaying non-printable characters
Browse files Browse the repository at this point in the history
  • Loading branch information
pilleye committed Jun 5, 2024
1 parent b36dd1a commit 434dc95
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
1 change: 1 addition & 0 deletions crates/ruff_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod rule_selector;
pub mod rules;
pub mod settings;
pub mod source_kind;
mod text_helpers;
pub mod upstream_categories;

#[cfg(any(test, fuzzing))]
Expand Down
12 changes: 10 additions & 2 deletions crates/ruff_linter/src/message/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -102,9 +103,16 @@ impl Display for Diff<'_> {

for (emphasized, value) in change.iter_strings_lossy() {
if emphasized {
write!(f, "{}", line_style.apply_to(&value).underline().on_black())?;
write!(
f,
"{}",
line_style
.apply_to(&value.show_nonprinting())
.underline()
.on_black()
)?;
} else {
write!(f, "{}", line_style.apply_to(&value))?;
write!(f, "{}", line_style.apply_to(&value.show_nonprinting()))?;
}
}
if change.missing_newline() {
Expand Down
5 changes: 4 additions & 1 deletion crates/ruff_linter/src/message/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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();
Expand All @@ -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| {
Expand Down
21 changes: 16 additions & 5 deletions crates/ruff_linter/src/source_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ruff_python_ast::PySourceType;
use colored::Colorize;

use crate::fs;
use crate::text_helpers::ShowNonprinting;

#[derive(Clone, Debug, PartialEq, is_macro::Is)]
pub enum SourceKind {
Expand Down Expand Up @@ -220,8 +221,8 @@ impl<'a> CodeDiff<'a> {
impl std::fmt::Display for CodeDiff<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some((original, modified)) = self.header {
writeln!(f, "--- {}", original.red())?;
writeln!(f, "+++ {}", modified.green())?;
writeln!(f, "--- {}", original.show_nonprinting().red())?;
writeln!(f, "+++ {}", modified.show_nonprinting().green())?;
}

let mut unified = self.diff.unified_diff();
Expand All @@ -234,9 +235,19 @@ impl std::fmt::Display for CodeDiff<'_> {
// individual lines
for change in hunk.iter_changes() {
match change.tag() {
ChangeTag::Equal => write!(f, " {}", change.value())?,
ChangeTag::Delete => write!(f, "{}{}", "-".red(), change.value().red())?,
ChangeTag::Insert => write!(f, "{}{}", "+".green(), change.value().green())?,
ChangeTag::Equal => write!(f, " {}", change.value().show_nonprinting())?,
ChangeTag::Delete => write!(
f,
"{}{}",
"-".red(),
change.value().show_nonprinting().red()
)?,
ChangeTag::Insert => write!(
f,
"{}{}",
"+".green(),
change.value().show_nonprinting().green()
)?,
}

if !self.diff.newline_terminated() {
Expand Down
23 changes: 23 additions & 0 deletions crates/ruff_linter/src/text_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::borrow::Cow;

pub(crate) trait ShowNonprinting {
fn show_nonprinting(&self) -> Cow<'_, str>;
}

macro_rules! impl_show_nonprinting {
($(($from:expr, $to:expr)),+) => {
impl ShowNonprinting for str {
fn show_nonprinting(&self) -> Cow<'_, str> {
if self.find(&[$($from),*][..]).is_some() {
Cow::Owned(
self.$(replace($from, $to)).*
)
} else {
Cow::Borrowed(self)
}
}
}
};
}

impl_show_nonprinting!(('\x07', "␇"), ('\x08', "␈"), ('\x1b', "␛"), ('\x7f', "␡"));

0 comments on commit 434dc95

Please sign in to comment.