codeAction提供代码错误解决方案重要笔记

简介: codeAction提供代码错误解决方案重要笔记

原地址:https://github.com/hoovercj/vscode-extension-tutorial

Commands and CodeActionProviders

Actions that should appear in the command palette (ctrl+shift+p) are declared in packages.json as a command. The generated Hello World extension has an example of this. These can then be registered by an extension to trigger any function with the line vscode.commands.registerCommand(‘extension.commandId’, functionNameOrDefinition).

However, for an action that is context specific and shouldn’t be in the command palette, don’t register it in packages.json. But then how will it be triggered? That’s where CodeActionProviders come in.

A CodeActionProvider makes the lightbulb show up in VS Code allowing users to perform refactorings, fix spelling mistakes, etc.

The CodeActionProvider interface defines a single method named provideCodeActions(). A class that implements this interface and registers with VS Code will have its provideCodeActions() method called whenever the user selects text or places the cursor in an area that contains a Diagnostic. It is up to the extension, then, to return an array of actions that are applicable for that Diagnostic.

The objects returned by provideCodeActions() are nothing more than references to a command as discussed above and an array of arguments to pass it. These will display as options if the user clicks the lightbulb. And when the user clicks on the lightbulb? The arguments are passed to whatever function the extension registered for that command as described above.

The code below illustrates how to add code actions to the HaskellLintingProvider class shown above. provideCodeActions() receives the diagnostics as a member of CodeActionContext and returns an array with a single command. runCodeAction() is the function that we want to trigger if a user selects our action. Using the arguments passed along with the command it uses a WorkspaceEdit to fix a users code according to the suggestions of hlint.

Copy the following code into the body of HaskellLintingProvider from src/features/hlintProvider after the doHlint() function.

// src/features/hlintProvider.ts

private static commandId: string = 'haskell.hlint.runCodeAction';
public provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken): vscode.Command[] {
  let diagnostic:vscode.Diagnostic = context.diagnostics[0];
  return [{
    title: "Accept hlint suggestion",
    command: HaskellLintingProvider.commandId,
    arguments: [document, diagnostic.range, diagnostic.message]
  }];
}
private runCodeAction(document: vscode.TextDocument, range: vscode.Range, message:string): any {
  let fromRegex:RegExp = /.*Replace:(.*)==>.*/g
  let fromMatch:RegExpExecArray = fromRegex.exec(message.replace(/\s/g, ''));
  let from = fromMatch[1];
  let to:string = document.getText(range).replace(/\s/g, '')
  if (from === to) {
    let newText = /.*==>\s(.*)/g.exec(message)[1]
    let edit = new vscode.WorkspaceEdit();
    edit.replace(document.uri, range, newText);
    return vscode.workspace.applyEdit(edit);
  } else {
    vscode.window.showErrorMessage("The suggestion was not applied because it is out of date. You might have tried to apply the same edit twice.");
  }
}
相关文章
|
8月前
|
Java Windows Spring
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
913 0
|
数据库 C++
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
172 0
|
JSON 安全 数据格式
Python读写yaml排版混乱还丢失注释?我来告诉你解决办法!
日常我们在使用Python读写Yaml时,都是使用推荐的Pyyaml模块。 安装: pip install pyyaml 导入: import yaml 至于操作,简直不要太简单... yaml只有两个方法load、dump,而且使用完全和json模块一样。但真的如此吗?显然不是...
885 0
vscode 编辑 makefile 文件,执行make操作时显示“Makefile:5: *** 遗漏分隔符 。 停止。“(终极解决办法)
vscode 编辑 makefile 文件,执行make操作时显示“Makefile:5: *** 遗漏分隔符 。 停止。“(终极解决办法)
|
编译器 开发工具 Windows
VS2008 未找到编译器可执行文件 csc.exe【当网上其他方法试玩了之后不起作用的时候再用这个方法】
被公司派遣到中国海洋石油惠州炼化公司做项目,做的是生产管理,来了发现他们的项目结构简直烂的要命,和同学们写的毕业设计差不多,然后开发工具用的是vs2008,我电脑是安装了vs2005和vs2010,vs2012就是没有安装vs2008,在安装vs2008的时候那是一番折腾好长时间,然后把vs2008安装好了打开项目代码,生成解决方案发现报了好多错,然后一一解决,最奇怪的是VS2008 未找到编译器可执行文件 csc.exe,我把所以路径都配好了,并且在dos环境下执行csc.exe都可以执行,环境变量路径设置的也么有问题,就是很奇怪重启机子打开项目还是找不到csc.exe
202 0
|
Web App开发 JavaScript 前端开发
Google浏览器Chrome安装失败,错误代码0xa0430721解决办法(★亲测可行★)
Google浏览器Chrome安装失败,错误代码0xa0430721解决办法(★亲测可行★)
703 0
|
数据采集 SQL Python
怎么让你的代码更Pythonic?光有技巧可不行,你还需要看这些……
写代码如同写文章,好的文章是反复修改出来的,代码也同样是反复的重构出来的。今天给大家分享下,怎么从一个编程学习者变为一个程序猿(程序媛)!起码不要让别人一看你的代码就知道你是个小菜鸟! 我们通常写一个代码,必然会经过一个...
1079 0