summaryrefslogtreecommitdiff
path: root/src/File.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/File.zig')
-rw-r--r--src/File.zig65
1 files changed, 11 insertions, 54 deletions
diff --git a/src/File.zig b/src/File.zig
index 336b8526..f33d0f1b 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -2,7 +2,7 @@
//! index, syntax highlights and the body it fills.
const panes = @import("panes.zig");
const std = @import("std");
-const uucode = @import("uucode");
+const vaxis = @import("vaxis");
const builtin = @import("builtin");
const pardes = @import("pardes.zig");
const config = @import("config.zig");
@@ -186,8 +186,10 @@ pub const NodeStep = struct { ranges: []modal.Selection, primary: usize };
pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
if (grapheme.len == 1 and grapheme[0] >= 0x20 and grapheme[0] < 0x7f) return 1;
- if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, utf8Width(grapheme)));
- // Each invalid byte is a cell, as surface.zig draws it, and each valid
+ if (std.unicode.utf8ValidateSlice(grapheme)) return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
+ // vaxis's gwidth counts back a replacement rune's 3 bytes over the one
+ // invalid byte it stood for (`\xee` before `e\u{301}` overflowed it):
+ // each invalid byte is a cell, as surface.zig draws it, and each valid
// run between is measured alone.
var width: usize = 0;
var run: usize = 0;
@@ -198,60 +200,15 @@ pub fn graphemeDisplayWidth(grapheme: []const u8) usize {
i += n;
continue;
}
- if (run < i) width += utf8Width(grapheme[run..i]);
+ if (run < i) width += vaxis.gwidth.gwidth(grapheme[run..i], .unicode);
width += 1;
i += 1;
run = i;
}
- if (run < grapheme.len) width += utf8Width(grapheme[run..]);
+ if (run < grapheme.len) width += vaxis.gwidth.gwidth(grapheme[run..], .unicode);
return @max(1, width);
}
-/// The cells valid UTF-8 takes, summed over its graphemes: a grapheme is
-/// as wide as its widest code point (a control, a nonspacing or enclosing
-/// mark and the joiners, selectors and other format characters below add
-/// none; East Asian wide and fullwidth two, the rest one), then at least
-/// one under the text selector U+FE0E, else at least two under the emoji
-/// selector U+FE0F, with an emoji-presentation code point, or as a flag's
-/// two regional indicators. It is libvaxis's `gwidth(.unicode)` over the
-/// same uucode tables, which the tui renders with in mode 2027.
-pub fn utf8Width(valid: []const u8) u16 {
- var total: u16 = 0;
- var widest: u16 = 0;
- var text_vs = false;
- var emoji = false;
- var flags: u8 = 0;
- var it = uucode.grapheme.utf8Iterator(valid);
- while (it.nextCodePoint()) |r| {
- const cp = r.code_point;
- if (cp == 0xfe0e) {
- text_vs = true;
- } else if (cp == 0xfe0f) {
- emoji = true;
- } else {
- if (uucode.get(.is_emoji_presentation, cp)) emoji = true;
- if (cp >= 0x1f1e6 and cp <= 0x1f1ff) flags += 1;
- widest = @max(widest, @as(u16, switch (cp) {
- 0...0x1f, 0x7f...0x9f, 0xad, 0x34f, 0x180b...0x180d, 0x200b...0x200d, 0x2060, 0xfe00...0xfe0d, 0xfeff, 0xe0100...0xe01ef => 0,
- else => switch (uucode.get(.general_category, cp)) {
- .mark_nonspacing, .mark_enclosing => 0,
- else => switch (uucode.get(.east_asian_width, cp)) {
- .fullwidth, .wide => 2,
- else => 1,
- },
- },
- }));
- }
- if (!r.is_break) continue;
- total += if (text_vs) @max(1, widest) else if (emoji or flags == 2) @max(2, widest) else widest;
- widest = 0;
- text_vs = false;
- emoji = false;
- flags = 0;
- }
- return total;
-}
-
pub fn displayWidth(text: []const u8) usize {
var width: usize = 0;
var at: usize = 0;
@@ -2249,7 +2206,7 @@ pub fn paintWordSelection(
}
}
-test "an invalid byte in a grapheme is one cell, and never reaches utf8Width" {
+test "an invalid byte in a grapheme is one cell, and never reaches vaxis's width" {
try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xeee\xcc\x81"));
try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\xee\xffe"));
try std.testing.expectEqual(@as(usize, 3), graphemeDisplayWidth("\u{4e16}\x80"));
@@ -2288,11 +2245,11 @@ test "indexed source rows match uncached scans across content changes" {
}
}
-test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" {
+test "the ASCII arm of graphemeDisplayWidth matches the gwidth it skips" {
const ref = struct {
fn width(grapheme: []const u8) usize {
if (std.mem.eql(u8, grapheme, "\t")) return config.tab_now;
- return @max(1, @as(usize, utf8Width(grapheme)));
+ return @max(1, @as(usize, vaxis.gwidth.gwidth(grapheme, .unicode)));
}
}.width;
@@ -2307,7 +2264,7 @@ test "the ASCII arm of graphemeDisplayWidth matches the utf8Width it skips" {
"\u{1f642}", "\u{1f1e6}\u{1f1e7}", "\xff",
}) |g| try std.testing.expectEqual(ref(g), graphemeDisplayWidth(g));
// Two invalid bytes are two cells, as surface.zig draws them, where
- // one U+FFFD for the pair would give one.
+ // vaxis's own count gives the pair one.
try std.testing.expectEqual(@as(usize, 2), graphemeDisplayWidth("\xe4\xb8"));
}