mirror of
https://github.com/crate-ci/typos.git
synced 2024-11-25 02:20:58 -05:00
Merge pull request #465 from foriequal0/fix/unicode-width
Fix misalignment in report highlight and indent when there are double width characters
This commit is contained in:
commit
4ad2f49190
6 changed files with 76 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1576,6 +1576,7 @@ dependencies = [
|
|||
"typos-vars",
|
||||
"unicase",
|
||||
"unicode-segmentation",
|
||||
"unicode-width",
|
||||
"varcon-core",
|
||||
"yansi",
|
||||
]
|
||||
|
|
|
@ -91,6 +91,7 @@ encoding = "0.2"
|
|||
kstring = { version = "2.0.0", features = ["serde"] }
|
||||
typed-arena = "2.0.1"
|
||||
maplit = "1.0"
|
||||
unicode-width = "0.1.9"
|
||||
|
||||
[dev-dependencies]
|
||||
assert_fs = "1.0"
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
use std::io::{self, Write};
|
||||
use std::sync::atomic;
|
||||
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use typos_cli::report::{Context, Message, Report, Typo};
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
|
@ -218,8 +220,11 @@ fn print_long_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Er
|
|||
let line_num = context.line_num.to_string();
|
||||
let line_indent: String = itertools::repeat_n(" ", line_num.len()).collect();
|
||||
|
||||
let hl_indent: String = itertools::repeat_n(" ", column).collect();
|
||||
let hl: String = itertools::repeat_n("^", msg.typo.len()).collect();
|
||||
let visible_column = UnicodeWidthStr::width(start.as_ref());
|
||||
let visible_len = UnicodeWidthStr::width(msg.typo);
|
||||
|
||||
let hl_indent: String = itertools::repeat_n(" ", visible_column).collect();
|
||||
let hl: String = itertools::repeat_n("^", visible_len).collect();
|
||||
|
||||
writeln!(handle, "{} |", line_indent)?;
|
||||
writeln!(
|
||||
|
|
34
tests/cmd/stdin-failure-multiwidth.stdin
Normal file
34
tests/cmd/stdin-failure-multiwidth.stdin
Normal file
|
@ -0,0 +1,34 @@
|
|||
---
|
||||
Korean character (NFC)
|
||||
Grapheme clusters: 1, codepoints: 1, UnicodeWidthStr::width() == 2
|
||||
|
||||
한 Apropriate world
|
||||
^^^^^^^^^^ highlight here
|
||||
|
||||
---
|
||||
Korean character (NFD).
|
||||
Grapheme clusters: 1, codepoints: 3, UnicodeWidthStr::width() == 2
|
||||
|
||||
한 Apropriate world
|
||||
^^^^^^^^^^ highlight here
|
||||
|
||||
---
|
||||
Eye in Speech Bubble Emoji (U+1F441 U+FE0F U+200D U+1F5E8 U+FE0F, Recommended Emoji ZWJ Sequences, v2.0)
|
||||
Grapheme clusters: 1, codepoints: 5, UnicodeWidthStr::width() == 2 (Read NOTE: https://github.com/unicode-rs/unicode-width)
|
||||
|
||||
👁️🗨️ Apropriate world
|
||||
^^^^^^^^^^ highlight here
|
||||
|
||||
---
|
||||
Face with spiral eyes (U+1F635 U+200D U+1F4AB, Recommended Emoji ZWJ Sequence, v13.1)
|
||||
Grapheme clusters: 1, codepoints: 3, UnicodeWidthStr::width() == 4 (Read NOTE: https://github.com/unicode-rs/unicode-width)
|
||||
|
||||
😵💫 Apropriate world
|
||||
^^^^^^^^^^ highlight here
|
||||
|
||||
---
|
||||
Horizontal tab (\t, U+09)
|
||||
Grapheme clusters: 1, codepoints: 1, UnicodeWidthStr::width() == 0
|
||||
|
||||
Apropriate world
|
||||
^^^^^^^^^^ highlight here
|
30
tests/cmd/stdin-failure-multiwidth.stdout
Normal file
30
tests/cmd/stdin-failure-multiwidth.stdout
Normal file
|
@ -0,0 +1,30 @@
|
|||
error: `Apropriate` should be `Appropriate`
|
||||
--> -:5:2
|
||||
|
|
||||
5 | 한 Apropriate world
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
error: `Apropriate` should be `Appropriate`
|
||||
--> -:12:2
|
||||
|
|
||||
12 | 한 Apropriate world
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
error: `Apropriate` should be `Appropriate`
|
||||
--> -:19:2
|
||||
|
|
||||
19 | 👁️🗨️ Apropriate world
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
error: `Apropriate` should be `Appropriate`
|
||||
--> -:26:2
|
||||
|
|
||||
26 | 😵💫 Apropriate world
|
||||
| ^^^^^^^^^^
|
||||
|
|
||||
error: `Apropriate` should be `Appropriate`
|
||||
--> -:33:1
|
||||
|
|
||||
33 | Apropriate world
|
||||
| ^^^^^^^^^^
|
||||
|
|
3
tests/cmd/stdin-failure-multiwidth.toml
Normal file
3
tests/cmd/stdin-failure-multiwidth.toml
Normal file
|
@ -0,0 +1,3 @@
|
|||
bin.name = "typos"
|
||||
args = "-"
|
||||
status.code = 2
|
Loading…
Reference in a new issue