repos / dotfiles

my dotfiles

commit
4b0b7b2
parent
039c04b
author
Eric Bower
date
2025-09-23 23:53:24 -0400 EDT
refactor(nvim): cqf more flexible
1 files changed,  +36, -17
M dot_config/nvim/init.lua
+36, -17
  1@@ -6,9 +6,11 @@
  2     - single file
  3     - use native nvim features (>= v0.12)
  4     - use default keybindings unless painful otherwise
  5+    - use built-ins unless painful otherwise
  6     - use default colorschemes
  7     - plugins must be integral to workflow
  8 ]]
  9+local vim = vim -- suppress lsp warnings
 10 local cqf
 11 local o = vim.opt
 12 o.tabstop = 2
 13@@ -53,6 +55,9 @@ map("n", "<leader>f", "<cmd>term fish<cr>", opts)
 14 map("n", "<leader>b", ":b ")
 15 map({ "n", "v" }, "<leader>u", "<cmd>GitLink<cr>", opts)
 16 map("n", "<leader>e", vim.diagnostic.open_float, opts)
 17+map("n", "<leader>y", function() -- copy relative filepath to clipboard
 18+	vim.fn.setreg("+", vim.fn.expand("%"))
 19+end)
 20 map("n", "<leader>l", function() -- https://github.com/shell-pool/shpool/issues/240#issuecomment-3097566679
 21 	io.stdout:write("\027[?2048h")
 22 end, opts)
 23@@ -65,9 +70,6 @@ map("n", "<leader>r", function()
 24 		vim.diagnostic.setloclist({ open = true })
 25 	end
 26 end, opts)
 27-map("n", "<leader>y", function() -- copy relative filepath to clipboard
 28-	vim.fn.setreg("+", vim.fn.expand("%"))
 29-end)
 30 map("n", "<leader>q", function() -- toggle quickfix
 31 	for _, win in ipairs(vim.fn.getwininfo()) do
 32 		if win.quickfix == 1 then
 33@@ -83,10 +85,15 @@ map("n", "<leader>g", function() -- grep entire project
 34 		vim.cmd("copen")
 35 	end)
 36 end, opts)
 37-map("n", "<leader>d", ":Qf git diff --name-only")
 38+map("n", "<leader>d", function()
 39+	cqf({ cmd = "git diff --name-only" })
 40+end)
 41 map("n", "<leader>s", function() -- fuzzy match files into quickfix
 42 	vim.ui.input({ prompt = "file: " }, function(file)
 43-		cqf(string.format("fd -t f | fzf -f %s", file), true)
 44+		cqf({
 45+			cmd = string.format("fd -t f | fzf -f %s", file),
 46+			auto_jump = true,
 47+		})
 48 	end)
 49 end)
 50 
 51@@ -190,26 +197,38 @@ require("treesitter-context").setup({ max_lines = 3, separator = ">", mode = "to
 52 require("neoscroll").setup({ duration_multiplier = 0.3 })
 53 require("gitlinker").setup()
 54 
 55-vim.api.nvim_create_user_command("Qf", function(opts)
 56-	cqf(opts.args, false)
 57+vim.api.nvim_create_user_command("Qf", function(props)
 58+	cqf({ cmd = props.args })
 59 end, { nargs = "+", desc = "convert command to quickfix" })
 60 
 61+local function cqf_fmt(line)
 62+	local path, rest = line:match("(%S+)%s+(.*)")
 63+	if path then
 64+		return string.format("%s:1:1:%s", path, rest)
 65+	end
 66+	return ""
 67+end
 68+
 69 ---Attempts to convert a command that returns a list of filepaths into a quickfix.
 70 ---It assumes the filepaths are in the first column without spaces.
 71 ---@param cmd string The system command to run
 72----@param jump boolean Should we auto navigate to first entry?
 73-function cqf(cmd, jump)
 74-	jump = jump or false
 75+---@param auto_jump boolean Should we auto navigate to first entry?
 76+---@param fmt function callback for each line to transform list into errorformat
 77+function cqf(args)
 78+	local jump = args.auto_jump or false
 79+	local cmd = args.cmd
 80 	if cmd == nil then
 81 		return
 82 	end
 83+	local efm = "%f:%l:%c:%m"
 84+	local fmt = args.fmt or cqf_fmt
 85 
 86-	local files = vim.fn.systemlist(cmd)
 87+	local lines = vim.fn.systemlist(cmd)
 88 	local fz = {}
 89-	for _, file in ipairs(files) do
 90-		local word = file:match("%S+")
 91-		if word then
 92-			table.insert(fz, word .. ":1:1:" .. word)
 93+	for _, line in ipairs(lines) do
 94+		local ln = fmt(line)
 95+		if #ln > 0 then
 96+			table.insert(fz, ln)
 97 		end
 98 	end
 99 
100@@ -219,14 +238,14 @@ function cqf(cmd, jump)
101 	end
102 
103 	if jump and #fz == 1 then
104-		vim.cmd(string.format("edit %s", files[1]))
105+		vim.cmd(string.format("edit %s", lines[1]))
106 		return
107 	end
108 
109 	vim.fn.setqflist({}, "r", {
110 		title = cmd,
111 		lines = fz,
112-		efm = "%f:%l:%c:%m",
113+		efm = efm,
114 	})
115 	vim.cmd("copen")
116 end