@@ -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 ) )
7180end
7281
7382--- @param params lsp.ExecuteCommandParams
7483--- @param callback fun ( err ?: lsp.ResponseError , result : any )
7584handlers [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)
201212end
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
207218return {
208219 name = " opencode" ,
0 commit comments