irongit

A Neovim plugin enabling you to solve LeetCode problems.

leetcode.nvim/README.md
500 lines10 KBMarkdown
1<div align="center">
2
3# leetcode.nvim
4
5๐Ÿ”ฅ Solve [LeetCode] problems within [Neovim] ๐Ÿ”ฅ
6
7<!-- ๐Ÿ‡บ๐Ÿ‡ธ English, ๐Ÿ‡จ๐Ÿ‡ณ <a href="README.zh.md">็ฎ€ไฝ“ไธญๆ–‡</a> -->
8
9</div>
10
11https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-123cb32b7563
12
13## โœจ Features
14
15- ๐Ÿ“Œ an intuitive dashboard for effortless navigation within [leetcode.nvim]
16
17- ๐Ÿ˜ question description formatting for a better readability
18
19- ๐Ÿ“ˆ [LeetCode] profile statistics within [Neovim]
20
21- ๐Ÿ”€ support for daily and random questions
22
23- ๐Ÿ’พ caching for optimized performance
24
25## ๐Ÿ“ฌ Requirements
26
27- [Neovim] >= 0.9.0
28
29- [telescope.nvim]
30
31- [nui.nvim]
32
33- [tree-sitter-html] _**(optional, but highly recommended)**_
34 used for formatting the question description.
35 Can be installed with [nvim-treesitter].
36
37- [nvim-notify] _**(optional)**_
38
39- [Nerd Font][nerd-font] & [nvim-web-devicons] _**(optional)**_
40
41## ๐Ÿ“ฆ Installation
42
43- [lazy.nvim]
44
45```lua
46{
47 "kawre/leetcode.nvim",
48 build = ":TSUpdate html",
49 dependencies = {
50 "nvim-telescope/telescope.nvim",
51 "nvim-lua/plenary.nvim", -- required by telescope
52 "MunifTanjim/nui.nvim",
53
54 -- optional
55 "nvim-treesitter/nvim-treesitter",
56 "rcarriga/nvim-notify",
57 "nvim-tree/nvim-web-devicons",
58 },
59 opts = {
60 -- configuration goes here
61 },
62}
63```
64
65## ๐Ÿ› ๏ธ Configuration
66
67To see full configuration types see [template.lua]./lua/leetcode/config/template.lua
68
69### โš™๏ธ default configuration
70
71```lua
72{
73 ---@type string
74 arg = "leetcode.nvim",
75
76 ---@type lc.lang
77 lang = "cpp",
78
79 cn = { -- leetcode.cn
80 enabled = false, ---@type boolean
81 translator = true, ---@type boolean
82 translate_problems = true, ---@type boolean
83 },
84
85 ---@type lc.storage
86 storage = {
87 home = vim.fn.stdpath("data") .. "/leetcode",
88 cache = vim.fn.stdpath("cache") .. "/leetcode",
89 },
90
91 ---@type table<string, boolean>
92 plugins = {
93 non_standalone = false,
94 },
95
96 ---@type boolean
97 logging = true,
98
99 injector = {}, ---@type table<lc.lang, lc.inject>
100
101 cache = {
102 update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days
103 },
104
105 console = {
106 open_on_runcode = true, ---@type boolean
107
108 dir = "row", ---@type lc.direction
109
110 size = { ---@type lc.size
111 width = "90%",
112 height = "75%",
113 },
114
115 result = {
116 size = "60%", ---@type lc.size
117 },
118
119 testcase = {
120 virt_text = true, ---@type boolean
121
122 size = "40%", ---@type lc.size
123 },
124 },
125
126 description = {
127 position = "left", ---@type lc.position
128
129 width = "40%", ---@type lc.size
130
131 show_stats = true, ---@type boolean
132 },
133
134 hooks = {
135 ---@type fun()[]
136 ["enter"] = {},
137
138 ---@type fun(question: lc.ui.Question)[]
139 ["question_enter"] = {},
140
141 ---@type fun()[]
142 ["leave"] = {},
143 },
144
145 keys = {
146 toggle = { "q" }, ---@type string|string[]
147 confirm = { "<CR>" }, ---@type string|string[]
148
149 reset_testcases = "r", ---@type string
150 use_testcase = "U", ---@type string
151 focus_testcases = "H", ---@type string
152 focus_result = "L", ---@type string
153 },
154
155 ---@type lc.highlights
156 theme = {},
157
158 ---@type boolean
159 image_support = false,
160}
161```
162
163### arg
164
165Argument for [Neovim]
166
167```lua
168---@type string
169arg = "leetcode.nvim"
170```
171
172<small>See [usage]#-usage for more info</small>
173
174### lang
175
176Language to start your session with
177
178```lua
179---@type lc.lang
180lang = "cpp"
181```
182
183<details>
184 <summary>available languages</summary>
185
186| Language | lang |
187| ---------- | ---------- |
188| C++ | cpp |
189| Java | java |
190| Python | python |
191| Python3 | python3 |
192| C | c |
193| C# | csharp |
194| JavaScript | javascript |
195| TypeScript | typescript |
196| PHP | php |
197| Swift | swift |
198| Kotlin | kotlin |
199| Dart | dart |
200| Go | golang |
201| Ruby | ruby |
202| Scala | scala |
203| Rust | rust |
204| Racket | racket |
205| Erlang | erlang |
206| Elixir | elixir |
207| Bash | bash |
208
209</details>
210
211### cn
212
213Use [leetcode.cn] instead of [leetcode.com][leetcode]
214
215```lua
216cn = { -- leetcode.cn
217 enabled = false, ---@type boolean
218 translator = true, ---@type boolean
219 translate_problems = true, ---@type boolean
220},
221```
222
223### storage
224
225storage directories
226
227```lua
228---@type lc.storage
229storage = {
230 home = vim.fn.stdpath("data") .. "/leetcode",
231 cache = vim.fn.stdpath("cache") .. "/leetcode",
232},
233```
234
235### plugins
236
237[plugins list]#-plugins
238
239```lua
240---@type table<string, boolean>
241plugins = {
242 non_standalone = false,
243},
244```
245
246### logging
247
248Whether to log [leetcode.nvim] status notifications
249
250```lua
251---@type boolean
252logging = true
253```
254
255### injector
256
257Inject code before or after your solution, injected code won't be submitted or run.
258
259#### default imports
260
261You can also pass `before = true` to inject default imports for the language.
262Supported languages are `python`, `python3`, `java`
263
264Access default imports via `require("leetcode.config.imports")`
265
266```lua
267injector = { ---@type table<lc.lang, lc.inject>
268 ["python3"] = {
269 before = true
270 },
271 ["cpp"] = {
272 before = { "#include <bits/stdc++.h>", "using namespace std;" },
273 after = "int main() {}",
274 },
275 ["java"] = {
276 before = "import java.util.*;",
277 },
278}
279```
280
281### hooks
282
283List of functions that get executed on specified event
284
285```lua
286hooks = {
287 ---@type fun()[]
288 ["enter"] = {},
289
290 ---@type fun(question: lc.ui.Question)[]
291 ["question_enter"] = {},
292
293 ---@type fun()[]
294 ["leave"] = {},
295},
296```
297
298### theme
299
300Override the [default theme]./lua/leetcode/theme/default.lua.
301
302Each value is the same type as val parameter in `:help nvim_set_hl`
303
304```lua
305---@type lc.highlights
306theme = {
307 ["alt"] = {
308 bg = "#FFFFFF",
309 },
310 ["normal"] = {
311 fg = "#EA4AAA",
312 },
313},
314```
315
316### image support
317
318Whether to render question description images using [image.nvim]
319
320> [!WARNING]
321> Enabling this will disable question description wrap,
322> because of https://github.com/3rd/image.nvim/issues/62#issuecomment-1778082534
323
324```lua
325---@type boolean
326image_support = false,
327```
328
329## ๐Ÿ“‹ Commands
330
331### `Leet` opens menu dashboard
332
333- `menu` same as `Leet`
334
335- `exit` close [leetcode.nvim]
336
337- `console` opens console pop-up for currently opened question
338
339- `info` opens a pop-up containing information about the currently opened question
340
341- `tabs` opens a picker with all currently opened question tabs
342
343- `yank` yanks the current question solution
344
345- `lang` opens a picker to change the language of the current question
346
347- `run` run currently opened question
348
349- `test` same as `Leet run`
350
351- `submit` submit currently opened question
352
353- `random` opens a random question
354
355- `daily` opens the question of today
356
357- `list` opens a problem list picker
358
359- `open` opens the current question in a default browser
360
361- `reset` reset current question to default code definition
362
363- `last_submit` retrieve last submitted code for the current question
364
365- `restore` try to restore default question layout
366
367- `inject` re-inject code for the current question
368
369- `session`
370
371 - `create` create a new session
372 - `change` change the current session
373
374 - `update` update the current session in case it went out of sync
375
376- `desc` toggle question description
377
378 - `toggle` same as `Leet desc`
379
380 - `stats` toggle description stats visibility
381
382- `cookie`
383
384 - `update` opens a prompt to enter a new cookie
385
386 - `delete` sign-out
387
388- `cache`
389
390 - `update` updates cache
391
392#### Some commands can take optional arguments. To stack argument values separate them by a `,`
393
394- `Leet list`
395
396 ```
397 Leet list status=<status> difficulty=<difficulty>
398 ```
399
400- `Leet random`
401
402 ```
403 Leet random status=<status> difficulty=<difficulty> tags=<tags>
404 ```
405
406## ๐Ÿš€ Usage
407
408This plugin can be initiated in two ways:
409
410- To start [leetcode.nvim], simply pass [`arg`]#arg
411 as the _first and **only**_ [Neovim] argument
412
413 ```
414 nvim leetcode.nvim
415 ```
416
417- _**(Experimental)**_ Alternatively, you can use `:Leet` command to open [leetcode.nvim]
418 within your preferred dashboard plugin. The only requirement is that [Neovim]
419 must not have any listed buffers open.
420
421### Switching between questions
422
423To switch between questions, use `Leet tabs`
424
425### Sign In
426
427It is **required** to be **signed-in** to use [leetcode.nvim]
428
429https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4ad3abeda5c3
430
431## ๐Ÿด Recipes
432
433### ๐Ÿ’ค lazy loading with [lazy.nvim]
434
435> [!WARNING]
436> opting for either option makes the alternative
437> launch method unavailable due to lazy loading
438
439- with [`arg`]#arg
440
441 ```lua
442 local leet_arg = "leetcode.nvim"
443 ```
444
445 ```lua
446 {
447 "kawre/leetcode.nvim",
448 lazy = leet_arg ~= vim.fn.argv()[1],
449 opts = { arg = leet_arg },
450 }
451 ```
452
453- with `:Leet`
454
455 ```lua
456 {
457 "kawre/leetcode.nvim",
458 cmd = "Leet",
459 }
460 ```
461
462### ๐ŸชŸ Windows
463
464If you are using Windows,
465it is recommended to use [Cygwin]https://www.cygwin.com/ for a more consistent and Unix-like experience.
466
467## ๐Ÿงฉ Plugins
468
469### Non-Standalone mode
470
471To run [leetcode.nvim] in a non-standalone mode (i.e. not with argument or an empty Neovim session),
472enable the `non_standalone` plugin in your config:
473
474```lua
475plugins = {
476 non_standalone = true,
477}
478```
479
480You can then exit [leetcode.nvim] using `:Leet exit` command
481
482## ๐Ÿ™Œ Credits
483
484- [Leetbuddy.nvim]https://github.com/Dhanus3133/Leetbuddy.nvim
485
486- [alpha-nvim]https://github.com/goolord/alpha-nvim
487
488[image.nvim]: https://github.com/3rd/image.nvim
489[lazy.nvim]: https://github.com/folke/lazy.nvim
490[leetcode]: https://leetcode.com
491[leetcode.cn]: https://leetcode.cn
492[leetcode.nvim]: https://github.com/kawre/leetcode.nvim
493[neovim]: https://github.com/neovim/neovim
494[nerd-font]: https://www.nerdfonts.com
495[nui.nvim]: https://github.com/MunifTanjim/nui.nvim
496[nvim-notify]: https://github.com/rcarriga/nvim-notify
497[nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter
498[nvim-web-devicons]: https://github.com/nvim-tree/nvim-web-devicons
499[telescope.nvim]: https://github.com/nvim-telescope/telescope.nvim
500[tree-sitter-html]: https://github.com/tree-sitter/tree-sitter-html