From f11c8db6d2c885fee16454eaf8864356220db8ab Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Sat, 22 Nov 2025 19:57:52 +0000 Subject: [PATCH] Replace all instead of just replace --- .../src/file-operations/file-operations.ts | 8 +++---- .../sync-client/src/services/sync-service.ts | 2 +- .../src/utils/line-and-column-to-position.ts | 2 +- .../utils/position-to-line-and-column.test.ts | 22 +++++++++++++++++++ .../src/utils/position-to-line-and-column.ts | 2 +- 5 files changed, 29 insertions(+), 7 deletions(-) diff --git a/frontend/sync-client/src/file-operations/file-operations.ts b/frontend/sync-client/src/file-operations/file-operations.ts index e85c7fda..038dbbe5 100644 --- a/frontend/sync-client/src/file-operations/file-operations.ts +++ b/frontend/sync-client/src/file-operations/file-operations.ts @@ -114,14 +114,14 @@ export class FileOperations { `Performing a 3-way merge for ${path} with the expected content` ); - text = text.replace(this.nativeLineEndings, "\n"); + text = text.replaceAll(this.nativeLineEndings, "\n"); const merged = reconcile( expectedText, { text, cursors }, newText ); - const resultText = merged.text.replace( + const resultText = merged.text.replaceAll( "\n", this.nativeLineEndings ); @@ -197,7 +197,7 @@ export class FileOperations { const decoder = new TextDecoder("utf-8"); let text = decoder.decode(content); - text = text.replace(this.nativeLineEndings, "\n"); + text = text.replaceAll(this.nativeLineEndings, "\n"); return new TextEncoder().encode(text); } @@ -208,7 +208,7 @@ export class FileOperations { const decoder = new TextDecoder("utf-8"); let text = decoder.decode(content); - text = text.replace("\n", this.nativeLineEndings); + text = text.replaceAll("\n", this.nativeLineEndings); return new TextEncoder().encode(text); } diff --git a/frontend/sync-client/src/services/sync-service.ts b/frontend/sync-client/src/services/sync-service.ts index 5bbf01e6..af3543da 100644 --- a/frontend/sync-client/src/services/sync-service.ts +++ b/frontend/sync-client/src/services/sync-service.ts @@ -343,7 +343,7 @@ export class SyncService { private getUrl(path: string): string { const { vaultName, remoteUri } = this.settings.getSettings(); - const safeRemoteUri = remoteUri.replace(/\/+$/, ""); + const safeRemoteUri = remoteUri.replace(/\/+$/g, ""); return `${safeRemoteUri}/vaults/${vaultName}${path}`; } diff --git a/frontend/sync-client/src/utils/line-and-column-to-position.ts b/frontend/sync-client/src/utils/line-and-column-to-position.ts index 670d8cac..2ee6b2a4 100644 --- a/frontend/sync-client/src/utils/line-and-column-to-position.ts +++ b/frontend/sync-client/src/utils/line-and-column-to-position.ts @@ -13,7 +13,7 @@ export function lineAndColumnToPosition( line: number, column: number ): number { - const lines = text.replace("\r", "").split("\n"); + const lines = text.replaceAll("\r", "").split("\n"); if (line >= lines.length) { throw new Error(`Line number ${line} is out of range.`); diff --git a/frontend/sync-client/src/utils/position-to-line-and-column.test.ts b/frontend/sync-client/src/utils/position-to-line-and-column.test.ts index bc21b983..2341b7c5 100644 --- a/frontend/sync-client/src/utils/position-to-line-and-column.test.ts +++ b/frontend/sync-client/src/utils/position-to-line-and-column.test.ts @@ -43,6 +43,28 @@ describe("positionToLineAndColumn", () => { }); }); + test("with multiple carriage returns", () => { + // Test that all \r characters are removed, not just the first one + const text = "line1\r\nline2\r\nline3\r\n"; + + assert.deepStrictEqual(positionToLineAndColumn(text, 0), { + line: 0, + column: 0 + }); + + // Position 6 = start of 'line2' after all \r removed + assert.deepStrictEqual(positionToLineAndColumn(text, 6), { + line: 1, + column: 0 + }); + + // Position 12 = start of 'line3' after all \r removed + assert.deepStrictEqual(positionToLineAndColumn(text, 12), { + line: 2, + column: 0 + }); + }); + test("handles empty input", () => { assert.deepStrictEqual(positionToLineAndColumn("", 0), { line: 0, diff --git a/frontend/sync-client/src/utils/position-to-line-and-column.ts b/frontend/sync-client/src/utils/position-to-line-and-column.ts index 3df61ded..15b74f8b 100644 --- a/frontend/sync-client/src/utils/position-to-line-and-column.ts +++ b/frontend/sync-client/src/utils/position-to-line-and-column.ts @@ -14,7 +14,7 @@ export function positionToLineAndColumn( throw new Error("Position cannot be negative"); } - text = text.replace("\r", ""); + text = text.replaceAll("\r", ""); if ( position >