如何接受NVIM LSP建议?

发布于 2025-01-28 19:24:52 字数 81 浏览 5 评论 0原文

举一个简单的例子。 LS的变量或功能拼写错误,提出了一个建议“找不到名称'投票'。您的意思是“投票”吗?

有没有办法快速接受这些建议?

To give a simple example. Having a variable or function misspelled, the LS gives a suggestion "Cannot find name 'voteings'. Did you mean 'votings'?

Is there a way to quickly accept those suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2025-02-04 19:24:52

正如Nalzok所建议的,:lua vim.lsp.buf.code_action()将打开可以在LSP建议上执行的代码操作。

如果您想在不打开代码操作的情况下自动接受该操作,则可以使用“选项”参数。选项参数在此处有两个方便的字段,filterapply。第一个字段将允许您过滤代码操作列表,如果是唯一可用的代码操作,则第二个字段将自动应用代码操作。

将这两个共同使用会产生以下命令:

lua vim.lsp.buf.code_action({
  filter = function(code_action)
    return string.find(action.title, "spelling")
  end,
  apply = true,
})

这将过滤代码操作,以显示建议拼写修复的命令,如果只剩下一个代码操作,则会自动应用它。

另外,如果仅希望唯一的代码操作是拼写修复程序,则可以将其删除过滤器字段。

有关更多信息,请查看:H vim.lsp.buf.code_action

As suggested by nalzok, :lua vim.lsp.buf.code_action() will open the code actions that can be performed on the LSP's suggestion.

If you want to automatically accept the action without having the code actions open up, you can use the options parameter. The options parameter has two fields that come in handy here, filter and apply. The first field will allow you to filter the list of code actions and the second field will auto apply the code action if it's the only one available.

Using these two together yields the following command:

lua vim.lsp.buf.code_action({
  filter = function(code_action)
    return string.find(action.title, "spelling")
  end,
  apply = true,
})

This will filter the code actions to only show the ones that suggest spelling fixes and will auto apply it if there is only one code action left.

Alternatively, you can leave out the filter field if you only want it to apply if the only code action is the spelling fix.

For more info, check out :h vim.lsp.buf.code_action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文