Move cursor after file updates

This commit is contained in:
Andras Schmelczer 2025-04-02 21:32:08 +01:00
parent 5deb10ab8b
commit 31a81921a1
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
8 changed files with 125 additions and 30 deletions

View file

@ -313,7 +313,10 @@ export class MockAgent extends MockClient {
`Decided to update file ${file} with ${content}`
);
this.doNotTouchWhileOffline.push(file);
await this.atomicUpdateText(file, (old) => old + ` ${content} `);
await this.atomicUpdateText(file, (old) => ({
text: old.text + ` ${content} `,
cursors: []
}));
}
private async deleteFileAction(files: RelativePath[]): Promise<void> {

View file

@ -1,4 +1,4 @@
import type { StoredDatabase } from "sync-client";
import type { StoredDatabase, TextWithCursors } from "sync-client";
import { assert } from "../utils/assert";
import {
type RelativePath,
@ -87,14 +87,14 @@ export class MockClient implements FileSystemOperations {
public async atomicUpdateText(
path: RelativePath,
updater: (currentContent: string) => string
updater: (currentContent: TextWithCursors) => TextWithCursors
): Promise<string> {
const file = this.localFiles.get(path);
if (!file) {
throw new Error(`File ${path} does not exist`);
}
const currentContent = new TextDecoder().decode(file);
const newContent = updater(currentContent);
const newContent = updater({ text: currentContent, cursors: [] }).text;
const newContentUint8Array = new TextEncoder().encode(newContent);
this.localFiles.set(path, newContentUint8Array);