mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-14 20:21:39 +03:00
fix(core): stop custom commands from overwriting $0 and re-filling $ARGUMENTS you typed (#59198)
This commit is contained in:
committed by
GitHub
Unverified
parent
0ca7885f26
commit
9f96a331e3
@@ -193,14 +193,14 @@ function evaluateTemplate(
|
||||
const args = parseArguments(input)
|
||||
const placeholders = template.match(placeholderRegex) ?? []
|
||||
const last = Math.max(0, ...placeholders.map((item) => Number(item.slice(1))))
|
||||
const expanded = template.replaceAll(placeholderRegex, (_, index) => {
|
||||
const withArguments = template.replaceAll(argumentRegex, (match, index) => {
|
||||
if (match === "$ARGUMENTS") return input
|
||||
const position = Number(index)
|
||||
const argIndex = position - 1
|
||||
if (argIndex >= args.length) return ""
|
||||
if (position === last) return args.slice(argIndex).join(" ")
|
||||
return args[argIndex]
|
||||
})
|
||||
const withArguments = expanded.replaceAll("$ARGUMENTS", () => input)
|
||||
const text =
|
||||
placeholders.length === 0 && !template.includes("$ARGUMENTS") && input.trim()
|
||||
? `${withArguments}\n\n${input}`.trim()
|
||||
@@ -239,6 +239,7 @@ function parseArguments(input: string) {
|
||||
}
|
||||
|
||||
const argsRegex = /(?:\[Image\s+\d+\]|"[^"]*"|'[^']*'|[^\s"']+)/gi
|
||||
const placeholderRegex = /\$(\d+)/g
|
||||
const placeholderRegex = /\$([1-9]\d*)/g
|
||||
const argumentRegex = /\$(?:ARGUMENTS|([1-9]\d*))/g
|
||||
const quoteTrimRegex = /^["']|["']$/g
|
||||
const shellRegex = /!`([^`]+)`/g
|
||||
|
||||
@@ -89,6 +89,21 @@ describe("ConfigCommandPlugin.Plugin", () => {
|
||||
input: '"alpha beta" gamma delta',
|
||||
expected: "First alpha beta. Rest gamma delta.",
|
||||
},
|
||||
{
|
||||
template: "Run awk '{print $0}' on $1.",
|
||||
input: "app.log",
|
||||
expected: "Run awk '{print $0}' on app.log.",
|
||||
},
|
||||
{
|
||||
template: "Run awk '{print $0}'.",
|
||||
input: "app.log",
|
||||
expected: "Run awk '{print $0}'.\n\napp.log",
|
||||
},
|
||||
{
|
||||
template: "First $1. Rest $2.",
|
||||
input: '"$ARGUMENTS" tail',
|
||||
expected: "First $ARGUMENTS. Rest tail.",
|
||||
},
|
||||
{ template: "$ARGUMENTS / $ARGUMENTS", input: "$& $$", expected: "$& $$ / $& $$" },
|
||||
]) {
|
||||
it.live(`interpolates ${JSON.stringify(item.template)} with literal input ${JSON.stringify(item.input)}`, () =>
|
||||
|
||||
Reference in New Issue
Block a user