I've implemented a VS Code extension where I've added a command to delete a diagnostic:
extension.ts
context.subscriptions.push(
vscode.commands.registerCommand(
DELETE_DIAGNOSTIC_COMMAND,
() => removeDiagnostic()
)
);
This is the function responsible for removing the diagnostic:
utils.ts
export function removeDiagnostic(): void {
const editor = window.activeTextEditor;
if(editor){
const diagnosticStart = editor.selection.start;
const diagnosticEnd = editor.selection.end;
const diagnosticRange = new Range(diagnosticStart, diagnosticEnd);
editor.edit(editBuilder => {
editBuilder.replace(diagnosticRange, "");
});
}
The DELETE_DIAGNOSTIC_COMMAND
refers to the code action listed here:
MyCodeActionProvider.ts
const DELETE_DIAGNOSTIC_COMMAND = 'delete-diagnostic.command';
export class MyCodeActionProvider implements vscode.CodeActionProvider {
public static readonly providedCodeActionKinds = [
vscode.CodeActionKind.QuickFix
];
provideCodeActions(document: vscode.TextDocument, range: vscode.Range | vscode.Selection, context: vscode.CodeActionContext, token: vscode.CancellationToken): vscode.CodeAction[] {
return context.diagnostics
.filter(diagnostic => diagnostic.code === 'myDiagnosticCode')
.map(diagnostic => this.createCommandCodeAction(diagnostic));
}
private createCommandCodeAction(diagnostic: vscode.Diagnostic): vscode.CodeAction {
const action = new vscode.CodeAction('Remove deprecated intrinsic', vscode.CodeActionKind.QuickFix);
action.command = { command: DELETE_DIAGNOSTIC_COMMAND, title: 'Remove deprecated intrinsic', tooltip: 'This will remove the deprecated intrinsic from the editor.' };
action.diagnostics = [diagnostic];
action.isPreferred = true;
return action;
}
}
The code actions provider is then registered in the following location:
extension.ts
context.subscriptions.push(
vscode.languages.registerCodeActionsProvider('myFileExtension', new MyCodeActionProvider(), {
providedCodeActionKinds: MyCodeActionProvider.providedCodeActionKinds
})
);
When executing the removeDiagnostic
function, the goal is to obtain and delete the diagnostic's range upon hovering over it, which seems to work well when using the command from the Problems tab but not during hover:
https://i.sstatic.net/ARHQ3.gif
How can I ensure that the diagnostic is successfully removed when running the command after hovering over it?