| 1 | <div align="center"> |
| 2 | |
| 3 | # leetcode.nvim |
| 4 | |
| 5 | 🔥 在 [Neovim] 中解决 [LeetCode] 问题 🔥 |
| 6 | |
| 7 | 🇺🇸 <a href="README.md">English</a>, 🇨🇳 简体中文 |
| 8 | |
| 9 | </div> |
| 10 | |
| 11 | https://github.com/kawre/leetcode.nvim/assets/69250723/aee6584c-e099-4409-b114-123cb32b7563 |
| 12 | |
| 13 | > [!CAUTION] |
| 14 | > 此插件仅与`Java`进行了专门测试。 |
| 15 | > 如果您在使用其他语言时遇到任何错误,请打开一个问题报告它们。 |
| 16 | |
| 17 | ## ✨ 特性 |
| 18 | |
| 19 | - 📌 直观的仪表板,轻松导航 [leetcode.nvim] 内 |
| 20 | |
| 21 | - 😍 更好的可读性的问题描述格式 |
| 22 | |
| 23 | - 📈 在 [Neovim] 中显示 [LeetCode] 个人统计信息 |
| 24 | |
| 25 | - 🔀 支持每日和随机问题 |
| 26 | |
| 27 | - 💾 缓存以优化性能 |
| 28 | |
| 29 | ## 📬 环境要求 |
| 30 | |
| 31 | - [Neovim] >= 0.9.0 |
| 32 | |
| 33 | - [telescope.nvim] |
| 34 | |
| 35 | - [nui.nvim] |
| 36 | |
| 37 | - [nvim-treesitter] _**(可选,但强烈推荐)**_ |
| 38 | 用于格式化问题描述。 |
| 39 | 确保安装 `html` 解析器。 |
| 40 | |
| 41 | - [nvim-notify] _**(可选)**_ |
| 42 | |
| 43 | - [Nerd Font][nerd-font] & [nvim-web-devicons] _**(可选)**_ |
| 44 | |
| 45 | ## 📦 安装 |
| 46 | |
| 47 | - [lazy.nvim] |
| 48 | |
| 49 | ```lua |
| 50 | { |
| 51 | "kawre/leetcode.nvim", |
| 52 | build = ":TSUpdate html", |
| 53 | dependencies = { |
| 54 | "nvim-telescope/telescope.nvim", |
| 55 | "nvim-lua/plenary.nvim", -- telescope 所需 |
| 56 | "MunifTanjim/nui.nvim", |
| 57 | |
| 58 | -- 可选 |
| 59 | "nvim-treesitter/nvim-treesitter", |
| 60 | "rcarriga/nvim-notify", |
| 61 | "nvim-tree/nvim-web-devicons", |
| 62 | }, |
| 63 | opts = { |
| 64 | -- 配置放在这里 |
| 65 | cn = { |
| 66 | enabled = true, |
| 67 | }, |
| 68 | }, |
| 69 | } |
| 70 | ``` |
| 71 | |
| 72 | ## 🛠️ 配置 |
| 73 | |
| 74 | 要查看完整的配置类型,请参见 [template.lua](./lua/leetcode/config/template.lua) |
| 75 | |
| 76 | ### ⚙️ 默认配置 |
| 77 | |
| 78 | ```lua |
| 79 | { |
| 80 | ---@type string |
| 81 | arg = "leetcode.nvim", |
| 82 | |
| 83 | ---@type lc.lang |
| 84 | lang = "cpp", |
| 85 | |
| 86 | cn = { -- leetcode.cn |
| 87 | enabled = false, ---@type boolean |
| 88 | translator = true, ---@type boolean |
| 89 | translate_problems = true, ---@type boolean |
| 90 | }, |
| 91 | |
| 92 | ---@type lc.storage |
| 93 | storage = { |
| 94 | home = vim.fn.stdpath("data") .. "/leetcode", |
| 95 | cache = vim.fn.stdpath("cache") .. "/leetcode", |
| 96 | }, |
| 97 | |
| 98 | ---@type table<string, boolean> |
| 99 | plugins = { |
| 100 | non_standalone = false, |
| 101 | }, |
| 102 | |
| 103 | ---@type boolean |
| 104 | logging = true, |
| 105 | |
| 106 | injector = {}, ---@type table<lc.lang, lc.inject> |
| 107 | |
| 108 | cache = { |
| 109 | update_interval = 60 * 60 * 24 * 7, ---@type integer 7 days |
| 110 | }, |
| 111 | |
| 112 | console = { |
| 113 | open_on_runcode = true, ---@type boolean |
| 114 | |
| 115 | dir = "row", ---@type lc.direction |
| 116 | |
| 117 | size = { ---@type lc.size |
| 118 | width = "90%", |
| 119 | height = "75%", |
| 120 | }, |
| 121 | |
| 122 | result = { |
| 123 | size = "60%", ---@type lc.size |
| 124 | }, |
| 125 | |
| 126 | testcase = { |
| 127 | virt_text = true, ---@type boolean |
| 128 | |
| 129 | size = "40%", ---@type lc.size |
| 130 | }, |
| 131 | }, |
| 132 | |
| 133 | description = { |
| 134 | position = "left", ---@type lc.position |
| 135 | |
| 136 | width = "40%", ---@type lc.size |
| 137 | |
| 138 | show_stats = true, ---@type boolean |
| 139 | }, |
| 140 | |
| 141 | hooks = { |
| 142 | ---@type fun()[] |
| 143 | ["enter"] = {}, |
| 144 | |
| 145 | ---@type fun(question: lc.ui.Question)[] |
| 146 | ["question_enter"] = {}, |
| 147 | |
| 148 | ---@type fun()[] |
| 149 | ["leave"] = {}, |
| 150 | }, |
| 151 | |
| 152 | keys = { |
| 153 | toggle = { "q" }, ---@type string|string[] |
| 154 | confirm = { "<CR>" }, ---@type string|string[] |
| 155 | |
| 156 | reset_testcases = "r", ---@type string |
| 157 | use_testcase = "U", ---@type string |
| 158 | focus_testcases = "H", ---@type string |
| 159 | focus_result = "L", ---@type string |
| 160 | }, |
| 161 | |
| 162 | ---@type lc.highlights |
| 163 | theme = {}, |
| 164 | |
| 165 | ---@type boolean |
| 166 | image_support = false, |
| 167 | } |
| 168 | ``` |
| 169 | |
| 170 | ### arg |
| 171 | |
| 172 | [Neovim] 的参数 |
| 173 | |
| 174 | ```lua |
| 175 | ---@type string |
| 176 | arg = "leetcode.nvim" |
| 177 | ``` |
| 178 | |
| 179 | <small>有关更多信息,请参见 [usage](#-usage)</small> |
| 180 | |
| 181 | ### lang |
| 182 | |
| 183 | 会话开始时使用的语言 |
| 184 | |
| 185 | ```lua |
| 186 | ---@type lc.lang |
| 187 | lang = "cpp" |
| 188 | ``` |
| 189 | |
| 190 | <details> |
| 191 | <summary>可用编程语言</summary> |
| 192 | |
| 193 | |
| 194 | |
| 195 | | C++ | cpp | |
| 196 | | Java | java | |
| 197 | | Python | python | |
| 198 | | Python3 | python3 | |
| 199 | | C | c | |
| 200 | | C# | csharp | |
| 201 | | JavaScript | javascript | |
| 202 | | TypeScript | typescript | |
| 203 | | PHP | php | |
| 204 | | Swift | swift | |
| 205 | | Kotlin | kotlin | |
| 206 | | Dart | dart | |
| 207 | | Go | golang | |
| 208 | | Ruby | ruby | |
| 209 | | Scala | scala | |
| 210 | | Rust | rust | |
| 211 | | Racket | racket | |
| 212 | | Erlang | erlang | |
| 213 | | Elixir | elixir | |
| 214 | | Bash | bash | |
| 215 | |
| 216 | </details> |
| 217 | |
| 218 | ### cn |
| 219 | |
| 220 | 将 [leetcode.com][leetcode] 替换为 [leetcode.cn] |
| 221 | |
| 222 | ```lua |
| 223 | cn = { -- leetcode.cn |
| 224 | enabled = false, ---@type boolean |
| 225 | translator = true, ---@type boolean |
| 226 | translate_problems = true, ---@type boolean |
| 227 | }, |
| 228 | ``` |
| 229 | |
| 230 | ### storage |
| 231 | |
| 232 | 存储目录 |
| 233 | |
| 234 | ```lua |
| 235 | ---@type lc.storage |
| 236 | storage = { |
| 237 | home = vim.fn.stdpath("data") .. "/leetcode", |
| 238 | cache = vim.fn.stdpath("cache") .. "/leetcode", |
| 239 | }, |
| 240 | ``` |
| 241 | |
| 242 | ### plugins |
| 243 | |
| 244 | [插件列表](#-plugins) |
| 245 | |
| 246 | ```lua |
| 247 | ---@type table<string, boolean> |
| 248 | plugins = { |
| 249 | non_standalone = false, |
| 250 | }, |
| 251 | ``` |
| 252 | |
| 253 | ### logging |
| 254 | |
| 255 | 是否记录 [leetcode.nvim] 状态通知 |
| 256 | |
| 257 | ```lua |
| 258 | ---@type boolean |
| 259 | logging = true |
| 260 | ``` |
| 261 | |
| 262 | ### injector |
| 263 | |
| 264 | 在你的答案前后注入额外代码,注入的代码不会被提交或测试。 |
| 265 | |
| 266 | #### 默认导入 |
| 267 | |
| 268 | 您还可以传递 `before = true` 以注入语言的默认导入。 |
| 269 | 支持的语言为 `python`、`python3`、`java` |
| 270 | |
| 271 | 通过 `require("leetcode.config.imports")` 访问默认导入 |
| 272 | |
| 273 | ```lua |
| 274 | injector = { ---@type table<lc.lang, lc.inject> |
| 275 | ["cpp"] = { |
| 276 | before = { "#include <bits/stdc++.h>", "using namespace std;" }, |
| 277 | after = "int main() {}", |
| 278 | }, |
| 279 | ["java"] = { |
| 280 | before = "import java.util.*;", |
| 281 | }, |
| 282 | } |
| 283 | ``` |
| 284 | |
| 285 | ### hooks |
| 286 | |
| 287 | 在指定事件上执行的函数列表 |
| 288 | |
| 289 | ```lua |
| 290 | hooks = { |
| 291 | ---@type fun()[] |
| 292 | ["enter"] = {}, |
| 293 | |
| 294 | ---@type fun(question: lc.ui.Question)[] |
| 295 | ["question_enter"] = {}, |
| 296 | |
| 297 | ---@type fun()[] |
| 298 | ["leave"] = {}, |
| 299 | }, |
| 300 | ``` |
| 301 | |
| 302 | ### theme |
| 303 | |
| 304 | 覆盖[默认主题](./lua/leetcode/theme/default.lua) |
| 305 | |
| 306 | 每个值都与 `:help nvim_set_hl` 中的val参数相同类型 |
| 307 | |
| 308 | ```lua |
| 309 | ---@type lc.highlights |
| 310 | theme = { |
| 311 | ["alt"] = { |
| 312 | bg = "#FFFFFF", |
| 313 | }, |
| 314 | ["normal"] = { |
| 315 | fg = "#EA4AAA", |
| 316 | }, |
| 317 | }, |
| 318 | ``` |
| 319 | |
| 320 | ### image support |
| 321 | |
| 322 | 是否使用 [image.nvim] 渲染问题描述中的图片 |
| 323 | |
| 324 | > [!WARNING] |
| 325 | > 启用此选项将禁用问题描述的换行,因为 |
| 326 | > https://github.com/3rd/image.nvim/issues/62#issuecomment-1778082534 |
| 327 | |
| 328 | ```lua |
| 329 | ---@type boolean |
| 330 | image_support = false, -- 将此设置为 `true` 将禁用问题描述的换行 |
| 331 | ``` |
| 332 | |
| 333 | ## 📋 命令 |
| 334 | |
| 335 | ### `Leet` 打开菜单仪表板 |
| 336 | |
| 337 | - `menu` 与 `Leet` 相同 |
| 338 | |
| 339 | - `exit` 关闭 [leetcode.nvim] |
| 340 | |
| 341 | - `console` 打开当前打开问题的控制台弹出窗口 |
| 342 | |
| 343 | - `info` 打开包含当前打开问题信息的弹出窗口 |
| 344 | |
| 345 | - `tabs` 打开所有当前打开问题选项卡的选择器 |
| 346 | |
| 347 | - `yank` 复制当前问题的解决方案 |
| 348 | |
| 349 | - `lang` 打开更改当前问题语言的选择器 |
| 350 | |
| 351 | - `run` 运行当前打开的问题 |
| 352 | |
| 353 | - `test` 与 `Leet run` 相同 |
| 354 | |
| 355 | - `submit` 提交当前打开的问题 |
| 356 | |
| 357 | - `random` 打开一个随机问题 |
| 358 | |
| 359 | - `daily` 打开今天的问题 |
| 360 | |
| 361 | - `list` 打开问题列表选择器 |
| 362 | |
| 363 | - `open` 在默认浏览器中打开当前问题 |
| 364 | |
| 365 | - `reset` 还原到默认的代码模版 |
| 366 | |
| 367 | - `last_submit` 检索上次提交的代码,用于当前问题 |
| 368 | |
| 369 | - `restore` 尝试恢复默认问题布局 |
| 370 | |
| 371 | - `inject` 重新注入当前问题的代码 |
| 372 | |
| 373 | - `session` |
| 374 | |
| 375 | - `create` 创建一个新的会话 |
| 376 | |
| 377 | - `change` 更改当前会话 |
| 378 | |
| 379 | - `update` 更新当前会话,以防它失去同步 |
| 380 | |
| 381 | - `desc` 切换问题描述 |
| 382 | |
| 383 | - `toggle` 与 `Leet desc` 相同 |
| 384 | |
| 385 | - `stats` 切换描述统计可见性 |
| 386 | |
| 387 | - `cookie` |
| 388 | |
| 389 | - `update` 打开提示输入新 cookie |
| 390 | |
| 391 | - `delete` 注销 |
| 392 | |
| 393 | - `cache` |
| 394 | |
| 395 | - `update` 更新缓存 |
| 396 | |
| 397 | #### 可以带有可选参数。要堆叠参数值,请使用 `,` 将它们分隔开 |
| 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 | ## 🚀 使用方法 |
| 412 | |
| 413 | 该插件可以通过两种方式启动: |
| 414 | |
| 415 | - 要启动 [leetcode.nvim],只需将 [`arg`](#arg) |
| 416 | 作为 第一个且唯一 [Neovim] 参数传递 |
| 417 | |
| 418 | ``` |
| 419 | nvim leetcode.nvim |
| 420 | ``` |
| 421 | |
| 422 | - _**(实验性)**_ 另外,您可以使用 `:Leet` 命令在您喜欢的仪表板插件中打开 |
| 423 | [leetcode.nvim]。唯一的要求是 [Neovim] 不能有任何列出的缓冲区打开。 |
| 424 | |
| 425 | ### 切换问题 |
| 426 | |
| 427 | 要在问题之间切换,请使用 `Leet tabs`。 |
| 428 | |
| 429 | ### 登录 |
| 430 | |
| 431 | 使用 [leetcode.nvim] 必须 **登录** |
| 432 | |
| 433 | https://github.com/kawre/leetcode.nvim/assets/69250723/b7be8b95-5e2c-4153-8845-4ad3abeda5c3 |
| 434 | |
| 435 | ## 🍴 示例 |
| 436 | |
| 437 | ### 💤 使用 [lazy.nvim] 进行延迟加载 |
| 438 | |
| 439 | > [!WARNING] |
| 440 | > 选择其中任一选项将由于延迟加载而使另一启动方法不可用。 |
| 441 | |
| 442 | - 使用 [`arg`](#arg) |
| 443 | |
| 444 | ```lua |
| 445 | local leet_arg = "leetcode.nvim" |
| 446 | ``` |
| 447 | |
| 448 | ```lua |
| 449 | { |
| 450 | "kawre/leetcode.nvim", |
| 451 | lazy = leet_arg ~= vim.fn.argv()[1], |
| 452 | opts = { arg = leet_arg }, |
| 453 | } |
| 454 | ``` |
| 455 | |
| 456 | - 使用 `:Leet` |
| 457 | |
| 458 | ```lua |
| 459 | { |
| 460 | "kawre/leetcode.nvim", |
| 461 | cmd = "Leet", |
| 462 | } |
| 463 | ``` |
| 464 | |
| 465 | ## 🧩 Plugins |
| 466 | |
| 467 | ### Non-Standalone mode |
| 468 | |
| 469 | 要在非独立模式下运行 [leetcode.nvim](即不带参数或在空的 Neovim 会话中运行), |
| 470 | 请在您的配置中启用 `non_standalone` 插件: |
| 471 | |
| 472 | ```lua |
| 473 | plugins = { |
| 474 | non_standalone = true, |
| 475 | } |
| 476 | ``` |
| 477 | |
| 478 | 你可以使用 `:Leet exit` 命令退出 [leetcode.nvim] |
| 479 | |
| 480 | ## 🙌 鸣谢 |
| 481 | |
| 482 | - [Leetbuddy.nvim](https://github.com/Dhanus3133/Leetbuddy.nvim) |
| 483 | |
| 484 | - [alpha-nvim](https://github.com/goolord/alpha-nvim) |
| 485 | |
| 486 | [image.nvim]: https://github.com/3rd/image.nvim |
| 487 | [lazy.nvim]: https://github.com/folke/lazy.nvim |
| 488 | [leetcode]: https://leetcode.com |
| 489 | [leetcode.cn]: https://leetcode.cn |
| 490 | [leetcode.nvim]: https://github.com/kawre/leetcode.nvim |
| 491 | [neovim]: https://github.com/neovim/neovim |
| 492 | [nerd-font]: https://www.nerdfonts.com |
| 493 | [nui.nvim]: https://github.com/MunifTanjim/nui.nvim |
| 494 | [nvim-notify]: https://github.com/rcarriga/nvim-notify |
| 495 | [nvim-treesitter]: https://github.com/nvim-treesitter/nvim-treesitter |
| 496 | [nvim-web-devicons]: https://github.com/nvim-tree/nvim-web-devicons |
| 497 | [telescope.nvim]: https://github.com/nvim-telescope/telescope.nvim |