Move cursor after file updates
This commit is contained in:
parent
5deb10ab8b
commit
31a81921a1
8 changed files with 127 additions and 32 deletions
|
|
@ -1,6 +1,12 @@
|
||||||
import type { Stat, Vault, Workspace } from "obsidian";
|
import type { Stat, Vault, Workspace } from "obsidian";
|
||||||
import { MarkdownView, normalizePath } from "obsidian";
|
import { MarkdownView, normalizePath } from "obsidian";
|
||||||
import type { FileSystemOperations, RelativePath } from "sync-client";
|
import type {
|
||||||
|
FileSystemOperations,
|
||||||
|
RelativePath,
|
||||||
|
TextWithCursors
|
||||||
|
} from "sync-client";
|
||||||
|
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
|
||||||
|
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
|
||||||
|
|
||||||
export class ObsidianFileSystemOperations implements FileSystemOperations {
|
export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
public constructor(
|
public constructor(
|
||||||
|
|
@ -42,20 +48,50 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
|
||||||
|
|
||||||
public async atomicUpdateText(
|
public async atomicUpdateText(
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (current: TextWithCursors) => TextWithCursors
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
path = normalizePath(path);
|
path = normalizePath(path);
|
||||||
|
|
||||||
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
const view = this.workspace.getActiveViewOfType(MarkdownView);
|
||||||
|
|
||||||
if (view?.file?.path === path) {
|
if (view?.file?.path === path) {
|
||||||
const result = updater(view.editor.getValue());
|
const cursor = view.editor.getCursor();
|
||||||
const position = view.editor.getCursor();
|
const text = view.editor.getValue();
|
||||||
view.editor.setValue(result);
|
const result = updater({
|
||||||
view.editor.setCursor(position);
|
text,
|
||||||
return result;
|
cursors: [
|
||||||
|
{
|
||||||
|
id: 0,
|
||||||
|
characterPosition: lineAndColumnToPosition(
|
||||||
|
text,
|
||||||
|
cursor.line,
|
||||||
|
cursor.ch
|
||||||
|
)
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
view.editor.setValue(result.text);
|
||||||
|
|
||||||
|
result.cursors.forEach((movedCursor) => {
|
||||||
|
const { line, column } = positionToLineAndColumn(
|
||||||
|
result.text,
|
||||||
|
movedCursor.characterPosition
|
||||||
|
);
|
||||||
|
view.editor.setCursor(line, column);
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.text;
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.vault.adapter.process(path, updater);
|
return this.vault.adapter.process(
|
||||||
|
path,
|
||||||
|
(text) =>
|
||||||
|
updater({
|
||||||
|
text,
|
||||||
|
cursors: []
|
||||||
|
}).text
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getFileSize(path: RelativePath): Promise<number> {
|
public async getFileSize(path: RelativePath): Promise<number> {
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@ import type {
|
||||||
import { FileOperations } from "./file-operations";
|
import { FileOperations } from "./file-operations";
|
||||||
import { Logger } from "../tracing/logger";
|
import { Logger } from "../tracing/logger";
|
||||||
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
|
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
|
||||||
import type { FileSystemOperations } from "./filesystem-operations";
|
import type {
|
||||||
|
FileSystemOperations,
|
||||||
|
TextWithCursors
|
||||||
|
} from "./filesystem-operations";
|
||||||
import init, { base64ToBytes } from "sync_lib";
|
import init, { base64ToBytes } from "sync_lib";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
|
|
@ -43,7 +46,7 @@ class FakeFileSystemOperations implements FileSystemOperations {
|
||||||
}
|
}
|
||||||
public async atomicUpdateText(
|
public async atomicUpdateText(
|
||||||
_path: RelativePath,
|
_path: RelativePath,
|
||||||
_updater: (currentContent: string) => string
|
_updater: (current: TextWithCursors) => TextWithCursors
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
throw new Error("Method not implemented.");
|
throw new Error("Method not implemented.");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,16 @@
|
||||||
import type { Logger } from "../tracing/logger";
|
import type { Logger } from "../tracing/logger";
|
||||||
import type { FileSystemOperations } from "./filesystem-operations";
|
import type {
|
||||||
|
FileSystemOperations,
|
||||||
|
TextWithCursors
|
||||||
|
} from "./filesystem-operations";
|
||||||
import type { Database, RelativePath } from "../persistence/database";
|
import type { Database, RelativePath } from "../persistence/database";
|
||||||
import { isBinary, isFileTypeMergable, mergeText } from "sync_lib";
|
import {
|
||||||
|
CursorPosition,
|
||||||
|
isBinary,
|
||||||
|
isFileTypeMergable,
|
||||||
|
mergeTextWithCursors,
|
||||||
|
TextWithCursors as RustTextWithCursors
|
||||||
|
} from "sync_lib";
|
||||||
import { SafeFileSystemOperations } from "./safe-filesystem-operations";
|
import { SafeFileSystemOperations } from "./safe-filesystem-operations";
|
||||||
|
|
||||||
export class FileOperations {
|
export class FileOperations {
|
||||||
|
|
@ -90,18 +99,45 @@ export class FileOperations {
|
||||||
const expectedText = new TextDecoder().decode(expectedContent); // this comes from a previous read which must only have \n line endings
|
const expectedText = new TextDecoder().decode(expectedContent); // this comes from a previous read which must only have \n line endings
|
||||||
const newText = new TextDecoder().decode(newContent); // this comes from the server which stores text with \n line endings
|
const newText = new TextDecoder().decode(newContent); // this comes from the server which stores text with \n line endings
|
||||||
|
|
||||||
await this.fs.atomicUpdateText(path, (currentText) => {
|
await this.fs.atomicUpdateText(
|
||||||
currentText = currentText.replace(this.nativeLineEndings, "\n");
|
path,
|
||||||
|
({ text, cursors }: TextWithCursors): TextWithCursors => {
|
||||||
|
text = text.replace(this.nativeLineEndings, "\n");
|
||||||
|
|
||||||
this.logger.debug(
|
this.logger.debug(
|
||||||
`Performing a 3-way merge for ${path} with the expected content`
|
`Performing a 3-way merge for ${path} with the expected content`
|
||||||
);
|
);
|
||||||
|
|
||||||
return mergeText(expectedText, currentText, newText).replace(
|
const left = new RustTextWithCursors(
|
||||||
"\n",
|
text,
|
||||||
this.nativeLineEndings
|
cursors.map(
|
||||||
);
|
(cursor) =>
|
||||||
});
|
new CursorPosition(
|
||||||
|
cursor.id,
|
||||||
|
cursor.characterPosition
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const right = new RustTextWithCursors(newText, []);
|
||||||
|
const merged = mergeTextWithCursors(expectedText, left, right);
|
||||||
|
|
||||||
|
const resultText = merged
|
||||||
|
.text()
|
||||||
|
.replace("\n", this.nativeLineEndings);
|
||||||
|
|
||||||
|
const resultCursors = merged.cursors().map((cursor) => ({
|
||||||
|
id: cursor.id(),
|
||||||
|
characterPosition: cursor.characterPosition()
|
||||||
|
}));
|
||||||
|
|
||||||
|
merged.free();
|
||||||
|
|
||||||
|
return {
|
||||||
|
text: resultText,
|
||||||
|
cursors: resultCursors
|
||||||
|
};
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(path: RelativePath): Promise<void> {
|
public async delete(path: RelativePath): Promise<void> {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
import type { RelativePath } from "../persistence/database";
|
import type { RelativePath } from "../persistence/database";
|
||||||
|
|
||||||
|
export interface Cursor {
|
||||||
|
id: number;
|
||||||
|
characterPosition: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TextWithCursors {
|
||||||
|
text: string;
|
||||||
|
cursors: Cursor[];
|
||||||
|
}
|
||||||
|
|
||||||
export interface FileSystemOperations {
|
export interface FileSystemOperations {
|
||||||
// List all files that should be synced.
|
// List all files that should be synced.
|
||||||
listAllFiles: () => Promise<RelativePath[]>;
|
listAllFiles: () => Promise<RelativePath[]>;
|
||||||
|
|
@ -13,7 +23,7 @@ export interface FileSystemOperations {
|
||||||
// Atomically update the content of a text file.
|
// Atomically update the content of a text file.
|
||||||
atomicUpdateText: (
|
atomicUpdateText: (
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (current: TextWithCursors) => TextWithCursors
|
||||||
) => Promise<string>;
|
) => Promise<string>;
|
||||||
|
|
||||||
// Get the size of a file in bytes.
|
// Get the size of a file in bytes.
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
import type { RelativePath } from "../persistence/database";
|
import type { RelativePath } from "../persistence/database";
|
||||||
import type { FileSystemOperations } from "./filesystem-operations";
|
import type {
|
||||||
|
FileSystemOperations,
|
||||||
|
TextWithCursors
|
||||||
|
} from "./filesystem-operations";
|
||||||
import type { Logger } from "../tracing/logger";
|
import type { Logger } from "../tracing/logger";
|
||||||
import { Locks } from "../utils/locks";
|
import { Locks } from "../utils/locks";
|
||||||
import { FileNotFoundError } from "./file-not-found-error";
|
import { FileNotFoundError } from "./file-not-found-error";
|
||||||
|
|
@ -44,7 +47,7 @@ export class SafeFileSystemOperations implements FileSystemOperations {
|
||||||
|
|
||||||
public async atomicUpdateText(
|
public async atomicUpdateText(
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (current: TextWithCursors) => TextWithCursors
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
this.logger.debug(`Atomically updating file '${path}'`);
|
this.logger.debug(`Atomically updating file '${path}'`);
|
||||||
return this.safeOperation(
|
return this.safeOperation(
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,11 @@ export { Logger, LogLevel, LogLine } from "./tracing/logger";
|
||||||
export { type SyncSettings } from "./persistence/settings";
|
export { type SyncSettings } from "./persistence/settings";
|
||||||
export { rateLimit } from "./utils/rate-limit";
|
export { rateLimit } from "./utils/rate-limit";
|
||||||
export type { RelativePath, StoredDatabase } from "./persistence/database";
|
export type { RelativePath, StoredDatabase } from "./persistence/database";
|
||||||
export type { FileSystemOperations } from "./file-operations/filesystem-operations";
|
export type {
|
||||||
|
FileSystemOperations,
|
||||||
|
TextWithCursors,
|
||||||
|
Cursor
|
||||||
|
} from "./file-operations/filesystem-operations";
|
||||||
export type { PersistenceProvider } from "./persistence/persistence";
|
export type { PersistenceProvider } from "./persistence/persistence";
|
||||||
|
|
||||||
export type { NetworkConnectionStatus } from "./sync-client";
|
export type { NetworkConnectionStatus } from "./sync-client";
|
||||||
|
|
|
||||||
|
|
@ -313,7 +313,10 @@ export class MockAgent extends MockClient {
|
||||||
`Decided to update file ${file} with ${content}`
|
`Decided to update file ${file} with ${content}`
|
||||||
);
|
);
|
||||||
this.doNotTouchWhileOffline.push(file);
|
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> {
|
private async deleteFileAction(files: RelativePath[]): Promise<void> {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import type { StoredDatabase } from "sync-client";
|
import type { StoredDatabase, TextWithCursors } from "sync-client";
|
||||||
import { assert } from "../utils/assert";
|
import { assert } from "../utils/assert";
|
||||||
import {
|
import {
|
||||||
type RelativePath,
|
type RelativePath,
|
||||||
|
|
@ -87,14 +87,14 @@ export class MockClient implements FileSystemOperations {
|
||||||
|
|
||||||
public async atomicUpdateText(
|
public async atomicUpdateText(
|
||||||
path: RelativePath,
|
path: RelativePath,
|
||||||
updater: (currentContent: string) => string
|
updater: (currentContent: TextWithCursors) => TextWithCursors
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const file = this.localFiles.get(path);
|
const file = this.localFiles.get(path);
|
||||||
if (!file) {
|
if (!file) {
|
||||||
throw new Error(`File ${path} does not exist`);
|
throw new Error(`File ${path} does not exist`);
|
||||||
}
|
}
|
||||||
const currentContent = new TextDecoder().decode(file);
|
const currentContent = new TextDecoder().decode(file);
|
||||||
const newContent = updater(currentContent);
|
const newContent = updater({ text: currentContent, cursors: [] }).text;
|
||||||
const newContentUint8Array = new TextEncoder().encode(newContent);
|
const newContentUint8Array = new TextEncoder().encode(newContent);
|
||||||
this.localFiles.set(path, newContentUint8Array);
|
this.localFiles.set(path, newContentUint8Array);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue