blob: 4664961af4258f91161c9dee770240e361132580 [file] [log] [blame]
lagygoill4df07722023-03-10 18:37:11 +00001vim9script
2
3# Maintainer: github user lacygoill
4# Last Change: 2023 Mar 08
5
6# Init {{{1
7
8const LINK: string = '->'
9
10# Interface {{{1
11export def HighlightTest() # {{{2
12 # Open a new window if the current one isn't empty
13 if line('$') != 1 || getline(1) != ''
14 new
15 endif
16
17 edit Highlight\ test
18
19 # `:help scratch-buffer`
20 &l:bufhidden = 'hide'
21 &l:buftype = 'nofile'
22 &l:swapfile = false
23
24 var report: list<string> =<< trim END
25 Highlighting groups for various occasions
26 -----------------------------------------
27 END
28
29 var various_groups: list<string> = GetVariousGroups()
30 ->filter((_, group: string): bool => group->hlexists() && !group->IsCleared())
31 ->sort()
32 ->uniq()
33
34 report->extend(various_groups->FollowChains())
35
36 var language_section: list<string> =<< trim END
37
38 Highlighting groups for language syntaxes
39 -----------------------------------------
40 END
41 report->extend(language_section)
42
43 var syntax_groups: list<string> = getcompletion('', 'highlight')
44 ->filter((_, group: string): bool =>
45 various_groups->index(group) == -1
46 && !group->IsCleared()
47 && group !~ '^HighlightTest')
48
49 # put the report
50 report
51 ->extend(syntax_groups->FollowChains())
52 ->setline(1)
53
54 # highlight the group names
55 execute $'silent! global /^\w\+\%(\%(\s*{LINK}\s*\)\w\+\)*$/ Highlight({bufnr('%')})'
56
57 cursor(1, 1)
58enddef
59# }}}1
60# Core {{{1
61def Highlight(buf: number) # {{{2
62 var lnum: number = line('.')
63 for group: string in getline('.')->split($'\s*{LINK}\s*')
64 silent! prop_type_add($'highlight-test-{group}', {
65 bufnr: buf,
66 highlight: group,
67 combine: false,
68 })
69 prop_add(lnum, col('.'), {
70 length: group->strlen(),
71 type: $'highlight-test-{group}'
72 })
73 search('\<\w\+\>', '', lnum)
74 endfor
75enddef
76# }}}1
77# Util {{{1
78def IsCleared(name: string): bool # {{{2
79 return name
80 ->hlget()
81 ->get(0, {})
82 ->get('cleared')
83enddef
84
85def FollowChains(groups: list<string>): list<string> # {{{2
86 # A group might be linked to another, which itself might be linked...
87 # We want the whole chain, for every group.
88 var chains: list<string>
89 for group: string in groups
90 var target: string = group->LinksTo()
91 var chain: string = group
92 while !target->empty()
93 chain ..= $' {LINK} {target}'
94 target = target->LinksTo()
95 endwhile
96 var a_link_is_cleared: bool = chain
97 ->split($'\s*{LINK}\s*')
98 ->indexof((_, g: string): bool => g->IsCleared()) >= 0
99 if a_link_is_cleared
100 continue
101 endif
102 chains->add(chain)
103 endfor
104 return chains
105enddef
106
107def LinksTo(group: string): string # {{{2
108 return group
109 ->hlget()
110 ->get(0, {})
111 ->get('linksto', '')
112enddef
113
114def GetVariousGroups(): list<string> # {{{2
115 return getcompletion('hl-', 'help')
116 ->filter((_, helptag: string): bool => helptag =~ '^hl-\w\+$')
117 ->map((_, helptag: string) => helptag->substitute('^hl-', '', ''))
118 ->extend(range(1, 9)->map((_, n: number) => $'User{n}'))
119enddef