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.");
  }
}
相关文章
|
12天前
|
域名解析 人工智能 Linux
|
24天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。
|
消息中间件 算法 Java
弥补延时消息的不足,RocketMQ 基于时间轮算法实现了定时消息!
弥补延时消息的不足,RocketMQ 基于时间轮算法实现了定时消息!
1606 1
弥补延时消息的不足,RocketMQ 基于时间轮算法实现了定时消息!
|
Ubuntu
Ubuntu系统镜像下载,国内镜像站大全(山大/清华/阿里/浙大/中科大...)
装Ubuntu,是很多理工科同学入门的第一个挑战,首先我们就需要找到一个能用的iso镜像,根据你的网络环境的不同,不同的站点下载速度会不一样,下面列举一下几个比较好用的,都是来自Ubuntu官方推荐镜像站链接导航国内分区
48400 1
|
8天前
|
弹性计算 运维 数据挖掘
买阿里云服务器一年到底要花多少钱?38元、68元、99元和199元配置随意选择!
阿里云2026年四款高性价比服务器:38元/年(新用户秒杀,2核2G轻量)、68元/年(新用户常规款,同配置)、99元/年(新老用户可用,2核2G ECS经济型)、199元/年(企业专享,2核4G通用算力型)。带宽、续费规则与购买条件各不同,按需选择。
177 0
|
28天前
|
人工智能 测试技术 开发者
OpenAI 官方Codex Windows安装教程:微软商店无法下载怎么办?Codex安装包下载及MSIX安装方法(2026最新版)
本文提供2026最新Codex Windows离线安装方案:当微软商店打不开、提示“地区不可用”或下载失败时,可直接下载官方MSIX安装包,通过管理员PowerShell执行`Add-AppxPackage`命令一键安装,无需商店、不依赖网络,安全可靠,功能与商店版完全一致。(239字)
2427 0
|
Shell Android开发 C++
Android通过JNI操作GPIO
Android通过JNI操作GPIO
1070 0
Android通过JNI操作GPIO
Word有哪些快捷键?
【10月更文挑战第24天】Word有哪些快捷键?
1005 4
|
测试技术 Android开发
快速上手App自动化测试利器,Toast原理解析及操作实例
`Toast`是Android中的轻量级通知,短暂显示在屏幕任意位置,1-2秒后自动消失,不获取焦点且不可点击。Appium通过uiautomator2在控件树中处理Toast。在测试中,可设置隐式等待,利用XPath或Accessibility ID定位Toast元素进行检测和验证。示例代码展示了如何初始化driver,点击触发Toast,以及如何定位并读取Toast文本。