Skip to content
Closed
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
12 changes: 9 additions & 3 deletions src/runtime/replacement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { CommandToken } from "./parser.js";
import { Suggestion } from "./model.js";
import { wcswidth } from "../utils/unicode.js";

export type ReplacementAction = {
backspaceCount: number;
Expand All @@ -15,6 +16,11 @@ const commonPrefixLength = (a: string, b: string): number => {
return i;
};

// Shells delete one character per backspace, but tokenLength is a display width in which wide
// characters (e.g. CJK) count as 2. Quotes and escapes are single-width, so the difference between
// the width and the character count of the token text is exactly the extra width to remove.
const typedCharCount = (token: CommandToken): number => token.tokenLength - (wcswidth(token.token) - Array.from(token.token).length);

export const calculateReplacement = (token: CommandToken | undefined, suggestion: Suggestion): ReplacementAction | undefined => {
const suggestionText = suggestion.insertValue ?? suggestion.name + " ";

Expand All @@ -24,10 +30,10 @@ export const calculateReplacement = (token: CommandToken | undefined, suggestion
}

const typedText = token.token;
const tokenDisplayWidth = token.tokenLength;
const tokenCharCount = typedCharCount(token);

if (suggestion.insertValue != null && suggestion.insertValue !== suggestion.name) {
return { backspaceCount: tokenDisplayWidth, insertText: suggestionText };
return { backspaceCount: tokenCharCount, insertText: suggestionText };
}

const target = suggestion.name + " ";
Expand All @@ -39,7 +45,7 @@ export const calculateReplacement = (token: CommandToken | undefined, suggestion
return { backspaceCount: 0, insertText: remaining };
}

return { backspaceCount: tokenDisplayWidth, insertText: target };
return { backspaceCount: tokenCharCount, insertText: target };
};

export const applyReplacement = (action: ReplacementAction): string => {
Expand Down
11 changes: 9 additions & 2 deletions src/tests/runtime/replacement.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,19 @@ describe("calculateReplacement", () => {
insertText: "好 ",
});
});
test("wide-char divergent uses display width for backspace", () => {
test("wide-char divergent uses character count for backspace", () => {
// shells delete one character per backspace regardless of its display width
expect(calculateReplacement(makeToken({ token: "你", tokenLength: 2 }), makeSuggestion({ name: "世界" }))).toEqual({
backspaceCount: 2,
backspaceCount: 1,
insertText: "世界 ",
});
});
test("mixed-width divergent deletes one backspace per character", () => {
expect(calculateReplacement(makeToken({ token: "a你", tokenLength: 3 }), makeSuggestion({ name: "b" }))).toEqual({
backspaceCount: 2,
insertText: "b ",
});
});
test("option prefix appends remaining", () => {
expect(calculateReplacement(makeToken({ token: "--ver", isOption: true }), makeSuggestion({ name: "--version" }))).toEqual({
backspaceCount: 0,
Expand Down