@@ -15,7 +15,7 @@ Context.__index = Context
1515local 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
1919local 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,29 @@ function Context.input_highlight(rendered)
265265end
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" )
272-
271+ function Context .format (loc , args )
273272 local result = " "
274- local filepath = (args .buf and get_filename (args .buf )) or args .path
273+ -- Not we check number, not integer - I think integer is only an annotations thing, and at runtime only numbers exist?
274+ local filepath = (type (loc ) == " number" and get_filename (loc )) or type (loc ) == " string" and loc or nil
275+
275276 if filepath and filepath ~= " " then
276277 local absolute_path = vim .fn .fnamemodify (filepath , " :p:~" )
277278 result = absolute_path
278279 else
279280 return nil
280281 end
281- if args .start_line and args .end_line and args .start_line > args .end_line then
282+ if args and args .start_line and args .end_line and args .start_line > args .end_line then
282283 -- Ensure start is before end (for reversed visual selections)
283284 args .start_line , args .end_line = args .end_line , args .start_line
284285 if args .start_col and args .end_col then
285286 -- Can happen in visual block mode
286287 args .start_col , args .end_col = args .end_col , args .start_col
287288 end
288289 end
289- if args .start_line then
290+ if args and args .start_line then
290291 -- Previously we'd need a space here so `opencode` would recognize the file reference and insert the context,
291292 -- but that has regressed with the v1.0.0 `opentui` update :/
292293 -- There's a GH issue somewhere.
@@ -310,16 +311,14 @@ end
310311--- Range if present, else cursor position.
311312function Context :this ()
312313 if self .range then
313- return Context .format ({
314- buf = self .buf ,
314+ return Context .format (self .buf , {
315315 start_line = self .range .from [1 ],
316316 start_col = (self .range .kind ~= " line" ) and self .range .from [2 ] or nil ,
317317 end_line = self .range .to [1 ],
318318 end_col = (self .range .kind ~= " line" ) and self .range .to [2 ] or nil ,
319319 })
320320 else
321- return Context .format ({
322- buf = self .buf ,
321+ return Context .format (self .buf , {
323322 start_line = self .cursor [1 ],
324323 start_col = self .cursor [2 ] + 1 ,
325324 })
@@ -328,16 +327,14 @@ end
328327
329328--- The current buffer.
330329function Context :buffer ()
331- return Context .format ({
332- buf = self .buf ,
333- })
330+ return Context .format (self .buf )
334331end
335332
336333--- All open buffers.
337334function Context :buffers ()
338335 local file_list = {}
339336 for _ , buf in ipairs (vim .api .nvim_list_bufs ()) do
340- local path = Context .format ({ buf = buf } )
337+ local path = Context .format (buf )
341338 if path then
342339 table.insert (file_list , path )
343340 end
@@ -353,8 +350,7 @@ function Context:visible_text()
353350 local visible = {}
354351 for _ , win in ipairs (vim .api .nvim_list_wins ()) do
355352 local buf = vim .api .nvim_win_get_buf (win )
356- local location = Context .format ({
357- buf = buf ,
353+ local location = Context .format (buf , {
358354 start_line = vim .fn .line (" w0" , win ),
359355 end_line = vim .fn .line (" w$" , win ),
360356 })
371367--- @param diagnostic vim.Diagnostic
372368--- @return string
373369function Context .format_diagnostic (diagnostic )
374- local location = Context .format ({
370+ local location = Context .format (diagnostic . bufnr , {
375371 start_line = diagnostic .lnum + 1 ,
376372 start_col = diagnostic .col + 1 ,
377373 end_line = diagnostic .end_lnum + 1 ,
@@ -393,13 +389,11 @@ function Context:diagnostics()
393389 return nil
394390 end
395391
396- local location = Context .format ({ buf = self .buf })
397-
398392 local diagnostic_strings = vim .tbl_map (function (diagnostic )
399393 return " - " .. Context .format_diagnostic (diagnostic )
400394 end , diagnostics )
401395
402- return # diagnostics .. " diagnostics in " .. location .. " \n " .. table.concat (diagnostic_strings , " \n " )
396+ return # diagnostics .. " diagnostics: " .. " \n " .. table.concat (diagnostic_strings , " \n " )
403397end
404398
405399--- Formatted quickfix list entries.
@@ -414,8 +408,7 @@ function Context:quickfix()
414408 if has_buf then
415409 table.insert (
416410 lines ,
417- Context .format ({
418- buf = entry .bufnr ,
411+ Context .format (entry .bufnr , {
419412 start_line = entry .lnum ,
420413 start_col = entry .col ,
421414 })
@@ -446,8 +439,7 @@ function Context:marks()
446439 if mark .mark :match (" ^'[A-Z]$" ) then
447440 table.insert (
448441 marks ,
449- Context .format ({
450- buf = mark .pos [1 ],
442+ Context .format (mark .pos [1 ], {
451443 start_line = mark .pos [2 ],
452444 start_col = mark .pos [3 ],
453445 })
@@ -472,7 +464,7 @@ function Context:grapple_tags()
472464 end
473465 local paths = {}
474466 for _ , tag in ipairs (tags ) do
475- table.insert (paths , Context .format ({ path = tag .path } ))
467+ table.insert (paths , Context .format (tag .path ))
476468 end
477469 return table.concat (paths , " , " )
478470end
0 commit comments