事件 VSCode API CodeAction 已接受

Event VSCode API CodeAction accepted

有没有办法在应用 CodeAction(QuickFix - 灯泡)时触发一些代码?

在实现 vscode.CodeActionProvider

的 CustomActionProvider 中
public provideCodeActions(document: vscode.TextDocument, range: vscode.Range): vscode.CodeAction[] | undefined {
    let ifPowershell = this.isAtIfPS(document, range);
    if (ifPowershell[0] === "true") {
        //Tenemos que calcular el Range, para hacer el replace
        let line = document.lineAt(range.start.line);
        let startIndex = line.firstNonWhitespaceCharacterIndex;
        let range2 = new vscode.Range(new vscode.Position(range.start.line, startIndex), line.range.end);
        //El texto que va a remplazar al código existente
        let fix = `if (${ifPowershell[1]} ${ifPowershell[2]} ${ifPowershell[3]})`;
        return [this.createFix(document, range2, fix)];
    }

    //Si llegamos aquí es que no hay 
    return;
}

private createFix(document: vscode.TextDocument, range: vscode.Range, replace: string): vscode.CodeAction {
    const fix = new vscode.CodeAction(`Convert to ${replace}`, vscode.CodeActionKind.QuickFix);
    fix.edit = new vscode.WorkspaceEdit();
    fix.edit.replace(document.uri, new vscode.Range(range.start, range.end), replace);
    return fix;
}

我已经可以建议这个了

if (var -eq 0)if (var == 0)都可以,但性能不同。因此,建议使用 == 运算符而不是 -eq

现在...我想计算有多少用户接受了这个 quickfix,我已经有一个 API 可以将其记录在数据库中。

问题是:如何编写一个事件、回调或某些函数,以便在用户 'accepted' 快速修复时触发?

您可以为此使用 CodeAction.command

首先注册一个内部命令:

vscode.commands.registerCommand('_myExt.didApplyFix', () => { 
    // handle fix applied
})

然后在您的代码操作上使用此命令return:

const action = new vscode.CodeAction('action title');
action.command = { command: '_myExt.didApplyFix', title: 'action title' }

每当应用修复程序时都会调用该命令