|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { CodeAction, CodeActionResolveRequest, LSPAny } from 'vscode-languageserver-protocol'; |
| 8 | +import { RoslynLanguageServer } from './roslynLanguageServer'; |
| 9 | +import { URIConverter, createConverter } from 'vscode-languageclient/lib/common/protocolConverter'; |
| 10 | +import { UriConverter } from './uriConverter'; |
| 11 | +import { getFixAllResponse } from './fixAllCodeAction'; |
| 12 | + |
| 13 | +export function registerNestedCodeActionCommands( |
| 14 | + context: vscode.ExtensionContext, |
| 15 | + languageServer: RoslynLanguageServer, |
| 16 | + outputChannel: vscode.OutputChannel |
| 17 | +) { |
| 18 | + context.subscriptions.push( |
| 19 | + vscode.commands.registerCommand( |
| 20 | + 'roslyn.client.nestedCodeAction', |
| 21 | + async (request): Promise<void> => registerNestedResolveCodeAction(languageServer, request, outputChannel) |
| 22 | + ) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +async function registerNestedResolveCodeAction( |
| 27 | + languageServer: RoslynLanguageServer, |
| 28 | + codeActionData: any, |
| 29 | + outputChannel: vscode.OutputChannel |
| 30 | +): Promise<void> { |
| 31 | + if (codeActionData) { |
| 32 | + const data = <LSPAny>codeActionData; |
| 33 | + const actions = data.NestedCodeActions; |
| 34 | + |
| 35 | + if (actions.length > 0) { |
| 36 | + const codeActionTitles = getCodeActionTitles(actions); |
| 37 | + const selectedValue = await vscode.window.showQuickPick(codeActionTitles, { |
| 38 | + placeHolder: vscode.l10n.t(data.UniqueIdentifier), |
| 39 | + ignoreFocusOut: true, |
| 40 | + }); |
| 41 | + if (selectedValue) { |
| 42 | + const selectedAction = selectedValue.codeAction; |
| 43 | + if (!selectedAction) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + if (selectedAction.data.FixAllFlavors) { |
| 48 | + await getFixAllResponse(selectedAction.data, languageServer, outputChannel); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + await vscode.window.withProgress( |
| 53 | + { |
| 54 | + location: vscode.ProgressLocation.Notification, |
| 55 | + title: vscode.l10n.t('Nested Code Action'), |
| 56 | + cancellable: true, |
| 57 | + }, |
| 58 | + async (_, token) => { |
| 59 | + const nestedCodeActionResolve: CodeAction = { |
| 60 | + title: selectedAction.title, |
| 61 | + data: selectedAction.data, |
| 62 | + }; |
| 63 | + |
| 64 | + const response = await languageServer.sendRequest( |
| 65 | + CodeActionResolveRequest.type, |
| 66 | + nestedCodeActionResolve, |
| 67 | + token |
| 68 | + ); |
| 69 | + |
| 70 | + if (!response.edit) { |
| 71 | + outputChannel.show(); |
| 72 | + outputChannel.appendLine(`Failed to make an edit for completion.`); |
| 73 | + throw new Error('Tried to retrieve a code action edit, but an error occurred.'); |
| 74 | + } |
| 75 | + |
| 76 | + const uriConverter: URIConverter = (value: string): vscode.Uri => |
| 77 | + UriConverter.deserialize(value); |
| 78 | + const protocolConverter = createConverter(uriConverter, true, true); |
| 79 | + const fixAllEdit = await protocolConverter.asWorkspaceEdit(response.edit); |
| 80 | + if (!(await vscode.workspace.applyEdit(fixAllEdit))) { |
| 81 | + const componentName = '[roslyn.client.nestedCodeAction]'; |
| 82 | + const errorMessage = 'Failed to make am edit for completion.'; |
| 83 | + outputChannel.show(); |
| 84 | + outputChannel.appendLine(`${componentName} ${errorMessage}`); |
| 85 | + throw new Error('Tried to insert code action edit, but an error occurred.'); |
| 86 | + } |
| 87 | + } |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function getCodeActionTitles(nestedActions: any): any[] { |
| 95 | + return nestedActions.map((nestedAction: { data: { CodeActionPath: any; FixAllFlavors: any } }) => { |
| 96 | + const codeActionPath = nestedAction.data.CodeActionPath; |
| 97 | + const fixAllFlavors = nestedAction.data.FixAllFlavors; |
| 98 | + // If there's only one string, return it directly |
| 99 | + if (codeActionPath.length === 1) { |
| 100 | + return codeActionPath; |
| 101 | + } |
| 102 | + |
| 103 | + // Concatenate multiple strings with " ->" |
| 104 | + const concatenatedString = codeActionPath.slice(1).join(' -> '); |
| 105 | + const fixAllString = vscode.l10n.t('Fix All: '); |
| 106 | + return { |
| 107 | + label: fixAllFlavors ? `${fixAllString}${concatenatedString}` : concatenatedString, |
| 108 | + codeAction: nestedAction, |
| 109 | + }; |
| 110 | + }); |
| 111 | +} |
0 commit comments