Skip to content

Commit fb26308

Browse files
chore: generate
1 parent b41fa8e commit fb26308

3 files changed

Lines changed: 64 additions & 60 deletions

File tree

packages/opencode/src/tool/edit.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,50 +70,10 @@ export const EditTool = Tool.defineEffect(
7070
let contentOld = ""
7171
let contentNew = ""
7272
yield* filetime.withLock(filePath, async () => {
73-
if (params.oldString === "") {
74-
const existed = await Filesystem.exists(filePath)
75-
contentNew = params.newString
76-
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
77-
await ctx.ask({
78-
permission: "edit",
79-
patterns: [path.relative(Instance.worktree, filePath)],
80-
always: ["*"],
81-
metadata: {
82-
filepath: filePath,
83-
diff,
84-
},
85-
})
86-
await Filesystem.write(filePath, params.newString)
87-
await Format.file(filePath)
88-
Bus.publish(File.Event.Edited, { file: filePath })
89-
await Bus.publish(FileWatcher.Event.Updated, {
90-
file: filePath,
91-
event: existed ? "change" : "add",
92-
})
93-
await FileTime.read(ctx.sessionID, filePath)
94-
return
95-
}
96-
97-
const stats = Filesystem.stat(filePath)
98-
if (!stats) throw new Error(`File ${filePath} not found`)
99-
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
100-
await FileTime.assert(ctx.sessionID, filePath)
101-
contentOld = await Filesystem.readText(filePath)
102-
103-
const ending = detectLineEnding(contentOld)
104-
const old = convertToLineEnding(normalizeLineEndings(params.oldString), ending)
105-
const next = convertToLineEnding(normalizeLineEndings(params.newString), ending)
106-
107-
contentNew = replace(contentOld, old, next, params.replaceAll)
108-
109-
diff = trimDiff(
110-
createTwoFilesPatch(
111-
filePath,
112-
filePath,
113-
normalizeLineEndings(contentOld),
114-
normalizeLineEndings(contentNew),
115-
),
116-
)
73+
if (params.oldString === "") {
74+
const existed = await Filesystem.exists(filePath)
75+
contentNew = params.newString
76+
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
11777
await ctx.ask({
11878
permission: "edit",
11979
patterns: [path.relative(Instance.worktree, filePath)],
@@ -123,25 +83,65 @@ export const EditTool = Tool.defineEffect(
12383
diff,
12484
},
12585
})
126-
127-
await Filesystem.write(filePath, contentNew)
86+
await Filesystem.write(filePath, params.newString)
12887
await Format.file(filePath)
12988
Bus.publish(File.Event.Edited, { file: filePath })
13089
await Bus.publish(FileWatcher.Event.Updated, {
13190
file: filePath,
132-
event: "change",
91+
event: existed ? "change" : "add",
13392
})
134-
contentNew = await Filesystem.readText(filePath)
135-
diff = trimDiff(
136-
createTwoFilesPatch(
137-
filePath,
138-
filePath,
139-
normalizeLineEndings(contentOld),
140-
normalizeLineEndings(contentNew),
141-
),
142-
)
14393
await FileTime.read(ctx.sessionID, filePath)
94+
return
95+
}
96+
97+
const stats = Filesystem.stat(filePath)
98+
if (!stats) throw new Error(`File ${filePath} not found`)
99+
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
100+
await FileTime.assert(ctx.sessionID, filePath)
101+
contentOld = await Filesystem.readText(filePath)
102+
103+
const ending = detectLineEnding(contentOld)
104+
const old = convertToLineEnding(normalizeLineEndings(params.oldString), ending)
105+
const next = convertToLineEnding(normalizeLineEndings(params.newString), ending)
106+
107+
contentNew = replace(contentOld, old, next, params.replaceAll)
108+
109+
diff = trimDiff(
110+
createTwoFilesPatch(
111+
filePath,
112+
filePath,
113+
normalizeLineEndings(contentOld),
114+
normalizeLineEndings(contentNew),
115+
),
116+
)
117+
await ctx.ask({
118+
permission: "edit",
119+
patterns: [path.relative(Instance.worktree, filePath)],
120+
always: ["*"],
121+
metadata: {
122+
filepath: filePath,
123+
diff,
124+
},
125+
})
126+
127+
await Filesystem.write(filePath, contentNew)
128+
await Format.file(filePath)
129+
Bus.publish(File.Event.Edited, { file: filePath })
130+
await Bus.publish(FileWatcher.Event.Updated, {
131+
file: filePath,
132+
event: "change",
144133
})
134+
contentNew = await Filesystem.readText(filePath)
135+
diff = trimDiff(
136+
createTwoFilesPatch(
137+
filePath,
138+
filePath,
139+
normalizeLineEndings(contentOld),
140+
normalizeLineEndings(contentNew),
141+
),
142+
)
143+
await FileTime.read(ctx.sessionID, filePath)
144+
})
145145

146146
const filediff: Snapshot.FileDiff = {
147147
file: filePath,

packages/opencode/src/tool/multiedit.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ export const MultiEditTool = Tool.defineEffect(
2727
)
2828
.describe("Array of edit operations to perform sequentially on the file"),
2929
}),
30-
execute: (params: { filePath: string; edits: Array<{ filePath: string; oldString: string; newString: string; replaceAll?: boolean }> }, ctx: Tool.Context) =>
30+
execute: (
31+
params: {
32+
filePath: string
33+
edits: Array<{ filePath: string; oldString: string; newString: string; replaceAll?: boolean }>
34+
},
35+
ctx: Tool.Context,
36+
) =>
3137
Effect.gen(function* () {
3238
const results = []
3339
for (const [, entry] of params.edits.entries()) {

packages/opencode/test/tool/edit.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ async function touch(file: string, time: number) {
2929
await fs.utimes(file, date, date)
3030
}
3131

32-
const runtime = ManagedRuntime.make(
33-
Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer),
34-
)
32+
const runtime = ManagedRuntime.make(Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer))
3533

3634
afterAll(async () => {
3735
await runtime.dispose()

0 commit comments

Comments
 (0)