Merge "Update comment thread creation/removal to no longer use locationRange"

This commit is contained in:
Becky Siegel
2018-02-13 17:27:54 +00:00
committed by Gerrit Code Review
8 changed files with 72 additions and 43 deletions

View File

@@ -349,20 +349,33 @@
return result; return result;
}; };
/**
* @param {number} changeNum
* @param {number|string} patchNum
* @param {string} path
* @param {boolean} isOnParent
* @param {string} commentSide
* @return {!Object}
*/
GrDiffBuilder.prototype.createCommentThreadGroup = function(changeNum, GrDiffBuilder.prototype.createCommentThreadGroup = function(changeNum,
patchNum, path, isOnParent, range) { patchNum, path, isOnParent, commentSide) {
const threadGroupEl = const threadGroupEl =
document.createElement('gr-diff-comment-thread-group'); document.createElement('gr-diff-comment-thread-group');
threadGroupEl.changeNum = changeNum; threadGroupEl.changeNum = changeNum;
threadGroupEl.commentSide = commentSide;
threadGroupEl.patchForNewThreads = patchNum; threadGroupEl.patchForNewThreads = patchNum;
threadGroupEl.path = path; threadGroupEl.path = path;
threadGroupEl.isOnParent = isOnParent; threadGroupEl.isOnParent = isOnParent;
threadGroupEl.projectName = this._projectName; threadGroupEl.projectName = this._projectName;
threadGroupEl.range = range;
threadGroupEl.parentIndex = this._parentIndex; threadGroupEl.parentIndex = this._parentIndex;
return threadGroupEl; return threadGroupEl;
}; };
/**
* @param {number} line
* @param {string=} opt_side
* @return {!Object}
*/
GrDiffBuilder.prototype._commentThreadGroupForLine = function( GrDiffBuilder.prototype._commentThreadGroupForLine = function(
line, opt_side) { line, opt_side) {
const comments = const comments =
@@ -385,7 +398,7 @@
} }
const threadGroupEl = this.createCommentThreadGroup( const threadGroupEl = this.createCommentThreadGroup(
this._comments.meta.changeNum, patchNum, this._comments.meta.path, this._comments.meta.changeNum, patchNum, this._comments.meta.path,
isOnParent); isOnParent, opt_side);
threadGroupEl.comments = comments; threadGroupEl.comments = comments;
if (opt_side) { if (opt_side) {
threadGroupEl.setAttribute('data-side', opt_side); threadGroupEl.setAttribute('data-side', opt_side);

View File

@@ -32,14 +32,15 @@ limitations under the License.
<template is="dom-repeat" items="[[_threads]]" as="thread"> <template is="dom-repeat" items="[[_threads]]" as="thread">
<gr-diff-comment-thread <gr-diff-comment-thread
comments="[[thread.comments]]" comments="[[thread.comments]]"
comment-side="[[thread.commentSide]]" comment-side="[[commentSide]]"
is-on-parent="[[isOnParent]]" is-on-parent="[[isOnParent]]"
parent-index="[[parentIndex]]" parent-index="[[parentIndex]]"
change-num="[[changeNum]]" change-num="[[changeNum]]"
location-range="[[thread.locationRange]]"
patch-num="[[thread.patchNum]]" patch-num="[[thread.patchNum]]"
root-id="[[thread.rootId]]"
path="[[path]]" path="[[path]]"
project-name="[[projectName]]"></gr-diff-comment-thread> project-name="[[projectName]]"
range="[[thread.range]]"></gr-diff-comment-thread>
</template> </template>
</template> </template>
<script src="gr-diff-comment-thread-group.js"></script> <script src="gr-diff-comment-thread-group.js"></script>

View File

@@ -19,6 +19,7 @@
properties: { properties: {
changeNum: String, changeNum: String,
commentSide: String,
comments: { comments: {
type: Array, type: Array,
value() { return []; }, value() { return []; },
@@ -44,28 +45,41 @@
'_commentsChanged(comments.*)', '_commentsChanged(comments.*)',
], ],
addNewThread(locationRange) { /**
* Adds a new thread. Range is optional because a comment can be
* added to a line without a range selected.
*
* @param {!Object} opt_range
*/
addNewThread(opt_range) {
this.push('_threads', { this.push('_threads', {
comments: [], comments: [],
locationRange,
patchNum: this.patchForNewThreads, patchNum: this.patchForNewThreads,
range: opt_range,
}); });
}, },
removeThread(locationRange) { removeThread(rootId) {
for (let i = 0; i < this._threads.length; i++) { for (let i = 0; i < this._threads.length; i++) {
if (this._threads[i].locationRange === locationRange) { if (this._threads[i].rootId === rootId) {
this.splice('_threads', i, 1); this.splice('_threads', i, 1);
return; return;
} }
} }
}, },
getThreadForRange(rangeToCheck) { /**
* Fetch the thread group at the given range, or the range-less thread
* on the line if no range is provided.
*
* @param {!Object=} opt_range
* @return {!Object|undefined}
*/
getThread(opt_range) {
const threads = [].filter.call( const threads = [].filter.call(
Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread'), Polymer.dom(this.root).querySelectorAll('gr-diff-comment-thread'),
thread => { thread => {
return thread.locationRange === rangeToCheck; return thread.range === opt_range;
}); });
if (threads.length === 1) { if (threads.length === 1) {
return threads[0]; return threads[0];
@@ -116,13 +130,6 @@
const threads = []; const threads = [];
for (const comment of sortedComments) { for (const comment of sortedComments) {
let locationRange;
if (!comment.range) {
locationRange = 'line-' + comment.__commentSide;
} else {
locationRange = this._calculateLocationRange(comment.range, comment);
}
// If the comment is in reply to another comment, find that comment's // If the comment is in reply to another comment, find that comment's
// thread and append to it. // thread and append to it.
if (comment.in_reply_to) { if (comment.in_reply_to) {
@@ -138,9 +145,9 @@
threads.push({ threads.push({
start_datetime: comment.updated, start_datetime: comment.updated,
comments: [comment], comments: [comment],
locationRange,
commentSide: comment.__commentSide, commentSide: comment.__commentSide,
patchNum: this._getPatchNum(comment), patchNum: this._getPatchNum(comment),
rootId: comment.id,
}); });
} }
return threads; return threads;

View File

@@ -83,7 +83,7 @@ limitations under the License.
__commentSide: 'left', __commentSide: 'left',
in_reply_to: 'sallys_confession', in_reply_to: 'sallys_confession',
}], }],
locationRange: 'line-left', rootId: 'sallys_confession',
patchNum: 3, patchNum: 3,
}, },
]; ];
@@ -122,7 +122,7 @@ limitations under the License.
__commentSide: 'left', __commentSide: 'left',
}], }],
patchNum: 3, patchNum: 3,
locationRange: 'line-left', rootId: 'sallys_confession',
}, },
{ {
start_datetime: '2015-12-24 15:00:10.396000000', start_datetime: '2015-12-24 15:00:10.396000000',
@@ -140,7 +140,7 @@ limitations under the License.
__commentSide: 'left', __commentSide: 'left',
}], }],
patchNum: 3, patchNum: 3,
locationRange: 'range-1-1-1-2-left', rootId: 'betsys_confession',
}, },
]; ];

