From a1e32452b36cc63c6a8c884cfb8147d1d7a6783c Mon Sep 17 00:00:00 2001 From: aleidk Date: Thu, 26 Oct 2023 16:38:22 -0300 Subject: [PATCH] update quickfix setup nvim --- config/nvim/lazy-lock.json | 2 + config/nvim/lua/aleidk/plugins/quickfix.lua | 96 +++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 config/nvim/lua/aleidk/plugins/quickfix.lua diff --git a/config/nvim/lazy-lock.json b/config/nvim/lazy-lock.json index 13f0571..d082ca4 100644 --- a/config/nvim/lazy-lock.json +++ b/config/nvim/lazy-lock.json @@ -12,6 +12,7 @@ "diffview.nvim": { "branch": "main", "commit": "d38c1b5266850f77f75e006bcc26213684e1e141" }, "dressing.nvim": { "branch": "master", "commit": "1f2d1206a03bd3add8aedf6251e4534611de577f" }, "friendly-snippets": { "branch": "main", "commit": "43727c2ff84240e55d4069ec3e6158d74cb534b6" }, + "fzf": { "branch": "master", "commit": "ec208af474fa3eadad5779c3d30db6d2db300932" }, "fzf-lua": { "branch": "main", "commit": "6589bdd872385d120a1eb3cb85e5d15d37621c1d" }, "gitsigns.nvim": { "branch": "main", "commit": "5a9a6ac29a7805c4783cda21b80a1e361964b3f2" }, "harpoon": { "branch": "master", "commit": "c1aebbad9e3d13f20bedb8f2ce8b3a94e39e424a" }, @@ -33,6 +34,7 @@ "nui.nvim": { "branch": "main", "commit": "c0c8e347ceac53030f5c1ece1c5a5b6a17a25b32" }, "null-ls.nvim": { "branch": "main", "commit": "0010ea927ab7c09ef0ce9bf28c2b573fc302f5a7" }, "nvim": { "branch": "main", "commit": "ea52fe8a0b1e4a820df0d0cf9a6a5a0e18c3eaa0" }, + "nvim-bqf": { "branch": "main", "commit": "8784eebf34371049b641646d00232c2603215297" }, "nvim-cmp": { "branch": "main", "commit": "51260c02a8ffded8e16162dcf41a23ec90cfba62" }, "nvim-lint": { "branch": "master", "commit": "962a76877a4479a535b935bd7ef35ad41ba308b2" }, "nvim-lspconfig": { "branch": "master", "commit": "6428fcab6f3c09e934bc016c329806314384a41e" }, diff --git a/config/nvim/lua/aleidk/plugins/quickfix.lua b/config/nvim/lua/aleidk/plugins/quickfix.lua new file mode 100644 index 0000000..b2da610 --- /dev/null +++ b/config/nvim/lua/aleidk/plugins/quickfix.lua @@ -0,0 +1,96 @@ +return { + { + "kevinhwang91/nvim-bqf", + event = "VeryLazy", + dependencies = { + { + "junegunn/fzf", + build = function() + vim.fn["fzf#install"]() + end, + }, + }, + config = function() + local fn = vim.fn + + function _G.qftf(info) + local items + local ret = {} + -- The name of item in list is based on the directory of quickfix window. + -- Change the directory for quickfix window make the name of item shorter. + -- It's a good opportunity to change current directory in quickfixtextfunc :) + -- + -- local alterBufnr = fn.bufname('#') -- alternative buffer is the buffer before enter qf window + -- local root = getRootByAlterBufnr(alterBufnr) + -- vim.cmd(('noa lcd %s'):format(fn.fnameescape(root))) + -- + if info.quickfix == 1 then + items = fn.getqflist({ id = info.id, items = 0 }).items + else + items = fn.getloclist(info.winid, { id = info.id, items = 0 }).items + end + local limit = 31 + local fnameFmt1, fnameFmt2 = "%-" .. limit .. "s", "…%." .. (limit - 1) .. "s" + local validFmt = "%s │%5d:%-3d│%s %s" + for i = info.start_idx, info.end_idx do + local e = items[i] + local fname = "" + local str + if e.valid == 1 then + if e.bufnr > 0 then + fname = fn.bufname(e.bufnr) + if fname == "" then + fname = "[No Name]" + else + fname = fname:gsub("^" .. vim.env.HOME, "~") + end + -- char in fname may occur more than 1 width, ignore this issue in order to keep performance + if #fname <= limit then + fname = fnameFmt1:format(fname) + else + fname = fnameFmt2:format(fname:sub(1 - limit)) + end + end + local lnum = e.lnum > 99999 and -1 or e.lnum + local col = e.col > 999 and -1 or e.col + local qtype = e.type == "" and "" or " " .. e.type:sub(1, 1):upper() + str = validFmt:format(fname, lnum, col, qtype, e.text) + else + str = e.text + end + table.insert(ret, str) + end + return ret + end + + vim.o.qftf = "{info -> v:lua._G.qftf(info)}" + + -- Adapt fzf's delimiter in nvim-bqf + require("bqf").setup({ + filter = { + fzf = { + extra_opts = { "--bind", "ctrl-o:toggle-all", "--delimiter", "│" }, + }, + }, + }) + + local toggle_qf = function() + local qf_open = false + for _, win in pairs(vim.fn.getwininfo()) do + if win["quickfix"] == 1 then + qf_open = true + end + end + if qf_open == true then + vim.cmd("cclose") + return + end + if not vim.tbl_isempty(vim.fn.getqflist()) then + vim.cmd("copen") + end + end + + MAP("n", "fq", toggle_qf, "Toggle quickfix") + end, + }, +}