Skip to content

Commit c6adef8

Browse files
committed
fix(prompts): escape prompt args during substitution
1 parent b38fc16 commit c6adef8

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

lua/neocrush/prompts.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ function M.execute(name, args)
8383
if args and args ~= '' then
8484
-- Substitute %s with arguments, or append if no %s
8585
if template:find '%%s' then
86-
text = template:gsub('%%s', args)
86+
text = template:gsub('%%s', function()
87+
return args
88+
end)
8789
else
8890
text = template .. ' ' .. args
8991
end

tests/prompts_spec.lua

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,53 @@ describe('neocrush.prompts', function()
106106
end)
107107

108108
describe('execute', function()
109-
-- Note: execute() calls terminal functions which we can't easily test
110-
-- without mocking. Test the error case for unknown prompts.
109+
it('should substitute prompt arguments', function()
110+
prompts.register {
111+
Review = 'review PR #%s',
112+
}
113+
local sent
114+
local original_send = prompts.send
115+
prompts.send = function(text)
116+
sent = text
117+
end
118+
119+
prompts.execute('Review', '12')
120+
121+
prompts.send = original_send
122+
eq('review PR #12', sent)
123+
end)
124+
125+
it('should substitute prompt arguments containing percent signs', function()
126+
prompts.register {
127+
Note = 'summarize %s',
128+
}
129+
local sent
130+
local original_send = prompts.send
131+
prompts.send = function(text)
132+
sent = text
133+
end
134+
135+
prompts.execute('Note', '50% done')
136+
137+
prompts.send = original_send
138+
eq('summarize 50% done', sent)
139+
end)
140+
141+
it('should append arguments when template has no placeholder', function()
142+
prompts.register {
143+
Simple = 'explain',
144+
}
145+
local sent
146+
local original_send = prompts.send
147+
prompts.send = function(text)
148+
sent = text
149+
end
150+
151+
prompts.execute('Simple', 'this code')
152+
153+
prompts.send = original_send
154+
eq('explain this code', sent)
155+
end)
111156

112157
it('should error for unknown prompt', function()
113158
local notified = false

0 commit comments

Comments
 (0)