1
0

Custom syntax highlighting

For c and quickfix
Function to format the quickfix and location jump lists
Fixed issue with "help" window layouts accessing incorrect variable
This commit is contained in:
2025-09-25 00:04:21 +01:00
parent ce611a0e14
commit da251e7223
3 changed files with 136 additions and 7 deletions

View File

@@ -143,6 +143,74 @@ local function PromptBuild()
)
end
local function FormatJumpList()
local qf = vim.fn.getqflist( { winid = true, qfbufnr = true })
local loc = vim.fn.getloclist(0, { winid = true, qfbufnr = true })
if qf.winid ~= 0 then
-- This means the quickfix list is open and needs formatting
local ns = vim.api.nvim_create_namespace("__qf.format")
local bufnr = qf.qfbufnr
local entries = vim.fn.getqflist()
local text = {}
local max = { 0, 0, 0 }
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
for _, it in ipairs(entries) do
max[1] = math.max(max[1], #tostring(it.lnum))
max[2] = math.max(max[2], #tostring(it.col))
max[3] = math.max(max[3], #vim.fn.bufname(it.bufnr))
end
-- +2 for the extra colons we put between the filename, line number and column number
local extformat = string.format("[%%s] %%-%ds | %%s", max[1] + max[2] + max[3] + 2)
for _, it in ipairs(entries) do
local sign = it.type:lower() == "e" and "!" or "#"
local fname = string.format("%s:%d:%d" , vim.fn.bufname(it.bufnr), it.lnum, it.col)
table.insert(text, extformat:format(sign, fname, it.text))
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, text)
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
vim.api.nvim_buf_set_option(bufnr, "modified", false)
end
if loc.winid ~= 0 then
-- This means the location list is open and needs formatting
local ns = vim.api.nvim_create_namespace("__loc.format")
local bufnr = loc.qfbufnr
local entries = vim.fn.getloclist(0)
local text = {}
local max = { 0, 0 }
vim.api.nvim_buf_clear_namespace(bufnr, ns, 0, -1)
vim.api.nvim_buf_set_option(bufnr, "modifiable", true)
for _, it in ipairs(entries) do
max[1] = math.max(max[1], #vim.fn.bufname(it.bufnr))
max[2] = math.max(max[2], #tostring(it.lnum))
end
-- +1 for the extra colon we put between the filename and the line number
local extformat = string.format("[~] %%-%ds | %%s", max[1] + max[2] + 1)
for _, it in ipairs(entries) do
local fname = string.format("%s:%d", vim.fn.bufname(it.bufnr), it.lnum)
table.insert(text, extformat:format(fname, it.text))
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, text)
vim.api.nvim_buf_set_option(bufnr, "modifiable", false)
vim.api.nvim_buf_set_option(bufnr, "modified", false)
end
end
-- Buffers
local function BufferComplete()
@@ -238,7 +306,7 @@ local function LayoutHelp(a)
if w ~= winid then
local bufnr = vim.api.nvim_win_get_buf(w)
if _is_help[vim.bo[bufnr].filetype] then
vim.api.nvim_win_close(wi.winid, true)
vim.api.nvim_win_close(w, true)
end
end
end
@@ -278,9 +346,10 @@ MAP("n", "<Leader>m", PromptBuild)
AUTOCMD('BufEnter', { command = "let b:man_default_sects=\"2,3\"", pattern = "*.c" })
AUTOCMD('BufReadPost', { command = "setlocal nornu", pattern = "quickfix" })
AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" })
AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" })
AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" })
AUTOCMD('WinEnter', { callback = ExpandHelp, pattern = "*" })
AUTOCMD('WinLeave', { callback = ExpandHelp, pattern = "*" })
AUTOCMD('BufRead', { callback = LoadDiagnostics, pattern = "*" })
AUTOCMD('BufReadPost', { callback = FormatJumpList, pattern = "quickfix" })
AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" })
AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" })
AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" })
AUTOCMD('WinEnter', { callback = ExpandHelp, pattern = "*" })
AUTOCMD('WinLeave', { callback = ExpandHelp, pattern = "*" })
AUTOCMD('BufRead', { callback = LoadDiagnostics, pattern = "*" })