robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
2.6 KiB79 linesraw
1-- Highlighting
2local ns = vim.api.nvim_create_namespace('nvim.pack.confirm')
3vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
4
5local priority = 100
6
7--- @param lnum integer
8--- @param start_col integer
9--- @param end_col integer
10--- @param hl string?
11--- @param pr? integer
12local function hi_range(lnum, start_col, end_col, hl, pr)
13  vim.api.nvim_buf_set_extmark(0, ns, lnum - 1, start_col, {
14    end_row = lnum - 1,
15    end_col = end_col,
16    hl_group = hl,
17    priority = pr or priority,
18    -- Set expanding gravity for easier testing. Should not make big difference.
19    right_gravity = false,
20    end_right_gravity = true,
21  })
22end
23
24local header_hl_groups =
25  { Error = 'DiagnosticError', Update = 'DiagnosticWarn', Same = 'DiagnosticHint' }
26local cur_header_hl_group --- @type string?
27
28local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
29for i, l in ipairs(lines) do
30  local cur_group = l:match('^# (%S+)')
31  local cur_info = l:match('^Path: +') or l:match('^Source: +') or l:match('^Revision[^:]*: +')
32  if cur_group ~= nil then
33    --- @cast cur_group string
34    -- Header 1
35    cur_header_hl_group = header_hl_groups[cur_group]
36    hi_range(i, 0, l:len(), cur_header_hl_group)
37  elseif l:find('^## (.+)$') ~= nil then
38    -- Header 2 with possibly "(not active)" suffix
39    hi_range(i, 0, l:len(), cur_header_hl_group)
40    local col = (l:match('() %(not active%)$') or l:len()) --[[@as integer]]
41    hi_range(i, col, l:len(), 'DiagnosticError', priority + 1)
42  elseif cur_info ~= nil then
43    -- Plugin info
44    local end_col = (l:match('(). +%b()$') or l:len()) --[[@as integer]]
45    hi_range(i, cur_info:len(), end_col, 'DiagnosticInfo')
46
47    -- Plugin version after update
48    local col = l:match('() %b()$') --[[@as integer?]]
49    if col then
50      hi_range(i, col, l:len(), 'DiagnosticHint')
51    end
52  elseif l:match('^> ') then
53    -- Added change with possibly "breaking message"
54    hi_range(i, 0, l:len(), 'Added')
55    local col = (l:match('│() %S+!:') or l:match('│() %S+%b()!:') or l:len()) --[[@as integer]]
56    hi_range(i, col, l:len(), 'DiagnosticWarn', priority + 1)
57  elseif l:match('^< ') then
58    -- Removed change
59    hi_range(i, 0, l:len(), 'Removed')
60  elseif l:match('^• ') then
61    -- Available newer tags
62    hi_range(i, 4, l:len(), 'DiagnosticHint')
63  end
64end
65
66--- Mappings
67--- @param lhs string
68--- @param search_flags string
69--- @param desc string
70local function map_section_jump(lhs, search_flags, desc)
71  vim.keymap.set({ 'n', 'x' }, lhs, function()
72    for _ = 1, vim.v.count1 do
73      vim.fn.search('^## ', search_flags)
74    end
75  end, { buf = 0, desc = desc })
76end
77
78map_section_jump('[[', 'bsW', 'Previous plugin')
79map_section_jump(']]', 'sW', 'Next plugin')