|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -import { commands, window, ExtensionContext, ViewColumn, Uri, Disposable } from 'vscode'; |
4 | | -import { CodeActionParams } from 'vscode-languageclient'; |
| 3 | +import { commands, window, ExtensionContext, ViewColumn, Uri, Disposable, workspace, TextEditorRevealType } from 'vscode'; |
| 4 | +import { CodeActionParams, WorkspaceEdit } from 'vscode-languageclient'; |
5 | 5 | import { LanguageClient } from 'vscode-languageclient/node'; |
6 | 6 | import { Commands } from './commands'; |
7 | 7 | import { applyWorkspaceEdit } from './extension'; |
8 | 8 | import { ListOverridableMethodsRequest, AddOverridableMethodsRequest, CheckHashCodeEqualsStatusRequest, GenerateHashCodeEqualsRequest, |
9 | | -OrganizeImportsRequest, ImportCandidate, ImportSelection, GenerateToStringRequest, CheckToStringStatusRequest, VariableBinding, ResolveUnimplementedAccessorsRequest, GenerateAccessorsRequest, CheckConstructorStatusRequest, GenerateConstructorsRequest, CheckDelegateMethodsStatusRequest, GenerateDelegateMethodsRequest } from './protocol'; |
| 9 | +OrganizeImportsRequest, ImportCandidate, ImportSelection, GenerateToStringRequest, CheckToStringStatusRequest, VariableBinding, GenerateAccessorsRequest, CheckConstructorStatusRequest, GenerateConstructorsRequest, CheckDelegateMethodsStatusRequest, GenerateDelegateMethodsRequest, AccessorKind, AccessorCodeActionRequest, AccessorCodeActionParams } from './protocol'; |
10 | 10 |
|
11 | 11 | export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) { |
12 | 12 | registerOverrideMethodsCommand(languageClient, context); |
@@ -63,6 +63,7 @@ function registerOverrideMethodsCommand(languageClient: LanguageClient, context: |
63 | 63 | overridableMethods: selectedItems.map((item) => item.originalMethod), |
64 | 64 | }); |
65 | 65 | await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 66 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
66 | 67 | })); |
67 | 68 | } |
68 | 69 |
|
@@ -106,6 +107,7 @@ function registerHashCodeEqualsCommand(languageClient: LanguageClient, context: |
106 | 107 | regenerate |
107 | 108 | }); |
108 | 109 | await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 110 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
109 | 111 | })); |
110 | 112 | } |
111 | 113 |
|
@@ -204,44 +206,64 @@ function registerGenerateToStringCommand(languageClient: LanguageClient, context |
204 | 206 | fields, |
205 | 207 | }); |
206 | 208 | await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 209 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
207 | 210 | })); |
208 | 211 | } |
209 | 212 |
|
210 | 213 | function registerGenerateAccessorsCommand(languageClient: LanguageClient, context: ExtensionContext): void { |
211 | | - context.subscriptions.push(commands.registerCommand(Commands.GENERATE_ACCESSORS_PROMPT, async (params: CodeActionParams) => { |
212 | | - const accessors = await languageClient.sendRequest(ResolveUnimplementedAccessorsRequest.type, params); |
213 | | - if (!accessors || !accessors.length) { |
214 | | - return; |
215 | | - } |
| 214 | + context.subscriptions.push(commands.registerCommand(Commands.GENERATE_ACCESSORS_PROMPT, async (params: AccessorCodeActionParams) => { |
| 215 | + await generateAccessors(languageClient, params); |
| 216 | + })); |
| 217 | +} |
216 | 218 |
|
217 | | - const accessorItems = accessors.map((accessor) => { |
218 | | - const description = []; |
219 | | - if (accessor.generateGetter) { |
220 | | - description.push('getter'); |
221 | | - } |
222 | | - if (accessor.generateSetter) { |
223 | | - description.push('setter'); |
224 | | - } |
225 | | - return { |
226 | | - label: accessor.fieldName, |
227 | | - description: (accessor.isStatic ? 'static ' : '')+ description.join(', '), |
228 | | - originalField: accessor, |
229 | | - }; |
230 | | - }); |
231 | | - const selectedAccessors = await window.showQuickPick(accessorItems, { |
232 | | - canPickMany: true, |
233 | | - placeHolder: 'Select the fields to generate getters and setters.' |
234 | | - }); |
235 | | - if (!selectedAccessors || !selectedAccessors.length) { |
236 | | - return; |
237 | | - } |
| 219 | +async function generateAccessors(languageClient: LanguageClient, params: AccessorCodeActionParams): Promise<void> { |
| 220 | + const accessors = await languageClient.sendRequest(AccessorCodeActionRequest.type, params); |
| 221 | + if (!accessors || !accessors.length) { |
| 222 | + return; |
| 223 | + } |
238 | 224 |
|
239 | | - const workspaceEdit = await languageClient.sendRequest(GenerateAccessorsRequest.type, { |
240 | | - context: params, |
241 | | - accessors: selectedAccessors.map((item) => item.originalField), |
242 | | - }); |
243 | | - await applyWorkspaceEdit(workspaceEdit, languageClient); |
244 | | - })); |
| 225 | + const accessorItems = accessors.map((accessor) => { |
| 226 | + const description = []; |
| 227 | + if (accessor.generateGetter) { |
| 228 | + description.push('getter'); |
| 229 | + } |
| 230 | + if (accessor.generateSetter) { |
| 231 | + description.push('setter'); |
| 232 | + } |
| 233 | + return { |
| 234 | + label: accessor.fieldName, |
| 235 | + description: (accessor.isStatic ? 'static ' : '')+ description.join(', '), |
| 236 | + originalField: accessor, |
| 237 | + }; |
| 238 | + }); |
| 239 | + let accessorsKind: string; |
| 240 | + switch (params.kind) { |
| 241 | + case AccessorKind.BOTH: |
| 242 | + accessorsKind = "getters and setters"; |
| 243 | + break; |
| 244 | + case AccessorKind.GETTER: |
| 245 | + accessorsKind = "getters"; |
| 246 | + break; |
| 247 | + case AccessorKind.SETTER: |
| 248 | + accessorsKind = "setters"; |
| 249 | + break; |
| 250 | + default: |
| 251 | + return; |
| 252 | + } |
| 253 | + const selectedAccessors = await window.showQuickPick(accessorItems, { |
| 254 | + canPickMany: true, |
| 255 | + placeHolder: `Select the fields to generate ${accessorsKind}` |
| 256 | + }); |
| 257 | + if (!selectedAccessors || !selectedAccessors.length) { |
| 258 | + return; |
| 259 | + } |
| 260 | + |
| 261 | + const workspaceEdit = await languageClient.sendRequest(GenerateAccessorsRequest.type, { |
| 262 | + context: params, |
| 263 | + accessors: selectedAccessors.map((item) => item.originalField), |
| 264 | + }); |
| 265 | + await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 266 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
245 | 267 | } |
246 | 268 |
|
247 | 269 | function registerGenerateConstructorsCommand(languageClient: LanguageClient, context: ExtensionContext): void { |
@@ -295,6 +317,7 @@ function registerGenerateConstructorsCommand(languageClient: LanguageClient, con |
295 | 317 | fields: selectedFields, |
296 | 318 | }); |
297 | 319 | await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 320 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
298 | 321 | })); |
299 | 322 | } |
300 | 323 |
|
@@ -356,5 +379,21 @@ function registerGenerateDelegateMethodsCommand(languageClient: LanguageClient, |
356 | 379 | delegateEntries, |
357 | 380 | }); |
358 | 381 | await applyWorkspaceEdit(workspaceEdit, languageClient); |
| 382 | + await revealWorkspaceEdit(workspaceEdit, languageClient); |
359 | 383 | })); |
360 | 384 | } |
| 385 | + |
| 386 | +async function revealWorkspaceEdit(workspaceEdit: WorkspaceEdit, languageClient: LanguageClient): Promise<void> { |
| 387 | + const codeWorkspaceEdit = languageClient.protocol2CodeConverter.asWorkspaceEdit(workspaceEdit); |
| 388 | + if (!codeWorkspaceEdit) { |
| 389 | + return; |
| 390 | + } |
| 391 | + for (const entry of codeWorkspaceEdit.entries()) { |
| 392 | + await workspace.openTextDocument(entry[0]); |
| 393 | + if (entry[1].length > 0) { |
| 394 | + // reveal first available change of the workspace edit |
| 395 | + window.activeTextEditor.revealRange(entry[1][0].range, TextEditorRevealType.InCenter); |
| 396 | + break; |
| 397 | + } |
| 398 | + } |
| 399 | +} |
0 commit comments