1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
|
-------------------------------------------------------------------------------
-- This file is auto generated by vim9jit. Do not edit by hand.
-- All content is in the source repository.
-- Bugs should be reported to: github.com/tjdevries/vim9jit
--
-- In addition, this file is considered "private" by neovim. You should
-- not expect any of the APIs, functions, etc to be stable. They are subject
-- to change at any time.
-------------------------------------------------------------------------------
local vim9 = (function()
local M = {}
M.ternary = function(cond, if_true, if_false)
if cond then
if type(if_true) == 'function' then
return if_true()
else
return if_true
end
else
if type(if_false) == 'function' then
return if_false()
else
return if_false
end
end
end
M.fn_ref = function(module, name, copied, ...)
for _, val in ipairs({ ... }) do
table.insert(copied, val)
end
local funcref = name
if type(funcref) == 'function' then
return funcref(unpack(copied))
elseif type(funcref) == 'string' then
if vim.fn.exists('*' .. funcref) == 1 then
return vim.fn[funcref](unpack(copied))
end
if module[funcref] then
module[funcref](unpack(copied))
end
error('unknown function: ' .. funcref)
else
error(string.format('unable to call funcref: %s', funcref))
end
end
M.fn_mut = function(name, args, info)
local result = vim.fn._Vim9ScriptFn(name, args)
for idx, val in pairs(result[2]) do
M.replace(args[idx], val)
end
-- Substitute returning the reference to the
-- returned value
if info.replace then
return args[info.replace + 1]
end
return result[1]
end
M.replace = function(orig, new)
if type(orig) == 'table' and type(new) == 'table' then
for k in pairs(orig) do
orig[k] = nil
end
for k, v in pairs(new) do
orig[k] = v
end
return orig
end
return new
end
M.index = function(obj, idx)
if vim.tbl_islist(obj) then
if idx < 0 then
return obj[#obj + idx + 1]
else
return obj[idx + 1]
end
elseif type(obj) == 'table' then
return obj[idx]
elseif type(obj) == 'string' then
return string.sub(obj, idx + 1, idx + 1)
end
error('invalid type for indexing: ' .. vim.inspect(obj))
end
M.index_expr = function(idx)
if type(idx) == 'string' then
return idx
elseif type(idx) == 'number' then
return idx + 1
else
error(string.format('not yet handled: %s', vim.inspect(idx)))
end
end
M.slice = function(obj, start, finish)
if start == nil then
start = 0
end
if start < 0 then
start = #obj + start
end
assert(type(start) == 'number')
if finish == nil then
finish = #obj
end
if finish < 0 then
finish = #obj + finish
end
assert(type(finish) == 'number')
local slicer
if vim.tbl_islist(obj) then
slicer = vim.list_slice
elseif type(obj) == 'string' then
slicer = string.sub
else
error('invalid type for slicing: ' .. vim.inspect(obj))
end
return slicer(obj, start + 1, finish + 1)
end
-- Currently unused, but this could be used to embed vim9jit within a
-- running nvim application and transpile "on the fly" as files are
-- sourced. There would still need to be some work done to make that
-- work correctly with imports and what not, but overall it could
-- work well for calling ":source X" from within a vimscript/vim9script
-- function
M.make_source_cmd = function()
local group = vim.api.nvim_create_augroup('vim9script-source', {})
vim.api.nvim_create_autocmd('SourceCmd', {
pattern = '*.vim',
group = group,
callback = function(a)
local file = vim.fn.readfile(a.file)
for _, line in ipairs(file) do
-- TODO: Or starts with def <something>
-- You can use def in legacy vim files
if vim.startswith(line, 'vim9script') then
-- TODO: Use the rust lib to actually
-- generate the corresponding lua code and then
-- execute that (instead of sourcing it directly)
return
end
end
vim.api.nvim_exec2(table.concat(file, '\n'), { output = false })
end,
})
end
M.iter = function(expr)
if vim.tbl_islist(expr) then
return ipairs(expr)
else
return pairs(expr)
end
end
M.ITER_DEFAULT = 0
M.ITER_CONTINUE = 1
M.ITER_BREAK = 2
M.ITER_RETURN = 3
return M
end)()
vim.cmd([[
function! _Vim9ScriptFn(name, args) abort
try
let ret = function(a:name, a:args)()
catch
echo "Failed..."
echo a:name
echo a:args
throw v:errmsg
endtry
return [ret, a:args]
endfunction
]])
vim9['autoload'] = (function()
return function(path)
return loadfile(path)()
end
end)()
vim9['bool'] = (function()
return function(...)
return vim9.convert.to_vim_bool(...)
end
end)()
vim9['convert'] = (function()
local M = {}
M.decl_bool = function(val)
if type(val) == 'boolean' then
return val
elseif type(val) == 'number' then
if val == 0 then
return false
elseif val == 1 then
return true
else
error(string.format('bad number passed to bool declaration: %s', val))
end
end
error(string.format('invalid bool declaration: %s', vim.inspect(val)))
end
M.decl_dict = function(val)
if type(val) == 'nil' then
return vim.empty_dict()
elseif type(val) == 'table' then
if vim.tbl_isempty(val) then
return vim.empty_dict()
elseif vim.tbl_islist(val) then
error(string.format('Cannot pass list to dictionary? %s', vim.inspect(val)))
else
return val
end
end
error(string.format('invalid dict declaration: %s', vim.inspect(val)))
end
M.to_vim_bool = function(val)
if type(val) == 'boolean' then
return val
elseif type(val) == 'number' then
return val ~= 0
elseif type(val) == 'string' then
return string.len(val) ~= 0
elseif type(val) == 'table' then
return not vim.tbl_isempty(val)
elseif val == nil then
return false
end
error('unhandled type: ' .. vim.inspect(val))
end
return M
end)()
vim9['fn'] = (function()
local M = {}
M.insert = function(list, item, idx)
if idx == nil then
idx = 1
end
table.insert(list, idx + 1, item)
return list
end
M.extend = function(left, right, expr3)
if expr3 ~= nil then
error("haven't written this code yet")
end
if vim.tbl_islist(right) then
vim.list_extend(left, right)
return left
else
-- local result = vim.tbl_extend(left, right)
for k, v in pairs(right) do
left[k] = v
end
return left
end
end
M.add = function(list, item)
table.insert(list, item)
return list
end
M.has_key = function(obj, key)
return not not obj[key]
end
M.prop_type_add = function(...)
local args = { ... }
print('[prop_type_add]', vim.inspect(args))
end
do
local has_overrides = {
-- We do have vim9script ;) that's this plugin
['vim9script'] = true,
-- Include some vim patches that are sometimes required by various vim9script plugins
-- that we implement via vim9jit
[ [[patch-8.2.2261]] ] = true,
[ [[patch-8.2.4257]] ] = true,
}
M.has = function(patch)
if has_overrides[patch] then
return true
end
return vim.fn.has(patch)
end
end
--[=[
Currently missing patch, can be removed in the future.
readdirex({directory} [, {expr} [, {dict}]]) *readdirex()*
Extended version of |readdir()|.
Return a list of Dictionaries with file and directory
information in {directory}.
This is useful if you want to get the attributes of file and
directory at the same time as getting a list of a directory.
This is much faster than calling |readdir()| then calling
|getfperm()|, |getfsize()|, |getftime()| and |getftype()| for
each file and directory especially on MS-Windows.
The list will by default be sorted by name (case sensitive),
the sorting can be changed by using the optional {dict}
argument, see |readdir()|.
The Dictionary for file and directory information has the
following items:
group Group name of the entry. (Only on Unix)
name Name of the entry.
perm Permissions of the entry. See |getfperm()|.
size Size of the entry. See |getfsize()|.
time Timestamp of the entry. See |getftime()|.
type Type of the entry.
On Unix, almost same as |getftype()| except:
Symlink to a dir "linkd"
Other symlink "link"
On MS-Windows:
Normal file "file"
Directory "dir"
Junction "junction"
Symlink to a dir "linkd"
Other symlink "link"
Other reparse point "reparse"
user User name of the entry's owner. (Only on Unix)
On Unix, if the entry is a symlink, the Dictionary includes
the information of the target (except the "type" item).
On MS-Windows, it includes the information of the symlink
itself because of performance reasons.
--]=]
M.readdirex = function(dir)
local files = vim.fn.readdir(dir)
local direx = {}
for _, f in ipairs(files) do
table.insert(direx, {
name = f,
type = vim.fn.getftype(f),
})
end
return direx
end
M.mapnew = function(tbl, expr)
return vim.fn.map(tbl, expr)
end
M.typename = function(val)
local ty = type(val)
if ty == 'string' then
return 'string'
elseif ty == 'boolean' then
return 'bool'
elseif ty == 'number' then
return 'number'
else
error(string.format('typename: %s', val))
end
end
-- Popup menu stuff: Could be rolled into other plugin later
-- but currently is here for testing purposes (and implements
-- some very simple compat layers at the moment)
do
local pos_map = {
topleft = 'NW',
topright = 'NE',
botleft = 'SW',
botright = 'SE',
}
M.popup_menu = function(_, options)
-- print "OPTIONS:"
local buf = vim.api.nvim_create_buf(false, true)
local win = vim.api.nvim_open_win(buf, true, {
relative = 'editor',
style = 'minimal',
anchor = pos_map[options.pos],
height = options.maxheight or options.minheight,
width = options.maxwidth or options.minwidth,
row = options.line,
col = options.col,
})
if options.filter then
local loop
loop = function()
vim.cmd([[redraw!]])
local ok, ch = pcall(vim.fn.getcharstr)
if not ok then
return
end -- interrupted
if ch == '<C-C>' then
return
end
if not require('vim9script').bool(options.filter(nil, ch)) then
vim.cmd.normal(ch)
end
vim.schedule(loop)
end
vim.schedule(loop)
end
return win
end
M.popup_settext = function(id, text)
if type(text) == 'string' then
-- text = vim.split(text, "\n")
error("Haven't handled string yet")
end
local lines = {}
for _, obj in ipairs(text) do
table.insert(lines, obj.text)
end
vim.api.nvim_buf_set_lines(vim.api.nvim_win_get_buf(id), 0, -1, false, lines)
end
M.popup_filter_menu = function()
print('ok, just pretend we filtered the menu')
end
M.popup_setoptions = function(id, _)
print('setting options...', id)
end
end
M = setmetatable(M, {
__index = vim.fn,
})
return M
end)()
vim9['heredoc'] = (function()
local M = {}
M.trim = function(lines)
local min_whitespace = 9999
for _, line in ipairs(lines) do
local _, finish = string.find(line, '^%s*')
min_whitespace = math.min(min_whitespace, finish)
end
local trimmed_lines = {}
for _, line in ipairs(lines) do
table.insert(trimmed_lines, string.sub(line, min_whitespace + 1))
end
return trimmed_lines
end
return M
end)()
vim9['import'] = (function()
local imported = {}
imported.autoload = setmetatable({}, {
__index = function(_, name)
local luaname = 'autoload/' .. string.gsub(name, '%.vim$', '.lua')
local runtime_file = vim.api.nvim_get_runtime_file(luaname, false)[1]
if not runtime_file then
error('unable to find autoload file:' .. name)
end
return imported.absolute[vim.fn.fnamemodify(runtime_file, ':p')]
end,
})
imported.absolute = setmetatable({}, {
__index = function(self, name)
if vim.uv.fs_stat(name) then
local result = loadfile(name)()
rawset(self, name, result)
return result
end
error(string.format('unabled to find absolute file: %s', name))
end,
})
return function(info)
local name = info.name
if info.autoload then
return imported.autoload[info.name]
end
local debug_info = debug.getinfo(2, 'S')
local sourcing_path = vim.fn.fnamemodify(string.sub(debug_info.source, 2), ':p')
-- Relative paths
if vim.startswith(name, '../') or vim.startswith(name, './') then
local luaname = string.gsub(name, '%.vim$', '.lua')
local directory = vim.fn.fnamemodify(sourcing_path, ':h')
local search = directory .. '/' .. luaname
return imported.absolute[search]
end
if vim.startswith(name, '/') then
error('absolute path')
-- local luaname = string.gsub(name, "%.vim", ".lua")
-- local runtime_file = vim.api.nvim_get_runtime_file(luaname, false)[1]
-- if runtime_file then
-- runtime_file = vim.fn.fnamemodify(runtime_file, ":p")
-- return loadfile(runtime_file)()
-- end
end
error('Unhandled case' .. vim.inspect(info) .. vim.inspect(debug_info))
end
end)()
vim9['ops'] = (function()
local lib = vim9
local M = {}
M['And'] = function(left, right)
return lib.bool(left) and lib.bool(right)
end
M['Or'] = function(left, right)
return lib.bool(left) or lib.bool(right)
end
M['Plus'] = function(left, right)
return left + right
end
M['Multiply'] = function(left, right)
return left * right
end
M['Divide'] = function(left, right)
return left / right
end
M['StringConcat'] = function(left, right)
return left .. right
end
M['EqualTo'] = function(left, right)
return left == right
end
M['NotEqualTo'] = function(left, right)
return not M['EqualTo'](left, right)
end
M['LessThan'] = function(left, right)
return left < right
end
M['LessThanOrEqual'] = function(left, right)
return left <= right
end
M['GreaterThan'] = function(left, right)
return left > right
end
M['GreaterThanOrEqual'] = function(left, right)
return left >= right
end
M['RegexpMatches'] = function(left, right)
return not not vim.regex(right):match_str(left)
end
M['RegexpMatchesIns'] = function(left, right)
return not not vim.regex('\\c' .. right):match_str(left)
end
M['NotRegexpMatches'] = function(left, right)
return not M['RegexpMatches'](left, right)
end
M['Modulo'] = function(left, right)
return left % right
end
M['Minus'] = function(left, right)
-- TODO: This is not right :)
return left - right
end
return M
end)()
vim9['prefix'] = (function()
local lib = vim9
local M = {}
M['Minus'] = function(right)
return -right
end
M['Bang'] = function(right)
return not lib.bool(right)
end
return M
end)()
return vim9
|