Skip to content

Commit 727bc24

Browse files
committed
simplify and slightly optimise create_syn_match
1 parent 741dd18 commit 727bc24

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

autoload/css_color.vim

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,21 +138,17 @@ function! s:create_syn_match()
138138
let rgb_color = get( s:pattern_color, pattern, '' )
139139

140140
if ! strlen( rgb_color )
141-
let hexcolor = submatch(1)
141+
let hex = submatch(1)
142142
let funcname = submatch(2)
143143

144-
if funcname == 'rgb'
145-
let rgb_color = s:rgb2color(submatch(3),submatch(4),submatch(5))
146-
elseif funcname == 'hsl'
147-
let rgb_color = s:hsl2color(submatch(3),submatch(4),submatch(5))
148-
elseif strlen(hexcolor) == 6
149-
let rgb_color = tolower(hexcolor)
150-
elseif strlen(hexcolor) == 3
151-
let rgb_color = substitute(tolower(hexcolor), '\(.\)', '\1\1', 'g')
152-
else
153-
throw 'css_color: create_syn_match invoked on bad match data'
154-
endif
144+
let rgb_color
145+
\ = funcname == 'rgb' ? s:rgb2color(submatch(3),submatch(4),submatch(5))
146+
\ : funcname == 'hsl' ? s:hsl2color(submatch(3),submatch(4),submatch(5))
147+
\ : strlen(hex) == 6 ? tolower(hex)
148+
\ : strlen(hex) == 3 ? tolower(hex[0].hex[0].hex[1].hex[1].hex[2].hex[2])
149+
\ : ''
155150

151+
if rgb_color == '' | throw 'css_color: create_syn_match invoked on bad match data' | endif
156152
let s:pattern_color[pattern] = rgb_color
157153
endif
158154

@@ -202,7 +198,7 @@ function! s:create_matches()
202198
endfor
203199
endfunction
204200

205-
let s:_hexcolor = '#\(\x\{3}\|\x\{6}\)\>' " submatch 1
201+
let s:_hexcolor = '#\(\x\{3}\%(\>\|\x\{3}\>\)\)' " submatch 1
206202
let s:_funcname = '\(rgb\|hsl\)a\?' " submatch 2
207203
let s:_ws_ = '\s*'
208204
let s:_numval = s:_ws_ . '\(\d\{1,3}%\?\)' " submatch 3,4,5

0 commit comments

Comments
 (0)