From 14b8bded1d5af0f20817a8e19fa726418e710df5 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 12 Oct 2025 16:39:06 -0400 Subject: [PATCH] fix: buffer name --- lua/cp/setup.lua | 42 ++++++++---------------------------------- lua/cp/state.lua | 7 ++----- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/lua/cp/setup.lua b/lua/cp/setup.lua index 4c5fcae..3d5715e 100644 --- a/lua/cp/setup.lua +++ b/lua/cp/setup.lua @@ -79,7 +79,6 @@ function M.setup_contest(platform, contest_id, problem_id, language) start_tests(platform, contest_id, problems) if contest_data.url and config_module.get_config().open_url then - vim.print('opening') vim.ui.open(contest_data.url) end end @@ -92,20 +91,9 @@ function M.setup_contest(platform, contest_id, problem_id, language) vim.cmd.only({ mods = { silent = true } }) local bufnr = vim.api.nvim_create_buf(true, false) vim.api.nvim_win_set_buf(0, bufnr) - if lang then - vim.bo[bufnr].filetype = lang - end + vim.bo[bufnr].filetype = lang or '' vim.bo[bufnr].buftype = '' - - local ext = cfg.runtime - and cfg.runtime.effective[platform] - and cfg.runtime.effective[platform][lang] - and cfg.runtime.effective[platform][lang].extension - local provisional_name = nil - if ext then - provisional_name = (config_module.default_filename(contest_id) .. '.' .. ext) - vim.api.nvim_buf_set_name(bufnr, provisional_name) - end + vim.bo[bufnr].swapfile = false if cfg.hooks and cfg.hooks.setup_code and not vim.b[bufnr].cp_setup_done then local ok = pcall(cfg.hooks.setup_code, state) @@ -114,16 +102,6 @@ function M.setup_contest(platform, contest_id, problem_id, language) end end - if provisional_name then - cache.set_file_state( - vim.fn.fnamemodify(provisional_name, ':p'), - platform, - contest_id, - '', - lang - ) - end - state.set_provisional({ bufnr = bufnr, platform = platform, @@ -181,18 +159,14 @@ function M.setup_problem(problem_id, language) return end + vim.fn.mkdir(vim.fn.fnamemodify(source_file, ':h'), 'p') + local prov = state.get_provisional() if prov and prov.platform == platform and prov.contest_id == (state.get_contest_id() or '') then if vim.api.nvim_buf_is_valid(prov.bufnr) then - local old = vim.api.nvim_buf_get_name(prov.bufnr) - local new = source_file - if old ~= '' and old ~= new then - local st = vim.loop.fs_stat(old) - if st and st.type == 'file' then - pcall(vim.loop.fs_rename, old, new) - end - end - vim.api.nvim_buf_set_name(prov.bufnr, new) + vim.api.nvim_buf_set_name(prov.bufnr, source_file) + vim.bo[prov.bufnr].swapfile = true + vim.cmd(string.format('silent keepalt noautocmd write! %s', vim.fn.fnameescape(source_file))) if config.hooks and config.hooks.setup_code and not vim.b[prov.bufnr].cp_setup_done then local ok = pcall(config.hooks.setup_code, state) if ok then @@ -200,7 +174,7 @@ function M.setup_problem(problem_id, language) end end cache.set_file_state( - vim.fn.fnamemodify(new, ':p'), + vim.fn.fnamemodify(source_file, ':p'), platform, state.get_contest_id() or '', state.get_problem_id() or '', diff --git a/lua/cp/state.lua b/lua/cp/state.lua index 9172fe4..63613a3 100644 --- a/lua/cp/state.lua +++ b/lua/cp/state.lua @@ -84,11 +84,6 @@ function M.get_base_name() end end ----@return string|nil -function M.get_language() - return -end - ---@param language? string ---@return string|nil function M.get_source_file(language) @@ -103,12 +98,14 @@ function M.get_source_file(language) if not platform_cfg then return nil end + local target_language = language or platform_cfg.default_language local eff = config.runtime.effective[plat] and config.runtime.effective[plat][target_language] or nil if not eff or not eff.extension then return nil end + return base_name .. '.' .. eff.extension end