Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Handling jk motion for cjk and full width characters #9471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 130 additions & 13 deletions src/actions/motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,52 @@ import { ModeDataFor } from '../mode/modeData';
* A movement is something like 'h', 'k', 'w', 'b', 'gg', etc.
*/

export function isFullWidthCharacter(charCode: number): boolean {
// Do a cheap trick to better support wrapping of wide characters, treat them as 2 columns
// http://jrgraphix.net/research/unicode_blocks.php
// 2E80 - 2EFF CJK Radicals Supplement
// 2F00 - 2FDF Kangxi Radicals
// 2FF0 - 2FFF Ideographic Description Characters
// 3000 - 303F CJK Symbols and Punctuation
// 3040 - 309F Hiragana
// 30A0 - 30FF Katakana
// 3100 - 312F Bopomofo
// 3130 - 318F Hangul Compatibility Jamo
// 3190 - 319F Kanbun
// 31A0 - 31BF Bopomofo Extended
// 31F0 - 31FF Katakana Phonetic Extensions
// 3200 - 32FF Enclosed CJK Letters and Months
// 3300 - 33FF CJK Compatibility
// 3400 - 4DBF CJK Unified Ideographs Extension A
// 4DC0 - 4DFF Yijing Hexagram Symbols
// 4E00 - 9FFF CJK Unified Ideographs
// A000 - A48F Yi Syllables
// A490 - A4CF Yi Radicals
// AC00 - D7AF Hangul Syllables
// [IGNORE] D800 - DB7F High Surrogates
// [IGNORE] DB80 - DBFF High Private Use Surrogates
// [IGNORE] DC00 - DFFF Low Surrogates
// [IGNORE] E000 - F8FF Private Use Area
// F900 - FAFF CJK Compatibility Ideographs
// [IGNORE] FB00 - FB4F Alphabetic Presentation Forms
// [IGNORE] FB50 - FDFF Arabic Presentation Forms-A
// [IGNORE] FE00 - FE0F Variation Selectors
// [IGNORE] FE20 - FE2F Combining Half Marks
// [IGNORE] FE30 - FE4F CJK Compatibility Forms
// [IGNORE] FE50 - FE6F Small Form Variants
// [IGNORE] FE70 - FEFF Arabic Presentation Forms-B
// FF00 - FFEF Halfwidth and Fullwidth Forms
// [https://en.wikipedia.org/wiki/Halfwidth_and_fullwidth_forms]
// of which FF01 - FF5E fullwidth ASCII of 21 to 7E
// [IGNORE] and FF65 - FFDC halfwidth of Katakana and Hangul
// [IGNORE] FFF0 - FFFF Specials
return (
(charCode >= 0x2e80 && charCode <= 0xd7af) ||
(charCode >= 0xf900 && charCode <= 0xfaff) ||
(charCode >= 0xff01 && charCode <= 0xff5e)
);
}

