irongit

A Neovim plugin enabling you to solve LeetCode problems.

fix: invalid range logger

kawreauthored
parent 5741350commit 04a4419d1d5d8e31e0e1baf12cc3347cac542c70Browse files

3 files changed, +54 -46

+2-1lua/leetcode-ui/layout/console.lua
@@ -60,7 +60,8 @@ function ConsoleLayout:mount()
6060 end
6161
6262 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
6465 return
6566 end
6667
+50-30lua/leetcode-ui/question.lua
@@ -107,14 +107,13 @@ function Question:editor_fold_imports(strict)
107107 return
108108 end
109109
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
112112 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
115114 end)
116- else
117- log.warn("`@leet imports` section not found in editor.")
115+ elseif strict then
116+ range.log_not_found()
118117 end
119118 end
120119
@@ -123,8 +122,8 @@ function Question:editor_yank_code()
123122 return
124123 end
125124
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
128127 vim.api.nvim_buf_call(self.bufnr, function()
129128 vim.cmd(("%d,%dyank"):format(range.start_i, range.end_i))
130129 end)
@@ -218,11 +217,6 @@ function Question:inject(before)
218217 end
219218 end
220219
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-
226220 ---@param lines string|string[]
227221 ---@param name lc.editor.section
228222 ---@return string
@@ -241,7 +235,7 @@ end
241235 function Question:editor_section_replace(lines, name)
242236 local range = self:editor_section_range(name)
243237
244- if range then
238+ if range:is_valid_or_log() then
245239 self:editor_set_lines(range.start_i - 1, range.end_i, lines)
246240 end
247241 end
@@ -361,10 +355,8 @@ end
361355 ---@field complete boolean
362356
363357 ---@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)
368360 local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
369361 local start_i, end_i
370362
@@ -373,31 +365,59 @@ function Question:editor_section_range(name, opts)
373365
374366 for i, line in ipairs(lines) do
375367 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)
377369 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)
379371 end
380372 end
381373
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.")
385395 end
386396
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
388411 end
389412
390413 ---@param submit boolean
391414 ---@return string
392415 function Question:editor_submit_lines(submit)
393416 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")
398418
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)
401421 end
402422
403423 ---@param self lc.ui.Question
+2-15lua/leetcode/command/init.lua
@@ -392,21 +392,8 @@ function cmd.inject()
392392
393393 if q.bufnr and api.nvim_buf_is_valid(q.bufnr) then
394394 local range = q:editor_section_range("code")
395- assert(range, "Code section should be present")
396395
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
410397 return
411398 end
412399
@@ -424,7 +411,7 @@ function cmd.fold()
424411 return
425412 end
426413
427- q:editor_fold_imports(false)
414+ q:editor_fold_imports(true)
428415 end
429416
430417 function cmd.get_active_session()