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

@@ -0,0 +1,32 @@
syn clear cTodo
syn clear cType
" Custom keywords
"
syn keyword cStorageClass function export internal global_var thread_var local_persist
syn keyword cOperator cast
" Type highlighting
"
" There are inconsistencies around this due to the horrific declaration syntax of C/C++ and
" trying to capture that inside a regexp statement is near impossible. These are made to mostly
" match the way I format my code.
"
syn match cType "\I\i*\ze\s\+\**\I\i*\s*[=;,\)\[]" " Regular declarations
syn match cType "\I\i*\ze\s\+\**\I\i*(" " Function return types
" Todo notes
"
syn match cTodo "@\I\i*\((\I\i*)\)\=:\?" contained
syn match cNote "\s:\I\i*" contained
" Functions
"
syn match cFunction "\I\i*\ze("
" Contain clusters
"
syn cluster cCommentGroup add=cNote
syn cluster cPreProcGroup add=cFunc,cType
hi def link cFunction Function

View File

@@ -0,0 +1,28 @@
syn clear
" I have completely overriden the formatting for the quickfix and location list buffers so this
" has to be changed to give proper highlighting of the new format
"
syn match qfSignError "\[!\]" contained
syn match qfSignWarn "\[#\]" contained
syn match qfSignSearch "\[\~\]" contained
syn match qfFileName "^[^:]*" contains=@qfSign nextgroup=qfSeparator1
syn match qfSeparator1 ":" contained nextgroup=qfLineNr
syn match qfLineNr "[^:|]*" contained nextgroup=qfSeparator2,qfSeparator3
syn match qfSeparator2 ":" contained nextgroup=qfColumnNr
syn match qfColumnNr "[^|]*" contained
syn match qfSeparator3 "|" contained
syn cluster qfSign contains=qfSignError,qfSignWarn,qfSignSearch
hi clear qfFileName
hi clear qfLineNr
hi def link qfSignError DiagnosticSignError
hi def link qfSignWarn DiagnosticSignWarn
hi def link qfSignSearch LineNr
hi def link qfFileName Function
hi def link qfLineNr Number
hi def link qfColumnNr Number
let b:current_syntax = "qf"

View File

@@ -143,6 +143,74 @@ local function PromptBuild()
) )
end 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 -- Buffers
local function BufferComplete() local function BufferComplete()
@@ -238,7 +306,7 @@ local function LayoutHelp(a)
if w ~= winid then if w ~= winid then
local bufnr = vim.api.nvim_win_get_buf(w) local bufnr = vim.api.nvim_win_get_buf(w)
if _is_help[vim.bo[bufnr].filetype] then 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 end
end end
@@ -278,6 +346,7 @@ MAP("n", "<Leader>m", PromptBuild)
AUTOCMD('BufEnter', { command = "let b:man_default_sects=\"2,3\"", pattern = "*.c" }) AUTOCMD('BufEnter', { command = "let b:man_default_sects=\"2,3\"", pattern = "*.c" })
AUTOCMD('BufReadPost', { command = "setlocal nornu", pattern = "quickfix" }) AUTOCMD('BufReadPost', { command = "setlocal nornu", pattern = "quickfix" })
AUTOCMD('BufReadPost', { callback = FormatJumpList, pattern = "quickfix" })
AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" }) AUTOCMD('BufWritePre', { callback = TrimBuffer, pattern = "*" })
AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" }) AUTOCMD('VimEnter', { callback = MakeScratch, pattern = "*" })
AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" }) AUTOCMD('BufWinEnter', { callback = LayoutHelp, pattern = "*" })