如何仅在执行 QuickFix 操作时触发代码?

时间:2021-04-30 09:28:56

标签: typescript visual-studio-code vscode-extensions

我想创建一个代码操作,用其他函数补充一个函数或更新它们(如果已经创建)。每当执行 Create-action 时,该函数都会存储在 Cache 中,如果该函数与缓存的版本差异太大,则 Update-action 变得可用。

但是,我无法在执行代码后触发事件。正如此处所建议的,我包含了 resolveCodeAction 类的 vscode.CodeActionProvider 方法的定义,但实际上并没有调用 if。

我定义的方法有误吗?

以下是我的代码的相关摘录。

export function activate(context: ExtensionContext) {
    context.subscriptions.push(
        languages.registerCodeActionsProvider('al',new EventSupplement(procedureCache),{
            providedCodeActionKinds: EventSupplement.providedCodeActionKinds
        })
    )
}
export class EventSupplement 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.ProviderResult<(vscode.CodeAction | vscode.Command)[]> {
        let lineIndex = range.start.line;

        if(!StringUtil.IsLineValid(document,range))
            return;

        if(!StringUtil.ProcedureHasEvents(this.procedureCache[cacheIndex])){
            quickFixActions.push(this.AddEvents(cacheIndex));
        } else if(needsUpdate){
            quickFixActions.push(this.UpdateEvents(cacheIndex));
        }
        
        return quickFixActions;
    }

    resolveCodeAction(
        codeAction: vscode.CodeAction,
        token: vscode.CancellationToken
    ): vscode.ProviderResult<vscode.CodeAction> {
        if(codeAction.title == this.addActionTitle){
            if(typeof this.newProcedure == 'undefined')
                return(codeAction);
            this.eventCache.push(this.newProcedure);
        } else if(codeAction.title == this.updateActionTitle){
            if(typeof this.newProcedure == 'undefined')
                return(codeAction);
            let eventCacheIndex = ProcedureUtil.GetCacheIndexOfProcedure(this.newProcedure,this.eventCache);
            if(eventCacheIndex == -1)
                this.eventCache.push(this.newProcedure);
            else
                this.eventCache[eventCacheIndex] = this.newProcedure;
        }

        return(codeAction);
    }
}
private AddEvents(cacheIndex:number):vscode.CodeAction{
    let add = new vscode.CodeAction('Add Before- and After-Events',vscode.CodeActionKind.QuickFix);
    const procedure = this.procedureCache[cacheIndex];

    add.edit = new vscode.WorkspaceEdit();
    add.edit.replace(procedure.Document.uri,procedure.RangeAtStart,procedure.AddEvents());

    return(add);
}

0 个答案:

没有答案
相关问题