From 0cd98a3e6bd87d5c295dfa27f5285bd48ff14eda Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 1 Jul 2024 16:58:46 +0530 Subject: [PATCH] Use char-wise width instead of `str`-width --- crates/ruff_linter/src/fix/snippet.rs | 9 +++++++-- crates/ruff_linter/src/rules/isort/sorting.rs | 12 +++++++++--- crates/ruff_linter/src/rules/pycodestyle/overlong.rs | 4 +--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/crates/ruff_linter/src/fix/snippet.rs b/crates/ruff_linter/src/fix/snippet.rs index 19e7680db212e..a7bd7bc6ea465 100644 --- a/crates/ruff_linter/src/fix/snippet.rs +++ b/crates/ruff_linter/src/fix/snippet.rs @@ -1,4 +1,4 @@ -use unicode_width::UnicodeWidthStr; +use unicode_width::UnicodeWidthChar; /// A snippet of source code for user-facing display, as in a diagnostic. #[derive(Debug, Clone, PartialEq, Eq)] @@ -35,6 +35,11 @@ impl SourceCodeSnippet { /// Returns `true` if the source code should be truncated when included in a user-facing /// diagnostic. fn should_truncate(source_code: &str) -> bool { - source_code.width() > 50 || source_code.contains(['\r', '\n']) + source_code + .chars() + .map(|c| c.width().unwrap_or(0)) + .sum::() + > 50 + || source_code.contains(['\r', '\n']) } } diff --git a/crates/ruff_linter/src/rules/isort/sorting.rs b/crates/ruff_linter/src/rules/isort/sorting.rs index 0bf6646513440..829d743e4d01d 100644 --- a/crates/ruff_linter/src/rules/isort/sorting.rs +++ b/crates/ruff_linter/src/rules/isort/sorting.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, cmp::Ordering, cmp::Reverse}; use natord; -use unicode_width::UnicodeWidthStr; +use unicode_width::UnicodeWidthChar; use ruff_python_stdlib::str; @@ -106,7 +106,11 @@ impl<'a> ModuleKey<'a> { let maybe_length = (settings.length_sort || (settings.length_sort_straight && style == ImportStyle::Straight)) - .then_some(name.map(str::width).unwrap_or_default() + level as usize); + .then_some( + name.map(|name| name.chars().map(|c| c.width().unwrap_or(0)).sum::()) + .unwrap_or_default() + + level as usize, + ); let distance = match level { 0 => Distance::None, @@ -157,7 +161,9 @@ impl<'a> MemberKey<'a> { let member_type = settings .order_by_type .then_some(member_type(name, settings)); - let maybe_length = settings.length_sort.then_some(name.width()); + let maybe_length = settings + .length_sort + .then(|| name.chars().map(|c| c.width().unwrap_or(0)).sum()); let maybe_lowercase_name = (!settings.case_sensitive).then_some(NatOrdStr(maybe_lowercase(name))); let module_name = NatOrdStr::from(name); diff --git a/crates/ruff_linter/src/rules/pycodestyle/overlong.rs b/crates/ruff_linter/src/rules/pycodestyle/overlong.rs index b724f15659e3f..691ea9dd231aa 100644 --- a/crates/ruff_linter/src/rules/pycodestyle/overlong.rs +++ b/crates/ruff_linter/src/rules/pycodestyle/overlong.rs @@ -1,7 +1,5 @@ use std::ops::Deref; -use unicode_width::UnicodeWidthStr; - use ruff_python_trivia::{is_pragma_comment, CommentRanges}; use ruff_source_file::Line; use ruff_text_size::{TextLen, TextRange}; @@ -61,7 +59,7 @@ impl Overlong { // begins before the limit. let last_chunk = chunks.last().unwrap_or(second_chunk); if last_chunk.contains("://") { - if width.get() - last_chunk.width() <= limit.value() as usize { + if width.get() - measure(last_chunk, tab_size).get() <= limit.value() as usize { return None; } }