Start using reconcile-text
This commit is contained in:
parent
4543180f7b
commit
0ce5787858
13 changed files with 69 additions and 102 deletions
|
|
@ -6,12 +6,10 @@ import type {
|
|||
import { FileOperations } from "./file-operations";
|
||||
import { Logger } from "../tracing/logger";
|
||||
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
|
||||
import type {
|
||||
FileSystemOperations,
|
||||
TextWithCursors
|
||||
} from "./filesystem-operations";
|
||||
import init, { base64ToBytes } from "sync_lib";
|
||||
import type { FileSystemOperations } from "./filesystem-operations";
|
||||
import init from "sync_lib";
|
||||
import fs from "fs";
|
||||
import { TextWithCursors } from "reconcile-text";
|
||||
|
||||
class MockDatabase implements Partial<Database> {
|
||||
public getLatestDocumentByRelativePath(
|
||||
|
|
|
|||
|
|
@ -1,18 +1,10 @@
|
|||
import type { Logger } from "../tracing/logger";
|
||||
import type {
|
||||
FileSystemOperations,
|
||||
TextWithCursors
|
||||
} from "./filesystem-operations";
|
||||
import type { FileSystemOperations } from "./filesystem-operations";
|
||||
import type { Database, RelativePath } from "../persistence/database";
|
||||
import {
|
||||
CursorPosition,
|
||||
isBinary,
|
||||
isFileTypeMergable,
|
||||
mergeTextWithCursors,
|
||||
TextWithCursors as RustTextWithCursors
|
||||
} from "sync_lib";
|
||||
import { isFileTypeMergable } from "sync_lib";
|
||||
import { SafeFileSystemOperations } from "./safe-filesystem-operations";
|
||||
|
||||
import type { TextWithCursors } from "reconcile-text";
|
||||
import { isBinary, reconcile } from "reconcile-text";
|
||||
export class FileOperations {
|
||||
private static readonly PARENTHESES_REGEX = / \((\d+)\)$/;
|
||||
private readonly fs: SafeFileSystemOperations;
|
||||
|
|
@ -102,39 +94,25 @@ export class FileOperations {
|
|||
await this.fs.atomicUpdateText(
|
||||
path,
|
||||
({ text, cursors }: TextWithCursors): TextWithCursors => {
|
||||
text = text.replace(this.nativeLineEndings, "\n");
|
||||
|
||||
this.logger.debug(
|
||||
`Performing a 3-way merge for ${path} with the expected content`
|
||||
);
|
||||
|
||||
const left = new RustTextWithCursors(
|
||||
text,
|
||||
cursors.map(
|
||||
(cursor) =>
|
||||
new CursorPosition(
|
||||
cursor.id,
|
||||
cursor.characterPosition
|
||||
)
|
||||
)
|
||||
text = text.replace(this.nativeLineEndings, "\n");
|
||||
const merged = reconcile(
|
||||
expectedText,
|
||||
{ text, cursors },
|
||||
newText
|
||||
);
|
||||
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();
|
||||
const resultText = merged.text.replace(
|
||||
"\n",
|
||||
this.nativeLineEndings
|
||||
);
|
||||
|
||||
return {
|
||||
text: resultText,
|
||||
cursors: resultCursors
|
||||
cursors: merged.cursors
|
||||
};
|
||||
}
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,6 @@
|
|||
import type { RelativePath } from "../persistence/database";
|
||||
|
||||
export interface Cursor {
|
||||
id: number;
|
||||
|
||||
/// The character position is the index of the character in the text where the text lines are separated by '\n' new line character even if the actual text uses different line endings.
|
||||
characterPosition: number;
|
||||
}
|
||||
|
||||
export interface TextWithCursors {
|
||||
text: string;
|
||||
cursors: Cursor[];
|
||||
}
|
||||
import type { TextWithCursors } from "reconcile-text";
|
||||
|
||||
export interface FileSystemOperations {
|
||||
// List all files that should be synced.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,9 @@
|
|||
import type { RelativePath } from "../persistence/database";
|
||||
import type {
|
||||
FileSystemOperations,
|
||||
TextWithCursors
|
||||
} from "./filesystem-operations";
|
||||
import type { FileSystemOperations } from "./filesystem-operations";
|
||||
import type { Logger } from "../tracing/logger";
|
||||
import { Locks } from "../utils/locks";
|
||||
import { FileNotFoundError } from "./file-not-found-error";
|
||||
import { TextWithCursors } from "reconcile-text";
|
||||
|
||||
/**
|
||||
* Decorates `FileSystemOperations` to replace errors with `FileNotFoundError`
|
||||
|
|
|
|||
|
|
@ -13,14 +13,13 @@ export { Logger, LogLevel, LogLine } from "./tracing/logger";
|
|||
export { type SyncSettings, DEFAULT_SETTINGS } from "./persistence/settings";
|
||||
export { rateLimit } from "./utils/rate-limit";
|
||||
export type { RelativePath, StoredDatabase } from "./persistence/database";
|
||||
export type {
|
||||
FileSystemOperations,
|
||||
TextWithCursors,
|
||||
Cursor
|
||||
} from "./file-operations/filesystem-operations";
|
||||
export type { FileSystemOperations } from "./file-operations/filesystem-operations";
|
||||
export type { PersistenceProvider } from "./persistence/persistence";
|
||||
export type { CursorSpan } from "./services/types/CursorSpan";
|
||||
export type { ClientCursors } from "./services/types/ClientCursors";
|
||||
export type { NetworkConnectionStatus } from "./types/network-connection-status";
|
||||
export { DocumentUpdateStatus } from "./types/document-update-status";
|
||||
export { SyncClient } from "./sync-client";
|
||||
|
||||
// re-export reconcile-text types as they're part of the public API
|
||||
export type { TextWithCursors, CursorPosition } from "reconcile-text";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue