Skip to content

Commit 585cf5e

Browse files
author
Matthias Gessinger
committed
Implement paste action on button click
1 parent de2e82d commit 585cf5e

File tree

2 files changed

+76
-39
lines changed

2 files changed

+76
-39
lines changed

src/cmd_line/commands/register.ts

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ import { Register, RegisterContent } from '../../register/register';
66
import { RecordedState } from '../../state/recordedState';
77
import { VimState } from '../../state/vimState';
88
import { ExCommand } from '../../vimscript/exCommand';
9+
import { IPutCommandArguments, PutExCommand } from './put';
910

1011
class RegisterDisplayItem implements vscode.QuickPickItem {
1112
public readonly label: string;
1213
public readonly description: string;
14+
public readonly buttons: readonly vscode.QuickInputButton[];
1315

16+
public readonly key: string;
1417
public readonly content: RegisterContent | undefined;
1518
public readonly stringContent: string;
1619

17-
public readonly buttons: readonly vscode.QuickInputButton[];
18-
1920
constructor(registerKey: string, content: RegisterContent | undefined) {
2021
this.label = registerKey;
22+
this.key = registerKey;
23+
2124
this.content = content;
2225
this.stringContent = '';
2326
this.description = '';
@@ -26,15 +29,14 @@ class RegisterDisplayItem implements vscode.QuickPickItem {
2629
if (typeof content === 'string') {
2730
this.stringContent = content;
2831
this.description = this.stringContent;
29-
} else if (content instanceof Array) {
30-
this.stringContent = content.join('\n');
31-
this.description = this.stringContent;
3232
} else if (content instanceof RecordedState) {
3333
this.description = content.actionsRun.map((x) => x.keysPressed.join('')).join('');
3434
}
3535

36-
// maximum length of 100 characters for the description
37-
this.description = this.description.slice(0, 100);
36+
if (this.description.length > 100) {
37+
// maximum length of 100 characters for the description
38+
this.description = this.description.slice(0, 97) + '...';
39+
}
3840

3941
if (this.stringContent !== '') {
4042
this.buttons = [
@@ -58,27 +60,15 @@ export class RegisterCommand extends ExCommand {
5860

5961
constructor(registers: string[]) {
6062
super();
61-
this.registerKeys = Register.getKeys()
62-
.filter((reg) => reg !== '_' && (registers.length === 0 || registers.includes(reg)))
63-
.sort((reg1: string, reg2: string) => this.regSortOrder(reg1) - this.regSortOrder(reg2));
64-
}
6563

66-
private regSortOrder(register: string): number {
67-
const specials = ['-', '*', '+', '.', ':', '%', '#', '/', '='];
68-
if (register === '"') {
69-
return 0;
70-
} else if (register >= '0' && register <= '9') {
71-
return 10 + parseInt(register, 10);
72-
} else if (register >= 'a' && register <= 'z') {
73-
return 100 + (register.charCodeAt(0) - 'a'.charCodeAt(0));
74-
} else if (specials.includes(register)) {
75-
return 1000 + specials.indexOf(register);
76-
} else {
77-
throw new Error(`Unexpected register ${register}`);
64+
this.registerKeys = Register.getKeysSorted().filter((r) => !Register.isBlackHoleRegister(r));
65+
66+
if (registers.length > 0) {
67+
this.registerKeys = this.registerKeys.filter((r) => registers.includes(r));
7868
}
7969
}
8070

81-
async execute(_: VimState): Promise<void> {
71+
async execute(vimState: VimState): Promise<void> {
8272
const quickPick = vscode.window.createQuickPick<RegisterDisplayItem>();
8373

8474
quickPick.items = await Promise.all(
@@ -92,27 +82,53 @@ export class RegisterCommand extends ExCommand {
9282
if (items.length === 0) {
9383
return;
9484
}
95-
const item = items[0];
9685

97-
vscode.window.showInformationMessage(`${item.label} ${item.stringContent}`);
86+
RegisterCommand.showRegisterContent(vimState, items[0]);
9887
quickPick.dispose();
9988
});
10089

101-
quickPick.onDidTriggerItemButton((event) => {
102-
const content = event.item.stringContent;
103-
104-
if (content !== '') {
105-
const editor = vscode.window.activeTextEditor;
106-
if (editor) {
107-
editor.edit((editBuilder) => {
108-
editBuilder.insert(editor.selection.active, content);
109-
});
110-
}
111-
}
112-
90+
quickPick.onDidTriggerItemButton(async (event) => {
91+
void RegisterCommand.paste(vimState, event.item);
11392
quickPick.dispose();
11493
});
11594

11695
quickPick.show();
11796
}
97+
98+
private static showRegisterContent(vimState: VimState, item: RegisterDisplayItem) {
99+
const paste: vscode.MessageItem = {
100+
title: 'Paste',
101+
isCloseAffordance: false,
102+
};
103+
104+
void vscode.window
105+
.showInformationMessage(`${item.label} ${item.stringContent}`, paste)
106+
.then((action) => {
107+
if (!action || action !== paste) {
108+
return;
109+
}
110+
111+
void RegisterCommand.paste(vimState, item);
112+
});
113+
}
114+
115+
private static async paste(vimState: VimState, item: RegisterDisplayItem) {
116+
// TODO: Can I reuse PutCommand here?
117+
118+
const content = item.stringContent;
119+
if (content === '') {
120+
return;
121+
}
122+
123+
const editor = vscode.window.activeTextEditor;
124+
if (!editor) {
125+
return;
126+
}
127+
128+
vimState.recordedState.registerKey = item.key;
129+
130+
editor.edit((builder) => {
131+
builder.insert(vimState.cursorStopPosition, content);
132+
});
133+
}
118134
}

src/register/register.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export class Register {
9494
return /^[a-zA-Z0-9:]$/.test(register);
9595
}
9696

97-
private static isBlackHoleRegister(registerName: string): boolean {
97+
public static isBlackHoleRegister(registerName: string): boolean {
9898
return registerName === '_';
9999
}
100100

@@ -322,6 +322,12 @@ export class Register {
322322
return [...Register.registers.keys()];
323323
}
324324

325+
public static getKeysSorted(): string[] {
326+
return this.getKeys().sort(
327+
(reg1: string, reg2: string) => this.sortIndex(reg1) - this.sortIndex(reg2),
328+
);
329+
}
330+
325331
public static clearAllRegisters(): void {
326332
Register.registers.clear();
327333
}
@@ -367,4 +373,19 @@ export class Register {
367373
Register.registers = new Map();
368374
}
369375
}
376+
377+
private static sortIndex(register: string): number {
378+
const specials = ['-', '*', '+', '.', ':', '%', '#', '/', '='];
379+
if (register === '"') {
380+
return 0;
381+
} else if (register >= '0' && register <= '9') {
382+
return 10 + parseInt(register, 10);
383+
} else if (register >= 'a' && register <= 'z') {
384+
return 100 + (register.charCodeAt(0) - 'a'.charCodeAt(0));
385+
} else if (specials.includes(register)) {
386+
return 1000 + specials.indexOf(register);
387+
} else {
388+
throw new Error(`Unexpected register ${register}`);
389+
}
390+
}
370391
}

0 commit comments

Comments
 (0)