Replace all instead of just replace

This commit is contained in:
Andras Schmelczer 2025-11-22 19:57:52 +00:00
parent 9c3dedad76
commit f11c8db6d2
5 changed files with 29 additions and 7 deletions

View file

@ -114,14 +114,14 @@ export class FileOperations {
`Performing a 3-way merge for ${path} with the expected content` `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( const merged = reconcile(
expectedText, expectedText,
{ text, cursors }, { text, cursors },
newText newText
); );
const resultText = merged.text.replace( const resultText = merged.text.replaceAll(
"\n", "\n",
this.nativeLineEndings this.nativeLineEndings
); );
@ -197,7 +197,7 @@ export class FileOperations {
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let text = decoder.decode(content); let text = decoder.decode(content);
text = text.replace(this.nativeLineEndings, "\n"); text = text.replaceAll(this.nativeLineEndings, "\n");
return new TextEncoder().encode(text); return new TextEncoder().encode(text);
} }
@ -208,7 +208,7 @@ export class FileOperations {
const decoder = new TextDecoder("utf-8"); const decoder = new TextDecoder("utf-8");
let text = decoder.decode(content); let text = decoder.decode(content);
text = text.replace("\n", this.nativeLineEndings); text = text.replaceAll("\n", this.nativeLineEndings);
return new TextEncoder().encode(text); return new TextEncoder().encode(text);
} }

View file

@ -343,7 +343,7 @@ export class SyncService {
private getUrl(path: string): string { private getUrl(path: string): string {
const { vaultName, remoteUri } = this.settings.getSettings(); const { vaultName, remoteUri } = this.settings.getSettings();
const safeRemoteUri = remoteUri.replace(/\/+$/, ""); const safeRemoteUri = remoteUri.replace(/\/+$/g, "");
return `${safeRemoteUri}/vaults/${vaultName}${path}`; return `${safeRemoteUri}/vaults/${vaultName}${path}`;
} }

View file

@ -13,7 +13,7 @@ export function lineAndColumnToPosition(
line: number, line: number,
column: number column: number
): number { ): number {
const lines = text.replace("\r", "").split("\n"); const lines = text.replaceAll("\r", "").split("\n");
if (line >= lines.length) { if (line >= lines.length) {
throw new Error(`Line number ${line} is out of range.`); throw new Error(`Line number ${line} is out of range.`);

View file

@ -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", () => { test("handles empty input", () => {
assert.deepStrictEqual(positionToLineAndColumn("", 0), { assert.deepStrictEqual(positionToLineAndColumn("", 0), {
line: 0, line: 0,

View file

@ -14,7 +14,7 @@ export function positionToLineAndColumn(
throw new Error("Position cannot be negative"); throw new Error("Position cannot be negative");
} }
text = text.replace("\r", ""); text = text.replaceAll("\r", "");
if ( if (
position > position >