View File

@@ -65,6 +65,7 @@ limitations under the License.
show-actions="[[_showActions]]" show-actions="[[_showActions]]"
comment-side="[[comment.__commentSide]]" comment-side="[[comment.__commentSide]]"
side="[[comment.side]]" side="[[comment.side]]"
root-id="[[rootId]]"
project-config="[[_projectConfig]]" project-config="[[_projectConfig]]"
on-create-fix-comment="_handleCommentFix" on-create-fix-comment="_handleCommentFix"
on-comment-discard="_handleCommentDiscard"></gr-diff-comment> on-comment-discard="_handleCommentDiscard"></gr-diff-comment>

View File

@@ -57,6 +57,7 @@
type: Number, type: Number,
value: null, value: null,
}, },
rootId: String,
unresolved: { unresolved: {
type: Boolean, type: Boolean,
notify: true, notify: true,

View File

@@ -389,13 +389,21 @@
const patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl); const patchNum = this._getPatchNumByLineAndContent(lineEl, contentEl);
const isOnParent = const isOnParent =
this._getIsParentCommentByLineAndContent(lineEl, contentEl); this._getIsParentCommentByLineAndContent(lineEl, contentEl);
const threadEl = this._getOrCreateThreadAtLineRange(contentEl, patchNum, const threadEl = this._getOrCreateThread(contentEl, patchNum,
side, isOnParent, opt_range); side, isOnParent, opt_range);
threadEl.addOrEditDraft(opt_lineNum, opt_range); threadEl.addOrEditDraft(opt_lineNum, opt_range);
}, },
_getThreadForRange(threadGroupEl, rangeToCheck) { /**
return threadGroupEl.getThreadForRange(rangeToCheck); * Fetch the thread group at the given range, or the range-less thread
* on the line if no range is provided.
*
* @param {!Object} threadGroupEl
* @param {!Object=} opt_range
* @return {!Object}
*/
_getThread(threadGroupEl, opt_range) {
return threadGroupEl.getThread(opt_range);
}, },
_getThreadGroupForLine(contentEl) { _getThreadGroupForLine(contentEl) {
@@ -417,31 +425,32 @@
}, },
/** /**
* Gets or creates a comment thread for a specific spot on a diff.
* May include a range, if the comment is a range comment.
*
* @param {!Object} contentEl * @param {!Object} contentEl
* @param {number} patchNum * @param {number} patchNum
* @param {string} commentSide * @param {string} commentSide
* @param {boolean} isOnParent * @param {boolean} isOnParent
* @param {!Object=} opt_range * @param {!Object=} opt_range
* @return {!Object}
*/ */
_getOrCreateThreadAtLineRange(contentEl, patchNum, commentSide, _getOrCreateThread(contentEl, patchNum, commentSide,
isOnParent, opt_range) { isOnParent, opt_range) {
const rangeToCheck = this._getRangeString(commentSide, opt_range);
// Check if thread group exists. // Check if thread group exists.
let threadGroupEl = this._getThreadGroupForLine(contentEl); let threadGroupEl = this._getThreadGroupForLine(contentEl);
if (!threadGroupEl) { if (!threadGroupEl) {
threadGroupEl = this.$.diffBuilder.createCommentThreadGroup( threadGroupEl = this.$.diffBuilder.createCommentThreadGroup(
this.changeNum, patchNum, this.path, isOnParent); this.changeNum, patchNum, this.path, isOnParent, commentSide);
contentEl.appendChild(threadGroupEl); contentEl.appendChild(threadGroupEl);
} }
let threadEl = this._getThreadForRange(threadGroupEl, rangeToCheck); let threadEl = this._getThread(threadGroupEl, opt_range);
if (!threadEl) { if (!threadEl) {
threadGroupEl.addNewThread(rangeToCheck, commentSide); threadGroupEl.addNewThread(opt_range);
Polymer.dom.flush(); Polymer.dom.flush();
threadEl = this._getThreadForRange(threadGroupEl, rangeToCheck); threadEl = this._getThread(threadGroupEl, opt_range);
threadEl.commentSide = commentSide;
} }
return threadEl; return threadEl;
}, },
@@ -495,7 +504,7 @@
_handleThreadDiscard(e) { _handleThreadDiscard(e) {
const el = Polymer.dom(e).rootTarget; const el = Polymer.dom(e).rootTarget;
el.parentNode.removeThread(el.locationRange); el.parentNode.removeThread(el.rootId);
}, },
_handleCommentDiscard(e) { _handleCommentDiscard(e) {

View File

@@ -358,18 +358,15 @@ limitations under the License.
element.patchRange = {basePatchNum: 1, patchNum: 2}; element.patchRange = {basePatchNum: 1, patchNum: 2};
element.path = 'file.txt'; element.path = 'file.txt';
sandbox.stub(element.$.diffBuilder, 'createCommentThreadGroup', () => { const mock = document.createElement('mock-diff-response');
const threadGroup = element.$.diffBuilder._builder = element.$.diffBuilder._getDiffBuilder(
document.createElement('gr-diff-comment-thread-group'); mock.diffResponse, {}, {tab_size: 2, line_length: 80});
threadGroup.patchForNewThreads = 1;
return threadGroup;
});
// No thread groups. // No thread groups.
assert.isNotOk(element._getThreadGroupForLine(contentEl)); assert.isNotOk(element._getThreadGroupForLine(contentEl));
// A thread group gets created. // A thread group gets created.
assert.isOk(element._getOrCreateThreadAtLineRange(contentEl, assert.isOk(element._getOrCreateThread(contentEl,
patchNum, commentSide, side)); patchNum, commentSide, side));
// Try to fetch a thread with a different range. // Try to fetch a thread with a different range.
@@ -380,7 +377,7 @@ limitations under the License.
endChar: 3, endChar: 3,
}; };
assert.isOk(element._getOrCreateThreadAtLineRange( assert.isOk(element._getOrCreateThread(
contentEl, patchNum, commentSide, side, range)); contentEl, patchNum, commentSide, side, range));
// The new thread group can be fetched. // The new thread group can be fetched.
assert.isOk(element._getThreadGroupForLine(contentEl)); assert.isOk(element._getThreadGroupForLine(contentEl));