A Neovim plugin enabling you to solve LeetCode problems.
feat: `Leet inject`
5 files changed, +127 -25
+2-0README.md
| @@ -314,6 +314,8 @@ image_support = false, | ||
| 314 | 314 | |
| 315 | 315 | - `restore` try to restore default question layout |
| 316 | 316 | |
| 317 | +- `inject` re-inject code for the current question | |
| 318 | + | |
| 317 | 319 | - `session` |
| 318 | 320 | |
| 319 | 321 | - `create` create a new session |
+29-25lua/leetcode-ui/question.lua
| @@ -51,35 +51,38 @@ function Question:create_file() | ||
| 51 | 51 | return self.file:absolute() |
| 52 | 52 | end |
| 53 | 53 | |
| 54 | ----@param code string | |
| 55 | -function Question:injector(code) | |
| 56 | - local injector = config.user.injector | |
| 57 | - local inject = injector[self.lang] or {} | |
| 58 | - | |
| 59 | - ---@param inj? string|string[] | |
| 60 | - ---@param before boolean | |
| 61 | - local function norm_inject(inj, before) | |
| 62 | - local res | |
| 63 | - | |
| 64 | - if type(inj) == "table" then | |
| 65 | - res = table.concat(inj, "\n") | |
| 66 | - elseif type(inj) == "string" then | |
| 67 | - res = inj | |
| 68 | - end | |
| 54 | +---@param before boolean | |
| 55 | +function Question:inject(before) | |
| 56 | + local inject = config.user.injector[self.lang] or {} | |
| 57 | + local inj = before and inject.before or inject.after | |
| 69 | 58 | |
| 70 | - if res and res ~= "" then | |
| 71 | - return before and (res .. "\n\n") or ("\n\n" .. res) | |
| 72 | - else | |
| 73 | - return "" | |
| 74 | - end | |
| 59 | + local res | |
| 60 | + | |
| 61 | + if type(inj) == "table" then | |
| 62 | + res = table.concat(inj, "\n") | |
| 63 | + elseif type(inj) == "string" then | |
| 64 | + res = inj | |
| 75 | 65 | end |
| 76 | 66 | |
| 67 | + if res and res ~= "" then | |
| 68 | + return before and (res .. "\n\n") or ("\n\n" .. res) | |
| 69 | + else | |
| 70 | + return nil | |
| 71 | + end | |
| 72 | +end | |
| 73 | + | |
| 74 | +---@param code string | |
| 75 | +function Question:injector(code) | |
| 77 | 76 | local lang = utils.get_lang(self.lang) |
| 78 | - return norm_inject(inject.before, true) -- | |
| 77 | + | |
| 78 | + local inj_before = self:inject(true) or "" | |
| 79 | + local inj_after = self:inject(false) or "" | |
| 80 | + | |
| 81 | + return inj_before -- | |
| 79 | 82 | .. ("%s @leet start\n"):format(lang.comment) |
| 80 | 83 | .. code |
| 81 | 84 | .. ("\n%s @leet end"):format(lang.comment) |
| 82 | - .. norm_inject(inject.after, false) | |
| 85 | + .. inj_after | |
| 83 | 86 | end |
| 84 | 87 | |
| 85 | 88 | ---@param pre? boolean |
| @@ -156,16 +159,17 @@ function Question:mount() | ||
| 156 | 159 | return self |
| 157 | 160 | end |
| 158 | 161 | |
| 162 | +---@param inclusive? boolean | |
| 159 | 163 | ---@return integer, integer, string[] |
| 160 | -function Question:range() | |
| 164 | +function Question:range(inclusive) | |
| 161 | 165 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) |
| 162 | 166 | local start_i, end_i = 1, #lines |
| 163 | 167 | |
| 164 | 168 | for i, line in ipairs(lines) do |
| 165 | 169 | if line:match("@leet start") then |
| 166 | - start_i = i + 1 | |
| 170 | + start_i = i + (inclusive and 0 or 1) | |
| 167 | 171 | elseif line:match("@leet end") then |
| 168 | - end_i = i - 1 | |
| 172 | + end_i = i - (inclusive and 0 or 1) | |
| 169 | 173 | end |
| 170 | 174 | end |
| 171 | 175 |
+22-0lua/leetcode/command/init.lua
| @@ -335,6 +335,27 @@ function cmd.restore() | ||
| 335 | 335 | end |
| 336 | 336 | end |
| 337 | 337 | |
| 338 | +function cmd.inject() | |
| 339 | + local utils = require("leetcode.utils") | |
| 340 | + utils.auth_guard() | |
| 341 | + local q = utils.curr_question() | |
| 342 | + if not q then return end | |
| 343 | + | |
| 344 | + local start_i, end_i = q:range(true) | |
| 345 | + | |
| 346 | + if vim.api.nvim_buf_is_valid(q.bufnr) then | |
| 347 | + local before = q:inject(true) | |
| 348 | + if before then | |
| 349 | + vim.api.nvim_buf_set_lines(q.bufnr, 0, start_i - 1, false, vim.split(before, "\n")) | |
| 350 | + end | |
| 351 | + | |
| 352 | + local after = q:inject(false) | |
| 353 | + if after then | |
| 354 | + vim.api.nvim_buf_set_lines(q.bufnr, end_i + 1, -1, false, vim.split(after, "\n")) | |
| 355 | + end | |
| 356 | + end | |
| 357 | +end | |
| 358 | + | |
| 338 | 359 | function cmd.get_active_session() |
| 339 | 360 | local sessions = config.sessions.all |
| 340 | 361 | return vim.tbl_filter(function(s) return s.is_active end, sessions)[1] |
| @@ -506,6 +527,7 @@ cmd.commands = { | ||
| 506 | 527 | reset = { cmd.reset }, |
| 507 | 528 | last_submit = { cmd.last_submit }, |
| 508 | 529 | restore = { cmd.restore }, |
| 530 | + inject = { cmd.inject }, | |
| 509 | 531 | |
| 510 | 532 | session = { |
| 511 | 533 | change = { |
+73-0lua/leetcode/config/imports.lua
| @@ -0,0 +1,73 @@ | ||
| 1 | +---@class lc.Imports | |
| 2 | +local imports = {} | |
| 3 | + | |
| 4 | +imports["python3"] = { | |
| 5 | + "from string import *", | |
| 6 | + "from re import *", | |
| 7 | + "from datetime import *", | |
| 8 | + "from collections import *", | |
| 9 | + "from heapq import *", | |
| 10 | + "from bisect import *", | |
| 11 | + "from copy import *", | |
| 12 | + "from math import *", | |
| 13 | + "from random import *", | |
| 14 | + "from statistics import *", | |
| 15 | + "from itertools import *", | |
| 16 | + "from functools import *", | |
| 17 | + "from operator import *", | |
| 18 | + "from io import *", | |
| 19 | + "from sys import *", | |
| 20 | + "from json import *", | |
| 21 | + "from builtins import *", | |
| 22 | + "import string", | |
| 23 | + "import re", | |
| 24 | + "import datetime", | |
| 25 | + "import collections", | |
| 26 | + "import heapq", | |
| 27 | + "import bisect", | |
| 28 | + "import copy", | |
| 29 | + "import math", | |
| 30 | + "import random", | |
| 31 | + "import statistics", | |
| 32 | + "import itertools", | |
| 33 | + "import functools", | |
| 34 | + "import operator", | |
| 35 | + "import io", | |
| 36 | + "import sys", | |
| 37 | + "import json", | |
| 38 | + "from typing import *", | |
| 39 | +} | |
| 40 | + | |
| 41 | +imports["python"] = { | |
| 42 | + "from bisect import *", | |
| 43 | + "from collections import *", | |
| 44 | + "from copy import *", | |
| 45 | + "from datetime import *", | |
| 46 | + "from heapq import *", | |
| 47 | + "from math import *", | |
| 48 | + "from re import *", | |
| 49 | + "from string import *", | |
| 50 | + "from random import *", | |
| 51 | + "from itertools import *", | |
| 52 | + "from functools import *", | |
| 53 | + "from operator import *", | |
| 54 | + "from __builtin__ import *", | |
| 55 | + "import string", | |
| 56 | + "import re", | |
| 57 | + "import datetime", | |
| 58 | + "import collections", | |
| 59 | + "import heapq", | |
| 60 | + "import bisect", | |
| 61 | + "import copy", | |
| 62 | + "import math", | |
| 63 | + "import random", | |
| 64 | + "import itertools", | |
| 65 | + "import functools", | |
| 66 | + "import operator", | |
| 67 | +} | |
| 68 | + | |
| 69 | +imports["java"] = { | |
| 70 | + "import java.util.*;", | |
| 71 | +} | |
| 72 | + | |
| 73 | +return imports |
+1-0lua/leetcode/config/init.lua
| @@ -28,6 +28,7 @@ local config = { | ||
| 28 | 28 | icons = require("leetcode.config.icons"), |
| 29 | 29 | sessions = require("leetcode.config.sessions"), |
| 30 | 30 | stats = require("leetcode.config.stats"), |
| 31 | + imports = require("leetcode.config.imports"), | |
| 31 | 32 | |
| 32 | 33 | ---@type lc.UserStatus |
| 33 | 34 | auth = {}, ---@diagnostic disable-line |