feat: add 'location' config option

This commit is contained in:
2026-06-16 21:11:20 +02:00
parent 21d75ab601
commit 95dd54336a
3 changed files with 61 additions and 11 deletions
+39 -4
View File
@@ -21,6 +21,14 @@ local state = {
local config = {
width = 0.3,
location = "right",
}
local valid_locations = {
left = true,
right = true,
top = true,
bottom = true,
}
local function is_valid_window(win)
@@ -35,10 +43,28 @@ local function terminal_width()
return math.max(1, math.floor(vim.o.columns * config.width))
end
local function open_side_window()
vim.cmd("botright vertical " .. terminal_width() .. "new")
local function terminal_height()
return math.max(1, math.floor(vim.o.lines * config.width))
end
local function open_terminal_window()
if config.location == "left" then
vim.cmd("topleft vertical " .. terminal_width() .. "new")
elseif config.location == "right" then
vim.cmd("botright vertical " .. terminal_width() .. "new")
elseif config.location == "top" then
vim.cmd("topleft " .. terminal_height() .. "new")
else
vim.cmd("botright " .. terminal_height() .. "new")
end
state.win = vim.api.nvim_get_current_win()
vim.api.nvim_set_option_value("winfixwidth", true, { win = state.win })
if config.location == "left" or config.location == "right" then
vim.api.nvim_set_option_value("winfixwidth", true, { win = state.win })
else
vim.api.nvim_set_option_value("winfixheight", true, { win = state.win })
end
end
local function start_terminal()
@@ -62,7 +88,7 @@ function M.toggle()
return
end
open_side_window()
open_terminal_window()
if is_valid_buffer(state.buf) then
vim.api.nvim_win_set_buf(state.win, state.buf)
@@ -85,6 +111,15 @@ function M.setup(opts)
config.width = opts.width
end
if opts.location ~= nil then
assert(
type(opts.location) == "string" and valid_locations[opts.location],
"opencode.nvim: location must be one of: left, right, top, bottom"
)
config.location = opts.location
end
vim.api.nvim_create_user_command("OpenCodeToggle", function()
M.toggle()
end, { force = true })