fix some cachign
This commit is contained in:
parent
a925686a17
commit
e6c09a4897
5 changed files with 42 additions and 25 deletions
|
|
@ -31,6 +31,7 @@
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
local logger = require('cp.log')
|
||||||
local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json'
|
local cache_file = vim.fn.stdpath('data') .. '/cp-nvim.json'
|
||||||
local cache_data = {}
|
local cache_data = {}
|
||||||
local loaded = false
|
local loaded = false
|
||||||
|
|
@ -41,7 +42,7 @@ function M.load()
|
||||||
end
|
end
|
||||||
|
|
||||||
if vim.fn.filereadable(cache_file) == 0 then
|
if vim.fn.filereadable(cache_file) == 0 then
|
||||||
cache_data = {}
|
vim.fn.writefile({}, cache_file)
|
||||||
loaded = true
|
loaded = true
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
@ -57,28 +58,19 @@ function M.load()
|
||||||
if ok then
|
if ok then
|
||||||
cache_data = decoded
|
cache_data = decoded
|
||||||
else
|
else
|
||||||
cache_data = {}
|
logger.log('Could not decode json in cache file', vim.log.levels.ERROR)
|
||||||
end
|
end
|
||||||
loaded = true
|
loaded = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.save()
|
function M.save()
|
||||||
local ok, _ = pcall(vim.fn.mkdir, vim.fn.fnamemodify(cache_file, ':h'), 'p')
|
vim.schedule(function()
|
||||||
if not ok then
|
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p')
|
||||||
vim.schedule(function()
|
|
||||||
vim.fn.mkdir(vim.fn.fnamemodify(cache_file, ':h'), 'p')
|
|
||||||
end)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local encoded = vim.json.encode(cache_data)
|
local encoded = vim.json.encode(cache_data)
|
||||||
local lines = vim.split(encoded, '\n')
|
local lines = vim.split(encoded, '\n')
|
||||||
local write_ok, _ = pcall(vim.fn.writefile, lines, cache_file)
|
vim.fn.writefile(lines, cache_file)
|
||||||
if not write_ok then
|
end)
|
||||||
vim.schedule(function()
|
|
||||||
vim.fn.writefile(lines, cache_file)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param platform string
|
---@param platform string
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ local logger = require('cp.log')
|
||||||
local platforms = constants.PLATFORMS
|
local platforms = constants.PLATFORMS
|
||||||
|
|
||||||
function M.handle_cache_command(cmd)
|
function M.handle_cache_command(cmd)
|
||||||
cmd.platform = cmd.platform:lower()
|
|
||||||
if cmd.subcommand == 'clear' then
|
if cmd.subcommand == 'clear' then
|
||||||
cache.load()
|
cache.load()
|
||||||
if cmd.platform then
|
if cmd.platform then
|
||||||
|
|
|
||||||
|
|
@ -128,11 +128,10 @@ function M.scrape_problem_tests(platform, contest_id, problem_id, callback)
|
||||||
expected_file
|
expected_file
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
if type(callback) == 'function' then
|
||||||
|
callback(result.data)
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
if type(callback) == 'function' then
|
|
||||||
callback(result.data)
|
|
||||||
end
|
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,25 @@ local function scrape_contest_problems(platform, contest_id, problems)
|
||||||
end
|
end
|
||||||
|
|
||||||
for _, prob in ipairs(missing_problems) do
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -63,12 +81,15 @@ function M.setup_contest(platform, contest_id, problem_id, language)
|
||||||
end
|
end
|
||||||
|
|
||||||
state.set_contest_id(contest_id)
|
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)
|
logger.log('Fetching contests problems...', vim.log.levels.INFO, true)
|
||||||
|
|
||||||
scraper.scrape_contest_metadata(platform, contest_id, function(result)
|
scraper.scrape_contest_metadata(platform, contest_id, function(result)
|
||||||
local problems = result.problems
|
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
|
local target_problem = problem_id or problems[1].id
|
||||||
|
|
||||||
|
|
@ -166,7 +187,12 @@ function M.navigate_problem(direction, language)
|
||||||
return
|
return
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ function M.toggle_interactive()
|
||||||
local cache = require('cp.cache')
|
local cache = require('cp.cache')
|
||||||
cache.load()
|
cache.load()
|
||||||
local contest_data = cache.get_contest_data(platform, contest_id)
|
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
|
if contest_data and not contest_data.interactive then
|
||||||
logger.log(
|
logger.log(
|
||||||
'This is NOT an interactive problem. Use :CP run instead - aborting.',
|
'This is NOT an interactive problem. Use :CP run instead - aborting.',
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue