@@ -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,33 @@ 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 " )
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
306312end
307313
@@ -310,16 +316,14 @@ end
310316--- Range if present, else cursor position.
311317function 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.
330334function Context :buffer ()
331- return Context .format ({
332- buf = self .buf ,
333- })
335+ return Context .format (self .buf )
334336end
335337
336338--- All open buffers.
337339function 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 })
371372--- @param diagnostic vim.Diagnostic
372373--- @return string
373374function 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 " )
403402end
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 , " , " )
478475end
0 commit comments