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.");
  }
}
相关文章
|
C++
工具使用心得和错误解决方案
一.vs2008 当连接器报出LINK: fatal error LNK1102: out of memory时,解决方法: 首先调出任务管理器,然后在进程页面中,找到mspdbsrv.exe,将其关掉。
1056 0
|
Java
【技术贴】myeclipse自动提示代码很慢的解决方法 |自动提示卡慢
今天用rs.getString()这个方法的时候,卡的蛋疼。按如下方法,即可不蛋疼! 解决办法:1. 找到你的JDK安装目录下的src.zip文件;2. 打开: Window菜单->Preference->Java->Installed JREs;3. 在列表中选择你正在使用的JRE,然后Edit;4. 选择 rt.jar,然后点击"Source Attachment"按钮,弹出对话框后选择"External File",然后把你的JDK安装目录下的src.zip文件与rt.jar关联上;ok 已经解决。
1049 0
|
Java
对上一篇笔记提到的JAR打包问题的解决方法
   今晚看了马士兵老师的视频,才知道原来解决上一篇笔记提到的打包问题是这么简单。     首先,在自定义的MF文件里,编写如下内容:Main-Class:MainClass。其中MainClass是你的主类名。
632 0
|
数据库
代码被嫌弃写的太烂?装上这个IDEA插件再试试!
代码被嫌弃写的太烂?装上这个IDEA插件再试试!
230 0
代码被嫌弃写的太烂?装上这个IDEA插件再试试!
|
9月前
|
Java Windows Spring
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
IDEA中报错:因为在此系统上禁止运行脚本有关详细信息,请参阅...(图文解释 亲测已解决)
954 0
|
jenkins 测试技术 持续交付
jenkins中RobotFramework用例错误截图打不开解决方法
具体表现为:执行用例报错,打开open_log.html查看日志,发现错误截图显示未找到,如图: 解决方案 配置Robotframework插件必要设置即可解决 原因分析 测试用例日志是robot-plugin目录下面log.
2011 0
MyEclipse注册码生成器(不用修改直接能用)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
762 0
|
移动开发 前端开发 小程序
为了偷懒,我用google/zx一键自动打包编译了前后端项目并发布到指定环境
由于正在负责的一个项目,就说前端涉及到PC端、公众号端、APP端的H5、小程序端、可视化大屏端,而PC和APP又通过qiankun引入了微前端的理念。整体一圈下来可能光前端编译打包就要build差不多二十次。而有时候经常性的bug改动,这个时候便只需要进行测试后需要进行小范围的测试。
229 0

热门文章

最新文章