Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve findAlignedLines #706

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions packages/nbdime/src/common/mergeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@

import { valueIn, hasEntries, splitLines } from './util';

const PICKER_SYMBOL = '\u27ad';
const CONFLICT_MARKER = '\u26A0';
const PICKER_SYMBOL = '\u27ad\uFE0E';
const CONFLICT_MARKER = '\u26A0\uFE0E';

export enum DIFF_OP {
DIFF_DELETE = -1,
Expand Down Expand Up @@ -1179,17 +1179,26 @@
*
*/
function getMatchingEditLineLC(toMatch: Chunk, chunks: Chunk[]): number {
let editLine = toMatch.baseFrom;
console.log(toMatch, chunks)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log statement

const editLine = toMatch.baseFrom;

Check warning on line 1183 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1182-L1183

Added lines #L1182 - L1183 were not covered by tests
// Initialize with the last chunk in case we don't hit one of the
// two escape conditions in the for loop.
let previous: Chunk | undefined = chunks[chunks.length - 1];

Check warning on line 1186 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1186

Added line #L1186 was not covered by tests
for (let i = 0; i < chunks.length; ++i) {
let chunk = chunks[i];
const chunk = chunks[i];

Check warning on line 1188 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1188

Added line #L1188 was not covered by tests
if (chunk.baseFrom === editLine) {
// Chunk is part of the chunk list
return chunk.remoteTo;
}
if (chunk.baseFrom > editLine) {
// Remaining chunks are after the one we are interested
previous = chunks[i - 1];

Check warning on line 1195 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1195

Added line #L1195 was not covered by tests
break;
}
}
return toMatch.baseTo;
// toMatch is not in chunks list, add lines delta from the last chunk
console.log(previous, toMatch.baseTo + (previous ? (previous.remoteTo - previous.baseTo) : 0))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug log statement left behind

return toMatch.baseTo + (previous ? (previous.remoteTo - previous.baseTo) : 0);
}

/**
Expand All @@ -1211,7 +1220,7 @@
let chunk = dv.lineChunks[i];
let lines = [chunk.baseTo, chunk.remoteTo];

for (let o of others) {
for (const o of others) {

Check warning on line 1223 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1223

Added line #L1223 was not covered by tests
lines.push(getMatchingEditLineLC(chunk, o.lineChunks));
}
if (
Expand All @@ -1226,6 +1235,7 @@
if (linesToAlign.length > 0) {
let prev = linesToAlign[linesToAlign.length - 1];
let diff: number | null = lines[0] - prev[0];
// Skip this chunk if it does not required spacers
for (let j = 1; j < lines.length; ++j) {
if (diff !== lines[j] - prev[j]) {
diff = null;
Expand Down Expand Up @@ -1619,53 +1629,56 @@
* Align the matching lines of the different editors
*/
alignViews() {
let lineHeight = this._showBase
const lineHeight = this._showBase
? this._base.cm.defaultLineHeight
: this._diffViews[0].remoteEditorWidget.cm.defaultLineHeight;
if (this._aligning) {
return;
}
this._aligning = true;
// Find matching lines
let linesToAlign = findAlignedLines(this._diffViews);
const linesToAlign = findAlignedLines(this._diffViews);
console.log(linesToAlign)

Check warning on line 1641 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1640-L1641

Added lines #L1640 - L1641 were not covered by tests
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug log statement left behind


// Function modifying DOM to perform alignment:
let editors: EditorView[] = [
const editors: EditorView[] = [

Check warning on line 1644 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1644

Added line #L1644 was not covered by tests
this.base.cm,
...this._diffViews.map(dv => dv.remoteEditorWidget.cm),
];
let builders: RangeSetBuilder<Decoration>[] = [];
const builders: RangeSetBuilder<Decoration>[] = [];

Check warning on line 1648 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1648

Added line #L1648 was not covered by tests
for (let i = 0; i < editors.length; i++) {
builders.push(new RangeSetBuilder<Decoration>());
}

let sumDeltas = new Array(editors.length).fill(0);
let nLines = editors.map(editor => editor.state.doc.lines);
const sumDeltas = new Array(editors.length).fill(0);
const nLines = editors.map(editor => editor.state.doc.lines);

Check warning on line 1654 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1653-L1654

Added lines #L1653 - L1654 were not covered by tests

for (let alignment_ of linesToAlign) {
let alignment = this._showBase ? alignment_.slice(0, 3) : alignment_;
let lastLine = Math.max(...alignment);
let lineDeltas = alignment.map(
for (const alignment_ of linesToAlign) {

Check warning on line 1656 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1656

Added line #L1656 was not covered by tests
const alignment = this._showBase ? alignment_.slice(0, 3) : alignment_;
const lastLine = Math.max(...alignment);
const lineDeltas = alignment.map(

Check warning on line 1659 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1658-L1659

Added lines #L1658 - L1659 were not covered by tests
(line, i) => lastLine - line - sumDeltas[i],
);
// If some paddings will be before the current line, it means all other editors
// must add a padding.
let minDelta = this._showBase
const minDelta = this._showBase
? Math.min(...lineDeltas)
: Math.min(...lineDeltas.slice(1));
let correctedDeltas = lineDeltas.map(line => line - minDelta);
const correctedDeltas = lineDeltas.map(line => line - minDelta);

Check warning on line 1667 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1667

Added line #L1667 was not covered by tests

correctedDeltas.forEach((delta, i) => {
// Don't compute anything for the base editor if it is hidden
if (!this._showBase && i === 0) {
return;
}
// Alignments are zero-based
let line = alignment[i];

if (delta > 0 && line < nLines[i]) {
sumDeltas[i] += delta;

let offset = posToOffset(editors[i].state.doc, {
// This method include the correction from zero-based lines to one-based lines
const offset = posToOffset(editors[i].state.doc, {

Check warning on line 1681 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1681

Added line #L1681 was not covered by tests
line,
column: 0,
});
Expand All @@ -1684,12 +1697,12 @@
}

// Padding at the last line of the editor
let totalHeight = nLines.map((line, i) => line + sumDeltas[i]);
let maxHeight = Math.max(...totalHeight);
const totalHeight = nLines.map((line, i) => line + sumDeltas[i]);
const maxHeight = Math.max(...totalHeight);

Check warning on line 1701 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1700-L1701

Added lines #L1700 - L1701 were not covered by tests
totalHeight.slice(0, this._showBase ? 3 : 4).forEach((line, i) => {
if (maxHeight > line) {
let end = editors[i].state.doc.length;
let delta = maxHeight - line;
const end = editors[i].state.doc.length;
const delta = maxHeight - line;

Check warning on line 1705 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1704-L1705

Added lines #L1704 - L1705 were not covered by tests
sumDeltas[i] += delta;
builders[i].add(
end,
Expand Down Expand Up @@ -1717,7 +1730,7 @@
continue;
}

let decoSet: DecorationSet = builders[i].finish();
const decoSet: DecorationSet = builders[i].finish();

Check warning on line 1733 in packages/nbdime/src/common/mergeview.ts

View check run for this annotation

Codecov / codecov/patch

packages/nbdime/src/common/mergeview.ts#L1733

Added line #L1733 was not covered by tests
if (
!RangeSet.eq([decoSet], [editors[i].state.field(paddingWidgetField)])
) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 12 additions & 9 deletions ui-tests/tests/nbdime-merge-test1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,32 @@ test.describe('merge test1', () => {

test('choose left version for conflict', async ({ page }) => {
await page
.locator('div:nth-child(2) > .jp-Merge-gutter-picker')
.first()
.locator('.cm-merge-left-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.last()
.click();
await page.getByText('⚠').click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
});

test('choose central version for conflict', async ({ page }) => {
await page
.locator('div')
.filter({ hasText: /^➭➭➭$/ })
.locator('div')
.nth(3)
.locator('.cm-central-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.nth(2)
.click();
await page.getByText('⚠').click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
});

test('choose right version for conflict', async ({ page }) => {
await page
.locator(
'div:nth-child(3) > .cm-editor > .cm-scroller > .cm-gutters > div:nth-child(2) > div:nth-child(2) > .jp-Merge-gutter-picker',
)
.locator('.cm-merge-right-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.last()
.click();
await page.getByText('⚠').click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
Expand Down
21 changes: 12 additions & 9 deletions ui-tests/tests/nbdime-merge-test2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,30 @@ test.describe('merge test2 ', () => {

test('choose left version', async ({ page }) => {
await page
.locator('div:nth-child(2) > .jp-Merge-gutter-picker')
.first()
.locator('.cm-merge-left-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.last()
.click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
});

test('choose central version', async ({ page }) => {
await page
.locator('div')
.filter({ hasText: /^➭➭➭$/ })
.locator('div')
.nth(3)
.locator('.cm-central-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.nth(2)
.click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
});

test('choose right version', async ({ page }) => {
await page
.locator(
'div:nth-child(3) > .cm-editor > .cm-scroller > .cm-gutters > div:nth-child(2) > div:nth-child(2) > .jp-Merge-gutter-picker',
)
.locator('.cm-merge-right-editor')
.nth(1) // This select the cell; 0 being the notebook metadata
.locator('.jp-Merge-gutter-picker')
.last()
.click();
expect(await page.locator('#main').screenshot()).toMatchSnapshot();
});
Expand Down
16 changes: 16 additions & 0 deletions ui-tests/tests/nbdime-merge-test5.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from '@playwright/test';

test.describe('merge test5', () => {
test.beforeEach(async ({ page }) => {
await page.goto('http://localhost:41000/merge');
await page.locator('#merge-local').fill('data/merge_test5/left.ipynb');
await page.locator('#merge-base').fill('data/merge_test5/center.ipynb');
await page.locator('#merge-remote').fill('data/merge_test5/right.ipynb');
await page.getByRole('button', { name: 'Merge files' }).click();
});

test('take a snapshot at opening', async ({ page }) => {
await expect.soft(page.getByText('➭')).toHaveCount(16);
expect.soft(await page.locator('#main').screenshot()).toMatchSnapshot();
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading