Skip to content

Commit 8d95230

Browse files
committed
feat(lsp): add code action to explain diagnostic under cursor
1 parent c06a182 commit 8d95230

3 files changed

Lines changed: 19 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Command `opencode`:
343343
| LSP Function | `opencode.nvim` Handler |
344344
| ------------ | ----------------------------------------------------------------------- |
345345
| Hover | Asks `opencode` for a brief explanation of the symbol under the cursor. |
346-
| Code Actions | Asks `opencode` to fix diagnostics under the cursor. |
346+
| Code Actions | Asks `opencode` to explain or fix diagnostics under the cursor. |
347347

348348
## 👀 Events
349349

lsp/opencode.lua

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ handlers[ms.initialize] = function(params, callback)
4343
hoverProvider = config.lsp.handlers.hover.enabled,
4444
codeActionProvider = config.lsp.handlers.code_action.enabled,
4545
executeCommandProvider = {
46-
commands = { "opencode.fix" },
46+
commands = { "opencode.fix", "opencode.explain" },
4747
},
4848
},
4949
serverInfo = {
@@ -66,26 +66,37 @@ handlers[ms.textDocument_codeAction] = function(params, callback)
6666
},
6767
}
6868
end, diagnostics or {})
69+
local explain_commands = vim.tbl_map(function(diagnostic)
70+
return {
71+
title = "Ask opencode to explain: " .. diagnostic.message,
72+
command = {
73+
command = "opencode.explain",
74+
arguments = { diagnostic },
75+
},
76+
}
77+
end, diagnostics or {})
6978

70-
callback(nil, fix_commands)
79+
callback(nil, vim.list_extend(explain_commands, fix_commands))
7180
end
7281

7382
---@param params lsp.ExecuteCommandParams
7483
---@param callback fun(err?: lsp.ResponseError, result: any)
7584
handlers[ms.workspace_executeCommand] = function(params, callback)
76-
if params.command == "opencode.fix" then
85+
if params.command == "opencode.fix" or params.command == "opencode.explain" then
7786
local diagnostic = params.arguments[1]
7887
---@cast diagnostic vim.Diagnostic
7988
local filepath = require("opencode.context").format({ buf = diagnostic.bufnr })
80-
local prompt = "Fix diagnostic: " .. filepath .. require("opencode.context").format_diagnostic(diagnostic)
89+
local prompt_prefix = params.command == "opencode.fix" and "Fix diagnostic: " or "Explain diagnostic: "
90+
local prompt = prompt_prefix .. filepath .. require("opencode.context").format_diagnostic(diagnostic)
8191

8292
require("opencode")
8393
.prompt(prompt, { submit = true })
8494
:next(function()
8595
callback(nil, nil) -- Indicate success
8696
end)
8797
:catch(function(err)
88-
callback({ code = -32000, message = "Failed to fix: " .. err })
98+
local err_msg = params.command == "opencode.fix" and "Failed to fix: " or "Failed to explain: "
99+
callback({ code = -32000, message = err_msg .. err })
89100
end)
90101
else
91102
callback({ code = -32601, message = "Unknown command: " .. params.command })
@@ -201,8 +212,8 @@ handlers[ms.textDocument_hover] = function(params, callback)
201212
end
202213

203214
---An in-process LSP that interacts with `opencode`.
204-
--- - Code actions: ask `opencode` to fix diagnostics under the cursor.
205-
---
215+
--- - Code actions: ask `opencode` to explain or fix diagnostics under the cursor.
216+
--- - Hover: ask `opencode` to explain the symbol under the cursor, using the surrounding code as context.
206217
---@type vim.lsp.Config
207218
return {
208219
name = "opencode",

lua/opencode/ui/ask/cmp.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ handlers[ms.completionItem_resolve] = function(params, callback)
112112
end
113113

114114
---An in-process LSP that provides completions for context placeholders and agents.
115-
---
116115
---@type vim.lsp.Config
117116
return {
118117
name = "opencode_ask_cmp",

0 commit comments

Comments
 (0)