Support for adding custom digraphs with :dig[raphs]

This commit is contained in:
Jason Fields 2024-03-27 20:08:02 -04:00
parent 331676c608
commit edef39e7c3
4 changed files with 32 additions and 26 deletions

View File

@ -1,5 +1,5 @@
// prettier-ignore
export const DefaultDigraphs = new Map<string, [string, number]>([
export const DefaultDigraphs = new Map<string, [string, number | number[]]>([
["NU", ["^@", 10]],
["SH", ["^A", 1]],
["SX", ["^B", 2]],

View File

@ -12,7 +12,7 @@ import { configuration } from './../../configuration/configuration';
export interface IDigraphsCommandArguments {
bang: boolean;
newDigraphs: Array<[string, string, number]>;
newDigraph: [string, string, number[]] | undefined;
}
interface DigraphQuickPickItem extends vscode.QuickPickItem {
@ -22,8 +22,8 @@ interface DigraphQuickPickItem extends vscode.QuickPickItem {
export class DigraphsCommand extends ExCommand {
public static readonly argParser: Parser<DigraphsCommand> = seq(
bangParser,
whitespace.then(seq(any, any, whitespace.then(numberParser))).many(),
).map(([bang, newDigraphs]) => new DigraphsCommand({ bang, newDigraphs }));
whitespace.then(seq(any, any, whitespace.then(numberParser).atLeast(1))).fallback(undefined),
).map(([bang, newDigraph]) => new DigraphsCommand({ bang, newDigraph }));
private readonly arguments: IDigraphsCommandArguments;
constructor(args: IDigraphsCommandArguments) {
@ -45,17 +45,21 @@ export class DigraphsCommand extends ExCommand {
}
async execute(vimState: VimState): Promise<void> {
// TODO: use arguments
if (this.arguments.newDigraph) {
const digraph = this.arguments.newDigraph[0] + this.arguments.newDigraph[1];
const charCodes = this.arguments.newDigraph[2];
DefaultDigraphs.set(digraph, [String.fromCharCode(...charCodes), charCodes]);
} else {
const digraphKeyAndContent = this.makeQuickPicks(
Object.entries(configuration.digraphs),
).concat(this.makeQuickPicks([...DefaultDigraphs.entries()]));
const digraphKeyAndContent = this.makeQuickPicks(Object.entries(configuration.digraphs)).concat(
this.makeQuickPicks([...DefaultDigraphs.entries()]),
);
void vscode.window.showQuickPick(digraphKeyAndContent).then(async (val) => {
if (val) {
const char = String.fromCharCode(...val.charCodes);
await TextEditor.insert(vimState.editor, char);
}
});
void vscode.window.showQuickPick(digraphKeyAndContent).then(async (val) => {
if (val) {
const char = String.fromCharCode(...val.charCodes);
await TextEditor.insert(vimState.editor, char);
}
});
}
}
}

View File

@ -494,6 +494,14 @@ suite('Mode Insert', () => {
assertEqualLines(['🚀🚀']);
});
newTest({
title: 'Can insert custom digraph made with :dig[raphs]`',
start: ['|'],
keysPressed: ':dig R! 55357 56960\n' + 'i<C-k>R!',
end: ['🚀|'],
endMode: Mode.Insert,
});
suite('<C-a>', () => {
newTest({
title: 'Basic <C-a> test',

View File

@ -200,20 +200,14 @@ suite('Ex command parsing', () => {
});
suite(':dig[raphs]', () => {
exParseTest(':dig', new DigraphsCommand({ bang: false, newDigraphs: [] }));
exParseTest(':dig!', new DigraphsCommand({ bang: true, newDigraphs: [] }));
exParseTest(':dig', new DigraphsCommand({ bang: false, newDigraph: undefined }));
exParseTest(':dig!', new DigraphsCommand({ bang: true, newDigraph: undefined }));
exParseTest(':dig e: 235', new DigraphsCommand({ bang: false, newDigraph: ['e', ':', [235]] }));
exParseTest(
':dig e: 235',
new DigraphsCommand({ bang: false, newDigraphs: [['e', ':', 235]] }),
);
exParseTest(
':dig e: 235 a: 238',
':dig R! 55357 56960',
new DigraphsCommand({
bang: false,
newDigraphs: [
['e', ':', 235],
['a', ':', 238],
],
newDigraph: ['R', '!', [55357, 56960]],
}),
);