chore: generate
This commit is contained in:
@@ -70,50 +70,10 @@ export const EditTool = Tool.defineEffect(
|
|||||||
let contentOld = ""
|
let contentOld = ""
|
||||||
let contentNew = ""
|
let contentNew = ""
|
||||||
yield* filetime.withLock(filePath, async () => {
|
yield* filetime.withLock(filePath, async () => {
|
||||||
if (params.oldString === "") {
|
if (params.oldString === "") {
|
||||||
const existed = await Filesystem.exists(filePath)
|
const existed = await Filesystem.exists(filePath)
|
||||||
contentNew = params.newString
|
contentNew = params.newString
|
||||||
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
|
diff = trimDiff(createTwoFilesPatch(filePath, filePath, contentOld, contentNew))
|
||||||
await ctx.ask({
|
|
||||||
permission: "edit",
|
|
||||||
patterns: [path.relative(Instance.worktree, filePath)],
|
|
||||||
always: ["*"],
|
|
||||||
metadata: {
|
|
||||||
filepath: filePath,
|
|
||||||
diff,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
await Filesystem.write(filePath, params.newString)
|
|
||||||
await Format.file(filePath)
|
|
||||||
Bus.publish(File.Event.Edited, { file: filePath })
|
|
||||||
await Bus.publish(FileWatcher.Event.Updated, {
|
|
||||||
file: filePath,
|
|
||||||
event: existed ? "change" : "add",
|
|
||||||
})
|
|
||||||
await FileTime.read(ctx.sessionID, filePath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const stats = Filesystem.stat(filePath)
|
|
||||||
if (!stats) throw new Error(`File ${filePath} not found`)
|
|
||||||
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
|
|
||||||
await FileTime.assert(ctx.sessionID, filePath)
|
|
||||||
contentOld = await Filesystem.readText(filePath)
|
|
||||||
|
|
||||||
const ending = detectLineEnding(contentOld)
|
|
||||||
const old = convertToLineEnding(normalizeLineEndings(params.oldString), ending)
|
|
||||||
const next = convertToLineEnding(normalizeLineEndings(params.newString), ending)
|
|
||||||
|
|
||||||
contentNew = replace(contentOld, old, next, params.replaceAll)
|
|
||||||
|
|
||||||
diff = trimDiff(
|
|
||||||
createTwoFilesPatch(
|
|
||||||
filePath,
|
|
||||||
filePath,
|
|
||||||
normalizeLineEndings(contentOld),
|
|
||||||
normalizeLineEndings(contentNew),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
await ctx.ask({
|
await ctx.ask({
|
||||||
permission: "edit",
|
permission: "edit",
|
||||||
patterns: [path.relative(Instance.worktree, filePath)],
|
patterns: [path.relative(Instance.worktree, filePath)],
|
||||||
@@ -123,26 +83,66 @@ export const EditTool = Tool.defineEffect(
|
|||||||
diff,
|
diff,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
await Filesystem.write(filePath, params.newString)
|
||||||
await Filesystem.write(filePath, contentNew)
|
|
||||||
await Format.file(filePath)
|
await Format.file(filePath)
|
||||||
Bus.publish(File.Event.Edited, { file: filePath })
|
Bus.publish(File.Event.Edited, { file: filePath })
|
||||||
await Bus.publish(FileWatcher.Event.Updated, {
|
await Bus.publish(FileWatcher.Event.Updated, {
|
||||||
file: filePath,
|
file: filePath,
|
||||||
event: "change",
|
event: existed ? "change" : "add",
|
||||||
})
|
})
|
||||||
contentNew = await Filesystem.readText(filePath)
|
|
||||||
diff = trimDiff(
|
|
||||||
createTwoFilesPatch(
|
|
||||||
filePath,
|
|
||||||
filePath,
|
|
||||||
normalizeLineEndings(contentOld),
|
|
||||||
normalizeLineEndings(contentNew),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
await FileTime.read(ctx.sessionID, filePath)
|
await FileTime.read(ctx.sessionID, filePath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const stats = Filesystem.stat(filePath)
|
||||||
|
if (!stats) throw new Error(`File ${filePath} not found`)
|
||||||
|
if (stats.isDirectory()) throw new Error(`Path is a directory, not a file: ${filePath}`)
|
||||||
|
await FileTime.assert(ctx.sessionID, filePath)
|
||||||
|
contentOld = await Filesystem.readText(filePath)
|
||||||
|
|
||||||
|
const ending = detectLineEnding(contentOld)
|
||||||
|
const old = convertToLineEnding(normalizeLineEndings(params.oldString), ending)
|
||||||
|
const next = convertToLineEnding(normalizeLineEndings(params.newString), ending)
|
||||||
|
|
||||||
|
contentNew = replace(contentOld, old, next, params.replaceAll)
|
||||||
|
|
||||||
|
diff = trimDiff(
|
||||||
|
createTwoFilesPatch(
|
||||||
|
filePath,
|
||||||
|
filePath,
|
||||||
|
normalizeLineEndings(contentOld),
|
||||||
|
normalizeLineEndings(contentNew),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
await ctx.ask({
|
||||||
|
permission: "edit",
|
||||||
|
patterns: [path.relative(Instance.worktree, filePath)],
|
||||||
|
always: ["*"],
|
||||||
|
metadata: {
|
||||||
|
filepath: filePath,
|
||||||
|
diff,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
await Filesystem.write(filePath, contentNew)
|
||||||
|
await Format.file(filePath)
|
||||||
|
Bus.publish(File.Event.Edited, { file: filePath })
|
||||||
|
await Bus.publish(FileWatcher.Event.Updated, {
|
||||||
|
file: filePath,
|
||||||
|
event: "change",
|
||||||
|
})
|
||||||
|
contentNew = await Filesystem.readText(filePath)
|
||||||
|
diff = trimDiff(
|
||||||
|
createTwoFilesPatch(
|
||||||
|
filePath,
|
||||||
|
filePath,
|
||||||
|
normalizeLineEndings(contentOld),
|
||||||
|
normalizeLineEndings(contentNew),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
await FileTime.read(ctx.sessionID, filePath)
|
||||||
|
})
|
||||||
|
|
||||||
const filediff: Snapshot.FileDiff = {
|
const filediff: Snapshot.FileDiff = {
|
||||||
file: filePath,
|
file: filePath,
|
||||||
patch: diff,
|
patch: diff,
|
||||||
|
|||||||
@@ -27,7 +27,13 @@ export const MultiEditTool = Tool.defineEffect(
|
|||||||
)
|
)
|
||||||
.describe("Array of edit operations to perform sequentially on the file"),
|
.describe("Array of edit operations to perform sequentially on the file"),
|
||||||
}),
|
}),
|
||||||
execute: (params: { filePath: string; edits: Array<{ filePath: string; oldString: string; newString: string; replaceAll?: boolean }> }, ctx: Tool.Context) =>
|
execute: (
|
||||||
|
params: {
|
||||||
|
filePath: string
|
||||||
|
edits: Array<{ filePath: string; oldString: string; newString: string; replaceAll?: boolean }>
|
||||||
|
},
|
||||||
|
ctx: Tool.Context,
|
||||||
|
) =>
|
||||||
Effect.gen(function* () {
|
Effect.gen(function* () {
|
||||||
const results = []
|
const results = []
|
||||||
for (const [, entry] of params.edits.entries()) {
|
for (const [, entry] of params.edits.entries()) {
|
||||||
|
|||||||
@@ -29,9 +29,7 @@ async function touch(file: string, time: number) {
|
|||||||
await fs.utimes(file, date, date)
|
await fs.utimes(file, date, date)
|
||||||
}
|
}
|
||||||
|
|
||||||
const runtime = ManagedRuntime.make(
|
const runtime = ManagedRuntime.make(Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer))
|
||||||
Layer.mergeAll(LSP.defaultLayer, FileTime.defaultLayer),
|
|
||||||
)
|
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await runtime.dispose()
|
await runtime.dispose()
|
||||||
|
|||||||
Reference in New Issue
Block a user