Skip to content

Commit c7594f8

Browse files
fix(context): multiple file references in diagnostics context, and improve format (#67)
Co-authored-by: Nick van Dyke <nickjvandyke15@gmail.com>
1 parent 8949725 commit c7594f8

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

lua/opencode/context.lua

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,20 @@ end
171171
---e.g. `@opencode.lua L21:C10-L65:C11`
172172
---@param args { buf?: integer, path?: string, start_line?: integer, start_col?: integer, end_line?: integer, end_col?: integer }
173173
function Context.format(args)
174-
assert(args.buf or args.path, "Must provide either `buf` or `path`")
175-
if args.buf and not is_buf_valid(args.buf) then
176-
return nil
174+
local result = ""
175+
if (args.buf and is_buf_valid(args.buf)) or args.path then
176+
local rel_path = vim.fn.fnamemodify(args.path or vim.api.nvim_buf_get_name(args.buf), ":.")
177+
-- Must be preceeded by @ and followed by space for `opencode` to parse as a file reference
178+
result = "@" .. rel_path .. " "
177179
end
178-
local rel_path = vim.fn.fnamemodify(args.path or vim.api.nvim_buf_get_name(args.buf), ":.")
179-
local result = "@" .. rel_path
180180
if args.start_line and args.end_line and args.start_line > args.end_line then
181181
args.start_line, args.end_line = args.end_line, args.start_line
182182
if args.start_col and args.end_col then
183183
args.start_col, args.end_col = args.end_col, args.start_col
184184
end
185185
end
186186
if args.start_line then
187-
result = result .. string.format(" L%d", args.start_line)
187+
result = result .. string.format("L%d", args.start_line)
188188
if args.start_col then
189189
result = result .. string.format(":C%d", args.start_col)
190190
end
@@ -281,29 +281,30 @@ function Context:diagnostics()
281281
if #diagnostics == 0 then
282282
return nil
283283
end
284+
285+
local file_ref = Context.format({ buf = self.buf })
286+
284287
local diagnostic_strings = {}
285288
for _, diagnostic in ipairs(diagnostics) do
289+
local location = Context.format({
290+
start_line = diagnostic.lnum + 1,
291+
start_col = diagnostic.col + 1,
292+
end_line = diagnostic.end_lnum + 1,
293+
end_col = diagnostic.end_col + 1,
294+
})
295+
286296
table.insert(
287297
diagnostic_strings,
288298
string.format(
289-
"%s (%s): %s",
290-
Context.format({
291-
buf = self.buf,
292-
start_line = diagnostic.lnum + 1,
293-
start_col = diagnostic.col + 1,
294-
end_line = diagnostic.end_lnum + 1,
295-
end_col = diagnostic.end_col + 1,
296-
}),
299+
"- %s (%s): %s",
300+
location,
297301
diagnostic.source or "unknown source",
298302
diagnostic.message:gsub("%s+", " "):gsub("^%s", ""):gsub("%s$", "")
299303
)
300304
)
301305
end
302-
return #diagnostics
303-
.. " diagnostic"
304-
.. (#diagnostics > 1 and "s" or "")
305-
.. ": "
306-
.. table.concat(diagnostic_strings, "; ")
306+
307+
return #diagnostics .. " diagnostics in " .. file_ref .. "\n" .. table.concat(diagnostic_strings, "\n")
307308
end
308309

309310
---Formatted quickfix list entries.

0 commit comments

Comments
 (0)