Skip to content

Commit

Permalink
fix: invert added/removed block
Browse files Browse the repository at this point in the history
  • Loading branch information
BearToCode committed Aug 19, 2024
1 parent f70ef26 commit f28742d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 45 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/lib/components/blocks/AddedBlock.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts">
import type { Line } from '$lib/internal/blocks';
import type { AddedBlock } from '$lib/internal/blocks/added';
import type { RemovedBlock } from '$lib/internal/blocks/removed';
import type { BlockComponent } from '$lib/internal/editor/component';
export let block: AddedBlock;
export let block: RemovedBlock;
export let component: BlockComponent;
export let lines: Line[];
</script>
Expand Down
24 changes: 16 additions & 8 deletions packages/core/src/lib/internal/blocks/added.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { type Line, LinkedComponentsBlock } from '.';
import type { Side } from '../editor/side';
import { type Side, OneWaySide, TwoWaySide } from '../editor/side';
import type { MaybeArray } from '../utils';
import { BlockComponent } from '../editor/component';
import AddedBlockComponent from '$lib/components/blocks/AddedBlock.svelte';
import AddedBlockPlaceholderComponent from '$lib/components/blocks/AddedBlockPlaceholder.svelte';
import MergeChange from '$lib/components/actions/MergeChange.svelte';
import DeleteChange from '$lib/components/actions/DeleteChange.svelte';

export type AddedSideData<SideType extends Side> = {
side: SideType;
Expand All @@ -18,7 +19,6 @@ export class AddedBlock<SideType extends Side = Side> extends LinkedComponentsBl

public readonly sidesData: MaybeArray<AddedSideData<SideType>>;
public readonly placeholderSide: MaybeArray<Side>;
public readonly unchangedSide?: Side;

constructor(params: {
sidesData: MaybeArray<AddedSideData<SideType>>;
Expand All @@ -40,14 +40,22 @@ export class AddedBlock<SideType extends Side = Side> extends LinkedComponentsBl
new BlockComponent({
component: AddedBlockComponent,
blockId: this.id,
sideAction:
side instanceof OneWaySide
? {
component: MergeChange,
props: {}
}
: side instanceof TwoWaySide && side.eq(TwoWaySide.ctr)
? {
component: DeleteChange,
props: {}
}
: undefined,
props: { block: this, lines },
linesCount: this.linesCount(side),
side,
type: this.type,
sideAction: {
component: MergeChange,
props: {}
}
type: this.type
})
),
...[this.placeholderSide].flat().map(
Expand All @@ -56,8 +64,8 @@ export class AddedBlock<SideType extends Side = Side> extends LinkedComponentsBl
component: AddedBlockPlaceholderComponent,
blockId: this.id,
props: { block: this },
side,
linesCount: 0,
side,
placeholder: true,
type: this.placeholderType
})
Expand Down
24 changes: 8 additions & 16 deletions packages/core/src/lib/internal/blocks/removed.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type Line, LinkedComponentsBlock } from '.';
import { type Side, OneWaySide, TwoWaySide } from '../editor/side';
import type { Side } from '../editor/side';
import type { MaybeArray } from '../utils';
import { BlockComponent } from '../editor/component';
import RemovedBlockComponent from '$lib/components/blocks/RemovedBlock.svelte';
import RemovedBlockPlaceholderComponent from '$lib/components/blocks/RemovedBlockPlaceholder.svelte';
import MergeChange from '$lib/components/actions/MergeChange.svelte';
import DeleteChange from '$lib/components/actions/DeleteChange.svelte';

