diff --git a/lua/cp/cache.lua b/lua/cp/cache.lua index 18ce0d3..18b75f7 100644 --- a/lua/cp/cache.lua +++ b/lua/cp/cache.lua @@ -31,6 +31,7 @@ local M = {} +local logger = require('cp.log') local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json' local cache_data = {} local loaded = false @@ -41,7 +42,7 @@ function M.load() end if vim.fn.filereadable(cache_file) == 0 then - cache_data = {} + vim.fn.writefile({}, cache_file) loaded = true return end @@ -57,28 +58,19 @@ function M.load() if ok then cache_data = decoded else - cache_data = {} + logger.log('Could not decode json in cache file', vim.log.levels.ERROR) end loaded = true end function M.save() - local ok, _ = pcall(vim.fn.mkdir, vim.fn.fnamemodify(cache_file, ':h'), 'p') - if not ok then - vim.schedule(function() - vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p') - end) - return - end + vim.schedule(function() + vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p') - local encoded = vim.json.encode(cache_data) - local lines = vim.split(encoded, '\n') - local write_ok, _ = pcall(vim.fn.writefile, lines, cache_file) - if not write_ok then - vim.schedule(function() - vim.fn.writefile(lines, cache_file) - end) - end + local encoded = vim.json.encode(cache_data) + local lines = vim.split(encoded, '\n') + vim.fn.writefile(lines, cache_file) + end) end ---@param platform string diff --git a/lua/cp/commands/cache.lua b/lua/cp/commands/cache.lua index 892ddf3..23135aa 100644 --- a/lua/cp/commands/cache.lua +++ b/lua/cp/commands/cache.lua @@ -7,7 +7,6 @@ local logger = require('cp.log') local platforms = constants.PLATFORMS function M.handle_cache_command(cmd) - cmd.platform = cmd.platform:lower() if cmd.subcommand == 'clear' then cache.load() if cmd.platform then diff --git a/lua/cp/scraper.lua b/lua/cp/scraper.lua index 38b1aff..d560039 100644 --- a/lua/cp/scraper.lua +++ b/lua/cp/scraper.lua @@ -128,11 +128,10 @@ function M.scrape_problem_tests(platform, contest_id, problem_id, callback) expected_file ) end + if type(callback) == 'function' then + callback(result.data) + end end) - - if type(callback) == 'function' then - callback(result.data) - end end, }) end diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index faab15e..d50c958 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -45,7 +45,25 @@ local function scrape_contest_problems(platform, contest_id, problems) end for _, prob in ipairs(missing_problems) do - scraper.scrape_problem_tests(platform, contest_id, prob.id) + scraper.scrape_problem_tests(platform, contest_id, prob.id, function(result) + local cached_tests = {} + for i, test_case in ipairs(result.tests) do + table.insert(cached_tests, { + index = i, + input = test_case.input, + expected = test_case.expected, + }) + end + + cache.set_test_cases( + platform, + contest_id, + state.get_problem_id(), + cached_tests, + result.timeout_ms, + result.memory_mb + ) + end) end end @@ -63,12 +81,15 @@ function M.setup_contest(platform, contest_id, problem_id, language) end state.set_contest_id(contest_id) + -- TODO: should check cache here, & other uses of gt_contest_data validate them logger.log('Fetching contests problems...', vim.log.levels.INFO, true) scraper.scrape_contest_metadata(platform, contest_id, function(result) local problems = result.problems - logger.log(('found %d problems'):format(#problems)) + cache.set_contest_data(platform, contest_id, problems) + + logger.log(('Found %d problems for %s contest %s'):format(#problems, platform, contest_id)) local target_problem = problem_id or problems[1].id @@ -166,7 +187,12 @@ function M.navigate_problem(direction, language) return end - M.setup_problem(contest_id, problems[new_index].id, language) + local cp = require('cp') + local args = { platform, contest_id, problems[new_index].id } + if language then + vim.list_extend(args, { '--lang', language }) + end + cp.handle_command({ fargs = args }) end return M diff --git a/lua/cp/ui/panel.lua b/lua/cp/ui/panel.lua index fd39061..c0da5df 100644 --- a/lua/cp/ui/panel.lua +++ b/lua/cp/ui/panel.lua @@ -62,6 +62,7 @@ function M.toggle_interactive() local cache = require('cp.cache') cache.load() local contest_data = cache.get_contest_data(platform, contest_id) + vim.print('checking cache - contes_data (DLETE ME): ', contest_data) if contest_data and not contest_data.interactive then logger.log( 'This is NOT an interactive problem. Use :CP run instead - aborting.',