typescript - 如何仅在执行 QuickFix 操作时触发代码?
问题描述
我想创建一个代码操作,用其他函数补充一个函数或更新它们(如果已经创建)。每当执行 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);
}
解决方案
推荐阅读
- python - Python:套接字通信在有线局域网中不起作用
- mysql - MySQL - 多项选择题 - 存储问题类别的新表?
- r - 使用 R 和 Tidyverse 重新组织所有因子水平(以防止我的最终 ds 被混合)
- reactjs - req.user 未定义。React.js,路线,快递,护照
- excel - 将包含 hr:min:sec 和 min:sec 的 Excel 列转换为 hr:min:sec 格式
- c# - 尝试通过分段控制,Xamarin IOS 在容器视图中滑动 PVC 中的页面
- linux - 无法在 alpine docker 文件中运行 cron 消息
- nunjucks - Eleventy/njk - 基于动态标签的集合
- python - 转换 Pandas 日期时间以插入 MS SQL Server
- r - 在给定点的多边形内创建线