Skip to content

Commit

Permalink
read item contents by bufnr instead of reading file by path (#287)
Browse files Browse the repository at this point in the history
* read item contents by `bufnr` instead of reading file by path

* not need to read all the lines

* only read buf when the filename is a uri
  • Loading branch information
eatgrass committed May 26, 2023
1 parent a0f2375 commit 7f7d6db
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,21 @@ function M.process_item(item, bufnr)
local col = start.character

if not item.message and filename then
local fd = assert(uv.fs_open(filename, "r", 438))
local stat = assert(uv.fs_fstat(fd))
local data = assert(uv.fs_read(fd, stat.size, 0))
assert(uv.fs_close(fd))

item.message = vim.split(data, "\n", { plain = true })[row + 1] or ""
-- check if the filename is a uri
if string.match(filename, "^%w+://") ~= nil then
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
end
local lines = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)
item.message = lines[1] or ""
else
local fd = assert(uv.fs_open(filename, "r", 438))
local stat = assert(uv.fs_fstat(fd))
local data = assert(uv.fs_read(fd, stat.size, 0))
assert(uv.fs_close(fd))

item.message = vim.split(data, "\n", { plain = true })[row + 1] or ""
end
end

---@class Item
Expand Down

0 comments on commit 7f7d6db

Please sign in to comment.