Skip to content

Commit

Permalink
perf(util): get_lines can now use a buf or filename or both
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent ba1ae49 commit e987642
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
23 changes: 14 additions & 9 deletions lua/trouble/item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ end
function M.add_text(items, opts)
opts = opts or {}
opts.mode = opts.mode or "range"
local buf_rows = {} ---@type table<number, number[]>
local todo = {} ---@type table<string, {buf?:number, rows:number[]}>

for _, item in ipairs(items) do
if not item.item.text then
if not item.item.text and item.filename then
-- schedule to get the lines
buf_rows[item.buf] = buf_rows[item.buf] or {}
todo[item.filename] = todo[item.filename] or { rows = {} }
todo[item.filename].buf = todo[item.filename].buf or item.buf
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)
table.insert(todo[item.filename].rows, r)
if not opts.multiline then
break
end
Expand All @@ -88,15 +89,19 @@ function M.add_text(items, opts)
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)
local buf_lines = {} ---@type table<string, table<number, string>>
for path, t in pairs(todo) do
buf_lines[path] = Util.get_lines({
rows = t.rows,
buf = t.buf,
path = path,
})
end
for _, item in ipairs(items) do
if not item.item.text then
if not item.item.text and item.filename then
local lines = {} ---@type string[]
for row = item.pos[1], item.end_pos[1] do
local line = buf_lines[item.buf][row] or ""
local line = buf_lines[item.filename][row] or ""
if row == item.pos[1] and row == item.end_pos[1] then
if opts.mode == "after" then
line = line:sub(item.pos[2] + 1)
Expand Down
43 changes: 28 additions & 15 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ function M.throttle(fn, opts)
xpcall(function()
local _args = vim.F.unpack_len(args)
if not trailing then
if not trailing and not timer:is_active() then
args = {} -- clear args so they can be gc'd
end
fn(_args)
Expand Down Expand Up @@ -203,37 +204,49 @@ function M.split(s, c)
end
end

---@param buf number
---@param rows number[] 1-indexed
--- Gets lines from a file or buffer
---@param opts {path?:string, buf?: number, rows?: number[]}
---@return table<number, string>
function M.get_lines(buf, rows)
local uri = vim.uri_from_bufnr(buf)
function M.get_lines(opts)
if opts.buf then
local uri = vim.uri_from_bufnr(opts.buf)

if uri:sub(1, 4) ~= "file" then
vim.fn.bufload(buf)
end
if uri:sub(1, 4) ~= "file" then
vim.fn.bufload(opts.buf)
end

if vim.api.nvim_buf_is_loaded(buf) then
local lines = {} ---@type table<number, string>
for _, row in ipairs(rows) do
lines[row] = (vim.api.nvim_buf_get_lines(buf, row - 1, row, false) or { "" })[1]
if vim.api.nvim_buf_is_loaded(opts.buf) then
local lines = {} ---@type table<number, string>
if not opts.rows then
return vim.api.nvim_buf_get_lines(opts.buf, 0, -1, false)
end
for _, row in ipairs(opts.rows) do
lines[row] = vim.api.nvim_buf_get_lines(opts.buf, row - 1, row, false)[1]
end
return lines
end
return lines
opts.path = vim.uri_to_fname(uri)
elseif not opts.path then
error("buf or filename is required")
end

local filename = vim.api.nvim_buf_get_name(buf)
local fd = uv.fs_open(filename, "r", 438)
local fd = uv.fs_open(opts.path, "r", 438)
if not fd then
return {}
end
local stat = assert(uv.fs_fstat(fd))
local data = assert(uv.fs_read(fd, stat.size, 0)) --[[@as string]]
assert(uv.fs_close(fd))
local todo = opts.rows and #opts.rows or -1

local ret = {} ---@type table<number, string>
for row, line in M.lines(data) do
if vim.tbl_contains(rows, row) then
if not opts.rows or vim.tbl_contains(opts.rows, row) then
todo = todo - 1
ret[row] = line
if todo == 0 then
break
end
end
end
return ret
Expand Down

0 comments on commit e987642

Please sign in to comment.