Skip to content

Commit

Permalink
Make sure that we render a max of 3 arrow lanes.
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Sep 15, 2016
1 parent 5898673 commit 56adf78
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/assertions.js
Expand Up @@ -1044,7 +1044,7 @@ module.exports = function (expect) {
if (subjectType.indent) {
output.indentLines();
}
var packing = utils.packArrows(changes);
var packing = utils.packArrows(changes); // NOTE: Will have side effects in changes if the packing results in too many arrow lanes
output.arrowsAlongsideChangeOutputs(packing, changes.map(function (diffItem, index) {
var delimiterOutput = subjectType.delimiter(output.clone(), index, indexOfLastNonInsert + 1);
return output.clone().block(function () {
Expand Down
1 change: 1 addition & 0 deletions lib/styles.js
Expand Up @@ -451,6 +451,7 @@ module.exports = function (expect) {
} else {
this.i();
}

this.block(function () {
changeOutputs.forEach(function (changeOutput, index) {
this.nl(index > 0 ? 1 : 0).i().append(changeOutput);
Expand Down
3 changes: 2 additions & 1 deletion lib/types.js
Expand Up @@ -557,7 +557,8 @@ module.exports = function (expect) {
var indexOfLastNonInsert = changes.reduce(function (previousValue, diffItem, index) {
return (diffItem.type === 'insert') ? previousValue : index;
}, -1);
output.arrowsAlongsideChangeOutputs(utils.packArrows(changes), changes.map(function (diffItem, index) {
var packing = utils.packArrows(changes); // NOTE: Will have side effects in changes if the packing results in too many arrow lanes
output.arrowsAlongsideChangeOutputs(packing, changes.map(function (diffItem, index) {
var delimiterOutput = type.delimiter(output.clone(), index, indexOfLastNonInsert + 1);
return output.clone().block(function () {
var type = diffItem.type;
Expand Down
10 changes: 9 additions & 1 deletion lib/utils.js
Expand Up @@ -179,7 +179,15 @@ var utils = module.exports = {
});
});

return greedyIntervalPacker(arrowSpecs);
var packing = greedyIntervalPacker(arrowSpecs);
while (packing.length > 3) {
// The arrow packing takes up too many lanes. Turn the moveSource/moveTarget items into inserts and removes
packing.shift().forEach(function (entry) {
changes[entry.direction === 'up' ? entry.start : entry.end].type = 'insert';
changes[entry.direction === 'up' ? entry.end : entry.start].type = 'remove';
});
}
return packing;
}
}
};
21 changes: 21 additions & 0 deletions test/types/array-like-type.spec.js
Expand Up @@ -206,6 +206,27 @@ describe('array-like type', function () {
);
});

it('should stop rendering more arrows when there would be more than 3 lanes', function () {
expect(function () {
expect(['a', 'b', 'c', 'd', 'e', 'f'], 'to equal', ['f', 'c', 'd', 'e', 'a', 'b']);
}, 'to error with',
"expected [ 'a', 'b', 'c', 'd', 'e', 'f' ] to equal [ 'f', 'c', 'd', 'e', 'a', 'b' ]\n" +
"\n" +
"[\n" +
" // missing 'f'\n" +
"┌─────▷\n" +
"│ ┌───▷\n" +
"│ │ ┌─▷\n" +
"│ │ │ 'a',\n" +
"│ │ │ 'b',\n" +
"└─│─│── 'c', // should be moved\n" +
" └─│── 'd', // should be moved\n" +
" └── 'e', // should be moved\n" +
" 'f' // should be removed\n" +
"]"
);
});

it('should render multiple moved items with arrows', function () {
expect(function () {
expect(['a', 'b', 'c', 'd'], 'to equal', ['d', 'b', 'a', 'c']);
Expand Down

0 comments on commit 56adf78

Please sign in to comment.