diff --git a/src/actions/commands/digraphs.ts b/src/actions/commands/digraphs.ts index c5f38258..c7b047fa 100644 --- a/src/actions/commands/digraphs.ts +++ b/src/actions/commands/digraphs.ts @@ -1,5 +1,5 @@ // prettier-ignore -export const DefaultDigraphs = new Map([ +export const DefaultDigraphs = new Map([ ["NU", ["^@", 10]], ["SH", ["^A", 1]], ["SX", ["^B", 2]], diff --git a/src/cmd_line/commands/digraph.ts b/src/cmd_line/commands/digraph.ts index cb3cfb6b..66058953 100644 --- a/src/cmd_line/commands/digraph.ts +++ b/src/cmd_line/commands/digraph.ts @@ -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 = 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 { - // 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); + } + }); + } } } diff --git a/test/mode/modeInsert.test.ts b/test/mode/modeInsert.test.ts index 3b67ca19..147b3e59 100644 --- a/test/mode/modeInsert.test.ts +++ b/test/mode/modeInsert.test.ts @@ -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' + 'iR!', + end: ['🚀|'], + endMode: Mode.Insert, + }); + suite('', () => { newTest({ title: 'Basic test', diff --git a/test/vimscript/exCommandParse.test.ts b/test/vimscript/exCommandParse.test.ts index 6a1ba176..bf693bb0 100644 --- a/test/vimscript/exCommandParse.test.ts +++ b/test/vimscript/exCommandParse.test.ts @@ -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]], }), );