A Neovim plugin enabling you to solve LeetCode problems.
fix: invalid range logger
3 files changed, +54 -46
+2-1lua/leetcode-ui/layout/console.lua
| @@ -60,7 +60,8 @@ function ConsoleLayout:mount() | ||
| 60 | 60 | end |
| 61 | 61 | |
| 62 | 62 | function ConsoleLayout:run(submit) |
| 63 | - if not self.question:editor_section_is_present("code") then | |
| 63 | + local range = self.question:editor_section_range("code") | |
| 64 | + if not range:is_valid_or_log() then | |
| 64 | 65 | return |
| 65 | 66 | end |
| 66 | 67 |
+50-30lua/leetcode-ui/question.lua
| @@ -107,14 +107,13 @@ function Question:editor_fold_imports(strict) | ||
| 107 | 107 | return |
| 108 | 108 | end |
| 109 | 109 | |
| 110 | - local range = self:editor_section_range("imports", { inclusive = true, strict = strict }) | |
| 111 | - if range and range.end_i then | |
| 110 | + local range = self:editor_section_range("imports", true) | |
| 111 | + if range.complete or range.end_i then | |
| 112 | 112 | vim.api.nvim_buf_call(self.bufnr, function() |
| 113 | - ---@diagnostic disable-next-line: param-type-mismatch | |
| 114 | - pcall(vim.cmd, ("%d,%dfold"):format(range.start_i or 1, range.end_i)) | |
| 113 | + pcall(vim.cmd, ("%d,%dfold"):format(range.start_i or 1, range.end_i)) ---@diagnostic disable-line: param-type-mismatch | |
| 115 | 114 | end) |
| 116 | - else | |
| 117 | - log.warn("`@leet imports` section not found in editor.") | |
| 115 | + elseif strict then | |
| 116 | + range.log_not_found() | |
| 118 | 117 | end |
| 119 | 118 | end |
| 120 | 119 | |
| @@ -123,8 +122,8 @@ function Question:editor_yank_code() | ||
| 123 | 122 | return |
| 124 | 123 | end |
| 125 | 124 | |
| 126 | - local range = self:editor_section_range("code", { strict = true }) | |
| 127 | - if range then | |
| 125 | + local range = self:editor_section_range("code") | |
| 126 | + if range:is_valid_or_log() then | |
| 128 | 127 | vim.api.nvim_buf_call(self.bufnr, function() |
| 129 | 128 | vim.cmd(("%d,%dyank"):format(range.start_i, range.end_i)) |
| 130 | 129 | end) |
| @@ -218,11 +217,6 @@ function Question:inject(before) | ||
| 218 | 217 | end |
| 219 | 218 | end |
| 220 | 219 | |
| 221 | -function Question:editor_section_is_present(name) | |
| 222 | - local range = self:editor_section_range(name) | |
| 223 | - return range and true or false | |
| 224 | -end | |
| 225 | - | |
| 226 | 220 | ---@param lines string|string[] |
| 227 | 221 | ---@param name lc.editor.section |
| 228 | 222 | ---@return string |
| @@ -241,7 +235,7 @@ end | ||
| 241 | 235 | function Question:editor_section_replace(lines, name) |
| 242 | 236 | local range = self:editor_section_range(name) |
| 243 | 237 | |
| 244 | - if range then | |
| 238 | + if range:is_valid_or_log() then | |
| 245 | 239 | self:editor_set_lines(range.start_i - 1, range.end_i, lines) |
| 246 | 240 | end |
| 247 | 241 | end |
| @@ -361,10 +355,8 @@ end | ||
| 361 | 355 | ---@field complete boolean |
| 362 | 356 | |
| 363 | 357 | ---@param name string |
| 364 | ----@param opts? { inclusive?: boolean, strict?: boolean } | |
| 365 | ----@return lc.Question.Editor.Range? | |
| 366 | -function Question:editor_section_range(name, opts) | |
| 367 | - opts = vim.tbl_extend("keep", opts or {}, { strict = true, inclusive = false }) | |
| 358 | +---@param inclusive? boolean | |
| 359 | +function Question:editor_section_range(name, inclusive) | |
| 368 | 360 | local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) |
| 369 | 361 | local start_i, end_i |
| 370 | 362 | |
| @@ -373,31 +365,59 @@ function Question:editor_section_range(name, opts) | ||
| 373 | 365 | |
| 374 | 366 | for i, line in ipairs(lines) do |
| 375 | 367 | if line:match(start_tag) then |
| 376 | - start_i = i + (opts.inclusive and 0 or 1) | |
| 368 | + start_i = i + (inclusive and 0 or 1) | |
| 377 | 369 | elseif line:match(end_tag) then |
| 378 | - end_i = i - (opts.inclusive and 0 or 1) | |
| 370 | + end_i = i - (inclusive and 0 or 1) | |
| 379 | 371 | end |
| 380 | 372 | end |
| 381 | 373 | |
| 382 | - if opts.strict and not (start_i and end_i) then | |
| 383 | - log.error(("Section `@leet %s` not found in editor."):format(name)) | |
| 384 | - return nil | |
| 374 | + local res = { | |
| 375 | + start_i = start_i, | |
| 376 | + end_i = end_i, | |
| 377 | + lines = lines, | |
| 378 | + complete = start_i and end_i, | |
| 379 | + } | |
| 380 | + | |
| 381 | + res.log_not_found = function() | |
| 382 | + if res.complete then | |
| 383 | + return | |
| 384 | + end | |
| 385 | + | |
| 386 | + local missing = {} | |
| 387 | + if not res.start_i then | |
| 388 | + table.insert(missing, ("`%s`"):format(start_tag)) | |
| 389 | + end | |
| 390 | + if not res.end_i then | |
| 391 | + table.insert(missing, ("`%s`"):format(end_tag)) | |
| 392 | + end | |
| 393 | + | |
| 394 | + log.warn(table.concat(missing, " and ") .. " not found.") | |
| 385 | 395 | end |
| 386 | 396 | |
| 387 | - return { start_i = start_i, end_i = end_i, lines = lines, complete = start_i and end_i } | |
| 397 | + res.is_partial = function() | |
| 398 | + return res.complete or (res.start_i or res.end_i) | |
| 399 | + end | |
| 400 | + | |
| 401 | + res.is_valid_or_log = function() | |
| 402 | + if res.complete then | |
| 403 | + return true | |
| 404 | + else | |
| 405 | + res.log_not_found() | |
| 406 | + return false | |
| 407 | + end | |
| 408 | + end | |
| 409 | + | |
| 410 | + return res | |
| 388 | 411 | end |
| 389 | 412 | |
| 390 | 413 | ---@param submit boolean |
| 391 | 414 | ---@return string |
| 392 | 415 | function Question:editor_submit_lines(submit) |
| 393 | 416 | local range = self:editor_section_range("code") |
| 394 | - assert(range, "Code section not found in editor") | |
| 395 | - | |
| 396 | - local start_i = range.start_i or 1 | |
| 397 | - local end_i = range.end_i or #range.lines | |
| 417 | + assert(range.complete, "Code section not found") | |
| 398 | 418 | |
| 399 | - local prefix = not submit and ("\n"):rep(start_i - 1) or "" | |
| 400 | - return prefix .. table.concat(range.lines, "\n", start_i, end_i) | |
| 419 | + local prefix = not submit and ("\n"):rep(range.start_i - 1) or "" | |
| 420 | + return prefix .. table.concat(range.lines, "\n", range.start_i, range.end_i) | |
| 401 | 421 | end |
| 402 | 422 | |
| 403 | 423 | ---@param self lc.ui.Question |
+2-15lua/leetcode/command/init.lua
| @@ -392,21 +392,8 @@ function cmd.inject() | ||
| 392 | 392 | |
| 393 | 393 | if q.bufnr and api.nvim_buf_is_valid(q.bufnr) then |
| 394 | 394 | local range = q:editor_section_range("code") |
| 395 | - assert(range, "Code section should be present") | |
| 396 | 395 | |
| 397 | - if not range.start_i or not range.end_i then | |
| 398 | - local missing = {} | |
| 399 | - | |
| 400 | - if not range.start_i then | |
| 401 | - local start_tag = utils.section_tag("code", true) | |
| 402 | - table.insert(missing, ("`%s`"):format(start_tag)) | |
| 403 | - end | |
| 404 | - if not range.end_i then | |
| 405 | - local end_tag = utils.section_tag("code", false) | |
| 406 | - table.insert(missing, ("`%s`"):format(end_tag)) | |
| 407 | - end | |
| 408 | - | |
| 409 | - log.error(table.concat(missing, " and ") .. " not found.") | |
| 396 | + if not range:is_valid_or_log() then | |
| 410 | 397 | return |
| 411 | 398 | end |
| 412 | 399 | |
| @@ -424,7 +411,7 @@ function cmd.fold() | ||
| 424 | 411 | return |
| 425 | 412 | end |
| 426 | 413 | |
| 427 | - q:editor_fold_imports(false) | |
| 414 | + q:editor_fold_imports(true) | |
| 428 | 415 | end |
| 429 | 416 | |
| 430 | 417 | function cmd.get_active_session() |