A Neovim plugin enabling you to solve LeetCode problems.
feat(config): `open_on_runcode` now defaults to `true`
6 files changed, +34 -25
+1-1README.md
| @@ -97,7 +97,7 @@ To see full configuration types see [template.lua](./lua/leetcode/config/templat | ||
| 97 | 97 | logging = true, |
| 98 | 98 | |
| 99 | 99 | console = { |
| 100 | - open_on_runcode = false, ---@type boolean | |
| 100 | + open_on_runcode = true, ---@type boolean | |
| 101 | 101 | |
| 102 | 102 | dir = "row", ---@type "col" | "row" |
| 103 | 103 |
+0-0lua/leetcode/command/options.lua → lua/leetcode/command/arguments.lua
No content changes (mode or rename only).
+29-21lua/leetcode/command/init.lua
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | local log = require("leetcode.logger") |
| 2 | -local arguments = require("leetcode.command.options") | |
| 2 | +local arguments = require("leetcode.command.arguments") | |
| 3 | 3 | |
| 4 | 4 | ---@class lc.Commands |
| 5 | 5 | local cmd = {} |
| @@ -187,14 +187,25 @@ local function cmds_keys(t) | ||
| 187 | 187 | end, vim.tbl_keys(t)) |
| 188 | 188 | end |
| 189 | 189 | |
| 190 | +---@param _ string | |
| 191 | +---@param line string | |
| 192 | +--- | |
| 193 | +---@return string[] | |
| 194 | +function cmd.complete(_, line) | |
| 195 | + local args, options = cmd.parse(line:gsub("Leet%s", "")) | |
| 196 | + return cmd.rec_complete(args, options, cmd.commands) | |
| 197 | +end | |
| 198 | + | |
| 190 | 199 | ---@param args string[] |
| 191 | 200 | ---@param options string[] |
| 192 | 201 | ---@param cmds table<string,any> |
| 193 | -function cmd.complete(args, options, cmds) | |
| 202 | +--- | |
| 203 | +---@return string[] | |
| 204 | +function cmd.rec_complete(args, options, cmds) | |
| 194 | 205 | if not cmds or vim.tbl_isempty(args) then return {} end |
| 195 | 206 | |
| 196 | 207 | if not cmds._args and cmds[args[1]] then |
| 197 | - return cmd.complete(args, options, cmds[table.remove(args, 1)]) | |
| 208 | + return cmd.rec_complete(args, options, cmds[table.remove(args, 1)]) | |
| 198 | 209 | end |
| 199 | 210 | |
| 200 | 211 | local txt, keys = args[#args], cmds_keys(cmds) |
| @@ -225,58 +236,55 @@ function cmd.complete(args, options, cmds) | ||
| 225 | 236 | ) |
| 226 | 237 | end |
| 227 | 238 | |
| 228 | -function cmd.cmd(args) | |
| 239 | +function cmd.exec(args) | |
| 229 | 240 | local t = cmd.commands |
| 241 | + | |
| 230 | 242 | local options = {} |
| 231 | 243 | for s in vim.gsplit(args.args, "%s+", { trimempty = true }) do |
| 232 | 244 | local opt = vim.split(s, "=") |
| 233 | 245 | if opt[2] then |
| 234 | - options[opt[1]] = vim.split(opt[2], ",%s*", { trimempty = true }) | |
| 235 | - else | |
| 246 | + options[opt[1]] = vim.split(opt[2], ",", { trimempty = true }) | |
| 247 | + elseif t then | |
| 236 | 248 | t = t[s] |
| 249 | + else | |
| 250 | + break | |
| 237 | 251 | end |
| 238 | 252 | end |
| 239 | 253 | |
| 240 | - t[1](options) | |
| 254 | + if t and type(t[1]) == "function" then | |
| 255 | + t[1](options) ---@diagnostic disable-line | |
| 256 | + else | |
| 257 | + log.error(("Invalid command: `%s %s`"):format(args.name, args.args)) | |
| 258 | + end | |
| 241 | 259 | end |
| 242 | 260 | |
| 243 | 261 | function cmd.setup() |
| 244 | - vim.api.nvim_create_user_command("Leet", function(args) | |
| 245 | - if not pcall(cmd.cmd, args) then log.error(("Invalid command: `%s`"):format(args.args)) end | |
| 246 | - end, { | |
| 262 | + vim.api.nvim_create_user_command("Leet", cmd.exec, { | |
| 247 | 263 | bar = true, |
| 248 | 264 | bang = true, |
| 249 | 265 | nargs = "?", |
| 250 | 266 | desc = "Leet", |
| 251 | - ---@param _ string | |
| 252 | - ---@param line string | |
| 253 | - complete = function(_, line) | |
| 254 | - local args, options = cmd.parse(line:gsub("Leet%s", "")) | |
| 255 | - return cmd.complete(args, options, cmd.commands) | |
| 256 | - end, | |
| 267 | + complete = cmd.complete, | |
| 257 | 268 | }) |
| 258 | 269 | end |
| 259 | 270 | |
| 260 | 271 | cmd.commands = { |
| 261 | 272 | cmd.menu, |
| 262 | 273 | |
| 274 | + menu = { cmd.menu }, | |
| 263 | 275 | console = { cmd.console }, |
| 264 | 276 | info = { cmd.info }, |
| 265 | - menu = { cmd.menu }, | |
| 266 | 277 | tabs = { cmd.question_tabs }, |
| 267 | 278 | lang = { cmd.change_lang }, |
| 268 | 279 | run = { cmd.q_run }, |
| 269 | 280 | test = { cmd.q_run }, |
| 270 | 281 | submit = { cmd.q_submit }, |
| 271 | - fix = { cmd.fix }, | |
| 272 | 282 | random = { cmd.random_question }, |
| 273 | 283 | daily = { cmd.qot }, |
| 284 | + fix = { cmd.fix }, | |
| 274 | 285 | |
| 275 | 286 | list = { |
| 276 | 287 | cmd.problems, |
| 277 | - | |
| 278 | - test = function() log.info("siema") end, | |
| 279 | - | |
| 280 | 288 | _args = arguments.list, |
| 281 | 289 | }, |
| 282 | 290 |
+1-1lua/leetcode/config/template.lua
| @@ -56,7 +56,7 @@ local M = { | ||
| 56 | 56 | logging = true, |
| 57 | 57 | |
| 58 | 58 | console = { |
| 59 | - open_on_runcode = false, ---@type boolean | |
| 59 | + open_on_runcode = true, ---@type boolean | |
| 60 | 60 | |
| 61 | 61 | dir = "row", ---@type "col" | "row" |
| 62 | 62 |