Move some diff-related methods and consts to gr-diff-utils
In this change some methods and consts from the global Gerrit object are
moved to the gr-diff-utils.js file.
Change-Id: I6b6466b91f7886722b140f2a58c04c5cea2b1717
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
index ba5bc46..2ed69b6 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.js
@@ -32,6 +32,7 @@
import {GrDiffBuilder} from '../gr-diff-builder/gr-diff-builder.js';
import {util} from '../../../scripts/util.js';
import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
+import {DiffSide, rangesEqual} from '../gr-diff/gr-diff-utils.js';
const MSG_EMPTY_BLAME = 'No blame information for this diff.';
@@ -78,12 +79,6 @@
return !!(diff.binary && (isA || isB));
}
-/** @enum {string} */
-Gerrit.DiffSide = {
- LEFT: 'left',
- RIGHT: 'right',
-};
-
/**
* Wrapper around gr-diff.
*
@@ -824,7 +819,7 @@
function matchesRange(threadEl) {
const threadRange = /** @type {!Gerrit.Range} */(
JSON.parse(threadEl.getAttribute('range')));
- return Gerrit.rangesEqual(threadRange, range);
+ return rangesEqual(threadRange, range);
}
const filteredThreadEls = this._filterThreadElsForLocation(
@@ -837,7 +832,7 @@
* @param {!{beforeNumber: (number|string|undefined|null),
* afterNumber: (number|string|undefined|null)}}
* lineInfo
- * @param {!Gerrit.DiffSide=} side The side (LEFT, RIGHT) for
+ * @param {!DiffSide=} side The side (LEFT, RIGHT) for
* which to return the threads.
* @return {!Array<!HTMLElement>} The thread elements matching the given
* location.
@@ -845,12 +840,12 @@
_filterThreadElsForLocation(threadEls, lineInfo, side) {
function matchesLeftLine(threadEl) {
return threadEl.getAttribute('comment-side') ==
- Gerrit.DiffSide.LEFT &&
+ DiffSide.LEFT &&
threadEl.getAttribute('line-num') == lineInfo.beforeNumber;
}
function matchesRightLine(threadEl) {
return threadEl.getAttribute('comment-side') ==
- Gerrit.DiffSide.RIGHT &&
+ DiffSide.RIGHT &&
threadEl.getAttribute('line-num') == lineInfo.afterNumber;
}
function matchesFileComment(threadEl) {
@@ -863,10 +858,10 @@
// Select the appropriate matchers for the desired side and line
// If side is BOTH, we want both the left and right matcher.
const matchers = [];
- if (side !== Gerrit.DiffSide.RIGHT) {
+ if (side !== DiffSide.RIGHT) {
matchers.push(matchesLeftLine);
}
- if (side !== Gerrit.DiffSide.LEFT) {
+ if (side !== DiffSide.LEFT) {
matchers.push(matchesRightLine);
}
if (lineInfo.afterNumber === 'FILE' ||
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
index a8565c1..86a8792 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
+++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host_test.html
@@ -36,6 +36,8 @@
import {GrDiffBuilderImage} from '../gr-diff-builder/gr-diff-builder-image.js';
import {GerritNav} from '../../core/gr-navigation/gr-navigation.js';
import {dom} from '@polymer/polymer/lib/legacy/polymer.dom.js';
+import {DiffSide} from '../gr-diff/gr-diff-utils.js';
+
suite('gr-diff-host tests', () => {
let element;
let sandbox;
@@ -1333,9 +1335,9 @@
const threads = [];
assert.deepEqual(element._filterThreadElsForLocation(threads, line), []);
assert.deepEqual(element._filterThreadElsForLocation(threads, line,
- Gerrit.DiffSide.LEFT), []);
+ DiffSide.LEFT), []);
assert.deepEqual(element._filterThreadElsForLocation(threads, line,
- Gerrit.DiffSide.RIGHT), []);
+ DiffSide.RIGHT), []);
});
test('_filterThreadElsForLocation for line comments', () => {
@@ -1361,9 +1363,9 @@
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line),
[l3, r5]);
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line,
- Gerrit.DiffSide.LEFT), [l3]);
+ DiffSide.LEFT), [l3]);
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line,
- Gerrit.DiffSide.RIGHT), [r5]);
+ DiffSide.RIGHT), [r5]);
});
test('_filterThreadElsForLocation for file comments', () => {
@@ -1381,11 +1383,11 @@
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line),
[l, r]);
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line,
- Gerrit.DiffSide.BOTH), [l, r]);
+ DiffSide.BOTH), [l, r]);
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line,
- Gerrit.DiffSide.LEFT), [l]);
+ DiffSide.LEFT), [l]);
assert.deepEqual(element._filterThreadElsForLocation(threadEls, line,
- Gerrit.DiffSide.RIGHT), [r]);
+ DiffSide.RIGHT), [r]);
});
suite('syntax layer with syntax_highlighting on', () => {
diff --git a/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.js b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.js
new file mode 100644
index 0000000..7eee071
--- /dev/null
+++ b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.js
@@ -0,0 +1,40 @@
+/**
+ * @license
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/** @enum {string} */
+export const 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;
+}
diff --git a/polygerrit-ui/app/elements/diff/gr-diff/gr-diff.js b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff.js
index 0597994..aa42ecd 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff/gr-diff.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff.js
@@ -32,6 +32,7 @@
import {htmlTemplate} from './gr-diff_html.js';
import {PatchSetBehavior} from '../../../behaviors/gr-patch-set-behavior/gr-patch-set-behavior.js';
import {GrDiffLine} from './gr-diff-line.js';
+import {DiffSide, rangesEqual} from './gr-diff-utils.js';
const ERR_COMMENT_ON_EDIT = 'You cannot comment on an edit.';
const ERR_COMMENT_ON_EDIT_BASE = 'You cannot comment on the base patch set ' +
@@ -46,55 +47,15 @@
UNIFIED: 'UNIFIED_DIFF',
};
-const DiffSide = {
- LEFT: 'left',
- RIGHT: 'right',
-};
-
const LARGE_DIFF_THRESHOLD_LINES = 10000;
const FULL_CONTEXT = -1;
const LIMITED_CONTEXT = 10;
-/**
- * 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 {Gerrit.Range=} a range 1
- * @param {Gerrit.Range=} b range 2
- * @return {boolean}
- */
-Gerrit.rangesEqual = function(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;
-};
-
function isThreadEl(node) {
return node.nodeType === Node.ELEMENT_NODE &&
node.classList.contains('comment-thread');
}
-/**
- * Turn a slot element into the corresponding content element.
- * Slots are only fully supported in Polymer 2 - in Polymer 1, they are
- * replaced with content elements during template parsing. This conversion is
- * not applied for imperatively created slot elements, so this method
- * implements the same behavior as the template parsing for imperative slots.
- */
-Gerrit.slotToContent = function(slot) {
- if (PolymerElement) {
- return slot;
- }
- const content = document.createElement('content');
- content.name = slot.name;
- content.setAttribute('select', `[slot='${slot.name}']`);
- return content;
-};
-
const COMMIT_MSG_PATH = '/COMMIT_MSG';
/**
* 72 is the inofficial length standard for git commit messages.
@@ -406,7 +367,7 @@
const i = this._commentRanges
.findIndex(
cr => cr.side === removedCommentRange.side &&
- Gerrit.rangesEqual(cr.range, removedCommentRange.range)
+ rangesEqual(cr.range, removedCommentRange.range)
);
this.splice('_commentRanges', i, 1);
}
@@ -665,7 +626,7 @@
* diff.
*
* @param {!Object} contentEl
- * @param {!Gerrit.DiffSide} commentSide
+ * @param {!DiffSide} commentSide
* @return {!Node}
*/
_getOrCreateThreadGroup(contentEl, commentSide) {
@@ -884,7 +845,7 @@
// are ignored.
const slot = document.createElement('slot');
slot.name = threadEl.getAttribute('slot');
- dom(threadGroupEl).appendChild(Gerrit.slotToContent(slot));
+ dom(threadGroupEl).appendChild(slot);
lastEl = threadEl;
}
diff --git a/polygerrit-ui/app/elements/gr-app-global-var-init.js b/polygerrit-ui/app/elements/gr-app-global-var-init.js
index 8550c78..a9891e3 100644
--- a/polygerrit-ui/app/elements/gr-app-global-var-init.js
+++ b/polygerrit-ui/app/elements/gr-app-global-var-init.js
@@ -68,6 +68,7 @@
import {getBaseUrl, getPluginNameFromUrl, getRestAPI, PLUGIN_LOADING_TIMEOUT_MS, PRELOADED_PROTOCOL, send} from './shared/gr-js-api-interface/gr-api-utils.js';
import {GerritNav} from './core/gr-navigation/gr-navigation.js';
import {getRootElement} from '../scripts/rootElement.js';
+import {rangesEqual} from './diff/gr-diff/gr-diff-utils.js';
export function initGlobalVariables() {
window.GrDisplayNameUtils = GrDisplayNameUtils;
@@ -132,4 +133,7 @@
window.Gerrit._pluginLoader = pluginLoader;
window.Gerrit._endpoints = pluginEndpoints;
+
+ window.Gerrit.slotToContent = slot => slot;
+ window.Gerrit.rangesEqual = rangesEqual;
}