export type RemovedSideData<SideType extends Side> = {
side: SideType;
Expand All @@ -19,6 +18,7 @@ export class RemovedBlock<SideType extends Side = Side> extends LinkedComponents

public readonly sidesData: MaybeArray<RemovedSideData<SideType>>;
public readonly placeholderSide: MaybeArray<Side>;
public readonly unchangedSide?: Side;

constructor(params: {
sidesData: MaybeArray<RemovedSideData<SideType>>;
Expand All @@ -40,22 +40,14 @@ export class RemovedBlock<SideType extends Side = Side> extends LinkedComponents
new BlockComponent({
component: RemovedBlockComponent,
blockId: this.id,
sideAction:
side instanceof OneWaySide
? {
component: MergeChange,
props: {}
}
: side instanceof TwoWaySide && side.eq(TwoWaySide.ctr)
? {
component: DeleteChange,
props: {}
}
: undefined,
props: { block: this, lines },
linesCount: this.linesCount(side),
side,
type: this.type
type: this.type,
sideAction: {
component: MergeChange,
props: {}
}
})
),
...[this.placeholderSide].flat().map(
Expand All @@ -64,8 +56,8 @@ export class RemovedBlock<SideType extends Side = Side> extends LinkedComponents
component: RemovedBlockPlaceholderComponent,
blockId: this.id,
props: { block: this },
linesCount: 0,
side,
linesCount: 0,
placeholder: true,
type: this.placeholderType
})
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib/internal/diff/one-way-assembler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DiffBlock } from '../blocks';
import { AddedBlock } from '../blocks/added';
import { RemovedBlock } from '../blocks/removed';
import { AddedBlock } from '../blocks/added';
import { UnchangedBlock } from '../blocks/unchanged';
import { type OneWayChange, oneWayDiff } from './base';
import { diff2Sides, equalIgnoringWhitespace, type LineDiffAlgorithm } from './line-diff';
Expand Down Expand Up @@ -105,15 +105,15 @@ class OneWayAssembler {
private assembleChangeBlock(change: OneWayChange, side: OneWaySide) {
let block: DiffBlock<OneWaySide>;
if (this.addSide.eq(side)) {
block = this.hashTable.new(AddedBlock, {
block = this.hashTable.new(RemovedBlock, {
sidesData: {
lines: this.intoLines(change.content),
side
},
placeholderSide: side.opposite()
});
} else {
block = this.hashTable.new(RemovedBlock, {
block = this.hashTable.new(AddedBlock, {
sidesData: {
lines: this.intoLines(change.content),
side
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/lib/internal/diff/two-way-assembler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { DiffBlock } from '../blocks';
import { AddedBlock, type AddedSideData } from '../blocks/added';
import { RemovedBlock, type RemovedSideData } from '../blocks/removed';
import { MergeConflictBlock } from '../blocks/merge-conflict';
import { ModifiedBlock } from '../blocks/modified';
import { RemovedBlock, type RemovedSideData } from '../blocks/removed';
import { AddedBlock, type AddedSideData } from '../blocks/added';
import { UnchangedBlock } from '../blocks/unchanged';
import { twoWayDiff, type TwoWayChange } from './base';
import { diff2Sides, equalIgnoringWhitespace, type LineDiffAlgorithm } from './line-diff';
Expand Down Expand Up @@ -87,7 +87,7 @@ class TwoWayAssembler {
private assembleAddedBlock(change: TwoWayChange) {
const side = change.lhs ? TwoWaySide.lhs : change.ctr ? TwoWaySide.ctr : TwoWaySide.rhs;

const block = this.hashTable.new(AddedBlock, {
const block = this.hashTable.new(RemovedBlock, {
sidesData: {
lines: this.intoLines(change.content),
side
Expand Down Expand Up @@ -123,7 +123,7 @@ class TwoWayAssembler {
side: TwoWaySide.rhs
});

const block = this.hashTable.new(RemovedBlock, {
const block = this.hashTable.new(AddedBlock, {
sidesData,
placeholderSide: side.adjacentSides().filter((side) => {
if (side.eq(TwoWaySide.lhs)) return !change.lhs;
Expand Down Expand Up @@ -170,9 +170,9 @@ class TwoWayAssembler {

private generateMergeConflictBlocks() {
const blocks: DiffBlock<TwoWaySide>[] = [];
let conflictBlocks: (AddedBlock<TwoWaySide> | RemovedBlock<TwoWaySide>)[] = [];
let conflictBlocks: (RemovedBlock<TwoWaySide> | AddedBlock<TwoWaySide>)[] = [];
for (const [index, block] of this.blocks.entries()) {
if (block instanceof AddedBlock || block instanceof RemovedBlock) {
if (block instanceof RemovedBlock || block instanceof AddedBlock) {
conflictBlocks.push(block);
}

Expand All @@ -182,7 +182,7 @@ class TwoWayAssembler {
blocks.push(conflictBlocks[0]);
conflictBlocks = [];
} else if (conflictBlocks.length > 1) {
const sidesData: (AddedSideData<TwoWaySide> | RemovedSideData<TwoWaySide>)[] = [];
const sidesData: (RemovedSideData<TwoWaySide> | AddedSideData<TwoWaySide>)[] = [];
const lhs = TwoWaySide.lhs;
const ctr = TwoWaySide.ctr;
const rhs = TwoWaySide.rhs;
Expand Down
18 changes: 9 additions & 9 deletions packages/core/src/tests/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { assembleOneWay } from '$lib/internal/diff/one-way-assembler';
import { expect, test } from 'vitest';
import dedent from 'dedent';
import { assembleTwoWay } from '$lib/internal/diff/two-way-assembler';
import { AddedBlock } from '$lib/internal/blocks/added';
import { RemovedBlock } from '$lib/internal/blocks/removed';
import { UnchangedBlock } from '$lib/internal/blocks/unchanged';
import { ModifiedBlock } from '$lib/internal/blocks/modified';
import { RemovedBlock } from '$lib/internal/blocks/removed';
import { AddedBlock } from '$lib/internal/blocks/added';

test('assemble-one-way unchanged', () => {
const blocks = assembleOneWay(
Expand All @@ -32,7 +32,7 @@ test('assemble-one-way added', () => {
);

expect(blocks).toHaveLength(2);
expect(blocks.at(1)).toBeInstanceOf(AddedBlock);
expect(blocks.at(1)).toBeInstanceOf(RemovedBlock);
});

test('assemble-one-way removed', () => {
Expand All @@ -47,7 +47,7 @@ test('assemble-one-way removed', () => {
);

expect(blocks).toHaveLength(2);
expect(blocks.at(1)).toBeInstanceOf(RemovedBlock);
expect(blocks.at(1)).toBeInstanceOf(AddedBlock);
});

test('assemble-one-way added and removed', () => {
Expand All @@ -63,9 +63,9 @@ test('assemble-one-way added and removed', () => {
);

expect(blocks).toHaveLength(3);
expect(blocks.at(0)).toBeInstanceOf(AddedBlock);
expect(blocks.at(0)).toBeInstanceOf(RemovedBlock);
expect(blocks.at(1)).toBeInstanceOf(UnchangedBlock);
expect(blocks.at(2)).toBeInstanceOf(RemovedBlock);
expect(blocks.at(2)).toBeInstanceOf(AddedBlock);
});

test('assemble-one-way modified', () => {
Expand Down Expand Up @@ -114,7 +114,7 @@ test('assemble-two-way removed', () => {
);

expect(blocks).toHaveLength(2);
expect(blocks.at(1)).toBeInstanceOf(RemovedBlock);
expect(blocks.at(1)).toBeInstanceOf(AddedBlock);
});

test('assemble-two-way added lhs', () => {
Expand All @@ -132,7 +132,7 @@ test('assemble-two-way added lhs', () => {
);

expect(blocks).toHaveLength(2);
expect(blocks.at(1)).toBeInstanceOf(AddedBlock);
expect(blocks.at(1)).toBeInstanceOf(RemovedBlock);
});

test('assemble-two-way added rhs', () => {
Expand All @@ -150,5 +150,5 @@ test('assemble-two-way added rhs', () => {
);

expect(blocks).toHaveLength(2);
expect(blocks.at(1)).toBeInstanceOf(AddedBlock);
expect(blocks.at(1)).toBeInstanceOf(RemovedBlock);
});

0 comments on commit f28742d

Please sign in to comment.