From 9986cfb35c1108c169c67fb119227f1273a9d511 Mon Sep 17 00:00:00 2001 From: Ruedi Steinmann Date: Sat, 30 Nov 2024 11:17:40 +0100 Subject: [PATCH] Fix ranges/union() --- core/diff/myers.test.ts | 10 +++++++++- core/indexing/CodebaseIndexer.test.ts | 1 - core/util/ranges.test.ts | 5 ++--- core/util/ranges.ts | 26 ++++++++++++++++++-------- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/diff/myers.test.ts b/core/diff/myers.test.ts index d07b298c7..2b47fa548 100644 --- a/core/diff/myers.test.ts +++ b/core/diff/myers.test.ts @@ -19,6 +19,14 @@ describe("Test myers diff function", () => { E `; const diffLines = myersDiff(linesA, linesB); - console.log(diffLines); + expect(diffLines).toEqual([ + { type: "same", line: "A" }, + { type: "same", line: "B" }, + { type: "old", line: "C" }, + { type: "old", line: "D" }, + { type: "new", line: "C'" }, + { type: "new", line: "D'" }, + { type: "same", line: "E" }, + ]); }); }); diff --git a/core/indexing/CodebaseIndexer.test.ts b/core/indexing/CodebaseIndexer.test.ts index ee2f108f7..4978819d7 100644 --- a/core/indexing/CodebaseIndexer.test.ts +++ b/core/indexing/CodebaseIndexer.test.ts @@ -137,7 +137,6 @@ describe("CodebaseIndexer", () => { del: number, ) { const plan = await getIndexPlan(); - console.log("PLAN: ", plan); expect(plan.compute).toHaveLength(compute); expect(plan.addTag).toHaveLength(addTag); expect(plan.removeTag).toHaveLength(removeTag); diff --git a/core/util/ranges.test.ts b/core/util/ranges.test.ts index d9d12c07a..84d1c8d54 100644 --- a/core/util/ranges.test.ts +++ b/core/util/ranges.test.ts @@ -1,8 +1,8 @@ // This file is generated by Continue import { - intersection, getRangeInString, + intersection, maxPosition, minPosition, union, @@ -80,8 +80,7 @@ describe("union", () => { }); }); - // TODO - test.skip("should handle edge case where start and end lines are the same", () => { + test("should handle edge case where start and end lines are the same", () => { rangeA = { start: { line: 1, character: 0 }, end: { line: 1, character: 5 }, diff --git a/core/util/ranges.ts b/core/util/ranges.ts index 3fb5b609e..a9bf5e972 100644 --- a/core/util/ranges.ts +++ b/core/util/ranges.ts @@ -58,17 +58,27 @@ export function intersection(a: Range, b: Range): Range | null { } export function union(a: Range, b: Range): Range { - const startLine = Math.min(a.start.line, b.start.line); - const endLine = Math.max(a.end.line, b.end.line); + let start: Position; + if (a.start.line === b.start.line) { + start = { + line: a.start.line, + character: Math.min(a.start.character, b.start.character), + }; + } else if (a.start.line < b.start.line) start = a.start; + else start = b.start; - const startCharacter = - startLine === a.start.line ? a.start.character : b.start.character; - const endCharacter = - endLine === a.end.line ? a.end.character : b.end.character; + let end: Position; + if (a.end.line === b.end.line) { + end = { + line: a.end.line, + character: Math.max(a.end.character, b.end.character), + }; + } else if (a.end.line > b.end.line) end = a.end; + else end = b.end; return { - start: { line: startLine, character: startCharacter }, - end: { line: endLine, character: endCharacter }, + start, + end, }; }