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.");
  }
}
相关文章
|
算法 Python
Pycharm里面的一些超级好用的功能——(TODO注释)用法防遗忘大法
Pycharm里面的一些超级好用的功能——(TODO注释)用法防遗忘大法
Pycharm里面的一些超级好用的功能——(TODO注释)用法防遗忘大法
|
6月前
|
Java Windows Spring
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
713 0
|
6月前
|
IDE 开发工具
一文解决Visual Studio 2022运行时系统找不到指定文件的错误(简单易懂 亲测有用)
一文解决Visual Studio 2022运行时系统找不到指定文件的错误(简单易懂 亲测有用)
2613 0
|
数据库 C++
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
137 0
|
数据采集 数据处理 Python
Python爬虫程序中的504错误:原因、常见场景和解决方法
Python爬虫程序中的504错误:原因、常见场景和解决方法
|
JSON 安全 数据格式
Python读写yaml排版混乱还丢失注释?我来告诉你解决办法!
日常我们在使用Python读写Yaml时,都是使用推荐的Pyyaml模块。 安装: pip install pyyaml 导入: import yaml 至于操作,简直不要太简单... yaml只有两个方法load、dump,而且使用完全和json模块一样。但真的如此吗?显然不是...
844 0
|
Linux API 开发工具
不知道如何看Android源码?试试这几种方式~
Android这个是一个**庞大的系统性**的工程,各个版本都有一定兼容性问题,为了能快速定位问题,也为了学习Android框架中一些优秀的思想,时常需要查看Android系统源码层面的知识。
|
JavaScript 前端开发
记一次vscode踩坑记录:"TypeScript 语言服务在其启动后已中止 5 次。将不会重启该服务。"
记一次vscode踩坑记录:"TypeScript 语言服务在其启动后已中止 5 次。将不会重启该服务。"
记一次vscode踩坑记录:"TypeScript 语言服务在其启动后已中止 5 次。将不会重启该服务。"
|
XML Android开发 数据格式
Android开发 常见异常和解决办法(一)(下)
Android Studio是Android开发的理想工具,但是由于版本的更新和配置的差异,会出现很多问题,下面是以《第一行代码 第二版》为基础进行开发学习可能遇见的一些问题及其解决办法。
Android开发 常见异常和解决办法(一)(下)
|
Java Shell API
Android开发 常见异常和解决办法(一)(上)
Android Studio是Android开发的理想工具,但是由于版本的更新和配置的差异,会出现很多问题,下面是以《第一行代码 第二版》为基础进行开发学习可能遇见的一些问题及其解决办法。
Android开发 常见异常和解决办法(一)(上)