Skip to content

Commit

Permalink
feat: store testcase snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
kawre committed Feb 23, 2024
1 parent 7cf551b commit 69e6b41
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
6 changes: 3 additions & 3 deletions lua/leetcode-ui/group/cases.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ function Cases:init(item, parent)
self.console = parent

local total = item.total_testcases ~= vim.NIL and item.total_testcases or 0
local testcases = parent.testcase:by_id(item.submission_id)

for i = 1, total do
self.cases[i] = Case({
-- TODO: cache the testcases on submission,
-- so it doesn't get out of sync
input = self.console.testcase.testcases[i],
input = testcases[i],
output = item.code_answer[i],
expected = item.expected_code_answer[i],
std_output = item.std_output_list[i],
Expand Down
45 changes: 30 additions & 15 deletions lua/leetcode-ui/popup/console/testcase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,53 @@ local t = require("leetcode.translator")

---@class lc.ui.Console.TestcasePopup : lc.ui.Console.Popup
---@field testcases table<string[]>
---@field testcase_len integer
---@field extmarks integer[]
---@field snapshots table<string, string[]>
local Testcase = ConsolePopup:extend("LeetTestcasePopup")

function Testcase:content()
self.testcases = {}
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
lines = vim.tbl_filter(function(line) return line ~= "" end, lines)
return table.concat(lines, "\n")
end

function Testcase:snapshot(id, data) --
if not data.test_case then return end
self.testcases[id] = data.test_case
end

---@return string[][]
function Testcase:by_id(id) --
local testcases = {} ---@type string[][]

local tbl = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
local str = table.concat(tbl, "\n")
local i, n = 0, self.testcase_len
for case in vim.gsplit(self.testcases[id] or "", "\n") do
local j = math.floor(i / n) + 1

local testcases = {}
for tcase in vim.gsplit(str, "\n\n") do
local s = vim.split(tcase, "\n")
if not testcases[j] then testcases[j] = {} end
table.insert(testcases[j], case)

table.insert(self.testcases, s)
testcases = vim.list_extend(testcases, s)
i = i + 1
end

return testcases
end

function Testcase:populate()
local tbl = {}
for i, case in ipairs(self.console.question.q.testcase_list) do
if i ~= 1 then table.insert(tbl, "") end
local lines = {}

table.insert(self.testcases, vim.split(case, "\n"))
local t_list = self.console.question.q.testcase_list
self.testcase_len = #vim.split(t_list[1] or "", "\n")

for i, case in ipairs(t_list) do
if i ~= 1 then table.insert(lines, "") end
for s in vim.gsplit(case, "\n", { trimempty = true }) do
table.insert(tbl, s)
table.insert(lines, s)
end
end

vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, tbl)

vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, lines)
return self:draw_extmarks()
end

Expand Down Expand Up @@ -140,6 +153,7 @@ function Testcase:mount()

self.testcases = {}
self.extmarks = {}
self.testcase_len = 0

self:autocmds()
self:populate()
Expand Down Expand Up @@ -172,6 +186,7 @@ function Testcase:init(parent)

self.testcases = {}
self.extmarks = {}
self.testcase_len = 0

self:populate()
end
Expand Down
3 changes: 2 additions & 1 deletion lua/leetcode/api/interpreter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ function interpreter.run(submit, q, body, callback)
end
callback(nil, nil, err)
else
q.console.result:clear()
local id = submit and res.submission_id or res.interpret_id
q.console.testcase:snapshot(id, res)
q.console.result:clear()
interpreter.listener(id, callback)
end
end,
Expand Down
9 changes: 8 additions & 1 deletion lua/leetcode/logger/spinner/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function spinner:spin()

self.index = (self.index + 1) % #stype.frames

local fps = 1000 / #stype.frames
local fps = math.floor(1000 / #stype.frames)
vim.defer_fn(function() self:spin() end, fps)
end

Expand All @@ -63,6 +63,9 @@ function spinner:set(msg, lvl, opts)
if msg then self:update(msg) end
lvl = lvl or vim.log.levels.INFO

local log = require("leetcode.logger")
log.debug(self.noti)

opts = vim.tbl_deep_extend("force", {
hide_from_history = true,
title = config.name,
Expand Down Expand Up @@ -97,6 +100,10 @@ function spinner:stop(msg, success, opts)

self.spinner = nil
local lvl = vim.log.levels[success and "INFO" or "ERROR"]

local log = require("leetcode.logger")
log.debug({ noti = self.noti, msg = "spinner stop" })

self:set(msg, lvl, opts)
end

Expand Down
2 changes: 1 addition & 1 deletion lua/leetcode/runner/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function Runner:handle(submit)
lang = question.lang,
typed_code = self.question:lines(submit),
question_id = question.q.id,
data_input = not submit and table.concat(question.console.testcase:content(), "\n") or nil,
data_input = not submit and question.console.testcase:content(),
}

local judge = Judge:init()
Expand Down

0 comments on commit 69e6b41

Please sign in to comment.