3 files changed
+52
-5
No files matched your search
@@ -9,7 +9,7 @@ import { DEFAULT_CUSTOM_THEME_DDL_COLORS } from "@/stores/settingsStore";
|
|||||||
import { useDiffScrollSync } from "@/composables/useDiffScrollSync";
|
import { useDiffScrollSync } from "@/composables/useDiffScrollSync";
|
||||||
import { buildHunks, type DiffLine } from "@/components/diff/DiffHunkBuilder";
|
import { buildHunks, type DiffLine } from "@/components/diff/DiffHunkBuilder";
|
||||||
import { buildSchemaDiffHighlightSegments, type SchemaDiffHighlightSegment } from "@/lib/schema/schemaDiffHighlight";
|
import { buildSchemaDiffHighlightSegments, type SchemaDiffHighlightSegment } from "@/lib/schema/schemaDiffHighlight";
|
||||||
import { findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
|
import { findFirstSchemaDiffChangedLineNumber, findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
|
||||||
import DiffSvgConnector from "@/components/diff/DiffSvgConnector.vue";
|
import DiffSvgConnector from "@/components/diff/DiffSvgConnector.vue";
|
||||||
import { FileCode, ScrollText, Copy, Play, FileDiff } from "@lucide/vue";
|
import { FileCode, ScrollText, Copy, Play, FileDiff } from "@lucide/vue";
|
||||||
import { Splitpanes, Pane } from "splitpanes";
|
import { Splitpanes, Pane } from "splitpanes";
|
||||||
@@ -206,14 +206,19 @@ function scrollPaneToLine(pane: HTMLDivElement | undefined, lineNumber: number |
|
|||||||
function locateFocusedObject() {
|
function locateFocusedObject() {
|
||||||
const selectedObject = props.selectedObject;
|
const selectedObject = props.selectedObject;
|
||||||
const focusedObject = props.focusedObject;
|
const focusedObject = props.focusedObject;
|
||||||
if (!selectedObject || !focusedObject || focusedObject.id === selectedObject.id) {
|
if (!selectedObject || !focusedObject) {
|
||||||
focusedSourceLineNumber.value = null;
|
focusedSourceLineNumber.value = null;
|
||||||
focusedTargetLineNumber.value = null;
|
focusedTargetLineNumber.value = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
focusedSourceLineNumber.value = findSchemaDiffDdlLineNumber(selectedObject.sourceDdl ?? "", focusedObject, "source");
|
if (focusedObject.id === selectedObject.id) {
|
||||||
focusedTargetLineNumber.value = findSchemaDiffDdlLineNumber(selectedObject.targetDdl ?? "", focusedObject, "target");
|
focusedSourceLineNumber.value = focusedObject.operationType === "modify" ? findFirstSchemaDiffChangedLineNumber(hunks.value, "source") : null;
|
||||||
|
focusedTargetLineNumber.value = focusedObject.operationType === "modify" ? findFirstSchemaDiffChangedLineNumber(hunks.value, "target") : null;
|
||||||
|
} else {
|
||||||
|
focusedSourceLineNumber.value = findSchemaDiffDdlLineNumber(selectedObject.sourceDdl ?? "", focusedObject, "source");
|
||||||
|
focusedTargetLineNumber.value = findSchemaDiffDdlLineNumber(selectedObject.targetDdl ?? "", focusedObject, "target");
|
||||||
|
}
|
||||||
scrollPaneToLine(leftPaneRef.value, focusedSourceLineNumber.value);
|
scrollPaneToLine(leftPaneRef.value, focusedSourceLineNumber.value);
|
||||||
scrollPaneToLine(rightPaneRef.value, focusedTargetLineNumber.value);
|
scrollPaneToLine(rightPaneRef.value, focusedTargetLineNumber.value);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
import { findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
|
import { buildHunks } from "@/components/diff/DiffHunkBuilder";
|
||||||
|
import { findFirstSchemaDiffChangedLineNumber, findSchemaDiffDdlLineNumber } from "@/lib/schema/schemaDiffDdlLocate";
|
||||||
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
|
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
|
||||||
|
|
||||||
const field: SchemaDiffObject = {
|
const field: SchemaDiffObject = {
|
||||||
@@ -28,4 +29,25 @@ describe("schema diff DDL location", () => {
|
|||||||
expect(findSchemaDiffDdlLineNumber(source, createdField, "source")).toBe(2);
|
expect(findSchemaDiffDdlLineNumber(source, createdField, "source")).toBe(2);
|
||||||
expect(findSchemaDiffDdlLineNumber(target, createdField, "target")).toBeNull();
|
expect(findSchemaDiffDdlLineNumber(target, createdField, "target")).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("finds the first changed line when a modified table row is selected", () => {
|
||||||
|
const source = ["CREATE TABLE `agents` (", " `id` bigint NOT NULL,", " `context_window` int NOT NULL", ");"].join("\n");
|
||||||
|
const target = ["CREATE TABLE `agents` (", " `id` bigint NOT NULL,", " `context_window` int DEFAULT NULL", ");"].join("\n");
|
||||||
|
const hunks = buildHunks(source, target);
|
||||||
|
|
||||||
|
expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBe(3);
|
||||||
|
expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips padding when a table change exists on only one side", () => {
|
||||||
|
const hunks = [
|
||||||
|
{
|
||||||
|
leftLines: [{ type: "equal", lineNumber: null, isPadding: true }],
|
||||||
|
rightLines: [{ type: "insert", lineNumber: 4, isPadding: false }],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
expect(findFirstSchemaDiffChangedLineNumber(hunks, "source")).toBeNull();
|
||||||
|
expect(findFirstSchemaDiffChangedLineNumber(hunks, "target")).toBe(4);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,5 +1,16 @@
|
|||||||
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
|
import type { SchemaDiffObject } from "@/lib/schema/schemaDiff";
|
||||||
|
|
||||||
|
interface SchemaDiffDdlLine {
|
||||||
|
type: string;
|
||||||
|
lineNumber: number | null;
|
||||||
|
isPadding: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SchemaDiffDdlHunk {
|
||||||
|
leftLines: readonly SchemaDiffDdlLine[];
|
||||||
|
rightLines: readonly SchemaDiffDdlLine[];
|
||||||
|
}
|
||||||
|
|
||||||
function readSqlIdentifier(value: string): string | null {
|
function readSqlIdentifier(value: string): string | null {
|
||||||
const input = value.trimStart();
|
const input = value.trimStart();
|
||||||
const opening = input[0];
|
const opening = input[0];
|
||||||
@@ -59,3 +70,12 @@ export function findSchemaDiffDdlLineNumber(ddl: string, object: SchemaDiffObjec
|
|||||||
const index = lines.findIndex((line) => lineMatchesObject(line, object, objectName));
|
const index = lines.findIndex((line) => lineMatchesObject(line, object, objectName));
|
||||||
return index >= 0 ? index + 1 : null;
|
return index >= 0 ? index + 1 : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function findFirstSchemaDiffChangedLineNumber(hunks: readonly SchemaDiffDdlHunk[], side: "source" | "target"): number | null {
|
||||||
|
const linesKey = side === "source" ? "leftLines" : "rightLines";
|
||||||
|
for (const hunk of hunks) {
|
||||||
|
const line = hunk[linesKey].find((candidate) => !candidate.isPadding && candidate.type !== "equal" && candidate.lineNumber !== null);
|
||||||
|
if (line?.lineNumber !== null && line?.lineNumber !== undefined) return line.lineNumber;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user