Skip to content

Commit 4be584b

Browse files
committed
extract replace file placeholder
1 parent aa003f8 commit 4be584b

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

lua/opencode.lua

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local M = {}
22

33
local config = require("opencode.config")
44
local terminal = require("opencode.terminal")
5+
local placeholders = require("opencode.placeholders")
56

67
--@param opts opencode.Config
78
function M.setup(opts)
@@ -15,19 +16,6 @@ end
1516
function M.ask(opts)
1617
local mode = vim.fn.mode()
1718

18-
local function replace_file_placeholder(input)
19-
-- Replace @file with the current file path
20-
if input:find("@file") then
21-
local relative_path = vim.fn.expand("%:.")
22-
if relative_path == "" then
23-
vim.notify("No file is currently open.", vim.log.levels.WARN)
24-
return input
25-
end
26-
return input:gsub("@file", "@" .. relative_path)
27-
end
28-
return input
29-
end
30-
3119
-- Visual mode handling
3220
if vim.tbl_contains({ "v", "V", "" }, mode) then
3321
local lines = vim.fn.getregion(vim.fn.getpos("v"), vim.fn.getpos("."), { type = mode })
@@ -36,7 +24,7 @@ function M.ask(opts)
3624
vim.ui.input({ prompt = "Add a prompt to your selection (empty to skip):" }, function(input)
3725
if input ~= nil then
3826
if input ~= "" then
39-
selected_text = selected_text .. "\n\n" .. replace_file_placeholder(input)
27+
selected_text = selected_text .. "\n\n" .. placeholders.replace_file(input)
4028
end
4129
terminal.send(selected_text, opts or {}, true)
4230
end
@@ -45,7 +33,7 @@ function M.ask(opts)
4533
-- Normal mode handling
4634
vim.ui.input({ prompt = "Ask opencode: " }, function(input)
4735
if input then
48-
terminal.send(replace_file_placeholder(input), opts or {})
36+
terminal.send(placeholders.replace_file(input), opts or {})
4937
end
5038
end)
5139
end

lua/opencode/placeholders.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
local M = {}
2+
3+
function M.replace_file(input)
4+
-- Replace @file with the current file path
5+
if input:find("@file") then
6+
local relative_path = vim.fn.expand("%:.")
7+
if relative_path == "" then
8+
vim.notify("No file is currently open.", vim.log.levels.WARN)
9+
return input
10+
end
11+
return input:gsub("@file", "@" .. relative_path)
12+
end
13+
return input
14+
end
15+
16+
return M

0 commit comments

Comments
 (0)