-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code Actions - Add fix all support #6310
Changes from 5 commits
536e046
581506d
e5af714
2bd0b97
c29e0e0
2f68597
6fe55e7
8a99627
ee1eea2
7b7292d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as RoslynProtocol from './roslynProtocol'; | ||
import { LSPAny } from 'vscode-languageserver-protocol'; | ||
import { RoslynLanguageServer } from './roslynLanguageServer'; | ||
import { URIConverter, createConverter } from 'vscode-languageclient/lib/common/protocolConverter'; | ||
import { UriConverter } from './uriConverter'; | ||
|
||
export function registerCodeActionFixAllCommands( | ||
context: vscode.ExtensionContext, | ||
languageServer: RoslynLanguageServer | ||
) { | ||
context.subscriptions.push( | ||
vscode.commands.registerCommand( | ||
'roslyn.client.fixAllCodeAction', | ||
async (request): Promise<void> => registerFixAllResolveCodeAction(languageServer, request) | ||
) | ||
); | ||
} | ||
|
||
async function registerFixAllResolveCodeAction(languageServer: RoslynLanguageServer, codeActionData: any) { | ||
if (codeActionData) { | ||
const data = <LSPAny>codeActionData; | ||
const result = await vscode.window.showQuickPick(data.FixAllFlavors, { | ||
placeHolder: vscode.l10n.t('Pick a fix all scope'), | ||
}); | ||
|
||
let fixAllEdit = new vscode.WorkspaceEdit(); | ||
await vscode.window.withProgress( | ||
{ | ||
location: vscode.ProgressLocation.Notification, | ||
title: 'Fix All Code Action', | ||
cancellable: true, | ||
}, | ||
async (_, token) => { | ||
if (result) { | ||
const fixAllCodeAction: RoslynProtocol.RoslynFixAllCodeAction = { | ||
title: data.UniqueIdentifier, | ||
data: data, | ||
scope: result, | ||
}; | ||
|
||
const response = await languageServer.sendRequest( | ||
RoslynProtocol.CodeActionFixAllResolveRequest.type, | ||
fixAllCodeAction, | ||
token | ||
); | ||
|
||
if (response.edit) { | ||
const uriConverter: URIConverter = (value: string): vscode.Uri => | ||
UriConverter.deserialize(value); | ||
const protocolConverter = createConverter(uriConverter, true, true); | ||
fixAllEdit = await protocolConverter.asWorkspaceEdit(response.edit); | ||
} | ||
} | ||
} | ||
); | ||
|
||
if (!(await vscode.workspace.applyEdit(fixAllEdit))) { | ||
throw new Error('Tried to insert multiple code action edits, but an error occurred.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test this to make sure the error is visible (hopefully in the C# output window). If not you may need to manually write it there via passing in the output channel |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ import { RoslynLanguageServerEvents } from './languageServerEvents'; | |
import { registerShowToastNotification } from './showToastNotification'; | ||
import { registerRazorCommands } from './razorCommands'; | ||
import { registerOnAutoInsert } from './onAutoInsert'; | ||
import { registerCodeActionFixAllCommands } from './fixAllCodeAction'; | ||
|
||
let _channel: vscode.OutputChannel; | ||
let _traceChannel: vscode.OutputChannel; | ||
|
@@ -202,6 +203,46 @@ export class RoslynLanguageServer { | |
workspace: { | ||
configuration: (params) => readConfigurations(params), | ||
}, | ||
// resolveCodeAction: async (codeAction, token, next) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should remove |
||
// const lspCodeAction = <CodeAction>codeAction; | ||
// const data = lspCodeAction.data; | ||
// if (data.FixAllFlavors) { | ||
// const result = await vscode.window.showQuickPick(data.FixAllFlavors, { | ||
// placeHolder: 'Pick a fix all scope', | ||
// }); | ||
|
||
// if (result) { | ||
// const fixAllCodeAction: RoslynProtocol.RoslynFixAllCodeAction = { | ||
// title: lspCodeAction.title, | ||
// edit: lspCodeAction.edit, | ||
// data: data, | ||
// scope: result, | ||
// }; | ||
|
||
// const response = await _languageServer.sendRequest( | ||
// RoslynProtocol.CodeActionFixAllResolveRequest.type, | ||
// fixAllCodeAction, | ||
// token | ||
// ); | ||
|
||
// if (response.edit) { | ||
// const uriConverter: URIConverter = (value: string): vscode.Uri => | ||
// UriConverter.deserialize(value); | ||
// const protocolConverter = createConverter(uriConverter, true, true); | ||
// const result = await protocolConverter.asWorkspaceEdit(response.edit); | ||
|
||
// if (!(await vscode.workspace.applyEdit(result))) { | ||
// throw new Error( | ||
// 'Tried to insert multiple code action edits, but an error occurred.' | ||
// ); | ||
// } | ||
// } | ||
// console.log(response); | ||
// } | ||
// } else { | ||
// return await next(codeAction, token); | ||
// } | ||
// }, | ||
}, | ||
}; | ||
|
||
|
@@ -745,7 +786,7 @@ export async function activateRoslynLanguageServer( | |
|
||
// Register any commands that need to be handled by the extension. | ||
registerCommands(context, languageServer, optionProvider, hostExecutableResolver, _channel); | ||
|
||
registerCodeActionFixAllCommands(context, languageServer); | ||
registerRazorCommands(context, languageServer); | ||
|
||
registerUnitTestingCommands(context, languageServer, dotnetTestChannel); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the apply edit should go inside the progress as well. I think it won't be cancellable because you don't pass in the cancellation token