feat: add tiling
This commit is contained in:
parent
244839da2e
commit
7d3d1487b4
4 changed files with 62 additions and 27 deletions
16
doc/cp.txt
16
doc/cp.txt
|
|
@ -84,6 +84,14 @@ example, with lazy.nvim (https://github.com/folke/lazy.nvim):
|
|||
...
|
||||
end
|
||||
},
|
||||
tile = function(source_buf, input_buf, output_buf)
|
||||
vim.api.nvim_set_current_buf(source_buf)
|
||||
vim.cmd.vsplit()
|
||||
vim.api.nvim_set_current_buf(output_buf)
|
||||
vim.cmd.vsplit()
|
||||
vim.api.nvim_set_current_buf(input_buf)
|
||||
vim.cmd('wincmd h | wincmd h')
|
||||
end,
|
||||
})
|
||||
end
|
||||
}
|
||||
|
|
@ -101,11 +109,19 @@ snippets LuaSnip snippets by contest type
|
|||
|
||||
hooks Functions called at specific events
|
||||
before_run Called before :CP run
|
||||
function(problem_id)
|
||||
(default: nil, do nothing)
|
||||
before_debug Called before :CP debug
|
||||
function(problem_id)
|
||||
(default: nil, do nothing)
|
||||
|
||||
debug Show info messages during operation
|
||||
(default: false, silent operation)
|
||||
|
||||
tile Custom function to arrange windows
|
||||
function(source_buf, input_buf, output_buf)
|
||||
(default: nil, uses built-in layout)
|
||||
|
||||
WORKFLOW *cp-workflow*
|
||||
|
||||
1. Set up contest environment: >
|
||||
|
|
|
|||
|
|
@ -2,6 +2,8 @@
|
|||
---@field contests table
|
||||
---@field snippets table
|
||||
---@field hooks table
|
||||
---@field debug boolean
|
||||
---@field tile? fun(source_buf: number, input_buf: number, output_buf: number)
|
||||
|
||||
local M = {}
|
||||
|
||||
|
|
@ -30,6 +32,7 @@ M.defaults = {
|
|||
before_debug = nil,
|
||||
},
|
||||
debug = false,
|
||||
tile = nil,
|
||||
}
|
||||
|
||||
---@param base_config table
|
||||
|
|
@ -58,6 +61,7 @@ function M.setup(user_config)
|
|||
snippets = { user_config.snippets, { "table", "nil" }, true },
|
||||
hooks = { user_config.hooks, { "table", "nil" }, true },
|
||||
debug = { user_config.debug, { "boolean", "nil" }, true },
|
||||
tile = { user_config.tile, { "function", "nil" }, true },
|
||||
})
|
||||
|
||||
if user_config.hooks then
|
||||
|
|
|
|||
|
|
@ -98,19 +98,15 @@ local function setup_problem(problem_id, problem_letter)
|
|||
vim.diagnostic.enable(false)
|
||||
|
||||
local base_fp = vim.fn.fnamemodify(filename, ":p:h")
|
||||
local input = ("%s/io/%s.in"):format(base_fp, full_problem_id)
|
||||
local output = ("%s/io/%s.out"):format(base_fp, full_problem_id)
|
||||
local input_file = ("%s/io/%s.in"):format(base_fp, full_problem_id)
|
||||
local output_file = ("%s/io/%s.out"):format(base_fp, full_problem_id)
|
||||
|
||||
vim.cmd.vsplit(output)
|
||||
vim.cmd.w()
|
||||
vim.bo.filetype = "cp"
|
||||
window.clearcol()
|
||||
vim.cmd(("vertical resize %d"):format(math.floor(vim.o.columns * 0.3)))
|
||||
vim.cmd.split(input)
|
||||
vim.cmd.w()
|
||||
vim.bo.filetype = "cp"
|
||||
window.clearcol()
|
||||
vim.cmd.wincmd("h")
|
||||
local source_buf = vim.api.nvim_get_current_buf()
|
||||
local input_buf = vim.fn.bufnr(input_file, true)
|
||||
local output_buf = vim.fn.bufnr(output_file, true)
|
||||
|
||||
local tile_fn = config.tile or window.default_tile
|
||||
tile_fn(source_buf, input_buf, output_buf)
|
||||
|
||||
logger.log(("switched to problem %s"):format(full_problem_id))
|
||||
end
|
||||
|
|
@ -172,7 +168,8 @@ end
|
|||
|
||||
local function diff_problem()
|
||||
if vim.g.cp_diff_mode then
|
||||
window.restore_layout(vim.g.cp_saved_layout)
|
||||
local tile_fn = config.tile or window.default_tile
|
||||
window.restore_layout(vim.g.cp_saved_layout, tile_fn)
|
||||
vim.g.cp_diff_mode = false
|
||||
vim.g.cp_saved_layout = nil
|
||||
logger.log("exited diff mode")
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ function M.clearcol()
|
|||
vim.api.nvim_set_option_value("relativenumber", false, { scope = "local" })
|
||||
vim.api.nvim_set_option_value("statuscolumn", "", { scope = "local" })
|
||||
vim.api.nvim_set_option_value("signcolumn", "no", { scope = "local" })
|
||||
vim.api.nvim_set_option_value("equalalways", false, { scope = "global" })
|
||||
vim.api.nvim_set_option_value("foldcolumn", "0", { scope = "local" })
|
||||
end
|
||||
|
||||
function M.save_layout()
|
||||
|
|
@ -29,7 +29,7 @@ function M.save_layout()
|
|||
}
|
||||
end
|
||||
|
||||
function M.restore_layout(state)
|
||||
function M.restore_layout(state, tile_fn)
|
||||
if not state then
|
||||
return
|
||||
end
|
||||
|
|
@ -53,19 +53,20 @@ function M.restore_layout(state)
|
|||
vim.cmd("silent only")
|
||||
|
||||
local base_fp = vim.fn.getcwd()
|
||||
local input = ("%s/io/%s.in"):format(base_fp, problem_id)
|
||||
local output = ("%s/io/%s.out"):format(base_fp, problem_id)
|
||||
local source = problem_id .. ".cc"
|
||||
local input_file = ("%s/io/%s.in"):format(base_fp, problem_id)
|
||||
local output_file = ("%s/io/%s.out"):format(base_fp, problem_id)
|
||||
local source_file = problem_id .. ".cc"
|
||||
|
||||
vim.cmd.edit(source)
|
||||
vim.cmd.vsplit(output)
|
||||
vim.bo.filetype = "cp"
|
||||
M.clearcol()
|
||||
vim.cmd(("vertical resize %d"):format(math.floor(vim.o.columns * 0.3)))
|
||||
vim.cmd.split(input)
|
||||
vim.bo.filetype = "cp"
|
||||
M.clearcol()
|
||||
vim.cmd.wincmd("h")
|
||||
vim.cmd.edit(source_file)
|
||||
local source_buf = vim.api.nvim_get_current_buf()
|
||||
local input_buf = vim.fn.bufnr(input_file, true)
|
||||
local output_buf = vim.fn.bufnr(output_file, true)
|
||||
|
||||
if tile_fn then
|
||||
tile_fn(source_buf, input_buf, output_buf)
|
||||
else
|
||||
default_tile(source_buf, input_buf, output_buf)
|
||||
end
|
||||
else
|
||||
vim.cmd(state.layout)
|
||||
|
||||
|
|
@ -107,7 +108,24 @@ function M.setup_diff_layout(actual_output, expected_output, input_file)
|
|||
vim.cmd(("botright split %s"):format(input_file))
|
||||
vim.bo.filetype = "cp"
|
||||
M.clearcol()
|
||||
vim.cmd(("resize %d"):format(math.floor(vim.o.lines * 0.3)))
|
||||
vim.cmd.wincmd("k")
|
||||
end
|
||||
|
||||
local function default_tile(source_buf, input_buf, output_buf)
|
||||
vim.api.nvim_set_current_buf(source_buf)
|
||||
vim.cmd.vsplit()
|
||||
vim.api.nvim_set_current_buf(output_buf)
|
||||
vim.bo.filetype = "cp"
|
||||
M.clearcol()
|
||||
vim.cmd(("vertical resize %d"):format(math.floor(vim.o.columns * 0.3)))
|
||||
vim.cmd.split()
|
||||
vim.api.nvim_set_current_buf(input_buf)
|
||||
vim.bo.filetype = "cp"
|
||||
M.clearcol()
|
||||
vim.cmd.wincmd("h")
|
||||
end
|
||||
|
||||
M.default_tile = default_tile
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue