Skip to content

Commit 531e188

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

1 file changed

Lines changed: 21 additions & 27 deletions

File tree

lua/opencode/context.lua

Lines changed: 21 additions & 27 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,31 @@ 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

273274
local result = ""
274-
local filepath = (args.buf and get_filename(args.buf)) or args.path
275-
if filepath and filepath ~= "" then
275+
-- Not we check number, not integer - I think integer is only an annotations thing, and at runtime only numbers exist?
276+
local filepath = (type(loc) == "number" and get_filename(loc)) or type(loc) == "string" and loc or nil
277+
278+
if filepath then
276279
local absolute_path = vim.fn.fnamemodify(filepath, ":p:~")
277280
result = absolute_path
278281
else
279282
return nil
280283
end
281-
if args.start_line and args.end_line and args.start_line > args.end_line then
284+
if args and args.start_line and args.end_line and args.start_line > args.end_line then
282285
-- Ensure start is before end (for reversed visual selections)
283286
args.start_line, args.end_line = args.end_line, args.start_line
284287
if args.start_col and args.end_col then
285288
-- Can happen in visual block mode
286289
args.start_col, args.end_col = args.end_col, args.start_col
287290
end
288291
end
289-
if args.start_line then
292+
if args and args.start_line then
290293
-- Previously we'd need a space here so `opencode` would recognize the file reference and insert the context,
291294
-- but that has regressed with the v1.0.0 `opentui` update :/
292295
-- There's a GH issue somewhere.
@@ -310,16 +313,14 @@ end
310313
---Range if present, else cursor position.
311314
function Context:this()
312315
if self.range then
313-
return Context.format({
314-
buf = self.buf,
316+
return Context.format(self.buf, {
315317
start_line = self.range.from[1],
316318
start_col = (self.range.kind ~= "line") and self.range.from[2] or nil,
317319
end_line = self.range.to[1],
318320
end_col = (self.range.kind ~= "line") and self.range.to[2] or nil,
319321
})
320322
else
321-
return Context.format({
322-
buf = self.buf,
323+
return Context.format(self.buf, {
323324
start_line = self.cursor[1],
324325
start_col = self.cursor[2] + 1,
325326
})
@@ -328,16 +329,14 @@ end
328329

329330
---The current buffer.
330331
function Context:buffer()
331-
return Context.format({
332-
buf = self.buf,
333-
})
332+
return Context.format(self.buf)
334333
end
335334

336335
---All open buffers.
337336
function Context:buffers()
338337
local file_list = {}
339338
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
340-
local path = Context.format({ buf = buf })
339+
local path = Context.format(buf)
341340
if path then
342341
table.insert(file_list, path)
343342
end
@@ -353,8 +352,7 @@ function Context:visible_text()
353352
local visible = {}
354353
for _, win in ipairs(vim.api.nvim_list_wins()) do
355354
local buf = vim.api.nvim_win_get_buf(win)
356-
local location = Context.format({
357-
buf = buf,
355+
local location = Context.format(buf, {
358356
start_line = vim.fn.line("w0", win),
359357
end_line = vim.fn.line("w$", win),
360358
})
@@ -371,7 +369,7 @@ end
371369
---@param diagnostic vim.Diagnostic
372370
---@return string
373371
function Context.format_diagnostic(diagnostic)
374-
local location = Context.format({
372+
local location = Context.format(diagnostic.bufnr, {
375373
start_line = diagnostic.lnum + 1,
376374
start_col = diagnostic.col + 1,
377375
end_line = diagnostic.end_lnum + 1,
@@ -393,13 +391,11 @@ function Context:diagnostics()
393391
return nil
394392
end
395393

396-
local location = Context.format({ buf = self.buf })
397-
398394
local diagnostic_strings = vim.tbl_map(function(diagnostic)
399395
return "- " .. Context.format_diagnostic(diagnostic)
400396
end, diagnostics)
401397

402-
return #diagnostics .. " diagnostics in " .. location .. "\n" .. table.concat(diagnostic_strings, "\n")
398+
return #diagnostics .. " diagnostics:" .. "\n" .. table.concat(diagnostic_strings, "\n")
403399
end
404400

405401
---Formatted quickfix list entries.
@@ -414,8 +410,7 @@ function Context:quickfix()
414410
if has_buf then
415411
table.insert(
416412
lines,
417-
Context.format({
418-
buf = entry.bufnr,
413+
Context.format(entry.bufnr, {
419414
start_line = entry.lnum,
420415
start_col = entry.col,
421416
})
@@ -446,8 +441,7 @@ function Context:marks()
446441
if mark.mark:match("^'[A-Z]$") then
447442
table.insert(
448443
marks,
449-
Context.format({
450-
buf = mark.pos[1],
444+
Context.format(mark.pos[1], {
451445
start_line = mark.pos[2],
452446
start_col = mark.pos[3],
453447
})
@@ -472,7 +466,7 @@ function Context:grapple_tags()
472466
end
473467
local paths = {}
474468
for _, tag in ipairs(tags) do
475-
table.insert(paths, Context.format({ path = tag.path }))
469+
table.insert(paths, Context.format(tag.path))
476470
end
477471
return table.concat(paths, ", ")
478472
end

0 commit comments

Comments
 (0)