Skip to content

Commit

Permalink
Wrap lines using the character width not count
Browse files Browse the repository at this point in the history
Instead of counting the number of grapheme clusters per line, we now
count the character _widths_. This ensures that lines containing double
width characters are wrapped correctly.

Changelog: fixed
  • Loading branch information
yorickpeterse committed Dec 20, 2024
1 parent 4ffcf15 commit 9cae074
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doctest = false
ast = { path = "../ast" }
types = { path = "../types" }
location = { path = "../location" }
unicode-segmentation = "^1.10"
unicode-width = "^0.2"
getopts = "^0.2"
fnv = "^1.0"
blake3 = "^1.5"
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::collections::HashSet;
use std::fs::{read, write};
use std::io::{stdin, stdout, Error as IoError, Read as _, Write as _};
use std::path::PathBuf;
use unicode_segmentation::UnicodeSegmentation as _;
use unicode_width::UnicodeWidthStr;

/// The characters to use for indentation.
const INDENT: char = ' ';
Expand Down Expand Up @@ -79,7 +79,7 @@ enum Node {
/// The arguments are:
///
/// 1. The text that may include Unicode characters
/// 2. The number of grapheme clusters in the string
/// 2. The width (in cells) of the string.
Unicode(String, usize),

/// A node to include if the code is to be wrapped across lines.
Expand Down Expand Up @@ -149,7 +149,7 @@ impl Node {
}

fn unicode(text: String) -> Node {
let width = text.graphemes(true).count();
let width = text.width();

Node::Unicode(text, width)
}
Expand Down
1 change: 1 addition & 0 deletions std/fixtures/fmt/double_width_characters/input.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let FULL_WEEKDAYS = ['日曜日', '月曜日', '火曜日', '水曜日', '木曜日', '金曜日', '土曜日']
9 changes: 9 additions & 0 deletions std/fixtures/fmt/double_width_characters/output.inko
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let FULL_WEEKDAYS = [
'日曜日',
'月曜日',
'火曜日',
'水曜日',
'木曜日',
'金曜日',
'土曜日',
]

0 comments on commit 9cae074

Please sign in to comment.