-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathinit.lua
696 lines (590 loc) · 16.8 KB
/
init.lua
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
local log = require("leetcode.logger")
local arguments = require("leetcode.command.arguments")
local config = require("leetcode.config")
local event = require("nui.utils.autocmd").event
local api = vim.api
local t = require("leetcode.translator")
---@class lc.Commands
local cmd = {}
---@param old_name string
---@param new_name string
function cmd.deprecate(old_name, new_name)
log.warn(("`%s` is deprecated, use `%s` instead."):format(old_name, new_name))
end
function cmd.cache_update()
require("leetcode.utils").auth_guard()
require("leetcode.cache").update()
end
---@param options table<string, string[]>
function cmd.problems(options)
require("leetcode.utils").auth_guard()
local p = require("leetcode.cache.problemlist").get()
require("leetcode.pickers.question").pick(p, options)
end
---@param cb? function
function cmd.cookie_prompt(cb)
local cookie = require("leetcode.cache.cookie")
local popup_options = {
relative = "editor",
position = {
row = "50%",
col = "50%",
},
size = 100,
border = {
style = "rounded",
text = {
top = (" %s "):format(t("Enter cookie")),
top_align = "left",
},
},
win_options = {
winhighlight = "Normal:Normal",
},
}
local NuiInput = require("nui.input")
local input = NuiInput(popup_options, {
prompt = " ",
on_submit = function(value)
local err = cookie.set(value)
if not err then
log.info("Sign-in successful")
cmd.start_user_session()
else
log.error("Sign-in failed: " .. err)
end
pcall(cb, not err and true or false)
end,
})
input:mount()
local keys = config.user.keys
input:map("n", keys.toggle, function()
input:unmount()
end)
input:on(event.BufLeave, function()
input:unmount()
end)
end
function cmd.sign_out()
cmd.menu()
log.warn("You're now signed out")
cmd.delete_cookie()
cmd.set_menu_page("signin")
end
---Sign out
function cmd.delete_cookie()
config.auth = {}
local cookie = require("leetcode.cache.cookie")
cookie.delete()
end
cmd.q_close_all = function()
local utils = require("leetcode.utils")
local qs = utils.question_tabs()
for _, tabp in ipairs(qs) do
tabp.question:unmount()
end
end
function cmd.exit()
local leetcode = require("leetcode")
leetcode.stop()
end
cmd.expire = vim.schedule_wrap(function()
local tabp = api.nvim_get_current_tabpage()
cmd.menu()
cmd.cookie_prompt(function(success)
if success then
if api.nvim_tabpage_is_valid(tabp) then
api.nvim_set_current_tabpage(tabp)
end
log.info("Successful re-login")
else
cmd.sign_out()
end
end)
end)
function cmd.qot()
require("leetcode.utils").auth_guard()
local problems = require("leetcode.api.problems")
local Question = require("leetcode-ui.question")
problems.question_of_today(function(qot, err)
if err then
return log.err(err)
end
local problemlist = require("leetcode.cache.problemlist")
Question(problemlist.get_by_title_slug(qot.title_slug)):mount()
end)
end
function cmd.random_question(opts)
require("leetcode.utils").auth_guard()
local problems = require("leetcode.cache.problemlist")
local question = require("leetcode.api.question")
if opts and opts.difficulty then
opts.difficulty = opts.difficulty[1]:upper()
end
if opts and opts.status then
opts.status = ({
ac = "AC",
notac = "TRIED",
todo = "NOT_STARTED",
})[opts.status[1]]
end
local q, err = question.random(opts)
if err then
return log.err(err)
end
local item = problems.get_by_title_slug(q.title_slug) or {}
local Question = require("leetcode-ui.question")
Question(item):mount()
end
function cmd.start_with_cmd()
local leetcode = require("leetcode")
if leetcode.start(false) then
cmd.menu()
end
end
function cmd.menu()
local winid, bufnr = _Lc_state.menu.winid, _Lc_state.menu.bufnr
local ok, tabp = pcall(api.nvim_win_get_tabpage, winid)
local ui = require("leetcode-ui.utils")
if ok then
api.nvim_set_current_tabpage(tabp)
ui.win_set_buf(winid, bufnr)
else
_Lc_state.menu:remount()
end
end
function cmd.yank()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if not q then
return
end
if
(q.bufnr and api.nvim_buf_is_valid(q.bufnr))
and (q.winid and api.nvim_win_is_valid(q.winid))
then
api.nvim_set_current_win(q.winid)
utils.with_version("0.10.0", nil, function()
api.nvim_set_current_buf(q.bufnr)
end)
local start_i, end_i, lines = q:range()
vim.cmd(("%d,%dyank"):format(start_i or 1, end_i or #lines))
end
end
---@param page lc-menu.page
function cmd.set_menu_page(page)
_Lc_state.menu:set_page(page)
end
function cmd.start_user_session() --
cmd.set_menu_page("menu")
config.stats.update()
end
function cmd.question_tabs()
require("leetcode.pickers.question-tabs").pick()
end
function cmd.change_lang()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if q then
require("leetcode.pickers.language").pick(q)
end
end
function cmd.desc_toggle()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if q then
q.description:toggle()
end
end
function cmd.desc_toggle_stats()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if q then
q.description:toggle_stats()
end
end
function cmd.console()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if q then
q.console:toggle()
end
end
function cmd.info()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if q then
q.info:toggle()
end
end
function cmd.hints()
-- cmd.deprecate("Leet hints", "Leet info")
cmd.info()
end
function cmd.q_run()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if q then
q.console:run()
end
end
function cmd.q_submit()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if q then
q.console:run(true)
end
end
function cmd.ui_skills()
if config.is_cn then
return
end
local skills = require("leetcode-ui.popup.skills")
skills:show()
end
function cmd.ui_languages()
local languages = require("leetcode-ui.popup.languages")
languages:show()
end
function cmd.open()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if q then
if vim.ui.open then
vim.ui.open(q.cache.link)
else
local command
local os_name = vim.loop.os_uname().sysname
if os_name == "Linux" then
command = string.format("xdg-open '%s'", q.cache.link)
elseif os_name == "Darwin" then
command = string.format("open '%s'", q.cache.link)
else
-- Fallback to Windows if uname is not available or does not match Linux/Darwin.
command = string.format("start \"\" \"%s\"", q.cache.link)
end
vim.fn.jobstart(command, { detach = true })
end
end
end
function cmd.reset()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if not q then
return
end
q:set_lines()
end
function cmd.last_submit()
local utils = require("leetcode.utils")
utils.auth_guard()
local q = utils.curr_question()
if not q then
return
end
local question_api = require("leetcode.api.question")
question_api.latest_submission(q.q.id, q.lang, function(res, err) --
if err then
if err.status == 404 then
log.error("You haven't submitted any code!")
else
log.err(err)
end
return
end
if type(res) == "table" and res.code then
q:set_lines(res.code)
else
log.error("Something went wrong")
end
end)
end
function cmd.restore()
local utils = require("leetcode.utils")
local ui = require("leetcode-ui.utils")
local q = utils.curr_question()
if not q then
return
end
if
(q.winid and api.nvim_win_is_valid(q.winid))
and (q.bufnr and api.nvim_buf_is_valid(q.bufnr))
then
ui.win_set_buf(q.winid, q.bufnr)
end
q.description:show()
local winid, bufnr = q.description.winid, q.description.bufnr
if
(winid and api.nvim_win_is_valid(winid)) --
and (bufnr and api.nvim_buf_is_valid(bufnr))
then
ui.win_set_buf(q.winid, q.bufnr)
end
end
function cmd.inject()
local utils = require("leetcode.utils")
local q = utils.curr_question()
if not q then
return
end
if q.bufnr and api.nvim_buf_is_valid(q.bufnr) then
local start_i, end_i = q:range(true)
local not_found = {}
if not start_i then
table.insert(not_found, "`@leet start`")
else
local before = q:inject(true)
if before then
api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n"))
end
end
if not end_i then
table.insert(not_found, "`@leet end`")
else
local after = q:inject(false)
if after then
api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n"))
end
end
if not vim.tbl_isempty(not_found) then
log.error(table.concat(not_found, " and ") .. " not found")
end
end
end
function cmd.get_active_session()
local sessions = config.sessions.all
return vim.tbl_filter(function(s)
return s.is_active
end, sessions)[1]
end
function cmd.get_session_by_name(name)
local sessions = config.sessions.all
name = name:lower()
if name == config.sessions.default then
name = ""
end
return vim.tbl_filter(function(s)
return s.name:lower() == name
end, sessions)[1]
end
function cmd.change_session(opts)
require("leetcode.utils").auth_guard()
local name = opts.name[1] or config.sessions.default
local session = cmd.get_session_by_name(name)
if not session then
return log.error("Session not found")
end
local stats_api = require("leetcode.api.statistics")
stats_api.change_session(session.id, function(_, err)
if err then
return log.err(err)
end
log.info(("Session changed to `%s`"):format(name))
config.stats.update()
end)
end
function cmd.create_session(opts)
require("leetcode.utils").auth_guard()
local name = opts.name[1]
if not name then
return log.error("Session name not provided")
end
local stats_api = require("leetcode.api.statistics")
stats_api.create_session(name, function(_, err)
if err then
return log.err(err)
end
log.info(("session `%s` created"):format(name))
end)
end
function cmd.update_sessions()
require("leetcode.utils").auth_guard()
config.stats.update_sessions()
end
function cmd.fix()
require("leetcode.cache.cookie").delete()
require("leetcode.cache.problemlist").delete()
vim.cmd("qa!")
end
function cmd.custom_list(options)
require("leetcode.utils").auth_guard()
if not options.file or #options.file == 0 then
return log.error("No file specified for custom list")
end
local filepath = options.file[1]
local filter_options = {}
-- Apply additional filters if specified
if options.filter then
for _, filter_type in ipairs(options.filter) do
if options[filter_type] then
filter_options[filter_type] = options[filter_type][1]
end
end
end
local problems = require("leetcode.cache.problemlist").filter_by_file(filepath, filter_options)
require("leetcode.pickers.question").pick(problems)
end
---@return string[], string[]
function cmd.parse(args)
local parts = vim.split(vim.trim(args), "%s+")
if args:sub(-1) == " " then
parts[#parts + 1] = ""
end
local options = {}
for _, part in ipairs(parts) do
local opt = part:match("(.-)=.-")
if opt then
table.insert(options, opt)
end
end
return parts, options
end
---@param tbl table
local function cmds_keys(tbl)
return vim.tbl_filter(function(key)
if type(key) ~= "string" then
return false
end
if key:sub(1, 1) == "_" then
return false
end
if tbl[key]._private then
return false
end
return true
end, vim.tbl_keys(tbl))
end
---@param _ string
---@param line string
---
---@return string[]
function cmd.complete(_, line)
local args, options = cmd.parse(line:gsub("Leet%s", ""))
return cmd.rec_complete(args, options, cmd.commands)
end
---@param args string[]
---@param options string[]
---@param cmds table<string,any>
---
---@return string[]
function cmd.rec_complete(args, options, cmds)
if not cmds or vim.tbl_isempty(args) then
return {}
end
if not cmds._args and cmds[args[1]] then
return cmd.rec_complete(args, options, cmds[table.remove(args, 1)])
end
local txt, keys = args[#args], cmds_keys(cmds)
if cmds._args then
local option_keys = cmds_keys(cmds._args)
option_keys = vim.tbl_filter(function(key)
return not vim.tbl_contains(options, key)
end, option_keys)
option_keys = vim.tbl_map(function(key)
return ("%s="):format(key)
end, option_keys)
keys = vim.tbl_extend("force", keys, option_keys)
local s = vim.split(txt, "=")
if s[2] and cmds._args[s[1]] then
local vals = vim.split(s[2], ",")
return vim.tbl_filter(function(key)
return not vim.tbl_contains(vals, key) and key:find(vals[#vals], 1, true) == 1
end, cmds._args[s[1]])
end
end
return vim.tbl_filter(function(key)
return not vim.tbl_contains(args, key) and key:find(txt, 1, true) == 1
end, keys)
end
function cmd.exec(args)
local cmds = cmd.commands
local options = vim.empty_dict()
for s in vim.gsplit(args.args:lower(), "%s+", { trimempty = true }) do
local opt = vim.split(s, "=")
if opt[2] then
options[opt[1]] = vim.split(opt[2], ",", { trimempty = true })
elseif cmds then
cmds = cmds[s]
else
break
end
end
if cmds and type(cmds[1]) == "function" then
cmds[1](options) ---@diagnostic disable-line
else
log.error(("Invalid command: `%s %s`"):format(args.name, args.args))
end
end
function cmd.setup()
api.nvim_create_user_command("Leet", cmd.exec, {
bar = true,
bang = true,
nargs = "?",
desc = "Leet",
complete = cmd.complete,
})
end
cmd.commands = {
cmd.menu,
menu = { cmd.menu },
exit = { cmd.exit },
console = { cmd.console },
info = { cmd.info },
hints = { cmd.hints },
tabs = { cmd.question_tabs },
lang = { cmd.change_lang },
run = { cmd.q_run },
test = { cmd.q_run },
submit = { cmd.q_submit },
daily = { cmd.qot },
yank = { cmd.yank },
open = { cmd.open },
reset = { cmd.reset },
last_submit = { cmd.last_submit },
restore = { cmd.restore },
inject = { cmd.inject },
-- session = {
-- change = {
-- cmd.change_session,
-- _args = arguments.session_change,
-- },
-- create = {
-- cmd.create_session,
-- _args = arguments.session_create,
-- },
-- update = { cmd.update_sessions },
-- },
list = {
cmd.problems,
_args = arguments.list,
},
random = {
cmd.random_question,
_args = arguments.random,
},
desc = {
cmd.desc_toggle,
stats = { cmd.desc_toggle_stats },
toggle = { cmd.desc_toggle },
},
cookie = {
update = { cmd.cookie_prompt },
delete = { cmd.sign_out },
},
cache = {
update = { cmd.cache_update },
},
fix = {
cmd.fix,
_private = true,
},
custom = {
cmd.custom_list,
_args = arguments.list_custom,
},
}
return cmd