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.");
  }
}
相关文章
|
9月前
|
Java 数据库
错误解决方案
错误解决方案
125 0
|
21天前
|
Java Windows Spring
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
76 0
|
21天前
|
IDE 开发工具
一文解决Visual Studio 2022运行时系统找不到指定文件的错误(简单易懂 亲测有用)
一文解决Visual Studio 2022运行时系统找不到指定文件的错误(简单易懂 亲测有用)
654 0
|
6月前
|
分布式计算 资源调度 Hadoop
从一个简单的命令阅读hadoop源码(上)
从一个简单的命令阅读hadoop源码
38 0
|
6月前
|
分布式计算 资源调度 Hadoop
从一个简单的命令阅读hadoop源码(下)
从一个简单的命令阅读hadoop源码(下)
32 0
|
7月前
|
数据库 C++
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
《C++避坑神器·十七》找到程序崩溃Bug的一个实用方法:dump调试
78 0
|
8月前
|
数据采集 数据处理 Python
Python爬虫程序中的504错误:原因、常见场景和解决方法
Python爬虫程序中的504错误:原因、常见场景和解决方法
|
9月前
|
监控 jenkins 持续交付
Python3,仅仅2段代码,就实现项目代码自动上传及部署,再也不需要Jenkins了。
Python3,仅仅2段代码,就实现项目代码自动上传及部署,再也不需要Jenkins了。
61 0
|
资源调度 分布式计算 Hadoop
从一个简单的命令阅读hadoop源码
从一个简单的命令阅读hadoop源码
127 0
从一个简单的命令阅读hadoop源码
|
C++
安利一个通过命令行使用 VS Code 打开项目的方法
安利一个通过命令行使用 VS Code 打开项目的方法
711 0
安利一个通过命令行使用 VS Code 打开项目的方法

热门文章

最新文章