|
| 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 * as RoslynProtocol from './roslynProtocol'; |
| 8 | +import { LSPAny } from 'vscode-languageserver-protocol'; |
| 9 | +import { RoslynLanguageServer } from './roslynLanguageServer'; |
| 10 | +import { URIConverter, createConverter } from 'vscode-languageclient/lib/common/protocolConverter'; |
| 11 | +import { UriConverter } from './uriConverter'; |
| 12 | + |
| 13 | +export function registerCodeActionFixAllCommands( |
| 14 | + context: vscode.ExtensionContext, |
| 15 | + languageServer: RoslynLanguageServer, |
| 16 | + outputChannel: vscode.OutputChannel |
| 17 | +) { |
| 18 | + context.subscriptions.push( |
| 19 | + vscode.commands.registerCommand( |
| 20 | + 'roslyn.client.fixAllCodeAction', |
| 21 | + async (request): Promise<void> => registerFixAllResolveCodeAction(languageServer, request, outputChannel) |
| 22 | + ) |
| 23 | + ); |
| 24 | +} |
| 25 | + |
| 26 | +async function registerFixAllResolveCodeAction( |
| 27 | + languageServer: RoslynLanguageServer, |
| 28 | + codeActionData: any, |
| 29 | + outputChannel: vscode.OutputChannel |
| 30 | +) { |
| 31 | + if (codeActionData) { |
| 32 | + const data = <LSPAny>codeActionData; |
| 33 | + const result = await vscode.window.showQuickPick(data.FixAllFlavors, { |
| 34 | + placeHolder: vscode.l10n.t('Pick a fix all scope'), |
| 35 | + }); |
| 36 | + |
| 37 | + await vscode.window.withProgress( |
| 38 | + { |
| 39 | + location: vscode.ProgressLocation.Notification, |
| 40 | + title: vscode.l10n.t('Fix All Code Action'), |
| 41 | + cancellable: true, |
| 42 | + }, |
| 43 | + async (_, token) => { |
| 44 | + if (result) { |
| 45 | + const fixAllCodeAction: RoslynProtocol.RoslynFixAllCodeAction = { |
| 46 | + title: data.UniqueIdentifier, |
| 47 | + data: data, |
| 48 | + scope: result, |
| 49 | + }; |
| 50 | + |
| 51 | + const response = await languageServer.sendRequest( |
| 52 | + RoslynProtocol.CodeActionFixAllResolveRequest.type, |
| 53 | + fixAllCodeAction, |
| 54 | + token |
| 55 | + ); |
| 56 | + |
| 57 | + if (response.edit) { |
| 58 | + const uriConverter: URIConverter = (value: string): vscode.Uri => |
| 59 | + UriConverter.deserialize(value); |
| 60 | + const protocolConverter = createConverter(uriConverter, true, true); |
| 61 | + const fixAllEdit = await protocolConverter.asWorkspaceEdit(response.edit); |
| 62 | + if (!(await vscode.workspace.applyEdit(fixAllEdit))) { |
| 63 | + const componentName = '[roslyn.client.fixAllCodeAction]'; |
| 64 | + const errorMessage = 'Failed to make a fix all edit for completion.'; |
| 65 | + outputChannel.show(); |
| 66 | + outputChannel.appendLine(`${componentName} ${errorMessage}`); |
| 67 | + throw new Error('Tried to insert multiple code action edits, but an error occurred.'); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments