Файли
openidea/src/editor/core/cm/visibleColumn.test.ts
T
l0ng-ai c6621c5c36 fix(editor): Count wide characters as two columns in column editing (#172)
Column-based editing computed columns with countColumn / findColumn, which count every grapheme as one column, and up/down and Clone Caret moved by pixel x, so block selections and cloned carets drifted on lines with CJK, fullwidth punctuation, Hangul, kana or emoji. A shared visible-column helper (UI side only, UTF-16 offsets in and out) now counts East Asian wide/fullwidth characters and emoji (ZWJ sequences, modifiers, flags, VS16) as 2, combining marks and format characters as 0, and expands tabs to the next tab stop.

It drives Alt+drag block selection, drag and Shift+arrows in Column Selection Mode, middle-drag block selection (new; not on Linux, where middle-click pastes the primary selection), the up/down goal column (kept across short lines; soft-wrapped and folded lines still move visually), Clone Caret Above/Below, the caret move after Comment Line, Tab padding to the next indent stop, and macro playback of arrow keys.

Claude-Session: https://claude.ai/code/session_01Dq6jQQn8ZhRfkkyj3PRutA
2026-10-29 08:32:15 +08:00

96 рядки
3.5 KiB
TypeScript
Неформатований Анотація Історія

Цей файл містить невидимі символи Юнікоду
Цей файл містить невидимі символи Юнікоду, які не розрізняються людиною, але можуть по-різному оброблятися комп'ютером. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
import { describe, expect, it } from "vitest";
import { findClusterBreak } from "@codemirror/state";
import {
clusterWidth,
codePointWidth,
offsetAtVisibleColumn,
stepVisibleColumn,
visibleColumn,
visibleWidth,
} from "./visibleColumn";
const WIDTHS: [label: string, text: string, width: number][] = [
["ASCII", "id = 42", 7],
["CJK ideographs", "张三", 4],
["mixed line", 'name = "张三" // 中文注释', 26],
["fullwidth punctuation", ",。!:()", 12],
["ideographic space", "a b", 4],
["fullwidth Latin", "AB1", 6],
["halfwidth katakana", "アイ", 2],
["hiragana / katakana", "かなカナ", 8],
["Hangul", "한글", 4],
["CJK Ext B", "𠀀", 2],
["emoji", "😀", 2],
["emoji ZWJ sequence", "👨‍👩‍👧", 2],
["emoji with skin tone", "👍🏽", 2],
["flag", "🇨🇳", 2],
["text-style pictograph", "❤", 1],
["pictograph + VS16", "❤️", 2],
["combining acute", "é", 1],
["several combining marks", "à̖b", 2],
["zero-width space", "a​b", 2],
["ambiguous arrow", "→", 1],
["tab", "\t", 4],
["tab after ASCII", "a\tb", 5],
["tab after CJK", "张\tb", 5],
["tab after 4 columns of CJK", "张三\tb", 9],
["empty", "", 0],
];
describe("visible columns", () => {
it.each(WIDTHS)("%s", (_label, text, width) => {
expect(visibleWidth(text, 4)).toBe(width);
});
it.each(WIDTHS)("round-trips every cluster boundary: %s", (_label, text) => {
for (let i = 0; ; i = findClusterBreak(text, i)) {
const col = visibleColumn(text, i, 4);
const back = offsetAtVisibleColumn(text, col, 4);
// A zero-width cluster shares its column with the boundary before it.
if (clusterWidth(text, back, i) === 0) expect(back).toBeLessThanOrEqual(i);
else expect(back).toBe(i);
if (i >= text.length) break;
}
});
it("code points", () => {
expect(codePointWidth(0x41)).toBe(1);
expect(codePointWidth(0x4e2d)).toBe(2);
expect(codePointWidth(0xff0c)).toBe(2);
expect(codePointWidth(0x0301)).toBe(0);
expect(codePointWidth(0x200d)).toBe(0);
expect(codePointWidth(0x1f600)).toBe(2);
expect(clusterWidth("é")).toBe(1);
});
it("maps a column inside a wide character to the nearer side", () => {
// "a张b": a [0,1) 张 [1,3) b [3,4); UTF-16 offsets 0, 1, 2, 3.
const t = "a张b";
expect([0, 1, 2, 3, 4].map((c) => offsetAtVisibleColumn(t, c, 4))).toEqual([0, 1, 2, 2, 3]);
expect(offsetAtVisibleColumn(t, 2, 4, "before")).toBe(1);
expect(offsetAtVisibleColumn(t, 2, 4, "after")).toBe(2);
expect(offsetAtVisibleColumn(t, 9, 4)).toBe(3);
expect(offsetAtVisibleColumn(t, 9, 4, "nearest", true)).toBe(-1);
});
it("returns UTF-16 offsets around astral characters", () => {
// "a😀b": the emoji is two code units and two columns.
expect(offsetAtVisibleColumn("a😀b", 3, 4)).toBe(3);
expect(visibleColumn("a😀b", 3, 4)).toBe(3);
expect(offsetAtVisibleColumn("a👨‍👩‍👧b", 3, 4)).toBe(1 + "👨‍👩‍👧".length);
});
it("steps one cluster at a time, then one column past the end", () => {
const t = "a张\tb";
// Columns: a 0, 张 1..3, tab 3..4, b 4..5.
expect(stepVisibleColumn(t, 0, 1, 4)).toBe(1);
expect(stepVisibleColumn(t, 1, 1, 4)).toBe(3);
expect(stepVisibleColumn(t, 3, 1, 4)).toBe(4);
expect(stepVisibleColumn(t, 5, 1, 4)).toBe(6);
expect(stepVisibleColumn(t, 6, -1, 4)).toBe(5);
expect(stepVisibleColumn(t, 3, -1, 4)).toBe(1);
expect(stepVisibleColumn(t, 2, -1, 4)).toBe(1);
expect(stepVisibleColumn(t, 1, -1, 4)).toBe(0);
});
});