Skip to content

Commit

Permalink
Use char-wise width instead of str-width
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvmanila committed Jul 1, 2024
1 parent d107968 commit 0cd98a3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
9 changes: 7 additions & 2 deletions crates/ruff_linter/src/fix/snippet.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<usize>()
> 50
|| source_code.contains(['\r', '\n'])
}
}
12 changes: 9 additions & 3 deletions crates/ruff_linter/src/rules/isort/sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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::<usize>())
.unwrap_or_default()
+ level as usize,
);

let distance = match level {
0 => Distance::None,
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions crates/ruff_linter/src/rules/pycodestyle/overlong.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 0cd98a3

Please sign in to comment.