Convert gr-diff helpers to TypeScript

Change-Id: If09a43e8d890f0ea51ca997b3281575c2e61d084
This commit is contained in:
Ben Rohlfs
2020-08-03 19:33:31 +02:00
parent 7a310be890
commit 5e2d1e7b8b
18 changed files with 419 additions and 388 deletions

View File

@@ -24,8 +24,8 @@ import './gr-diff-builder-element.js';
import {stubBaseUrl} from '../../../test/test-utils.js';
import {dom, flush} from '@polymer/polymer/lib/legacy/polymer.dom.js';
import {GrAnnotation} from '../gr-diff-highlight/gr-annotation.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group.js';
import {GrDiffLine, GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType} from '../gr-diff/gr-diff-group.js';
import {GrDiffBuilder} from './gr-diff-builder.js';
import {html} from '@polymer/polymer/lib/utils/html-tag.js';
@@ -85,14 +85,14 @@ suite('gr-diff-builder tests', () => {
const numLines = options.count || 10;
const lines = [];
for (let i = 0; i < numLines; i++) {
const line = new GrDiffLine(GrDiffLine.Type.BOTH);
const line = new GrDiffLine(GrDiffLineType.BOTH);
line.beforeNumber = offset + i + 1;
line.afterNumber = offset + i + 1;
line.text = 'lorem upsum';
lines.push(line);
}
return [new GrDiffGroup(GrDiffGroup.Type.BOTH, lines)];
return [new GrDiffGroup(GrDiffGroupType.BOTH, lines)];
}
test('no +10 buttons for 10 or less lines', () => {
@@ -314,30 +314,30 @@ suite('gr-diff-builder tests', () => {
suite('_isTotal', () => {
test('is total for add', () => {
const group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
const group = new GrDiffGroup(GrDiffGroupType.DELTA);
for (let idx = 0; idx < 10; idx++) {
group.addLine(new GrDiffLine(GrDiffLine.Type.ADD));
group.addLine(new GrDiffLine(GrDiffLineType.ADD));
}
assert.isTrue(GrDiffBuilder.prototype._isTotal(group));
});
test('is total for remove', () => {
const group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
const group = new GrDiffGroup(GrDiffGroupType.DELTA);
for (let idx = 0; idx < 10; idx++) {
group.addLine(new GrDiffLine(GrDiffLine.Type.REMOVE));
group.addLine(new GrDiffLine(GrDiffLineType.REMOVE));
}
assert.isTrue(GrDiffBuilder.prototype._isTotal(group));
});
test('not total for empty', () => {
const group = new GrDiffGroup(GrDiffGroup.Type.BOTH);
const group = new GrDiffGroup(GrDiffGroupType.BOTH);
assert.isFalse(GrDiffBuilder.prototype._isTotal(group));
});
test('not total for non-delta', () => {
const group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
const group = new GrDiffGroup(GrDiffGroupType.DELTA);
for (let idx = 0; idx < 10; idx++) {
group.addLine(new GrDiffLine(GrDiffLine.Type.BOTH));
group.addLine(new GrDiffLine(GrDiffLineType.BOTH));
}
assert.isFalse(GrDiffBuilder.prototype._isTotal(group));
});
@@ -1010,7 +1010,7 @@ suite('gr-diff-builder tests', () => {
sinon.stub(builder, 'findLinesByRange').callsFake(
(s, e, d, lines, elements) => {
// Add a line and a corresponding element.
lines.push(new GrDiffLine(GrDiffLine.Type.BOTH));
lines.push(new GrDiffLine(GrDiffLineType.BOTH));
const tr = document.createElement('tr');
const td = document.createElement('td');
const el = document.createElement('div');
@@ -1019,8 +1019,8 @@ suite('gr-diff-builder tests', () => {
elements.push(el);
// Add 2 lines without corresponding elements.
lines.push(new GrDiffLine(GrDiffLine.Type.BOTH));
lines.push(new GrDiffLine(GrDiffLine.Type.BOTH));
lines.push(new GrDiffLine(GrDiffLineType.BOTH));
lines.push(new GrDiffLine(GrDiffLineType.BOTH));
});
builder._renderContentByRange(1, 10, 'left');
@@ -1193,7 +1193,7 @@ suite('gr-diff-builder tests', () => {
const mocbBlameCell = document.createElement('span');
const getBlameStub = sinon.stub(builder, '_getBlameForBaseLine')
.returns(mocbBlameCell);
const line = new GrDiffLine(GrDiffLine.Type.BOTH);
const line = new GrDiffLine(GrDiffLineType.BOTH);
line.beforeNumber = 3;
line.afterNumber = 5;

View File

@@ -16,7 +16,7 @@
*/
import {GrDiffBuilder} from './gr-diff-builder.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group';
import {GrDiffGroupType} from '../gr-diff/gr-diff-group.js';
/** @constructor */
export function GrDiffBuilderSideBySide(diff, prefs, outputEl, layers) {
@@ -37,7 +37,7 @@ GrDiffBuilderSideBySide.prototype.buildSectionElement = function(group) {
if (group.ignoredWhitespaceOnly) {
sectionEl.classList.add('ignoredWhitespaceOnly');
}
if (group.type === GrDiffGroup.Type.CONTEXT_CONTROL) {
if (group.type === GrDiffGroupType.CONTEXT_CONTROL) {
sectionEl.appendChild(
this._createContextRow(sectionEl, group.contextGroups));
return sectionEl;
@@ -106,8 +106,8 @@ GrDiffBuilderSideBySide.prototype._createContextRow = function(section,
contextGroups) {
const row = this._createElement('tr');
row.classList.add('diff-row', 'side-by-side');
row.setAttribute('left-type', GrDiffGroup.Type.CONTEXT_CONTROL);
row.setAttribute('right-type', GrDiffGroup.Type.CONTEXT_CONTROL);
row.setAttribute('left-type', GrDiffGroupType.CONTEXT_CONTROL);
row.setAttribute('right-type', GrDiffGroupType.CONTEXT_CONTROL);
row.tabIndex = -1;
row.appendChild(this._createBlameCell(0));

View File

@@ -14,9 +14,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {GrDiffBuilder} from './gr-diff-builder.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group';
import {GrDiffGroupType} from '../gr-diff/gr-diff-group.js';
export function GrDiffBuilderUnified(diff, prefs, outputEl, layers) {
GrDiffBuilder.call(this, diff, prefs, outputEl, layers);
@@ -36,7 +36,7 @@ GrDiffBuilderUnified.prototype.buildSectionElement = function(group) {
if (group.ignoredWhitespaceOnly) {
sectionEl.classList.add('ignoredWhitespaceOnly');
}
if (group.type === GrDiffGroup.Type.CONTEXT_CONTROL) {
if (group.type === GrDiffGroupType.CONTEXT_CONTROL) {
sectionEl.appendChild(
this._createContextRow(sectionEl, group.contextGroups));
return sectionEl;
@@ -46,7 +46,7 @@ GrDiffBuilderUnified.prototype.buildSectionElement = function(group) {
const line = group.lines[i];
// If only whitespace has changed and the settings ask for whitespace to
// be ignored, only render the right-side line in unified diff mode.
if (group.ignoredWhitespaceOnly && line.type == GrDiffLine.Type.REMOVE) {
if (group.ignoredWhitespaceOnly && line.type == GrDiffLineType.REMOVE) {
continue;
}
sectionEl.appendChild(this._createRow(sectionEl, line));
@@ -84,10 +84,10 @@ GrDiffBuilderUnified.prototype._createRow = function(section, line) {
row.tabIndex = -1;
row.appendChild(this._createBlameCell(line.beforeNumber));
let lineNumberEl = this._createLineEl(line, line.beforeNumber,
GrDiffLine.Type.REMOVE, 'left');
GrDiffLineType.REMOVE, 'left');
row.appendChild(lineNumberEl);
lineNumberEl = this._createLineEl(line, line.afterNumber,
GrDiffLine.Type.ADD, 'right');
GrDiffLineType.ADD, 'right');
row.appendChild(lineNumberEl);
row.appendChild(this._createTextEl(lineNumberEl, line));
return row;
@@ -95,7 +95,7 @@ GrDiffBuilderUnified.prototype._createRow = function(section, line) {
GrDiffBuilderUnified.prototype._createContextRow = function(section,
contextGroups) {
const row = this._createElement('tr', GrDiffGroup.Type.CONTEXT_CONTROL);
const row = this._createElement('tr', GrDiffGroupType.CONTEXT_CONTROL);
row.classList.add('diff-row', 'unified');
row.tabIndex = -1;
row.appendChild(this._createBlameCell(0));

View File

@@ -19,8 +19,8 @@ import '../../../test/common-test-setup-karma.js';
import '../gr-diff/gr-diff-group.js';
import './gr-diff-builder.js';
import './gr-diff-builder-unified.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group.js';
import {GrDiffLine, GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType} from '../gr-diff/gr-diff-group.js';
import {GrDiffBuilderUnified} from './gr-diff-builder-unified.js';
suite('GrDiffBuilderUnified tests', () => {
@@ -44,15 +44,15 @@ suite('GrDiffBuilderUnified tests', () => {
setup(() => {
lines = [
new GrDiffLine(GrDiffLine.Type.BOTH, 1, 2),
new GrDiffLine(GrDiffLine.Type.BOTH, 2, 3),
new GrDiffLine(GrDiffLine.Type.BOTH, 3, 4),
new GrDiffLine(GrDiffLineType.BOTH, 1, 2),
new GrDiffLine(GrDiffLineType.BOTH, 2, 3),
new GrDiffLine(GrDiffLineType.BOTH, 3, 4),
];
lines[0].text = 'def hello_world():';
lines[1].text = ' print "Hello World";';
lines[2].text = ' return True';
group = new GrDiffGroup(GrDiffGroup.Type.BOTH, lines);
group = new GrDiffGroup(GrDiffGroupType.BOTH, lines);
});
test('creates the section', () => {
@@ -102,17 +102,17 @@ suite('GrDiffBuilderUnified tests', () => {
setup(() => {
lines = [
new GrDiffLine(GrDiffLine.Type.REMOVE, 1),
new GrDiffLine(GrDiffLine.Type.REMOVE, 2),
new GrDiffLine(GrDiffLine.Type.ADD, 2),
new GrDiffLine(GrDiffLine.Type.ADD, 3),
new GrDiffLine(GrDiffLineType.REMOVE, 1),
new GrDiffLine(GrDiffLineType.REMOVE, 2),
new GrDiffLine(GrDiffLineType.ADD, 2),
new GrDiffLine(GrDiffLineType.ADD, 3),
];
lines[0].text = 'def hello_world():';
lines[1].text = ' print "Hello World"';
lines[2].text = 'def hello_universe()';
lines[3].text = ' print "Hello Universe"';
group = new GrDiffGroup(GrDiffGroup.Type.DELTA, lines);
group = new GrDiffGroup(GrDiffGroupType.DELTA, lines);
});
test('creates the section', () => {

View File

@@ -15,8 +15,8 @@
* limitations under the License.
*/
import {getBaseUrl} from '../../../utils/url-util.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group.js';
import {GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroupType, hideInContextControl} from '../gr-diff/gr-diff-group.js';
import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
/**
@@ -187,8 +187,8 @@ GrDiffBuilder.prototype.findLinesByRange = function(start, end, opt_side,
for (const group of groups) {
let content = null;
for (const line of group.lines) {
if ((opt_side === 'left' && line.type === GrDiffLine.Type.ADD) ||
(opt_side === 'right' && line.type === GrDiffLine.Type.REMOVE)) {
if ((opt_side === 'left' && line.type === GrDiffLineType.ADD) ||
(opt_side === 'right' && line.type === GrDiffLineType.REMOVE)) {
continue;
}
const lineNumber = opt_side === 'left' ?
@@ -293,11 +293,11 @@ GrDiffBuilder.prototype._createContextButton = function(
groups.push(...contextGroups);
} else if (type === GrDiffBuilder.ContextButtonType.ABOVE) {
text = '+' + context + ' above';
groups = GrDiffGroup.hideInContextControl(
groups = hideInContextControl(
contextGroups, context, numLines);
} else if (type === GrDiffBuilder.ContextButtonType.BELOW) {
text = '+' + context + ' below';
groups = GrDiffGroup.hideInContextControl(
groups = hideInContextControl(
contextGroups, 0, numLines - context);
}
const textSpan = this._createElement('span', 'showContext');
@@ -319,10 +319,10 @@ GrDiffBuilder.prototype._createContextButton = function(
GrDiffBuilder.prototype._createLineEl = function(
line, number, type, side) {
const td = this._createElement('td');
if (line.type === GrDiffLine.Type.BLANK) {
if (line.type === GrDiffLineType.BLANK) {
return td;
}
if (line.type === GrDiffLine.Type.BOTH || line.type === type) {
if (line.type === GrDiffLineType.BOTH || line.type === type) {
// Both td and button need a number of classes/attributes for various
// selectors to work.
this._decorateLineEl(td, number, side);
@@ -346,9 +346,9 @@ GrDiffBuilder.prototype._createLineEl = function(
// the empty line number column for added/removed lines. This should not
// be announced to the screenreader.
if (number > 0) {
if (line.type === GrDiffLine.Type.REMOVE) {
if (line.type === GrDiffLineType.REMOVE) {
button.setAttribute('aria-label', `${number} removed`);
} else if (line.type === GrDiffLine.Type.ADD) {
} else if (line.type === GrDiffLineType.ADD) {
button.setAttribute('aria-label', `${number} added`);
}
}
@@ -365,7 +365,7 @@ GrDiffBuilder.prototype._decorateLineEl = function(el, number, side) {
GrDiffBuilder.prototype._createTextEl = function(
lineNumberEl, line, opt_side) {
const td = this._createElement('td');
if (line.type !== GrDiffLine.Type.BLANK) {
if (line.type !== GrDiffLineType.BLANK) {
td.classList.add('content');
}
@@ -523,7 +523,7 @@ GrDiffBuilder.prototype._getNextContentOnSide = function(content, side) {
* @return {boolean}
*/
GrDiffBuilder.prototype._isTotal = function(group) {
return group.type === GrDiffGroup.Type.DELTA &&
return group.type === GrDiffGroupType.DELTA &&
(!group.adds.length || !group.removes.length) &&
!(!group.adds.length && !group.removes.length);
};

View File

@@ -17,8 +17,8 @@
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group.js';
import {GrDiffLine, GrDiffLineType, FILE} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType, hideInContextControl} from '../gr-diff/gr-diff-group.js';
import {util} from '../../../scripts/util.js';
const WHOLE_FILE = -1;
@@ -308,7 +308,7 @@ class GrDiffProcessor extends GestureEventListeners(
const hiddenEnd = lineCount - (
firstUncollapsibleChunkIndex === chunks.length ?
0 : this.context);
groups = GrDiffGroup.hideInContextControl(
groups = hideInContextControl(
groups, hiddenStart, hiddenEnd);
}
@@ -353,7 +353,7 @@ class GrDiffProcessor extends GestureEventListeners(
* @return {!Object} (GrDiffGroup)
*/
_chunkToGroup(chunk, offsetLeft, offsetRight) {
const type = chunk.ab ? GrDiffGroup.Type.BOTH : GrDiffGroup.Type.DELTA;
const type = chunk.ab ? GrDiffGroupType.BOTH : GrDiffGroupType.DELTA;
const lines = this._linesFromChunk(chunk, offsetLeft, offsetRight);
const group = new GrDiffGroup(type, lines);
group.keyLocation = chunk.keyLocation;
@@ -365,28 +365,28 @@ class GrDiffProcessor extends GestureEventListeners(
_linesFromChunk(chunk, offsetLeft, offsetRight) {
if (chunk.ab) {
return chunk.ab.map((row, i) => this._lineFromRow(
GrDiffLine.Type.BOTH, offsetLeft, offsetRight, row, i));
GrDiffLineType.BOTH, offsetLeft, offsetRight, row, i));
}
let lines = [];
if (chunk.a) {
// Avoiding a.push(...b) because that causes callstack overflows for
// large b, which can occur when large files are added removed.
lines = lines.concat(this._linesFromRows(
GrDiffLine.Type.REMOVE, chunk.a, offsetLeft,
GrDiffLineType.REMOVE, chunk.a, offsetLeft,
chunk[DiffHighlights.REMOVED]));
}
if (chunk.b) {
// Avoiding a.push(...b) because that causes callstack overflows for
// large b, which can occur when large files are added removed.
lines = lines.concat(this._linesFromRows(
GrDiffLine.Type.ADD, chunk.b, offsetRight,
GrDiffLineType.ADD, chunk.b, offsetRight,
chunk[DiffHighlights.ADDED]));
}
return lines;
}
/**
* @param {string} lineType (GrDiffLine.Type)
* @param {string} lineType (GrDiffLineType)
* @param {!Array<string>} rows
* @param {number} offset
* @param {?Array<!Gerrit.IntralineInfo>=} opt_intralineInfos
@@ -400,7 +400,7 @@ class GrDiffProcessor extends GestureEventListeners(
}
/**
* @param {string} type (GrDiffLine.Type)
* @param {string} type (GrDiffLineType)
* @param {number} offsetLeft
* @param {number} offsetRight
* @param {string} row
@@ -411,8 +411,8 @@ class GrDiffProcessor extends GestureEventListeners(
_lineFromRow(type, offsetLeft, offsetRight, row, i, opt_highlights) {
const line = new GrDiffLine(type);
line.text = row;
if (type !== GrDiffLine.Type.ADD) line.beforeNumber = offsetLeft + i;
if (type !== GrDiffLine.Type.REMOVE) line.afterNumber = offsetRight + i;
if (type !== GrDiffLineType.ADD) line.beforeNumber = offsetLeft + i;
if (type !== GrDiffLineType.REMOVE) line.afterNumber = offsetRight + i;
if (opt_highlights) {
line.hasIntralineInfo = true;
line.highlights = opt_highlights.filter(hl => hl.contentIndex === i);
@@ -423,10 +423,10 @@ class GrDiffProcessor extends GestureEventListeners(
}
_makeFileComments() {
const line = new GrDiffLine(GrDiffLine.Type.BOTH);
line.beforeNumber = GrDiffLine.FILE;
line.afterNumber = GrDiffLine.FILE;
return new GrDiffGroup(GrDiffGroup.Type.BOTH, [line]);
const line = new GrDiffLine(GrDiffLineType.BOTH);
line.beforeNumber = FILE;
line.afterNumber = FILE;
return new GrDiffGroup(GrDiffGroupType.BOTH, [line]);
}
/**
@@ -577,7 +577,7 @@ class GrDiffProcessor extends GestureEventListeners(
*
* @param {!Array<string>} rows
* @param {!Array<!Gerrit.IntralineInfo>} intralineInfos
* @return {!Array<!Object>} (GrDiffLine.Highlight)
* @return {!Array<!Object>} (Highlights[] from GrDiffLine)
*/
_convertIntralineInfos(rows, intralineInfos) {
let rowIndex = 0;

View File

@@ -17,8 +17,8 @@
import '../../../test/common-test-setup-karma.js';
import './gr-diff-processor.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup} from '../gr-diff/gr-diff-group.js';
import {GrDiffLineType, FILE} from '../gr-diff/gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType} from '../gr-diff/gr-diff-group.js';
const basicFixture = fixtureFromElement('gr-diff-processor');
@@ -76,14 +76,14 @@ suite('gr-diff-processor tests', () => {
assert.equal(groups.length, 4);
let group = groups[0];
assert.equal(group.type, GrDiffGroup.Type.BOTH);
assert.equal(group.type, GrDiffGroupType.BOTH);
assert.equal(group.lines.length, 1);
assert.equal(group.lines[0].text, '');
assert.equal(group.lines[0].beforeNumber, GrDiffLine.FILE);
assert.equal(group.lines[0].afterNumber, GrDiffLine.FILE);
assert.equal(group.lines[0].beforeNumber, FILE);
assert.equal(group.lines[0].afterNumber, FILE);
group = groups[1];
assert.equal(group.type, GrDiffGroup.Type.BOTH);
assert.equal(group.type, GrDiffGroupType.BOTH);
assert.equal(group.lines.length, 2);
function beforeNumberFn(l) { return l.beforeNumber; }
@@ -98,7 +98,7 @@ suite('gr-diff-processor tests', () => {
]);
group = groups[2];
assert.equal(group.type, GrDiffGroup.Type.DELTA);
assert.equal(group.type, GrDiffGroupType.DELTA);
assert.equal(group.lines.length, 3);
assert.equal(group.adds.length, 1);
assert.equal(group.removes.length, 2);
@@ -113,7 +113,7 @@ suite('gr-diff-processor tests', () => {
]);
group = groups[3];
assert.equal(group.type, GrDiffGroup.Type.BOTH);
assert.equal(group.type, GrDiffGroupType.BOTH);
assert.equal(group.lines.length, 3);
assert.deepEqual(group.lines.map(beforeNumberFn), [5, 6, 7]);
assert.deepEqual(group.lines.map(afterNumberFn), [4, 5, 6]);
@@ -133,11 +133,11 @@ suite('gr-diff-processor tests', () => {
return element.process(content).then(() => {
const groups = element.groups;
assert.equal(groups[0].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[0].type, GrDiffGroupType.BOTH);
assert.equal(groups[0].lines.length, 1);
assert.equal(groups[0].lines[0].text, '');
assert.equal(groups[0].lines[0].beforeNumber, GrDiffLine.FILE);
assert.equal(groups[0].lines[0].afterNumber, GrDiffLine.FILE);
assert.equal(groups[0].lines[0].beforeNumber, FILE);
assert.equal(groups[0].lines[0].afterNumber, FILE);
});
});
@@ -155,14 +155,14 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
assert.equal(groups[1].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(groups[1].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.instanceOf(groups[1].contextGroups[0], GrDiffGroup);
assert.equal(groups[1].contextGroups[0].lines.length, 90);
for (const l of groups[1].contextGroups[0].lines) {
assert.equal(l.text, 'all work and no play make jack a dull boy');
}
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 10);
for (const l of groups[2].lines) {
assert.equal(l.text, 'all work and no play make jack a dull boy');
@@ -183,7 +183,7 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
assert.equal(groups[1].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[1].type, GrDiffGroupType.BOTH);
assert.equal(groups[1].lines.length, 5);
for (const l of groups[1].lines) {
assert.equal(l.text, 'all work and no play make jack a dull boy');
@@ -205,14 +205,14 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
// group[1] is the "a" group
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 10);
for (const l of groups[2].lines) {
assert.equal(
l.text, 'all work and no play make jill a dull girl');
}
assert.equal(groups[3].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(groups[3].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.instanceOf(groups[3].contextGroups[0], GrDiffGroup);
assert.equal(groups[3].contextGroups[0].lines.length, 90);
for (const l of groups[3].contextGroups[0].lines) {
@@ -236,7 +236,7 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
// group[1] is the "a" group
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 5);
for (const l of groups[2].lines) {
assert.equal(
@@ -280,14 +280,14 @@ suite('gr-diff-processor tests', () => {
// The first three interleaved chunks are completely shown because
// they are part of the context (3 * 3 <= 10)
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 3);
for (const l of groups[2].lines) {
assert.equal(
l.text, 'all work and no play make jill a dull girl');
}
assert.equal(groups[3].type, GrDiffGroup.Type.DELTA);
assert.equal(groups[3].type, GrDiffGroupType.DELTA);
assert.equal(groups[3].lines.length, 6);
assert.equal(groups[3].adds.length, 3);
assert.equal(groups[3].removes.length, 3);
@@ -300,7 +300,7 @@ suite('gr-diff-processor tests', () => {
l.text, ' all work and no play make jill a dull girl');
}
assert.equal(groups[4].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[4].type, GrDiffGroupType.BOTH);
assert.equal(groups[4].lines.length, 3);
for (const l of groups[4].lines) {
assert.equal(
@@ -309,7 +309,7 @@ suite('gr-diff-processor tests', () => {
// The next chunk is partially shown, so it results in two groups
assert.equal(groups[5].type, GrDiffGroup.Type.DELTA);
assert.equal(groups[5].type, GrDiffGroupType.DELTA);
assert.equal(groups[5].lines.length, 2);
assert.equal(groups[5].adds.length, 1);
assert.equal(groups[5].removes.length, 1);
@@ -322,7 +322,7 @@ suite('gr-diff-processor tests', () => {
l.text, ' all work and no play make jill a dull girl');
}
assert.equal(groups[6].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(groups[6].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.equal(groups[6].contextGroups.length, 2);
assert.equal(groups[6].contextGroups[0].lines.length, 4);
@@ -340,7 +340,7 @@ suite('gr-diff-processor tests', () => {
// The final chunk is completely hidden
assert.equal(
groups[6].contextGroups[1].type,
GrDiffGroup.Type.BOTH);
GrDiffGroupType.BOTH);
assert.equal(groups[6].contextGroups[1].lines.length, 3);
for (const l of groups[6].contextGroups[1].lines) {
assert.equal(
@@ -364,14 +364,14 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
// group[1] is the "a" group
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 10);
for (const l of groups[2].lines) {
assert.equal(
l.text, 'all work and no play make jill a dull girl');
}
assert.equal(groups[3].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(groups[3].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.instanceOf(groups[3].contextGroups[0], GrDiffGroup);
assert.equal(groups[3].contextGroups[0].lines.length, 80);
for (const l of groups[3].contextGroups[0].lines) {
@@ -379,7 +379,7 @@ suite('gr-diff-processor tests', () => {
l.text, 'all work and no play make jill a dull girl');
}
assert.equal(groups[4].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[4].type, GrDiffGroupType.BOTH);
assert.equal(groups[4].lines.length, 10);
for (const l of groups[4].lines) {
assert.equal(
@@ -403,7 +403,7 @@ suite('gr-diff-processor tests', () => {
// group[0] is the file group
// group[1] is the "a" group
assert.equal(groups[2].type, GrDiffGroup.Type.BOTH);
assert.equal(groups[2].type, GrDiffGroupType.BOTH);
assert.equal(groups[2].lines.length, 5);
for (const l of groups[2].lines) {
assert.equal(
@@ -644,7 +644,7 @@ suite('gr-diff-processor tests', () => {
// Results in one, uncollapsed group with all rows.
assert.equal(result.groups.length, 1);
assert.equal(result.groups[0].type, GrDiffGroup.Type.BOTH);
assert.equal(result.groups[0].type, GrDiffGroupType.BOTH);
assert.equal(result.groups[0].lines.length, rows.length);
// Line numbers are set correctly.
@@ -820,22 +820,22 @@ suite('gr-diff-processor tests', () => {
test('_linesFromRows', () => {
const startLineNum = 10;
let result = element._linesFromRows(GrDiffLine.Type.ADD, rows,
let result = element._linesFromRows(GrDiffLineType.ADD, rows,
startLineNum + 1);
assert.equal(result.length, rows.length);
assert.equal(result[0].type, GrDiffLine.Type.ADD);
assert.equal(result[0].type, GrDiffLineType.ADD);
assert.equal(result[0].afterNumber, startLineNum + 1);
assert.notOk(result[0].beforeNumber);
assert.equal(result[result.length - 1].afterNumber,
startLineNum + rows.length);
assert.notOk(result[result.length - 1].beforeNumber);
result = element._linesFromRows(GrDiffLine.Type.REMOVE, rows,
result = element._linesFromRows(GrDiffLineType.REMOVE, rows,
startLineNum + 1);
assert.equal(result.length, rows.length);
assert.equal(result[0].type, GrDiffLine.Type.REMOVE);
assert.equal(result[0].type, GrDiffLineType.REMOVE);
assert.equal(result[0].beforeNumber, startLineNum + 1);
assert.notOk(result[0].afterNumber);
assert.equal(result[result.length - 1].beforeNumber,

View File

@@ -14,70 +14,33 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {GrDiffLine} from './gr-diff-line.js';
import {BLANK_LINE, GrDiffLine, GrDiffLineType} from './gr-diff-line';
/**
* A chunk of the diff that should be rendered together.
*
* @constructor
* @param {!GrDiffGroup.Type} type
* @param {!Array<!GrDiffLine>=} opt_lines
*/
export function GrDiffGroup(type, opt_lines) {
/** @type {!GrDiffGroup.Type} */
this.type = type;
/** @type {boolean} */
this.dueToRebase = false;
/**
* True means all changes in this line are whitespace changes that should
* not be highlighted as changed as per the user settings.
*
* @type{boolean}
*/
this.ignoredWhitespaceOnly = false;
/**
* True means it should not be collapsed (because it was in the URL, or
* there is a comment on that line)
*/
this.keyLocation = false;
/** @type {?HTMLElement} */
this.element = null;
/** @type {!Array<!GrDiffLine>} */
this.lines = [];
/** @type {!Array<!GrDiffLine>} */
this.adds = [];
/** @type {!Array<!GrDiffLine>} */
this.removes = [];
/** @type {?Array<Object>} ?Array<!GrDiffGroup> */
this.contextGroups = null;
/** Both start and end line are inclusive. */
this.lineRange = {
left: {start: null, end: null},
right: {start: null, end: null},
};
if (opt_lines) {
opt_lines.forEach(this.addLine, this);
}
}
/** @enum {string} */
GrDiffGroup.Type = {
export enum GrDiffGroupType {
/** Unchanged context. */
BOTH: 'both',
BOTH = 'both',
/** A widget used to show more context. */
CONTEXT_CONTROL: 'contextControl',
CONTEXT_CONTROL = 'contextControl',
/** Added, removed or modified chunk. */
DELTA: 'delta',
};
DELTA = 'delta',
}
export interface GrDiffLinePair {
left: GrDiffLine;
right: GrDiffLine;
}
interface Range {
start: number | null;
end: number | null;
}
interface GrDiffGroupRange {
left: Range;
right: Range;
}
/**
* Hides lines in the given range behind a context control group.
@@ -91,36 +54,37 @@ GrDiffGroup.Type = {
* If the hidden range is 1 line or less, nothing is hidden and no context
* control group is created.
*
* @param {!Array<!GrDiffGroup>} groups Common groups, ordered by their line
* ranges.
* @param {number} hiddenStart The first element to be hidden, as a
* @param groups Common groups, ordered by their line ranges.
* @param hiddenStart The first element to be hidden, as a
* non-negative line number offset relative to the first group's start
* line, left and right respectively.
* @param {number} hiddenEnd The first visible element after the hidden range,
* @param hiddenEnd The first visible element after the hidden range,
* as a non-negative line number offset relative to the first group's
* start line, left and right respectively.
* @return {!Array<!GrDiffGroup>}
*/
GrDiffGroup.hideInContextControl = function(groups, hiddenStart, hiddenEnd) {
export function hideInContextControl(
groups: GrDiffGroup[],
hiddenStart: number,
hiddenEnd: number
): GrDiffGroup[] {
if (groups.length === 0) return [];
// Clamp hiddenStart and hiddenEnd - inspired by e.g. substring
hiddenStart = Math.max(hiddenStart, 0);
hiddenEnd = Math.max(hiddenEnd, hiddenStart);
let before = [];
let before: GrDiffGroup[] = [];
let hidden = groups;
let after = [];
let after: GrDiffGroup[] = [];
const numHidden = hiddenEnd - hiddenStart;
// Only collapse if there is more than 1 line to be hidden.
if (numHidden > 1) {
if (hiddenStart) {
[before, hidden] = GrDiffGroup._splitCommonGroups(hidden, hiddenStart);
[before, hidden] = _splitCommonGroups(hidden, hiddenStart);
}
if (hiddenEnd) {
[hidden, after] = GrDiffGroup._splitCommonGroups(
hidden, hiddenEnd - hiddenStart);
[hidden, after] = _splitCommonGroups(hidden, hiddenEnd - hiddenStart);
}
} else {
[hidden, after] = [[], hidden];
@@ -128,14 +92,13 @@ GrDiffGroup.hideInContextControl = function(groups, hiddenStart, hiddenEnd) {
const result = [...before];
if (hidden.length) {
const ctxGroup = new GrDiffGroup(
GrDiffGroup.Type.CONTEXT_CONTROL, []);
const ctxGroup = new GrDiffGroup(GrDiffGroupType.CONTEXT_CONTROL, []);
ctxGroup.contextGroups = hidden;
result.push(ctxGroup);
}
result.push(...after);
return result;
};
}
/**
* Splits a list of common groups into two lists of groups.
@@ -145,27 +108,34 @@ GrDiffGroup.hideInContextControl = function(groups, hiddenStart, hiddenEnd) {
* with some lines before and some lines after the split will be split into
* two groups, which will be put into the first and second list.
*
* @param {!Array<!GrDiffGroup>} groups
* @param {number} split A line number offset relative to the first group's
* @param groups
* @param split A line number offset relative to the first group's
* start line at which the groups should be split.
* @return {!Array<!Array<!GrDiffGroup>>} The outer array has 2 elements, the
* @return The outer array has 2 elements, the
* list of groups before and the list of groups after the split.
*/
GrDiffGroup._splitCommonGroups = function(groups, split) {
function _splitCommonGroups(
groups: GrDiffGroup[],
split: number
): GrDiffGroup[][] {
if (groups.length === 0) return [[], []];
const leftSplit = groups[0].lineRange.left.start + split;
const rightSplit = groups[0].lineRange.right.start + split;
const leftSplit = (groups[0].lineRange.left.start || 0) + split;
const rightSplit = (groups[0].lineRange.right.start || 0) + split;
const beforeGroups = [];
const afterGroups = [];
for (const group of groups) {
if (group.lineRange.left.end < leftSplit ||
group.lineRange.right.end < rightSplit) {
if (
(group.lineRange.left.end || 0) < leftSplit ||
(group.lineRange.right.end || 0) < rightSplit
) {
beforeGroups.push(group);
continue;
}
if (leftSplit <= group.lineRange.left.start ||
rightSplit <= group.lineRange.right.start) {
if (
leftSplit <= (group.lineRange.left.start || 0) ||
rightSplit <= (group.lineRange.right.start || 0)
) {
afterGroups.push(group);
continue;
}
@@ -173,8 +143,10 @@ GrDiffGroup._splitCommonGroups = function(groups, split) {
const before = [];
const after = [];
for (const line of group.lines) {
if ((line.beforeNumber && line.beforeNumber < leftSplit) ||
(line.afterNumber && line.afterNumber < rightSplit)) {
if (
(line.beforeNumber && line.beforeNumber < leftSplit) ||
(line.afterNumber && line.afterNumber < rightSplit)
) {
before.push(line);
} else {
after.push(line);
@@ -182,102 +154,162 @@ GrDiffGroup._splitCommonGroups = function(groups, split) {
}
if (before.length) {
beforeGroups.push(before.length === group.lines.length ?
group : group.cloneWithLines(before));
beforeGroups.push(
before.length === group.lines.length
? group
: group.cloneWithLines(before)
);
}
if (after.length) {
afterGroups.push(after.length === group.lines.length ?
group : group.cloneWithLines(after));
afterGroups.push(
after.length === group.lines.length
? group
: group.cloneWithLines(after)
);
}
}
return [beforeGroups, afterGroups];
};
}
/**
* Creates a new group with the same properties but different lines.
* A chunk of the diff that should be rendered together.
*
* The element property is not copied, because the original element is still a
* rendering of the old lines, so that would not make sense.
*
* @param {!Array<!GrDiffLine>} lines
* @return {!GrDiffGroup}
* @constructor
* @param {!GrDiffGroupType} type
* @param {!Array<!GrDiffLine>=} opt_lines
*/
GrDiffGroup.prototype.cloneWithLines = function(lines) {
const group = new GrDiffGroup(this.type, lines);
group.dueToRebase = this.dueToRebase;
group.ignoredWhitespaceOnly = this.ignoredWhitespaceOnly;
return group;
};
/** @param {!GrDiffLine} line */
GrDiffGroup.prototype.addLine = function(line) {
this.lines.push(line);
const notDelta = (this.type === GrDiffGroup.Type.BOTH ||
this.type === GrDiffGroup.Type.CONTEXT_CONTROL);
if (notDelta && (line.type === GrDiffLine.Type.ADD ||
line.type === GrDiffLine.Type.REMOVE)) {
throw Error('Cannot add delta line to a non-delta group.');
export class GrDiffGroup {
constructor(readonly type: GrDiffGroupType, lines: GrDiffLine[] = []) {
lines.forEach((line: GrDiffLine) => this.addLine(line));
}
if (line.type === GrDiffLine.Type.ADD) {
this.adds.push(line);
} else if (line.type === GrDiffLine.Type.REMOVE) {
this.removes.push(line);
}
this._updateRange(line);
};
dueToRebase = false;
/** @return {!Array<{left: GrDiffLine, right: GrDiffLine}>} */
GrDiffGroup.prototype.getSideBySidePairs = function() {
if (this.type === GrDiffGroup.Type.BOTH ||
this.type === GrDiffGroup.Type.CONTEXT_CONTROL) {
return this.lines.map(line => {
return {
left: line,
right: line,
};
});
/**
* True means all changes in this line are whitespace changes that should
* not be highlighted as changed as per the user settings.
*/
ignoredWhitespaceOnly = false;
/**
* True means it should not be collapsed (because it was in the URL, or
* there is a comment on that line)
*/
keyLocation = false;
element: HTMLElement | null = null;
lines: GrDiffLine[] = [];
adds: GrDiffLine[] = [];
removes: GrDiffLine[] = [];
contextGroups: GrDiffGroup[] = [];
/** Both start and end line are inclusive. */
lineRange: GrDiffGroupRange = {
left: {start: null, end: null},
right: {start: null, end: null},
};
/**
* Creates a new group with the same properties but different lines.
*
* The element property is not copied, because the original element is still a
* rendering of the old lines, so that would not make sense.
*/
cloneWithLines(lines: GrDiffLine[]): GrDiffGroup {
const group = new GrDiffGroup(this.type, lines);
group.dueToRebase = this.dueToRebase;
group.ignoredWhitespaceOnly = this.ignoredWhitespaceOnly;
return group;
}
const pairs = [];
let i = 0;
let j = 0;
while (i < this.removes.length || j < this.adds.length) {
pairs.push({
left: this.removes[i] || GrDiffLine.BLANK_LINE,
right: this.adds[j] || GrDiffLine.BLANK_LINE,
});
i++;
j++;
}
return pairs;
};
addLine(line: GrDiffLine) {
this.lines.push(line);
GrDiffGroup.prototype._updateRange = function(line) {
if (line.beforeNumber === 'FILE' || line.afterNumber === 'FILE') { return; }
if (line.type === GrDiffLine.Type.ADD ||
line.type === GrDiffLine.Type.BOTH) {
if (this.lineRange.right.start === null ||
line.afterNumber < this.lineRange.right.start) {
this.lineRange.right.start = line.afterNumber;
const notDelta =
this.type === GrDiffGroupType.BOTH ||
this.type === GrDiffGroupType.CONTEXT_CONTROL;
if (
notDelta &&
(line.type === GrDiffLineType.ADD || line.type === GrDiffLineType.REMOVE)
) {
throw Error('Cannot add delta line to a non-delta group.');
}
if (this.lineRange.right.end === null ||
line.afterNumber > this.lineRange.right.end) {
this.lineRange.right.end = line.afterNumber;
if (line.type === GrDiffLineType.ADD) {
this.adds.push(line);
} else if (line.type === GrDiffLineType.REMOVE) {
this.removes.push(line);
}
this._updateRange(line);
}
getSideBySidePairs(): GrDiffLinePair[] {
if (
this.type === GrDiffGroupType.BOTH ||
this.type === GrDiffGroupType.CONTEXT_CONTROL
) {
return this.lines.map(line => {
return {
left: line,
right: line,
};
});
}
const pairs: GrDiffLinePair[] = [];
let i = 0;
let j = 0;
while (i < this.removes.length || j < this.adds.length) {
pairs.push({
left: this.removes[i] || BLANK_LINE,
right: this.adds[j] || BLANK_LINE,
});
i++;
j++;
}
return pairs;
}
_updateRange(line: GrDiffLine) {
if (line.beforeNumber === 'FILE' || line.afterNumber === 'FILE') {
return;
}
if (line.type === GrDiffLineType.ADD || line.type === GrDiffLineType.BOTH) {
if (
this.lineRange.right.start === null ||
line.afterNumber < this.lineRange.right.start
) {
this.lineRange.right.start = line.afterNumber;
}
if (
this.lineRange.right.end === null ||
line.afterNumber > this.lineRange.right.end
) {
this.lineRange.right.end = line.afterNumber;
}
}
if (
line.type === GrDiffLineType.REMOVE ||
line.type === GrDiffLineType.BOTH
) {
if (
this.lineRange.left.start === null ||
line.beforeNumber < this.lineRange.left.start
) {
this.lineRange.left.start = line.beforeNumber;
}
if (
this.lineRange.left.end === null ||
line.beforeNumber > this.lineRange.left.end
) {
this.lineRange.left.end = line.beforeNumber;
}
}
}
if (line.type === GrDiffLine.Type.REMOVE ||
line.type === GrDiffLine.Type.BOTH) {
if (this.lineRange.left.start === null ||
line.beforeNumber < this.lineRange.left.start) {
this.lineRange.left.start = line.beforeNumber;
}
if (this.lineRange.left.end === null ||
line.beforeNumber > this.lineRange.left.end) {
this.lineRange.left.end = line.beforeNumber;
}
}
};
}

View File

@@ -16,15 +16,15 @@
*/
import '../../../test/common-test-setup-karma.js';
import {GrDiffLine} from './gr-diff-line.js';
import {GrDiffGroup} from './gr-diff-group.js';
import {GrDiffLine, GrDiffLineType, BLANK_LINE} from './gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType, hideInContextControl} from './gr-diff-group.js';
suite('gr-diff-group tests', () => {
test('delta line pairs', () => {
let group = new GrDiffGroup(GrDiffGroup.Type.DELTA);
const l1 = new GrDiffLine(GrDiffLine.Type.ADD, 0, 128);
const l2 = new GrDiffLine(GrDiffLine.Type.ADD, 0, 129);
const l3 = new GrDiffLine(GrDiffLine.Type.REMOVE, 64, 0);
let group = new GrDiffGroup(GrDiffGroupType.DELTA);
const l1 = new GrDiffLine(GrDiffLineType.ADD, 0, 128);
const l2 = new GrDiffLine(GrDiffLineType.ADD, 0, 129);
const l3 = new GrDiffLine(GrDiffLineType.REMOVE, 64, 0);
group.addLine(l1);
group.addLine(l2);
group.addLine(l3);
@@ -39,10 +39,10 @@ suite('gr-diff-group tests', () => {
let pairs = group.getSideBySidePairs();
assert.deepEqual(pairs, [
{left: l3, right: l1},
{left: GrDiffLine.BLANK_LINE, right: l2},
{left: BLANK_LINE, right: l2},
]);
group = new GrDiffGroup(GrDiffGroup.Type.DELTA, [l1, l2, l3]);
group = new GrDiffGroup(GrDiffGroupType.DELTA, [l1, l2, l3]);
assert.deepEqual(group.lines, [l1, l2, l3]);
assert.deepEqual(group.adds, [l1, l2]);
assert.deepEqual(group.removes, [l3]);
@@ -50,16 +50,16 @@ suite('gr-diff-group tests', () => {
pairs = group.getSideBySidePairs();
assert.deepEqual(pairs, [
{left: l3, right: l1},
{left: GrDiffLine.BLANK_LINE, right: l2},
{left: BLANK_LINE, right: l2},
]);
});
test('group/header line pairs', () => {
const l1 = new GrDiffLine(GrDiffLine.Type.BOTH, 64, 128);
const l2 = new GrDiffLine(GrDiffLine.Type.BOTH, 65, 129);
const l3 = new GrDiffLine(GrDiffLine.Type.BOTH, 66, 130);
const l1 = new GrDiffLine(GrDiffLineType.BOTH, 64, 128);
const l2 = new GrDiffLine(GrDiffLineType.BOTH, 65, 129);
const l3 = new GrDiffLine(GrDiffLineType.BOTH, 66, 130);
let group = new GrDiffGroup(GrDiffGroup.Type.BOTH, [l1, l2, l3]);
let group = new GrDiffGroup(GrDiffGroupType.BOTH, [l1, l2, l3]);
assert.deepEqual(group.lines, [l1, l2, l3]);
assert.deepEqual(group.adds, []);
@@ -77,7 +77,7 @@ suite('gr-diff-group tests', () => {
{left: l3, right: l3},
]);
group = new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL, [l1, l2, l3]);
group = new GrDiffGroup(GrDiffGroupType.CONTEXT_CONTROL, [l1, l2, l3]);
assert.deepEqual(group.lines, [l1, l2, l3]);
assert.deepEqual(group.adds, []);
assert.deepEqual(group.removes, []);
@@ -91,16 +91,16 @@ suite('gr-diff-group tests', () => {
});
test('adding delta lines to non-delta group', () => {
const l1 = new GrDiffLine(GrDiffLine.Type.ADD);
const l2 = new GrDiffLine(GrDiffLine.Type.REMOVE);
const l3 = new GrDiffLine(GrDiffLine.Type.BOTH);
const l1 = new GrDiffLine(GrDiffLineType.ADD);
const l2 = new GrDiffLine(GrDiffLineType.REMOVE);
const l3 = new GrDiffLine(GrDiffLineType.BOTH);
let group = new GrDiffGroup(GrDiffGroup.Type.BOTH);
let group = new GrDiffGroup(GrDiffGroupType.BOTH);
assert.throws(group.addLine.bind(group, l1));
assert.throws(group.addLine.bind(group, l2));
assert.doesNotThrow(group.addLine.bind(group, l3));
group = new GrDiffGroup(GrDiffGroup.Type.CONTEXT_CONTROL);
group = new GrDiffGroup(GrDiffGroupType.CONTEXT_CONTROL);
assert.throws(group.addLine.bind(group, l1));
assert.throws(group.addLine.bind(group, l2));
assert.doesNotThrow(group.addLine.bind(group, l3));
@@ -110,34 +110,34 @@ suite('gr-diff-group tests', () => {
let groups;
setup(() => {
groups = [
new GrDiffGroup(GrDiffGroup.Type.BOTH, [
new GrDiffLine(GrDiffLine.Type.BOTH, 5, 7),
new GrDiffLine(GrDiffLine.Type.BOTH, 6, 8),
new GrDiffLine(GrDiffLine.Type.BOTH, 7, 9),
new GrDiffGroup(GrDiffGroupType.BOTH, [
new GrDiffLine(GrDiffLineType.BOTH, 5, 7),
new GrDiffLine(GrDiffLineType.BOTH, 6, 8),
new GrDiffLine(GrDiffLineType.BOTH, 7, 9),
]),
new GrDiffGroup(GrDiffGroup.Type.DELTA, [
new GrDiffLine(GrDiffLine.Type.REMOVE, 8),
new GrDiffLine(GrDiffLine.Type.ADD, 0, 10),
new GrDiffLine(GrDiffLine.Type.REMOVE, 9),
new GrDiffLine(GrDiffLine.Type.ADD, 0, 11),
new GrDiffLine(GrDiffLine.Type.REMOVE, 10),
new GrDiffLine(GrDiffLine.Type.ADD, 0, 12),
new GrDiffGroup(GrDiffGroupType.DELTA, [
new GrDiffLine(GrDiffLineType.REMOVE, 8),
new GrDiffLine(GrDiffLineType.ADD, 0, 10),
new GrDiffLine(GrDiffLineType.REMOVE, 9),
new GrDiffLine(GrDiffLineType.ADD, 0, 11),
new GrDiffLine(GrDiffLineType.REMOVE, 10),
new GrDiffLine(GrDiffLineType.ADD, 0, 12),
]),
new GrDiffGroup(GrDiffGroup.Type.BOTH, [
new GrDiffLine(GrDiffLine.Type.BOTH, 11, 13),
new GrDiffLine(GrDiffLine.Type.BOTH, 12, 14),
new GrDiffLine(GrDiffLine.Type.BOTH, 13, 15),
new GrDiffGroup(GrDiffGroupType.BOTH, [
new GrDiffLine(GrDiffLineType.BOTH, 11, 13),
new GrDiffLine(GrDiffLineType.BOTH, 12, 14),
new GrDiffLine(GrDiffLineType.BOTH, 13, 15),
]),
];
});
test('hides hidden groups in context control', () => {
const collapsedGroups = GrDiffGroup.hideInContextControl(groups, 3, 6);
const collapsedGroups = hideInContextControl(groups, 3, 6);
assert.equal(collapsedGroups.length, 3);
assert.equal(collapsedGroups[0], groups[0]);
assert.equal(collapsedGroups[1].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(collapsedGroups[1].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.equal(collapsedGroups[1].contextGroups.length, 1);
assert.equal(collapsedGroups[1].contextGroups[0], groups[1]);
@@ -145,20 +145,20 @@ suite('gr-diff-group tests', () => {
});
test('splits partially hidden groups', () => {
const collapsedGroups = GrDiffGroup.hideInContextControl(groups, 4, 7);
const collapsedGroups = hideInContextControl(groups, 4, 7);
assert.equal(collapsedGroups.length, 4);
assert.equal(collapsedGroups[0], groups[0]);
assert.equal(collapsedGroups[1].type, GrDiffGroup.Type.DELTA);
assert.equal(collapsedGroups[1].type, GrDiffGroupType.DELTA);
assert.deepEqual(collapsedGroups[1].adds, [groups[1].adds[0]]);
assert.deepEqual(collapsedGroups[1].removes, [groups[1].removes[0]]);
assert.equal(collapsedGroups[2].type, GrDiffGroup.Type.CONTEXT_CONTROL);
assert.equal(collapsedGroups[2].type, GrDiffGroupType.CONTEXT_CONTROL);
assert.equal(collapsedGroups[2].contextGroups.length, 2);
assert.equal(
collapsedGroups[2].contextGroups[0].type,
GrDiffGroup.Type.DELTA);
GrDiffGroupType.DELTA);
assert.deepEqual(
collapsedGroups[2].contextGroups[0].adds,
groups[1].adds.slice(1));
@@ -168,23 +168,23 @@ suite('gr-diff-group tests', () => {
assert.equal(
collapsedGroups[2].contextGroups[1].type,
GrDiffGroup.Type.BOTH);
GrDiffGroupType.BOTH);
assert.deepEqual(
collapsedGroups[2].contextGroups[1].lines,
[groups[2].lines[0]]);
assert.equal(collapsedGroups[3].type, GrDiffGroup.Type.BOTH);
assert.equal(collapsedGroups[3].type, GrDiffGroupType.BOTH);
assert.deepEqual(collapsedGroups[3].lines, groups[2].lines.slice(1));
});
test('groups unchanged if the hidden range is empty', () => {
assert.deepEqual(
GrDiffGroup.hideInContextControl(groups, 0, 0), groups);
hideInContextControl(groups, 0, 0), groups);
});
test('groups unchanged if there is only 1 line to hide', () => {
assert.deepEqual(
GrDiffGroup.hideInContextControl(groups, 3, 4), groups);
hideInContextControl(groups, 3, 4), groups);
});
});
});

View File

@@ -15,37 +15,35 @@
* limitations under the License.
*/
/**
* @constructor
* @param {GrDiffLine.Type} type
* @param {number|string=} opt_beforeLine
* @param {number|string=} opt_afterLine
*/
export function GrDiffLine(type, opt_beforeLine, opt_afterLine) {
this.type = type;
type LineNumber = number | 'FILE';
/** @type {number|string} */
this.beforeNumber = opt_beforeLine || 0;
/** @type {number|string} */
this.afterNumber = opt_afterLine || 0;
/** @type {boolean} */
this.hasIntralineInfo = false;
/** @type {!Array<GrDiffLine.Highlights>} */
this.highlights = [];
this.text = '';
export enum GrDiffLineType {
ADD = 'add',
BOTH = 'both',
BLANK = 'blank',
REMOVE = 'remove',
}
/** @enum {string} */
GrDiffLine.Type = {
ADD: 'add',
BOTH: 'both',
BLANK: 'blank',
REMOVE: 'remove',
};
export const FILE = 'FILE';
export class GrDiffLine {
constructor(
readonly type: GrDiffLineType,
public beforeNumber: LineNumber = 0,
public afterNumber: LineNumber = 0
) {}
hasIntralineInfo = false;
readonly highlights: Highlights[] = [];
text = '';
// TODO(TS): remove this properties
static readonly Type = GrDiffLineType;
static readonly File = FILE;
}
/**
* A line highlight object consists of three fields:
@@ -55,15 +53,11 @@ GrDiffLine.Type = {
* - endIndex: (optional) Index of the character where the highlight should
* end. If omitted, the highlight is meant to be a continuation onto the
* next line.
*
* @typedef {{
* contentIndex: number,
* startIndex: number,
* endIndex: number
* }}
*/
GrDiffLine.Highlights;
export interface Highlights {
contentIndex: number;
startIndex: number;
endIndex?: number;
}
GrDiffLine.FILE = 'FILE';
GrDiffLine.BLANK_LINE = new GrDiffLine(GrDiffLine.Type.BLANK);
export const BLANK_LINE = new GrDiffLine(GrDiffLineType.BLANK);

View File

@@ -15,26 +15,29 @@
* limitations under the License.
*/
/** @enum {string} */
export const DiffSide = {
LEFT: 'left',
RIGHT: 'right',
};
import {CommentRange} from '../../../types/common';
export enum DiffSide {
LEFT = 'left',
RIGHT = 'right',
}
/**
* Compare two ranges. Either argument may be falsy, but will only return
* true if both are falsy or if neither are falsy and have the same position
* values.
*
* @param {Range=} a range 1
* @param {Range=} b range 2
* @return {boolean}
*/
export function rangesEqual(a, b) {
if (!a && !b) { return true; }
if (!a || !b) { return false; }
return a.start_line === b.start_line &&
a.start_character === b.start_character &&
a.end_line === b.end_line &&
a.end_character === b.end_character;
export function rangesEqual(a: CommentRange, b: CommentRange): boolean {
if (!a && !b) {
return true;
}
if (!a || !b) {
return false;
}
return (
a.start_line === b.start_line &&
a.start_character === b.start_character &&
a.end_line === b.end_line &&
a.end_character === b.end_character
);
}

View File

@@ -26,7 +26,7 @@ import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {htmlTemplate} from './gr-diff_html.js';
import {GrDiffLine} from './gr-diff-line.js';
import {FILE} from './gr-diff-line.js';
import {DiffSide, rangesEqual} from './gr-diff-utils.js';
import {getHiddenScroll} from '../../../scripts/hiddenscroll.js';
import {
@@ -395,8 +395,7 @@ class GrDiff extends GestureEventListeners(
for (const threadEl of threadEls) {
const commentSide = threadEl.getAttribute('comment-side');
const lineNum = Number(threadEl.getAttribute('line-num')) ||
GrDiffLine.FILE;
const lineNum = Number(threadEl.getAttribute('line-num')) || FILE;
const commentRange = threadEl.range || {};
keyLocations[commentSide][lineNum] = true;
// Add start_line as well if exists,
@@ -521,7 +520,7 @@ class GrDiff extends GestureEventListeners(
const value = el.getAttribute('data-value');
let lineNum;
if (value !== GrDiffLine.FILE) {
if (value !== FILE) {
lineNum = parseInt(value, 10);
if (isNaN(lineNum)) {
this.dispatchEvent(new CustomEvent('show-alert', {

View File

@@ -19,7 +19,7 @@ import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-l
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-ranged-comment-layer_html.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {strToClassName} from '../../../utils/dom-util.js';
// Polymer 1 adds # before array's key, while Polymer 2 doesn't
@@ -79,13 +79,13 @@ class GrRangedCommentLayer extends GestureEventListeners(
*/
annotate(el, lineNumberEl, line) {
let ranges = [];
if (line.type === GrDiffLine.Type.REMOVE || (
line.type === GrDiffLine.Type.BOTH &&
if (line.type === GrDiffLineType.REMOVE || (
line.type === GrDiffLineType.BOTH &&
el.getAttribute('data-side') !== 'right')) {
ranges = ranges.concat(this._getRangesForLine(line, 'left'));
}
if (line.type === GrDiffLine.Type.ADD || (
line.type === GrDiffLine.Type.BOTH &&
if (line.type === GrDiffLineType.ADD || (
line.type === GrDiffLineType.BOTH &&
el.getAttribute('data-side') !== 'left')) {
ranges = ranges.concat(this._getRangesForLine(line, 'right'));
}

View File

@@ -19,7 +19,7 @@ import '../../../test/common-test-setup-karma.js';
import '../gr-diff/gr-diff-line.js';
import './gr-ranged-comment-layer.js';
import {GrAnnotation} from '../gr-diff-highlight/gr-annotation.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffLine, GrDiffLineType} from '../gr-diff/gr-diff-line.js';
const basicFixture = fixtureFromElement('gr-ranged-comment-layer');
@@ -84,12 +84,12 @@ suite('gr-ranged-comment-layer', () => {
annotateElementStub = sinon.stub(GrAnnotation, 'annotateElement');
el = document.createElement('div');
el.setAttribute('data-side', 'left');
line = new GrDiffLine(GrDiffLine.Type.BOTH);
line = new GrDiffLine(GrDiffLineType.BOTH);
line.text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,';
});
test('type=Remove no-comment', () => {
line.type = GrDiffLine.Type.REMOVE;
line.type = GrDiffLineType.REMOVE;
line.beforeNumber = 40;
element.annotate(el, lineNumberEl, line);
@@ -98,7 +98,7 @@ suite('gr-ranged-comment-layer', () => {
});
test('type=Remove has-comment', () => {
line.type = GrDiffLine.Type.REMOVE;
line.type = GrDiffLineType.REMOVE;
line.beforeNumber = 36;
const expectedStart = 6;
const expectedLength = line.text.length - expectedStart;
@@ -114,7 +114,7 @@ suite('gr-ranged-comment-layer', () => {
});
test('type=Remove has-comment hovering', () => {
line.type = GrDiffLine.Type.REMOVE;
line.type = GrDiffLineType.REMOVE;
line.beforeNumber = 36;
element.set(['commentRanges', 0, 'hovering'], true);
@@ -134,7 +134,7 @@ suite('gr-ranged-comment-layer', () => {
});
test('type=Both has-comment', () => {
line.type = GrDiffLine.Type.BOTH;
line.type = GrDiffLineType.BOTH;
line.beforeNumber = 36;
const expectedStart = 6;
@@ -151,7 +151,7 @@ suite('gr-ranged-comment-layer', () => {
});
test('type=Both has-comment off side', () => {
line.type = GrDiffLine.Type.BOTH;
line.type = GrDiffLineType.BOTH;
line.beforeNumber = 36;
el.setAttribute('data-side', 'right');
@@ -161,7 +161,7 @@ suite('gr-ranged-comment-layer', () => {
});
test('type=Add has-comment', () => {
line.type = GrDiffLine.Type.ADD;
line.type = GrDiffLineType.ADD;
line.afterNumber = 12;
el.setAttribute('data-side', 'right');

View File

@@ -20,7 +20,7 @@ import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-l
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
import {htmlTemplate} from './gr-syntax-layer_html.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffLineType} from '../gr-diff/gr-diff-line.js';
import {util} from '../../../scripts/util.js';
const LANGUAGE_MAP = {
@@ -208,11 +208,11 @@ class GrSyntaxLayer extends GestureEventListeners(
// Determine the side.
let side;
if (line.type === GrDiffLine.Type.REMOVE || (
line.type === GrDiffLine.Type.BOTH &&
if (line.type === GrDiffLineType.REMOVE || (
line.type === GrDiffLineType.BOTH &&
el.getAttribute('data-side') !== 'right')) {
side = 'left';
} else if (line.type === GrDiffLine.Type.ADD || (
} else if (line.type === GrDiffLineType.ADD || (
el.getAttribute('data-side') !== 'left')) {
side = 'right';
}

View File

@@ -19,7 +19,7 @@ import '../../../test/common-test-setup-karma.js';
import {getMockDiffResponse} from '../../../test/mocks/diff-response.js';
import './gr-syntax-layer.js';
import {GrAnnotation} from '../gr-diff-highlight/gr-annotation.js';
import {GrDiffLine} from '../gr-diff/gr-diff-line.js';
import {GrDiffLine, GrDiffLineType} from '../gr-diff/gr-diff-line.js';
const basicFixture = fixtureFromElement('gr-syntax-layer');
@@ -57,7 +57,7 @@ suite('gr-syntax-layer tests', () => {
const annotationSpy = sinon.spy(GrAnnotation, 'annotateElement');
const el = document.createElement('div');
el.textContent = 'Etiam dui, blandit wisi.';
const line = new GrDiffLine(GrDiffLine.Type.REMOVE);
const line = new GrDiffLine(GrDiffLineType.REMOVE);
line.beforeNumber = 12;
element.annotate(el, lineNumberEl, line);
@@ -74,7 +74,7 @@ suite('gr-syntax-layer tests', () => {
const annotationSpy = sinon.spy(GrAnnotation, 'annotateElement');
const el = document.createElement('div');
el.textContent = str;
const line = new GrDiffLine(GrDiffLine.Type.REMOVE);
const line = new GrDiffLine(GrDiffLineType.REMOVE);
line.beforeNumber = 12;
element._baseRanges[11] = [{
start,
@@ -101,7 +101,7 @@ suite('gr-syntax-layer tests', () => {
const annotationSpy = sinon.spy(GrAnnotation, 'annotateElement');
const el = document.createElement('div');
el.textContent = str;
const line = new GrDiffLine(GrDiffLine.Type.REMOVE);
const line = new GrDiffLine(GrDiffLineType.REMOVE);
line.beforeNumber = 12;
element._baseRanges[11] = [{
start,

View File

@@ -25,8 +25,8 @@
import {getAccountDisplayName, getDisplayName, getGroupDisplayName, getUserName} from '../utils/display-name-util.js';
import {GrAnnotation} from './diff/gr-diff-highlight/gr-annotation.js';
import {GrAttributeHelper} from './plugins/gr-attribute-helper/gr-attribute-helper.js';
import {GrDiffLine} from './diff/gr-diff/gr-diff-line.js';
import {GrDiffGroup} from './diff/gr-diff/gr-diff-group.js';
import {GrDiffLine, GrDiffLineType} from './diff/gr-diff/gr-diff-line.js';
import {GrDiffGroup, GrDiffGroupType} from './diff/gr-diff/gr-diff-group.js';
import {GrDiffBuilder} from './diff/gr-diff-builder/gr-diff-builder.js';
import {GrDiffBuilderSideBySide} from './diff/gr-diff-builder/gr-diff-builder-side-by-side.js';
import {GrDiffBuilderImage} from './diff/gr-diff-builder/gr-diff-builder-image.js';
@@ -82,7 +82,9 @@ export function initGlobalVariables() {
window.GrAnnotation = GrAnnotation;
window.GrAttributeHelper = GrAttributeHelper;
window.GrDiffLine = GrDiffLine;
window.GrDiffLineType = GrDiffLineType;
window.GrDiffGroup = GrDiffGroup;
window.GrDiffGroupType = GrDiffGroupType;
window.GrDiffBuilder = GrDiffBuilder;
window.GrDiffBuilderSideBySide = GrDiffBuilderSideBySide;
window.GrDiffBuilderImage = GrDiffBuilderImage;

View File

@@ -30,7 +30,7 @@ import '../scripts/bundled-polymer.js';
import '../elements/diff/gr-diff/gr-diff.js';
import '../elements/diff/gr-diff-cursor/gr-diff-cursor.js';
import {initDiffAppContext} from './gr-diff-app-context-init.js';
import {GrDiffLine} from '../elements/diff/gr-diff/gr-diff-line.js';
import {GrDiffLine, GrDiffLineType} from '../elements/diff/gr-diff/gr-diff-line.js';
import {GrAnnotation} from '../elements/diff/gr-diff-highlight/gr-annotation.js';
// Setup appContext for diff.
@@ -38,4 +38,5 @@ import {GrAnnotation} from '../elements/diff/gr-diff-highlight/gr-annotation.js'
initDiffAppContext();
// Setup global variables for existing usages of this component
window.GrDiffLine = GrDiffLine;
window.GrDiffLineType = GrDiffLineType;
window.GrAnnotation = GrAnnotation;