|
426 | 426 | ---@param output Output Output object to write to |
427 | 427 | ---@param text string |
428 | 428 | function M._format_assistant_message(output, text) |
429 | | - -- output:add_empty_line() |
430 | | - output:add_lines(vim.split(text, '\n')) |
| 429 | + local reference_picker = require('opencode.ui.reference_picker') |
| 430 | + local references = reference_picker.get_references_for_text(text) |
| 431 | + |
| 432 | + -- If no references, just add the text as-is |
| 433 | + if #references == 0 then |
| 434 | + output:add_lines(vim.split(text, '\n')) |
| 435 | + return |
| 436 | + end |
| 437 | + |
| 438 | + -- Sort references by match_start position (ascending) |
| 439 | + table.sort(references, function(a, b) |
| 440 | + return a.match_start < b.match_start |
| 441 | + end) |
| 442 | + |
| 443 | + -- Build a new text with icons inserted before each reference |
| 444 | + local result = '' |
| 445 | + local last_pos = 1 |
| 446 | + local ref_icon = icons.get('reference') |
| 447 | + |
| 448 | + for _, ref in ipairs(references) do |
| 449 | + -- Add text before this reference |
| 450 | + result = result .. text:sub(last_pos, ref.match_start - 1) |
| 451 | + -- Add the icon and the reference |
| 452 | + result = result .. ref_icon .. text:sub(ref.match_start, ref.match_end) |
| 453 | + last_pos = ref.match_end + 1 |
| 454 | + end |
| 455 | + |
| 456 | + -- Add any remaining text after the last reference |
| 457 | + if last_pos <= #text then |
| 458 | + result = result .. text:sub(last_pos) |
| 459 | + end |
| 460 | + |
| 461 | + local lines = vim.split(result, '\n') |
| 462 | + local start_line = output:get_line_count() |
| 463 | + output:add_lines(lines) |
| 464 | + |
| 465 | + -- Add highlighting for reference icons |
| 466 | + -- We need to find the icon positions in the rendered lines and add extmarks |
| 467 | + for i, line in ipairs(lines) do |
| 468 | + local line_num = start_line + i - 1 |
| 469 | + local search_start = 1 |
| 470 | + while true do |
| 471 | + local icon_start, icon_end = line:find(ref_icon, search_start, true) |
| 472 | + if not icon_start then |
| 473 | + break |
| 474 | + end |
| 475 | + -- Add extmark for the reference icon |
| 476 | + output:add_extmark(line_num, { |
| 477 | + virt_text = { { ref_icon, 'OpencodeReference' } }, |
| 478 | + virt_text_pos = 'overlay', |
| 479 | + end_col = icon_end, |
| 480 | + hl_group = 'OpencodeReference', |
| 481 | + priority = 100, |
| 482 | + } --[[@as OutputExtmark]]) |
| 483 | + search_start = icon_end + 1 |
| 484 | + end |
| 485 | + end |
431 | 486 | end |
432 | 487 |
|
433 | 488 | ---@param output Output Output object to write to |
|
0 commit comments