@@ -6,38 +6,47 @@ set cpo&vim
6
6
let s: NONE = []
7
7
lockvar ! s: NONE
8
8
9
- let s: TYPE = {}
10
- let s: TYPE .NUMBER = 0
11
- let s: TYPE .STRING = 1
12
- let s: TYPE .FUNC = 2
13
- let s: TYPE .LIST = 3
14
- let s: TYPE .DICT = 4
15
- let s: TYPE .FLOAT = 5
16
- let s: TYPE .BOOL = 6
17
- let s: TYPE .NONE = 7
18
- let s: TYPE .JOB = 8
19
- let s: TYPE .CHANNEL = 9
20
- let s: TYPE .ANY = range (s: TYPE .NUMBER , s: TYPE .CHANNEL)
21
- let s: TYPE .OPTARG = []
22
- lockvar ! s: TYPE
23
-
24
9
function ! s: _vital_loaded (V) abort
25
10
let s: T = a: V .import (' Vim.Type' )
26
- endfunction
27
11
28
- function ! s: _vital_created (module) abort
29
- let a: module .TYPE = s: TYPE
12
+ let s: t_end = -1
13
+ for t in has (' nvim' ) ? [
14
+ \ exists (' v:null' ) ? type (v: null ) : -1 ,
15
+ \ exists (' v:true' ) ? type (v: true ) : -1 ,
16
+ \ type (0.0 ),
17
+ \ type ({}),
18
+ \ type ([]),
19
+ \ type (function (' function' )),
20
+ \ type (' ' ),
21
+ \ type (0 ),
22
+ \] : [
23
+ \ get (v: , ' t_blob' , -1 ),
24
+ \ get (v: , ' t_channel' , -1 ),
25
+ \ get (v: , ' t_job' , -1 ),
26
+ \ get (v: , ' t_none' , -1 ),
27
+ \ get (v: , ' t_bool' , -1 ),
28
+ \ get (v: , ' t_float' , -1 ),
29
+ \ get (v: , ' t_dict' , -1 ),
30
+ \ get (v: , ' t_list' , -1 ),
31
+ \ get (v: , ' t_func' , -1 ),
32
+ \ get (v: , ' t_string' , -1 ),
33
+ \ get (v: , ' t_number' , -1 ),
34
+ \]
35
+ if t >= 0
36
+ let s: t_end = t
37
+ break
38
+ endif
39
+ endfor
30
40
endfunction
31
41
32
42
function ! s: _vital_depends () abort
33
43
return [' Vim.Type' ]
34
44
endfunction
35
45
36
-
37
46
function ! s: of (prefix, ... ) abort
38
- if type (a: prefix ) isnot s: TYPE . STRING
39
- throw ' vital: Validator.Args: of(): expected ' . s: T .type_names[ s: TYPE . STRING ] .
40
- \ ' argument but got ' . s: T .type_names[ type (a: prefix )]
47
+ if type (a: prefix ) isnot # v: t_string
48
+ throw ' vital: Validator.Args: of(): expected ' . s: _type_name ( v: t_string ) .
49
+ \ ' argument but got ' . s: _type_name ( type (a: prefix ))
41
50
endif
42
51
let validator = {
43
52
\ ' _prefix' : a: prefix ,
@@ -58,7 +67,7 @@ function! s:of(prefix, ...) abort
58
67
call s: _check_out_of_range (a: no , self ._types)
59
68
endif
60
69
let self ._asserts[a: no - 1 ] = {
61
- \ ' funclist' : type (a: funclist ) is s: TYPE .LIST ? a: funclist : [a: funclist ],
70
+ \ ' funclist' : type (a: funclist ) is # v: t_list ? a: funclist : [a: funclist ],
62
71
\ ' msg' : get (a: 000 , 0 , ' the ' . a: no . ' th argument'' s assertion was failed' )
63
72
\}
64
73
return self
@@ -67,9 +76,9 @@ function! s:of(prefix, ...) abort
67
76
if ! self ._enable
68
77
return a: args
69
78
endif
70
- if type (a: args ) isnot s: TYPE .LIST
71
- throw ' vital: Validator.Args: Validator.validate(): expected ' . s: T .type_names[ s: TYPE .LIST] .
72
- \ ' argument but got ' . s: T .type_names[ type (a: args )]
79
+ if type (a: args ) isnot # v: t_list
80
+ throw ' vital: Validator.Args: Validator.validate(): expected ' . s: _type_name ( v: t_list ) .
81
+ \ ' argument but got ' . s: _type_name ( type (a: args ))
73
82
endif
74
83
if has_key (self , ' _types' )
75
84
call s: _validate_arg_types (a: args , self ._types, self ._prefix)
@@ -85,25 +94,47 @@ endfunction
85
94
function ! s: _check_type_args (args ) abort
86
95
let optarg = 0
87
96
for i in range (len (a: args ))
88
- if a: args [i ] is s: TYPE .OPTARG
97
+ if a: args [i ] is # ' option '
89
98
let optarg += 1
90
99
if optarg > 1
91
- throw ' vital: Validator.Args: Validator.type(): multiple OPTARG were given'
100
+ throw ' vital: Validator.Args: Validator.type(): multiple optional arguments were given'
92
101
endif
93
102
endif
94
- if ! (type (a: args [i ]) is s: TYPE .NUMBER &&
95
- \ a: args [i ] >= s: TYPE .NUMBER &&
96
- \ a: args [i ] <= s: TYPE .CHANNEL) &&
97
- \ ! (type (a: args [i ]) is s: TYPE .LIST &&
98
- \ empty (filter (copy (a: args [i ]),
99
- \ ' type(v:val) isnot s:TYPE.NUMBER || ' .
100
- \ ' v:val < s:TYPE.NUMBER || v:val > s:TYPE.CHANNEL' )))
103
+ if ! s: _is_valid_type_arg (a: args [i ])
101
104
throw ' vital: Validator.Args: Validator.type(): expected type or union types ' .
102
- \ ' but got ' . s: T .type_names[ type (a: args [i ])]
105
+ \ ' but got ' . s: _type_name ( type (a: args [i ]))
103
106
endif
104
107
endfor
105
108
endfunction
106
109
110
+ function ! s: _is_valid_type_arg (arg) abort
111
+ let n = type (a: arg )
112
+ if n is # v: t_number && (v: t_number <=# a: arg && a: arg <=# s: t_end )
113
+ return 1
114
+ endif
115
+ if n is # v: t_string && (a: arg is # ' any' || a: arg is # ' option' )
116
+ return 1
117
+ endif
118
+ if n is # v: t_list && empty (filter (copy (a: arg ), ' !s:_is_valid_type_arg(v:val)' ))
119
+ return 1
120
+ endif
121
+ return 0
122
+ endfunction
123
+
124
+ function ! s: _type_name (type ) abort
125
+ if ! s: _is_valid_type_arg (a: type )
126
+ throw ' vital: Validator.Args: invalid type value: ' . string (a: type )
127
+ endif
128
+ let n = type (a: type )
129
+ if n is # v: t_number
130
+ return s: T .type_names[a: type ]
131
+ elseif n is # v: t_string
132
+ return a: type
133
+ else
134
+ return join (map (copy (a: type ), ' s:_type_name(v:val)' ), ' or ' )
135
+ endif
136
+ endfunction
137
+
107
138
function ! s: _check_assert_args (args ) abort
108
139
let no = a:args[0]
109
140
if no <= 0
@@ -113,7 +144,7 @@ function! s:_check_assert_args(args) abort
113
144
endfunction
114
145
115
146
function ! s: _check_out_of_range (no , types) abort
116
- let idx = index (a: types , s: TYPE .OPTARG )
147
+ let idx = index (a: types , ' option ' )
117
148
if a: no > len (a: types ) - (idx >= 0 && idx !=# len (a: types ) - 1 ? 1 : 0 )
118
149
if idx >= 0
119
150
let arity = idx . ' -' . (len (a: types ) - idx)
@@ -133,13 +164,13 @@ function! s:_validate_arg_types(args, types, prefix) abort
133
164
let i = 0
134
165
while i < argslen
135
166
if i + optarg >= typelen
136
- if optarg && a: types [-1 ] is s: TYPE .OPTARG
167
+ if optarg && a: types [-1 ] is # ' option '
137
168
break
138
169
else
139
170
throw a: prefix . ' : too many arguments'
140
171
endif
141
172
endif
142
- if a: types [i + optarg] is s: TYPE .OPTARG
173
+ if a: types [i + optarg] is # ' option '
143
174
let optarg += 1
144
175
continue
145
176
endif
@@ -148,33 +179,36 @@ function! s:_validate_arg_types(args, types, prefix) abort
148
179
\)
149
180
let i += 1
150
181
endwhile
151
- if ! optarg && i < typelen && a: types [i ] isnot s: TYPE .OPTARG
182
+ if ! optarg && i < typelen && a: types [i ] isnot # ' option '
152
183
throw a: prefix . ' : too few arguments'
153
184
endif
154
185
endfunction
155
186
156
187
function ! s: _validate_type (value, expected_type, prefix, ... ) abort
157
- if type (a: expected_type ) is s: TYPE .LIST
188
+ if a: expected_type is # ' any'
189
+ return a: value
190
+ endif
191
+ if type (a: expected_type ) is # v: t_list
158
192
let matched = filter (copy (a: expected_type ),
159
- \ ' s:_validate_type(a:value, v:val, a:prefix, s:NONE) isnot s:NONE' )
193
+ \ ' s:_validate_type(a:value, v:val, a:prefix, s:NONE) isnot# s:NONE' )
160
194
if empty (matched)
161
195
if a: 0
162
196
return a: 1
163
197
endif
164
- let expected = join ( map ( copy ( a: expected_type), ' s:T.type_names[v:val] ' ), ' or ' )
198
+ let expected = s: _type_name ( a: expected_type )
165
199
throw a: prefix . ' : invalid type arguments were given ' .
166
200
\ ' (expected: ' . expected .
167
- \ ' , got: ' . s: T .type_names[ type (a: value )] . ' )'
201
+ \ ' , got: ' . s: _type_name ( type (a: value )) . ' )'
168
202
endif
169
203
return
170
204
endif
171
- if type (a: value ) isnot a: expected_type
205
+ if type (a: value ) isnot # a: expected_type
172
206
if a: 0
173
207
return a: 1
174
208
endif
175
209
throw a: prefix . ' : invalid type arguments were given ' .
176
- \ ' (expected: ' . s: T .type_names[ a: expected_type] .
177
- \ ' , got: ' . s: T .type_names[ type (a: value )] . ' )'
210
+ \ ' (expected: ' . s: _type_name ( a: expected_type) .
211
+ \ ' , got: ' . s: _type_name ( type (a: value )) . ' )'
178
212
endif
179
213
return a: value
180
214
endfunction
@@ -196,7 +230,7 @@ function! s:_validate_assert(value, funclist, msg, prefix) abort
196
230
endfunction
197
231
198
232
function ! s: _call1 (f , arg) abort
199
- if type (a: f ) is s: TYPE .FUNC
233
+ if type (a: f ) is # v: t_func
200
234
return call (a: f , [a: arg ])
201
235
else
202
236
return map ([a: arg ], a: f )[0 ]
0 commit comments