@@ -5,7 +5,7 @@ import { CodeActionParams, LanguageClient } from 'vscode-languageclient';
55import { Commands } from './commands' ;
66import { applyWorkspaceEdit } from './extension' ;
77import { ListOverridableMethodsRequest , AddOverridableMethodsRequest , CheckHashCodeEqualsStatusRequest , GenerateHashCodeEqualsRequest ,
8- OrganizeImportsRequest , ImportCandidate , ImportSelection , GenerateToStringRequest , CheckToStringStatusRequest , VariableField , ResolveUnimplementedAccessorsRequest , GenerateAccessorsRequest } from './protocol' ;
8+ OrganizeImportsRequest , ImportCandidate , ImportSelection , GenerateToStringRequest , CheckToStringStatusRequest , VariableBinding , ResolveUnimplementedAccessorsRequest , GenerateAccessorsRequest , CheckConstructorStatusRequest , GenerateConstructorsRequest } from './protocol' ;
99
1010export function registerCommands ( languageClient : LanguageClient , context : ExtensionContext ) {
1111 registerOverrideMethodsCommand ( languageClient , context ) ;
@@ -14,6 +14,7 @@ export function registerCommands(languageClient: LanguageClient, context: Extens
1414 registerChooseImportCommand ( context ) ;
1515 registerGenerateToStringCommand ( languageClient , context ) ;
1616 registerGenerateAccessorsCommand ( languageClient , context ) ;
17+ registerGenerateConstructorsCommand ( languageClient , context ) ;
1718}
1819
1920function registerOverrideMethodsCommand ( languageClient : LanguageClient , context : ExtensionContext ) : void {
@@ -176,7 +177,7 @@ function registerGenerateToStringCommand(languageClient: LanguageClient, context
176177 }
177178 }
178179
179- let fields : VariableField [ ] = [ ] ;
180+ let fields : VariableBinding [ ] = [ ] ;
180181 if ( result . fields && result . fields . length ) {
181182 const fieldItems = result . fields . map ( ( field ) => {
182183 return {
@@ -240,3 +241,57 @@ function registerGenerateAccessorsCommand(languageClient: LanguageClient, contex
240241 applyWorkspaceEdit ( workspaceEdit , languageClient ) ;
241242 } ) ) ;
242243}
244+
245+ function registerGenerateConstructorsCommand ( languageClient : LanguageClient , context : ExtensionContext ) : void {
246+ context . subscriptions . push ( commands . registerCommand ( Commands . GENERATE_CONSTRUCTORS_PROMPT , async ( params : CodeActionParams ) => {
247+ const status = await languageClient . sendRequest ( CheckConstructorStatusRequest . type , params ) ;
248+ if ( ! status || ! status . constructors || ! status . constructors . length ) {
249+ return ;
250+ }
251+
252+ let selectedConstructors = status . constructors ;
253+ let selectedFields = [ ] ;
254+ if ( status . constructors . length > 1 ) {
255+ const constructorItems = status . constructors . map ( ( constructor ) => {
256+ return {
257+ label : `${ constructor . name } (${ constructor . parameters . join ( ',' ) } )` ,
258+ originalConstructor : constructor ,
259+ } ;
260+ } ) ;
261+ const selectedConstructorItems = await window . showQuickPick ( constructorItems , {
262+ canPickMany : true ,
263+ placeHolder : 'Select super class constructor(s).' ,
264+ } ) ;
265+ if ( ! selectedConstructorItems || ! selectedConstructorItems . length ) {
266+ return ;
267+ }
268+
269+ selectedConstructors = selectedConstructorItems . map ( item => item . originalConstructor ) ;
270+ }
271+
272+ if ( status . fields . length ) {
273+ const fieldItems = status . fields . map ( ( field ) => {
274+ return {
275+ label : `${ field . name } : ${ field . type } ` ,
276+ originalField : field ,
277+ } ;
278+ } ) ;
279+ const selectedFieldItems = await window . showQuickPick ( fieldItems , {
280+ canPickMany : true ,
281+ placeHolder : 'Select fields to initialize by constructor(s).' ,
282+ } ) ;
283+ if ( ! selectedFieldItems ) {
284+ return ;
285+ }
286+
287+ selectedFields = selectedFieldItems . map ( item => item . originalField ) ;
288+ }
289+
290+ const workspaceEdit = await languageClient . sendRequest ( GenerateConstructorsRequest . type , {
291+ context : params ,
292+ constructors : selectedConstructors ,
293+ fields : selectedFields ,
294+ } ) ;
295+ applyWorkspaceEdit ( workspaceEdit , languageClient ) ;
296+ } ) ) ;
297+ }
0 commit comments