Skip to content

Commit

Permalink
feat(item): util method to add missing text to items
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 6940cd8 commit de9e7e6
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lua/trouble/item.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Cache = require("trouble.cache")
local Util = require("trouble.util")

---@alias trouble.Pos {[1]:number, [2]:number}

Expand Down Expand Up @@ -49,4 +50,43 @@ function M:__index(k)
end
end

---@param items trouble.Item[]
function M.add_text(items)
local buf_rows = {} ---@type table<number, number[]>

for _, item in ipairs(items) do
if not item.item.text then
-- schedule to get the lines
buf_rows[item.buf] = buf_rows[item.buf] or {}
for r = item.pos[1], item.end_pos and item.end_pos[1] or item.pos[1] do
table.insert(buf_rows[item.buf], r)
end
end
end

-- get the lines and range text
local buf_lines = {} ---@type table<number, table<number, string>>
for buf, rows in pairs(buf_rows) do
buf_lines[buf] = Util.get_lines(buf, rows)
end
for _, item in ipairs(items) do
if not item.item.text then
local lines = {} ---@type string[]
for row = item.pos[1], item.end_pos[1] do
local line = buf_lines[item.buf][row] or ""
if row == item.pos[1] and row == item.end_pos[1] then
line = line
elseif row == item.pos[1] then
line = line:sub(item.pos[2] + 1)
elseif row == item.end_pos[1] then
line = line:sub(1, item.end_pos[2]) --[[@as string]]
end
lines[#lines + 1] = line
end
item.item.text = table.concat(lines, "\n")
end
end
return items
end

return M

0 comments on commit de9e7e6

Please sign in to comment.