Something went wrong. Try again.
Just a preview for Neovim's quickfix list
Something went wrong. Try again.
4.5 kB · 191 lines
Lua
at master
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192-- TODO: support vertically split qflist
local defaults = require("qfpreview.config").defaultslocal fs = require("qfpreview.fs")
---@class qfpreview.Preview---@field config qfpreview.Config---@field winnr numberlocal Preview = {}Preview.__index = Preview
---@param config? qfpreview.Config---@return qfpreview.Previewfunction Preview:new(config) local p = { config = vim.tbl_deep_extend("force", defaults, config or {}), winnr = nil, } setmetatable(p, self) self.__index = self return pend
---@class QuickfixItem---@field bufnr number---@field module string---@field lnum number---@field end_lnum number---@field col number---@field end_col number---@field vcol boolean---@field pattern any---@field text string---@field type string---@field valid boolean---@field user_data any
---@return QuickfixItemfunction Preview:curr_item() ---@type QuickfixItem local qflist = vim.fn.getqflist() return qflist[vim.fn.line(".") - 1]end
---@return integerfunction Preview:bufnr() return vim.api.nvim_win_get_buf(self.winnr)end
function Preview:disable_lsp() local bufnr = self:bufnr()
if self.config.opts.lsp == false then for _, client in pairs(vim.lsp.get_clients({ bufnr = bufnr })) do vim.lsp.buf_detach_client(bufnr, client.id) end end
vim.diagnostic.enable(self.config.opts.diagnostics, { bufnr = bufnr })end
---@param qfwin number---@return number,numberlocal function get_aligned_col_width(qfwin) local has_border = vim.o.winborder ~= "none" local col = vim.api.nvim_win_get_position(qfwin)[2] local width = vim.api.nvim_win_get_width(qfwin)
if not has_border then return col, width end
if (col + width) == vim.o.columns then return col + 1, width - 1 elseif col == 0 then return col, width - 1 else return col - 1, width endend
---@param qfwin number---@return vim.api.keyset.win_configfunction Preview:win_config(qfwin) local has_border = vim.o.winborder ~= "none" local border_width = has_border and 2 or 0 local col, width = get_aligned_col_width(qfwin)
if self.config.ui.height == "fill" then local statusline_height = vim.o.laststatus == 0 and 0 or 1 local height = vim.o.lines - vim.api.nvim_win_get_height(qfwin) - vim.o.cmdheight - border_width - statusline_height
if height < 0 then height = 15 end
return { relative = "editor", width = width, height = height, row = 0, col = col, focusable = false, } end
local height = self.config.ui.height or 15
return { relative = "win", win = vim.api.nvim_get_current_win(), width = width, height = height, row = -1 * height - border_width, col = col, focusable = false, }end
---@return booleanfunction Preview:is_open() return self.winnr ~= nilend
---@param bufnr number---@return stringfunction Preview:title(bufnr) return fs.normalize_path(vim.fn.bufname(bufnr), vim.fn.getcwd())end
---@param qfwin numberfunction Preview:open(qfwin) local qf_list = vim.fn.getqflist() if vim.tbl_isempty(qf_list) then return end
local item = self:curr_item()
---@type vim.api.keyset.win_config local winconfig = vim.tbl_extend("force", self:win_config(qfwin), self.config.ui.win or {})
if self.config.ui.show_name then winconfig.title = self:title(item.bufnr) winconfig.title_pos = "left" end
self.winnr = vim.api.nvim_open_win(item.bufnr, false, winconfig) self:disable_lsp()
vim.wo[self.winnr].relativenumber = false vim.wo[self.winnr].number = true vim.wo[self.winnr].winblend = 0 vim.wo[self.winnr].cursorline = true
vim.bo[item.bufnr].buftype = "nowrite"
vim.api.nvim_win_set_cursor(self.winnr, { item.lnum, item.col + 1 })end
function Preview:close() if not self:is_open() then return end
if self.winnr and vim.api.nvim_win_is_valid(self.winnr) then local force = true vim.api.nvim_win_close(self.winnr, force) self.winnr = nil endend
---@param qfwin numberfunction Preview:refresh(qfwin) if not self:is_open() then self:open(qfwin) return end
local item = self:curr_item()
vim.api.nvim_win_set_buf(self.winnr, item.bufnr) if self.config.ui.show_name then local win_config = vim.tbl_extend("force", self:win_config(qfwin), { title = self:title(item.bufnr) }) vim.api.nvim_win_set_config(self.winnr, win_config) end
vim.api.nvim_win_set_cursor(self.winnr, { item.lnum, item.col + 1 })end
return Preview