@@ -6,18 +6,21 @@ import { Register, RegisterContent } from '../../register/register';
66import { RecordedState } from '../../state/recordedState' ;
77import { VimState } from '../../state/vimState' ;
88import { ExCommand } from '../../vimscript/exCommand' ;
9+ import { IPutCommandArguments , PutExCommand } from './put' ;
910
1011class 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}
0 commit comments