Skip to content

Commit

Permalink
fix(preview): dont show preview for directories. Fixes #410
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 30, 2024
1 parent 6c0204c commit 769ee0f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ end

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

local fd = uv.fs_open(opts.path, "r", 438)
if not fd then
return {}
return
end
local stat = assert(uv.fs_fstat(fd))
if not (stat.type == "file" or stat.type == "link") then
return
end
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
Expand Down
10 changes: 10 additions & 0 deletions lua/trouble/view/preview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ end
function M.create(item)
local buf = item.buf or vim.fn.bufnr(item.filename)

if item.filename and vim.fn.isdirectory(item.filename) then
return
end

-- create a scratch preview buffer when needed
if not (buf and vim.api.nvim_buf_is_loaded(buf)) then
buf = vim.api.nvim_create_buf(false, true)
vim.bo[buf].bufhidden = "wipe"
vim.bo[buf].buftype = "nofile"
local lines = Util.get_lines({ path = item.filename, buf = item.buf })
if not lines then
return
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, lines)
local ft = item:get_ft()
if ft then
Expand Down Expand Up @@ -85,6 +92,9 @@ function M.open(view, item)
end

local buf = M.create(item)
if not buf then
return
end

M.preview = M.preview_win(buf, view)

Expand Down

0 comments on commit 769ee0f

Please sign in to comment.