export abstract class ExpandingSelection extends BaseMovement {
protected override selectionType = SelectionType.Expanding;

Expand Down Expand Up @@ -285,6 +331,10 @@ class MoveDown extends BaseMovement {
keys = [['j'], ['<down>'], ['<C-j>'], ['<C-n>']];
override preservesDesiredColumn = true;

public static nextRenderTabStop(visibleColumn: number, tabSize: number): number {
return visibleColumn + tabSize - (visibleColumn % tabSize);
}

public override async execAction(position: Position, vimState: VimState): Promise<Position> {
if (
vimState.currentMode === Mode.Insert &&
Expand All @@ -303,7 +353,40 @@ class MoveDown extends BaseMovement {
}

if (position.line < vimState.document.lineCount - 1) {
return position.with({ character: vimState.desiredColumn }).getDown();
const newPosition = position.with({ character: vimState.desiredColumn }).getDown();
const lineText = vimState.document.lineAt(position.line).text;
const nextLineText = vimState.document.lineAt(newPosition.line).text;
let characterPositionNormalized = 0;
const minLineLength = Math.min(lineText.length, nextLineText.length, vimState.desiredColumn);
let fullWidthCharacters = 0;
for (let i = 0; i < minLineLength; i++) {
const charCode = nextLineText.charCodeAt(i);
if (charCode === 9) {
characterPositionNormalized += MoveDown.nextRenderTabStop(
characterPositionNormalized,
Number(vimState.editor.options.tabSize!),
);
} else if (isFullWidthCharacter(charCode)) {
characterPositionNormalized += 2;
fullWidthCharacters++;
} else {
characterPositionNormalized += 1;
}
}
for (let i = 0; i < minLineLength + fullWidthCharacters; i++) {
const charCode = nextLineText.charCodeAt(i);
if (charCode === 9) {
characterPositionNormalized += MoveDown.nextRenderTabStop(
characterPositionNormalized,
Number(vimState.editor.options.tabSize!),
);
} else if (isFullWidthCharacter(charCode)) {
characterPositionNormalized -= 1;
fullWidthCharacters--;
}
}

return newPosition.with({ character: characterPositionNormalized });
} else {
return position;
}
Expand Down Expand Up @@ -341,7 +424,41 @@ class MoveUp extends BaseMovement {
}

if (position.line > 0) {
return position.with({ character: vimState.desiredColumn }).getUp();
const newPosition = position.with({ character: vimState.desiredColumn }).getUp();
const lineText = vimState.document.lineAt(position.line).text;
const nextLineText = vimState.document.lineAt(newPosition.line).text;
let characterPositionNormalized = 0;
const minLineLength = Math.min(lineText.length, nextLineText.length, vimState.desiredColumn);
let fullWidthCharacters = 0;
for (let i = 0; i < minLineLength; i++) {
const charCode = nextLineText.charCodeAt(i);
if (charCode === 9) {
characterPositionNormalized += MoveDown.nextRenderTabStop(
characterPositionNormalized,
Number(vimState.editor.options.tabSize!),
);
} else if (isFullWidthCharacter(charCode)) {
characterPositionNormalized += 2;
fullWidthCharacters++;
} else {
characterPositionNormalized += 1;
}
}

for (let i = 0; i < minLineLength + fullWidthCharacters; i++) {
const charCode = nextLineText.charCodeAt(i);
if (charCode === 9) {
characterPositionNormalized += MoveDown.nextRenderTabStop(
characterPositionNormalized,
Number(vimState.editor.options.tabSize!),
);
} else if (isFullWidthCharacter(charCode)) {
characterPositionNormalized -= 1;
fullWidthCharacters--;
}
}

return newPosition.with({ character: characterPositionNormalized });
} else {
return position;
}
Expand Down Expand Up @@ -1458,12 +1575,12 @@ export class MoveWordBegin extends BaseMovement {
const char = line.text[position.character];

/*
From the Vim manual:
From the Vim manual:

Special case: "cw" and "cW" are treated like "ce" and "cE" if the cursor is
on a non-blank. This is because "cw" is interpreted as change-word, and a
word does not include the following white space.
*/
Special case: "cw" and "cW" are treated like "ce" and "cE" if the cursor is
on a non-blank. This is because "cw" is interpreted as change-word, and a
word does not include the following white space.
*/

if (' \t'.includes(char)) {
return position.nextWordStart(vimState.document);
Expand All @@ -1484,13 +1601,13 @@ export class MoveWordBegin extends BaseMovement {
const result = await this.execAction(position, vimState, firstIteration, lastIteration);

/*
From the Vim documentation:
From the Vim documentation:

Another special case: When using the "w" motion in combination with an
operator and the last word moved over is at the end of a line, the end of
that word becomes the end of the operated text, not the first word in the
next line.
*/
Another special case: When using the "w" motion in combination with an
operator and the last word moved over is at the end of a line, the end of
that word becomes the end of the operated text, not the first word in the
next line.
*/

if (
result.line > position.line + 1 ||
Expand Down
16 changes: 16 additions & 0 deletions test/mode/normalModeTests/motions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import { newTest, newTestSkip } from '../../testSimplifier';
suite('Motions in Normal Mode', () => {
suiteSetup(setupWorkspace);

suite('cjk', () => {
newTest({
title: 'j moves to right char downwards',
start: ['ああ|あああ123456', 'aaaaaaaaaa123456'],
keysPressed: 'j',
end: ['あああああ123456', 'aaaa|aaaaaa123456'],
});

newTest({
title: 'k moves to right char upwards',
start: ['あああああ123456', 'aaaa|aaaaaa123456'],
keysPressed: 'k',
end: ['ああ|あああ123456', 'aaaaaaaaaa123456'],
});
});

suite('w', () => {
newTest({
title: 'w moves to next word',
Expand Down