Skip to content

Commit 4df0772

Browse files
lagygoillbrammool
authored andcommitted
patch 9.0.1399: highlight test script has a few problems
Problem: Highlight test script has a few problems. Solution: Rewrite the script in Vim9 syntax. (closes #10379)
1 parent c0bdbfb commit 4df0772

File tree

3 files changed

+125
-143
lines changed

3 files changed

+125
-143
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
vim9script
2+
3+
# Maintainer: github user lacygoill
4+
# Last Change: 2023 Mar 08
5+
6+
# Init {{{1
7+
8+
const LINK: string = '->'
9+
10+
# Interface {{{1
11+
export 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)
58+
enddef
59+
# }}}1
60+
# Core {{{1
61+
def 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
75+
enddef
76+
# }}}1
77+
# Util {{{1
78+
def IsCleared(name: string): bool # {{{2
79+
return name
80+
->hlget()
81+
->get(0, {})
82+
->get('cleared')
83+
enddef
84+
85+
def 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
105+
enddef
106+
107+
def LinksTo(group: string): string # {{{2
108+
return group
109+
->hlget()
110+
->get(0, {})
111+
->get('linksto', '')
112+
enddef
113+
114+
def 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}'))
119+
enddef

runtime/syntax/hitest.vim

Lines changed: 4 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,10 @@
11
" Vim syntax file
22
" Language: none; used to see highlighting
3-
" Maintainer: Ronald Schild <[email protected]>
4-
" Last Change: 2019 Jun 06
5-
" Version: 5.4n.1
6-
" Additional Changes By: Lifepillar, Bram
3+
" Maintainer: github user lacygoill
4+
" Last Change: 2023 Mar 08
75

86
" To see your current highlight settings, do
97
" :so $VIMRUNTIME/syntax/hitest.vim
108

11-
" save global options and registers
12-
let s:hidden = &hidden
13-
let s:lazyredraw = &lazyredraw
14-
let s:more = &more
15-
let s:report = &report
16-
let s:whichwrap = &whichwrap
17-
let s:shortmess = &shortmess
18-
let s:wrapscan = &wrapscan
19-
let s:register_a = @a
20-
let s:register_se = @/
21-
22-
" set global options
23-
set hidden lazyredraw nomore report=99999 shortmess=aoOstTW wrapscan
24-
set whichwrap&
25-
26-
" print current highlight settings into register a
27-
redir @a
28-
silent highlight
29-
redir END
30-
31-
" Open a new window if the current one isn't empty
32-
if line("$") != 1 || getline(1) != ""
33-
new
34-
endif
35-
36-
" edit temporary file
37-
edit Highlight\ test
38-
39-
" set local options
40-
setlocal autoindent noexpandtab formatoptions=t shiftwidth=18 noswapfile tabstop=18
41-
let &textwidth=&columns
42-
43-
" insert highlight settings
44-
% delete
45-
put a
46-
47-
" remove cleared groups
48-
silent! g/ cleared$/d
49-
50-
" remove the colored xxx items
51-
g/xxx /s///e
52-
53-
" remove color settings (not needed here)
54-
global! /links to/ substitute /\s.*$//e
55-
56-
" Move split 'links to' lines to the same line
57-
% substitute /^\(\w\+\)\n\s*\(links to.*\)/\1\t\2/e
58-
59-
" move linked groups to the end of file
60-
global /links to/ move $
61-
62-
" move linked group names to the matching preferred groups
63-
" TODO: this fails if the group linked to isn't defined
64-
% substitute /^\(\w\+\)\s*\(links to\)\s*\(\w\+\)$/\3\t\2 \1/e
65-
silent! global /links to/ normal mz3ElD0#$p'zdd
66-
67-
" delete empty lines
68-
global /^ *$/ delete
69-
70-
" precede syntax command
71-
% substitute /^[^ ]*/syn keyword &\t&/
72-
73-
" execute syntax commands
74-
syntax clear
75-
% yank a
76-
@a
77-
78-
" remove syntax commands again
79-
% substitute /^syn keyword //
80-
81-
" pretty formatting
82-
global /^/ exe "normal Wi\<CR>\t\eAA\ex"
83-
global /^\S/ join
84-
85-
" find out first syntax highlighting
86-
let b:various = &highlight.',:Normal,:Cursor,:,'
87-
let b:i = 1
88-
while b:various =~ ':'.substitute(getline(b:i), '\s.*$', ',', '')
89-
let b:i = b:i + 1
90-
if b:i > line("$") | break | endif
91-
endwhile
92-
93-
" insert headlines
94-
call append(0, "Highlighting groups for various occasions")
95-
call append(1, "-----------------------------------------")
96-
97-
if b:i < line("$")-1
98-
let b:synhead = "Syntax highlighting groups"
99-
if exists("hitest_filetypes")
100-
redir @a
101-
let
102-
redir END
103-
let @a = substitute(@a, 'did_\(\w\+\)_syn\w*_inits\s*#1', ', \1', 'g')
104-
let @a = substitute(@a, "\n\\w[^\n]*", '', 'g')
105-
let @a = substitute(@a, "\n", '', 'g')
106-
let @a = substitute(@a, '^,', '', 'g')
107-
if @a != ""
108-
let b:synhead = b:synhead." - filetype"
109-
if @a =~ ','
110-
let b:synhead = b:synhead."s"
111-
endif
112-
let b:synhead = b:synhead.":".@a
113-
endif
114-
endif
115-
call append(b:i+1, "")
116-
call append(b:i+2, b:synhead)
117-
call append(b:i+3, substitute(b:synhead, '.', '-', 'g'))
118-
endif
119-
120-
" remove 'hls' highlighting
121-
nohlsearch
122-
normal 0
123-
124-
" we don't want to save this temporary file
125-
set nomodified
126-
127-
" the following trick avoids the "Press RETURN ..." prompt
128-
0 append
129-
.
130-
131-
" restore global options and registers
132-
let &hidden = s:hidden
133-
let &lazyredraw = s:lazyredraw
134-
let &more = s:more
135-
let &report = s:report
136-
let &shortmess = s:shortmess
137-
let &whichwrap = s:whichwrap
138-
let &wrapscan = s:wrapscan
139-
let @a = s:register_a
140-
141-
" restore last search pattern
142-
call histdel("search", -1)
143-
let @/ = s:register_se
144-
145-
" remove variables
146-
unlet s:hidden s:lazyredraw s:more s:report s:shortmess
147-
unlet s:whichwrap s:wrapscan s:register_a s:register_se
148-
149-
" vim: ts=8
9+
import 'dist/vimhighlight.vim'
10+
call vimhighlight.HighlightTest()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ static char *(features[]) =
695695

696696
static int included_patches[] =
697697
{ /* Add new patch number below this line */
698+
/**/
699+
1399,
698700
/**/
699701
1398,
700702
/**/

0 commit comments

Comments
 (0)