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