Skip to content

Commit a4dff90

Browse files
committed
fix(context): diagnostics context error
1 parent 8992d0c commit a4dff90

3 files changed

Lines changed: 37 additions & 42 deletions

File tree

lsp/opencode.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ handlers[ms.workspace_executeCommand] = function(params, callback)
8585
if params.command == "opencode.fix" or params.command == "opencode.explain" then
8686
local diagnostic = params.arguments[1]
8787
---@cast diagnostic vim.Diagnostic
88-
local filepath = require("opencode.context").format({ buf = diagnostic.bufnr })
88+
local filepath = require("opencode.context").format(diagnostic.bufnr)
8989
local prompt_prefix = params.command == "opencode.fix" and "Fix diagnostic: " or "Explain diagnostic: "
9090
local prompt = prompt_prefix .. filepath .. require("opencode.context").format_diagnostic(diagnostic)
9191

@@ -115,8 +115,7 @@ handlers[ms.textDocument_hover] = function(params, callback)
115115
-- local lines = vim.fn.readfile(params.textDocument.uri:gsub("^file://", ""))
116116
-- local text = table.concat(lines, "\n")
117117

118-
local location = require("opencode.context").format({
119-
path = params.textDocument.uri:gsub("^file://", ""),
118+
local location = require("opencode.context").format(params.textDocument.uri:gsub("^file://", ""), {
120119
start_line = params.position.line + 1,
121120
start_col = params.position.character + 1,
122121
})

lua/opencode/context.lua

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Context.__index = Context
1515
local ns_id = vim.api.nvim_create_namespace("OpencodeContext")
1616

1717
---Returns the filename for a buffer if it has one, else nil.
18-
---@param buf integer
18+
---@param buf number
1919
local function get_filename(buf)
2020
if vim.api.nvim_get_option_value("buftype", { buf = buf }) == "" then
2121
local name = vim.api.nvim_buf_get_name(buf)
@@ -265,28 +265,33 @@ function Context.input_highlight(rendered)
265265
end
266266

267267
---Format a location for `opencode`.
268-
---@param args { buf?: integer, path?: string, start_line?: integer, start_col?: integer, end_line?: integer, end_col?: integer } Lines and cols are 1-based
268+
---@param loc string|integer A buffer number or filepath.
269+
---@param args? { start_line?: integer, start_col?: integer, end_line?: integer, end_col?: integer } Location is a buffer number or filepath. Lines and cols are 1-based
269270
---@return string? location e.g. `opencode.lua:L21:C10-L65:C11`
270-
function Context.format(args)
271-
assert(args.buf or args.path, "Context.format: Either buf or path must be provided")
271+
function Context.format(loc, args)
272+
assert(type(loc) ~= "string" or #loc > 0, "Filepath cannot be an empty string")
272273

273-
local result = ""
274-
local filepath = (args.buf and get_filename(args.buf)) or args.path
275-
if filepath and filepath ~= "" then
276-
local absolute_path = vim.fn.fnamemodify(filepath, ":p:~")
277-
result = absolute_path
278-
else
274+
-- Not we check number, not integer - I think integer is only an annotations thing, and at runtime only numbers exist?
275+
local filepath = (type(loc) == "number" and get_filename(loc)) or type(loc) == "string" and loc or nil
276+
if not filepath then
279277
return nil
280278
end
281-
if args.start_line and args.end_line and args.start_line > args.end_line then
282-
-- Ensure start is before end (for reversed visual selections)
283-
args.start_line, args.end_line = args.end_line, args.start_line
284-
if args.start_col and args.end_col then
285-
-- Can happen in visual block mode
286-
args.start_col, args.end_col = args.end_col, args.start_col
279+
280+
local result = ""
281+
282+
local absolute_path = vim.fn.fnamemodify(filepath, ":p:~")
283+
result = result .. absolute_path
284+
285+
if args and args.start_line then
286+
if args.end_line and args.start_line > args.end_line then
287+
-- Ensure start is before end (for reversed visual selections)
288+
args.start_line, args.end_line = args.end_line, args.start_line
289+
if args.start_col and args.end_col then
290+
-- Can happen in visual block mode
291+
args.start_col, args.end_col = args.end_col, args.start_col
292+
end
287293
end
288-
end
289-
if args.start_line then
294+
290295
-- Previously we'd need a space here so `opencode` would recognize the file reference and insert the context,
291296
-- but that has regressed with the v1.0.0 `opentui` update :/
292297
-- There's a GH issue somewhere.
@@ -302,6 +307,7 @@ function Context.format(args)
302307
end
303308
end
304309
end
310+
305311
return result
306312
end
307313

@@ -310,16 +316,14 @@ end
310316
---Range if present, else cursor position.
311317
function Context:this()
312318
if self.range then
313-
return Context.format({
314-
buf = self.buf,
319+
return Context.format(self.buf, {
315320
start_line = self.range.from[1],
316321
start_col = (self.range.kind ~= "line") and self.range.from[2] or nil,
317322
end_line = self.range.to[1],
318323
end_col = (self.range.kind ~= "line") and self.range.to[2] or nil,
319324
})
320325
else
321-
return Context.format({
322-
buf = self.buf,
326+
return Context.format(self.buf, {
323327
start_line = self.cursor[1],
324328
start_col = self.cursor[2] + 1,
325329
})
@@ -328,16 +332,14 @@ end
328332

329333
---The current buffer.
330334
function Context:buffer()
331-
return Context.format({
332-
buf = self.buf,
333-
})
335+
return Context.format(self.buf)
334336
end
335337

336338
---All open buffers.
337339
function Context:buffers()
338340
local file_list = {}
339341
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
340-
local path = Context.format({ buf = buf })
342+
local path = Context.format(buf)
341343
if path then
342344
table.insert(file_list, path)
343345
end
@@ -353,8 +355,7 @@ function Context:visible_text()
353355
local visible = {}
354356
for _, win in ipairs(vim.api.nvim_list_wins()) do
355357
local buf = vim.api.nvim_win_get_buf(win)
356-
local location = Context.format({
357-
buf = buf,
358+
local location = Context.format(buf, {
358359
start_line = vim.fn.line("w0", win),
359360
end_line = vim.fn.line("w$", win),
360361
})
@@ -371,7 +372,7 @@ end
371372
---@param diagnostic vim.Diagnostic
372373
---@return string
373374
function Context.format_diagnostic(diagnostic)
374-
local location = Context.format({
375+
local location = Context.format(diagnostic.bufnr, {
375376
start_line = diagnostic.lnum + 1,
376377
start_col = diagnostic.col + 1,
377378
end_line = diagnostic.end_lnum + 1,
@@ -393,13 +394,11 @@ function Context:diagnostics()
393394
return nil
394395
end
395396

396-
local location = Context.format({ buf = self.buf })
397-
398397
local diagnostic_strings = vim.tbl_map(function(diagnostic)
399398
return "- " .. Context.format_diagnostic(diagnostic)
400399
end, diagnostics)
401400

402-
return #diagnostics .. " diagnostics in " .. location .. "\n" .. table.concat(diagnostic_strings, "\n")
401+
return #diagnostics .. " diagnostics:" .. "\n" .. table.concat(diagnostic_strings, "\n")
403402
end
404403

405404
---Formatted quickfix list entries.
@@ -414,8 +413,7 @@ function Context:quickfix()
414413
if has_buf then
415414
table.insert(
416415
lines,
417-
Context.format({
418-
buf = entry.bufnr,
416+
Context.format(entry.bufnr, {
419417
start_line = entry.lnum,
420418
start_col = entry.col,
421419
})
@@ -446,8 +444,7 @@ function Context:marks()
446444
if mark.mark:match("^'[A-Z]$") then
447445
table.insert(
448446
marks,
449-
Context.format({
450-
buf = mark.pos[1],
447+
Context.format(mark.pos[1], {
451448
start_line = mark.pos[2],
452449
start_col = mark.pos[3],
453450
})
@@ -472,7 +469,7 @@ function Context:grapple_tags()
472469
end
473470
local paths = {}
474471
for _, tag in ipairs(tags) do
475-
table.insert(paths, Context.format({ path = tag.path }))
472+
table.insert(paths, Context.format(tag.path))
476473
end
477474
return table.concat(paths, ", ")
478475
end

lua/opencode/integrations/pickers/snacks.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ function M.send(picker)
99
local items = vim.tbl_map(function(item)
1010
return item.file
1111
-- Prefer just the location if possible, so the LLM can also fetch context
12-
and require("opencode.context").format({
13-
path = item.file,
12+
and require("opencode.context").format(item.file, {
1413
start_line = item.pos and item.pos[1] or nil,
1514
start_col = item.pos and item.pos[2] or nil,
1615
end_line = item.end_pos and item.end_pos[1] or nil,

0 commit comments

Comments
 (0)