|
| 1 | +local M = {} |
| 2 | + |
| 3 | +local Output = require("opencode.ui.output") |
| 4 | +local formatter = require("opencode.ui.formatter") |
| 5 | +local output_window = require("opencode.ui.output_window") |
| 6 | +local util = require("opencode.util") |
| 7 | + |
| 8 | +---Format diff content with proper syntax highlighting |
| 9 | +---@param content string The diff content |
| 10 | +---@param file_type string The file type for syntax highlighting |
| 11 | +---@return Output |
| 12 | +local function format_diff_content(content, file_type) |
| 13 | + local output = Output.new() |
| 14 | + |
| 15 | + -- Format the diff with proper syntax highlighting |
| 16 | + formatter._format_diff(output, content, file_type) |
| 17 | + |
| 18 | + return output |
| 19 | +end |
| 20 | + |
| 21 | +---Render formatted data to a buffer |
| 22 | +---@param bufid integer Buffer ID |
| 23 | +---@param output Output |
| 24 | +local function render_to_buffer(bufid, output) |
| 25 | + -- Set lines |
| 26 | + output_window.set_lines(bufid, output.lines, 0, -1) |
| 27 | + |
| 28 | + -- Apply extmarks for syntax highlighting |
| 29 | + local extmarks = output.extmarks |
| 30 | + if extmarks then |
| 31 | + output_window.set_extmarks(bufid, extmarks, 0) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +---@param event table |
| 36 | +---@param on_choice? fun(choice?: string) |
| 37 | +function M.confirm(event, on_choice) |
| 38 | + local title = "Permit opencode to: " |
| 39 | + .. event.properties.permission |
| 40 | + .. " " |
| 41 | + .. table.concat(event.properties.patterns, ", ") |
| 42 | + .. "?" |
| 43 | + local content = event.properties.metadata.diff |
| 44 | + local file_type = util.get_markdown_filetype(event.properties.metadata.filepath or event.properties.patterns[1]) |
| 45 | + content = format_diff_content(content, file_type) |
| 46 | + M._confirm(title, content, file_type, on_choice) |
| 47 | +end |
| 48 | + |
| 49 | +---@param title string |
| 50 | +---@param content string|Output |
| 51 | +---@param file_type string |
| 52 | +---@param on_choice? fun(choice?: string) |
| 53 | +function M._confirm(title, content, file_type, on_choice) |
| 54 | + -- Format the diff content with syntax highlighting |
| 55 | + |
| 56 | + local output = content |
| 57 | + if type(content) == "string" then |
| 58 | + output = Output.new() |
| 59 | + output:add_line("`````" .. file_type) |
| 60 | + output:add_lines(vim.split(content, "\n", { plain = true })) |
| 61 | + output:add_line("`````") |
| 62 | + end |
| 63 | + |
| 64 | + local win_config = require("opencode.config").opts.events.permissions.confirm.window.config |
| 65 | + if type(win_config) == "function" then |
| 66 | + win_config = win_config() |
| 67 | + end |
| 68 | + local win_options = require("opencode.config").opts.events.permissions.confirm.window.options |
| 69 | + |
| 70 | + -- Build dynamic footer from mappings |
| 71 | + local mappings = require("opencode.config").opts.events.permissions.confirm.window.mappings |
| 72 | + local footer = {} |
| 73 | + local seen_actions = {} |
| 74 | + for key, action in pairs(mappings) do |
| 75 | + if type(action) == "string" and action ~= "close" then |
| 76 | + local action_lower = action:lower() |
| 77 | + if not seen_actions[action_lower] then |
| 78 | + seen_actions[action_lower] = {} |
| 79 | + table.insert(seen_actions[action_lower], key) |
| 80 | + else |
| 81 | + table.insert(seen_actions[action_lower], key) |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + -- Sort actions in desired order and build footer |
| 87 | + local action_order = { "once", "always", "reject" } |
| 88 | + for _, action in ipairs(action_order) do |
| 89 | + if seen_actions[action] then |
| 90 | + -- Sort keys to ensure consistent display order |
| 91 | + table.sort(seen_actions[action]) |
| 92 | + local keys = table.concat(seen_actions[action], "/") |
| 93 | + table.insert(footer, { " " .. keys .. " ", "Title" }) |
| 94 | + table.insert(footer, { "- " .. action:sub(1, 1):upper() .. action:sub(2) .. " ", "Comment" }) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + local bufid, winid = util.create_scratch_floatwin( |
| 99 | + title, |
| 100 | + vim.tbl_deep_extend("force", { |
| 101 | + footer = footer, |
| 102 | + footer_pos = "center", |
| 103 | + }, win_config) |
| 104 | + ) |
| 105 | + |
| 106 | + ---@cast output Output |
| 107 | + render_to_buffer(bufid, output) |
| 108 | + |
| 109 | + vim.bo.modifiable = false |
| 110 | + -- Set filetype to enable syntax highlighting |
| 111 | + vim.bo.filetype = "markdown" |
| 112 | + |
| 113 | + for option, value in pairs(win_options) do |
| 114 | + vim.api.nvim_set_option_value(option, value, { scope = "local", win = winid }) |
| 115 | + end |
| 116 | + |
| 117 | + local done = false |
| 118 | + |
| 119 | + vim.api.nvim_create_autocmd("BufWipeout", { |
| 120 | + buffer = bufid, |
| 121 | + callback = function() |
| 122 | + if not done then |
| 123 | + if on_choice then |
| 124 | + on_choice() |
| 125 | + end |
| 126 | + end |
| 127 | + end, |
| 128 | + }) |
| 129 | + |
| 130 | + local function finish(choice) |
| 131 | + if on_choice then |
| 132 | + on_choice(choice) |
| 133 | + end |
| 134 | + done = true |
| 135 | + vim.api.nvim_win_close(winid, false) |
| 136 | + end |
| 137 | + |
| 138 | + local function close_window() |
| 139 | + done = true |
| 140 | + vim.api.nvim_win_close(winid, false) |
| 141 | + end |
| 142 | + |
| 143 | + for key, action in pairs(mappings) do |
| 144 | + if type(action) == "string" then |
| 145 | + local action_lower = action:lower() |
| 146 | + if action_lower == "close" then |
| 147 | + -- Close window without calling callback |
| 148 | + vim.keymap.set("n", key, close_window, { buffer = bufid, remap = false, nowait = true }) |
| 149 | + else |
| 150 | + -- Pass the action (once/always/reject) to the callback |
| 151 | + vim.keymap.set("n", key, function() |
| 152 | + finish(action_lower) |
| 153 | + end, { buffer = bufid, remap = false, nowait = true }) |
| 154 | + end |
| 155 | + end |
| 156 | + end |
| 157 | +end |
| 158 | + |
| 159 | +return M |
0 commit comments