|
| 1 | +local config = require('opencode.config') |
| 2 | +local state = require('opencode.state') |
| 3 | + |
| 4 | +local M = {} |
| 5 | + |
| 6 | +---@param value number|nil |
| 7 | +---@param total integer |
| 8 | +---@param fallback number |
| 9 | +---@return integer |
| 10 | +local function resolve_dimension(value, total, fallback) |
| 11 | + local resolved = value or fallback |
| 12 | + if resolved > 0 and resolved <= 1 then |
| 13 | + return math.floor(total * resolved) |
| 14 | + end |
| 15 | + return math.floor(resolved) |
| 16 | +end |
| 17 | + |
| 18 | +---@param value number|nil |
| 19 | +---@param total integer |
| 20 | +---@param size integer |
| 21 | +---@return integer |
| 22 | +local function resolve_position(value, total, size) |
| 23 | + if value == nil then |
| 24 | + return math.floor((total - size) / 2) |
| 25 | + end |
| 26 | + if value > 0 and value <= 1 then |
| 27 | + return math.floor(total * value) |
| 28 | + end |
| 29 | + return math.floor(value) |
| 30 | +end |
| 31 | + |
| 32 | +---@param value integer |
| 33 | +---@param min_value integer |
| 34 | +---@param max_value integer |
| 35 | +---@return integer |
| 36 | +local function clamp(value, min_value, max_value) |
| 37 | + return math.min(max_value, math.max(min_value, value)) |
| 38 | +end |
| 39 | + |
| 40 | +---@param windows OpencodeWindowState|nil |
| 41 | +---@return integer |
| 42 | +local function input_height(windows) |
| 43 | + local line_count = 1 |
| 44 | + if windows and windows.input_buf and vim.api.nvim_buf_is_valid(windows.input_buf) then |
| 45 | + line_count = vim.api.nvim_buf_line_count(windows.input_buf) |
| 46 | + end |
| 47 | + |
| 48 | + local min_height = math.max(1, math.floor(vim.o.lines * config.ui.input.min_height)) |
| 49 | + local max_height = math.max(min_height, math.floor(vim.o.lines * config.ui.input.max_height)) |
| 50 | + return clamp(line_count, min_height, max_height) |
| 51 | +end |
| 52 | + |
| 53 | +---@param windows OpencodeWindowState|nil |
| 54 | +---@return vim.api.keyset.win_config |
| 55 | +local function base_config(windows) |
| 56 | + local float = config.ui.float or {} |
| 57 | + local width |
| 58 | + if windows and windows.saved_width_ratio then |
| 59 | + width = math.floor(vim.o.columns * windows.saved_width_ratio) |
| 60 | + windows.saved_width_ratio = nil |
| 61 | + elseif state.pre_zoom_width then |
| 62 | + width = math.floor(vim.o.columns * config.ui.zoom_width) |
| 63 | + else |
| 64 | + width = resolve_dimension(float.width, vim.o.columns, 0.95) |
| 65 | + end |
| 66 | + local height = resolve_dimension(float.height, vim.o.lines, 0.9) |
| 67 | + |
| 68 | + return { |
| 69 | + relative = 'editor', |
| 70 | + width = width, |
| 71 | + height = height, |
| 72 | + row = resolve_position(float.row, vim.o.lines, height), |
| 73 | + col = resolve_position(float.col, vim.o.columns, width), |
| 74 | + style = 'minimal', |
| 75 | + border = float.border, |
| 76 | + } |
| 77 | +end |
| 78 | + |
| 79 | +---@param windows OpencodeWindowState|nil |
| 80 | +---@param show_input boolean |
| 81 | +---@return vim.api.keyset.win_config output_config |
| 82 | +function M.window_configs(windows, show_input) |
| 83 | + local float = config.ui.float or {} |
| 84 | + local base = base_config(windows) |
| 85 | + local prompt_height = show_input and input_height(windows) or 0 |
| 86 | + local gap = show_input and (float.gap or 1) or 0 |
| 87 | + local output_height = math.max(1, base.height - prompt_height - gap) |
| 88 | + |
| 89 | + local output_config = vim.tbl_deep_extend('force', base, { |
| 90 | + height = output_height, |
| 91 | + zindex = float.zindex or 40, |
| 92 | + }) |
| 93 | + |
| 94 | + if not show_input then |
| 95 | + return output_config, nil |
| 96 | + end |
| 97 | + |
| 98 | + local input_config = vim.tbl_deep_extend('force', base, { |
| 99 | + height = prompt_height, |
| 100 | + zindex = (float.zindex or 40) + 1, |
| 101 | + }) |
| 102 | + |
| 103 | + if config.ui.input_position == 'top' then |
| 104 | + output_config.row = base.row + prompt_height + gap |
| 105 | + else |
| 106 | + input_config.row = base.row + output_height + gap |
| 107 | + end |
| 108 | + |
| 109 | + return output_config, input_config |
| 110 | +end |
| 111 | + |
| 112 | +---@param buf integer |
| 113 | +---@param enter boolean |
| 114 | +---@param win_config vim.api.keyset.win_config |
| 115 | +---@return integer |
| 116 | +function M.open_win(buf, enter, win_config) |
| 117 | + local win = vim.api.nvim_open_win(buf, enter, win_config) |
| 118 | + local float = config.ui.float or {} |
| 119 | + for opt, value in pairs(float.opts or {}) do |
| 120 | + pcall(vim.api.nvim_set_option_value, opt, value, { win = win, scope = 'local' }) |
| 121 | + end |
| 122 | + return win |
| 123 | +end |
| 124 | + |
| 125 | +---@param windows OpencodeWindowState|nil |
| 126 | +---@param show_input boolean |
| 127 | +function M.update(windows, show_input) |
| 128 | + if not windows or not windows.output_win or not vim.api.nvim_win_is_valid(windows.output_win) then |
| 129 | + return |
| 130 | + end |
| 131 | + |
| 132 | + local output_config, input_config = M.window_configs(windows, show_input) |
| 133 | + pcall(vim.api.nvim_win_set_config, windows.output_win, output_config) |
| 134 | + |
| 135 | + if show_input and input_config and windows.input_win and vim.api.nvim_win_is_valid(windows.input_win) then |
| 136 | + pcall(vim.api.nvim_win_set_config, windows.input_win, input_config) |
| 137 | + end |
| 138 | + |
| 139 | + if windows.footer_win and vim.api.nvim_win_is_valid(windows.footer_win) then |
| 140 | + require('opencode.ui.footer').update_window(windows) |
| 141 | + end |
| 142 | +end |
| 143 | + |
| 144 | +return M |
0 commit comments