commentsEditorContribution.ts 24.0 KB
Newer Older
P
Peng Lyu 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

P
Peng Lyu 已提交
7
import 'vs/css!./media/review';
8
import * as nls from 'vs/nls';
9
import * as arrays from 'vs/base/common/arrays';
M
Matt Bierner 已提交
10
import * as modes from 'vs/editor/common/modes';
P
Peng Lyu 已提交
11
import { IEditorContribution } from 'vs/editor/common/editorCommon';
12
import { ICodeEditor, IEditorMouseEvent, MouseTargetType, IViewZone } from 'vs/editor/browser/editorBrowser';
13
import { $ } from 'vs/base/browser/builder';
P
Peng Lyu 已提交
14 15 16
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
P
Peng Lyu 已提交
17
import { TrackedRangeStickiness, IModelDeltaDecoration } from 'vs/editor/common/model';
P
Peng Lyu 已提交
18
import { renderMarkdown } from 'vs/base/browser/htmlContentRenderer';
M
Matt Bierner 已提交
19
import { RawContextKey, IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
P
Peng Lyu 已提交
20 21 22 23 24
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { EmbeddedCodeEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
25 26 27 28 29
import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { Action } from 'vs/base/common/actions';
import { registerThemingParticipant, ITheme, IThemeService } from 'vs/platform/theme/common/themeService';
import { peekViewEditorBackground, peekViewBorder, } from 'vs/editor/contrib/referenceSearch/referencesWidget';
import { Color } from 'vs/base/common/color';
P
Peng Lyu 已提交
30
import { IMarginData } from 'vs/editor/browser/controller/mouseTarget';
31
import { ICommandService } from 'vs/platform/commands/common/commands';
32
import { Emitter, Event } from 'vs/base/common/event';
P
Peng Lyu 已提交
33
import { editorBackground, editorForeground } from 'vs/platform/theme/common/colorRegistry';
P
Peng Lyu 已提交
34 35
import { ZoneWidget, IOptions } from 'vs/editor/contrib/zoneWidget/zoneWidget';
import { ReviewModel, ReviewStyle } from 'vs/workbench/parts/comments/common/reviewModel';
36
import { ICommentService } from '../../../services/comments/electron-browser/commentService';
P
Peng Lyu 已提交
37 38 39 40 41 42

export const ctxReviewPanelVisible = new RawContextKey<boolean>('reviewPanelVisible', false);
export const ID = 'editor.contrib.review';

declare var ResizeObserver: any;

P
Peng Lyu 已提交
43
const REVIEW_DECORATION = ModelDecorationOptions.register({
P
Peng Lyu 已提交
44
	stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
45
	glyphMarginClassName: 'review'
P
Peng Lyu 已提交
46 47
});

P
Peng Lyu 已提交
48 49 50 51 52
const NEW_COMMENT_DECORATION = ModelDecorationOptions.register({
	stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges,
	glyphMarginClassName: 'new-comment-hint',
});

P
Peng Lyu 已提交
53 54 55 56 57 58 59 60 61
export class ReviewViewZone implements IViewZone {
	public readonly afterLineNumber: number;
	public readonly domNode: HTMLElement;
	private callback: (top: number) => void;

	constructor(afterLineNumber: number, onDomNodeTop: (top: number) => void) {
		this.afterLineNumber = afterLineNumber;
		this.callback = onDomNodeTop;

P
use $.  
Peng Lyu 已提交
62
		this.domNode = $('.review-viewzone').getHTMLElement();
P
Peng Lyu 已提交
63 64 65 66 67 68 69
	}

	onDomNodeTop(top: number): void {
		this.callback(top);
	}
}

P
Peng Lyu 已提交
70 71 72 73 74
export class CommentNode {
	private _domNode: HTMLElement;
	public get domNode(): HTMLElement {
		return this._domNode;
	}
P
Peng Lyu 已提交
75 76
	constructor(public readonly comment: modes.Comment, ) {
		this._domNode = $('div.review-comment').getHTMLElement();
P
Peng Lyu 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
		let avatar = $('span.float-left').appendTo(this._domNode).getHTMLElement();
		let img = <HTMLImageElement>$('img.avatar').appendTo(avatar).getHTMLElement();
		img.src = comment.gravatar;
		let commentDetailsContainer = $('.review-comment-contents').appendTo(this._domNode).getHTMLElement();

		let header = $('h4').appendTo(commentDetailsContainer).getHTMLElement();
		let author = $('strong.author').appendTo(header).getHTMLElement();
		author.innerText = comment.userName;
		let body = $('comment-body').appendTo(commentDetailsContainer).getHTMLElement();
		let md = comment.body;
		body.appendChild(renderMarkdown(md));
	}
}

P
Peng Lyu 已提交
91
export class ReviewZoneWidget extends ZoneWidget {
92 93 94 95 96 97
	private _headElement: HTMLElement;
	protected _primaryHeading: HTMLElement;
	protected _secondaryHeading: HTMLElement;
	protected _metaHeading: HTMLElement;
	protected _actionbarWidget: ActionBar;
	private _bodyElement: HTMLElement;
P
Peng Lyu 已提交
98
	private _commentsElement: HTMLElement;
P
Peng Lyu 已提交
99
	private _commentElements: CommentNode[];
P
Peng Lyu 已提交
100
	private _resizeObserver: any;
101
	private _onDidClose = new Emitter<ReviewZoneWidget>();
102 103
	private _isCollapsed = true;
	private _toggleAction: Action;
P
Peng Lyu 已提交
104 105 106 107
	private _commentThread: modes.CommentThread;
	public get commentThread(): modes.CommentThread {
		return this._commentThread;
	}
P
Peng Lyu 已提交
108

109
	constructor(
110
		editor: ICodeEditor,
P
Peng Lyu 已提交
111
		commentThread: modes.CommentThread,
112
		options: IOptions = {},
113
		private readonly themeService: IThemeService,
114 115
		private readonly commandService: ICommandService
	) {
P
Peng Lyu 已提交
116 117
		super(editor, options);
		this._resizeObserver = null;
P
Peng Lyu 已提交
118
		this._commentThread = commentThread;
P
Peng Lyu 已提交
119
		this.create();
120
		this.themeService.onThemeChange(this._applyTheme, this);
P
Peng Lyu 已提交
121 122
	}

123 124 125 126
	public get onDidClose(): Event<ReviewZoneWidget> {
		return this._onDidClose.event;
	}

127 128 129 130 131 132
	public reveal() {
		if (this._isCollapsed) {
			this._toggleAction.run();
		}
	}

P
Peng Lyu 已提交
133
	protected _fillContainer(container: HTMLElement): void {
134 135 136 137 138
		this.setCssClass('review-widget');
		this._headElement = <HTMLDivElement>$('.head').getHTMLElement();
		container.appendChild(this._headElement);
		this._fillHead(this._headElement);

P
Peng Lyu 已提交
139
		this._bodyElement = <HTMLDivElement>$('.body').getHTMLElement();
140 141 142 143 144 145 146 147 148 149 150 151
		container.appendChild(this._bodyElement);
	}

	protected _fillHead(container: HTMLElement): void {
		var titleElement = $('.review-title').
			appendTo(this._headElement).
			getHTMLElement();

		this._primaryHeading = $('span.filename').appendTo(titleElement).getHTMLElement();
		this._secondaryHeading = $('span.dirname').appendTo(titleElement).getHTMLElement();
		this._metaHeading = $('span.meta').appendTo(titleElement).getHTMLElement();

152
		let primaryHeading = 'Reviewers:';
153 154
		$(this._primaryHeading).safeInnerHtml(primaryHeading);
		this._primaryHeading.setAttribute('aria-label', primaryHeading);
155

P
Peng Lyu 已提交
156
		let secondaryHeading = this._commentThread.comments.filter(arrays.uniqueFilter(comment => comment.userName)).map(comment => `@${comment.userName}`).join(', ');
157 158 159
		$(this._secondaryHeading).safeInnerHtml(secondaryHeading);

		const actionsContainer = $('.review-actions').appendTo(this._headElement);
P
Peng Lyu 已提交
160
		this._actionbarWidget = new ActionBar(actionsContainer.getHTMLElement(), {});
161 162
		this._disposables.push(this._actionbarWidget);

163
		this._toggleAction = new Action('review.expand', nls.localize('label.expand', "Expand"), 'expand-review-action octicon octicon-chevron-down', true, () => {
P
Peng Lyu 已提交
164 165 166 167 168 169 170 171 172 173
			if (this._isCollapsed) {
				this._bodyElement.style.display = 'block';
				this._toggleAction.class = 'expand-review-action octicon octicon-chevron-up';
				this._isCollapsed = false;
			}
			else {
				this._bodyElement.style.display = 'none';
				this._toggleAction.class = 'expand-review-action octicon octicon-chevron-down';
				this._isCollapsed = true;
			}
174
			return null;
175
		});
176

177
		this._actionbarWidget.push(this._toggleAction, { label: false, icon: true });
P
use $.  
Peng Lyu 已提交
178
	}
179

P
use $.  
Peng Lyu 已提交
180
	toggleExpand() {
P
DRY  
Peng Lyu 已提交
181
		this._toggleAction.run();
P
Peng Lyu 已提交
182 183
	}

P
Peng Lyu 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	update(commentThread: modes.CommentThread) {
		const oldCommentsLen = this._commentElements.length;
		const newCommentsLen = commentThread.comments.length;

		let commentElementsToDel: CommentNode[] = [];
		let commentElementsToDelIndex: number[] = [];
		for (let i = 0; i < oldCommentsLen; i++) {
			let comment = this._commentElements[i].comment;
			if (!commentThread.comments.some(c => c.commentId === comment.commentId)) {
				commentElementsToDelIndex.push(i);
				commentElementsToDel.push(this._commentElements[i]);
			}
		}

		// del removed elements
		for (let i = commentElementsToDel.length - 1; i >= 0; i--) {
			this._commentElements.splice(commentElementsToDelIndex[i]);
			this._commentsElement.removeChild(commentElementsToDel[i].domNode);
		}

		if (this._commentElements.length === 0) {
			this._commentThread = commentThread;
			commentThread.comments.forEach(comment => {
				let newElement = new CommentNode(comment);
				this._commentElements.push(newElement);
				this._commentsElement.appendChild(newElement.domNode);
			});
			return;
		}

		let lastCommentElement: HTMLElement = null;
		let newCommentNodeList: CommentNode[] = [];
		for (let i = newCommentsLen - 1; i >= 0; i--) {
			let currentComment = commentThread.comments[i];
			let oldCommentNode = this._commentElements.filter(commentNode => commentNode.comment.commentId === currentComment.commentId);
			if (oldCommentNode.length) {
				lastCommentElement = oldCommentNode[0].domNode;
				newCommentNodeList.unshift(oldCommentNode[0]);
			} else {
				let newElement = new CommentNode(currentComment);
				newCommentNodeList.unshift(newElement);
				if (lastCommentElement) {
					this._commentsElement.insertBefore(newElement.domNode, lastCommentElement);
					lastCommentElement = newElement.domNode;
				} else {
					this._commentsElement.appendChild(newElement.domNode);
					lastCommentElement = newElement.domNode;
				}
			}
		}

		this._commentThread = commentThread;
		this._commentElements = newCommentNodeList;
	}

P
Peng Lyu 已提交
239
	display(lineNumber: number) {
P
Peng Lyu 已提交
240 241
		this.show({ lineNumber: lineNumber, column: 1 }, 2);

242 243 244 245
		var headHeight = Math.ceil(this.editor.getConfiguration().lineHeight * 1.2);
		this._headElement.style.height = `${headHeight}px`;
		this._headElement.style.lineHeight = this._headElement.style.height;

246
		this._bodyElement.style.display = 'none';
P
Peng Lyu 已提交
247
		this._commentsElement = $('div.comments-container').appendTo(this._bodyElement).getHTMLElement();
P
Peng Lyu 已提交
248
		this._commentElements = [];
P
Peng Lyu 已提交
249
		for (let i = 0; i < this._commentThread.comments.length; i++) {
P
Peng Lyu 已提交
250 251 252
			let newCommentNode = new CommentNode(this._commentThread.comments[i]);
			this._commentElements.push(newCommentNode);
			this._commentsElement.appendChild(newCommentNode.domNode);
P
Peng Lyu 已提交
253
		}
254

P
use $.  
Peng Lyu 已提交
255 256 257
		const commentForm = $('.comment-form').appendTo(this._bodyElement).getHTMLElement();
		const textArea = <HTMLTextAreaElement>$('textarea').appendTo(commentForm).getHTMLElement();
		const formActions = $('.form-actions').appendTo(commentForm).getHTMLElement();
258

P
Peng Lyu 已提交
259
		for (const action of this._commentThread.actions) {
P
use $.  
Peng Lyu 已提交
260
			const button = $('button').appendTo(formActions).getHTMLElement();
P
Peng Lyu 已提交
261
			button.onclick = async () => {
P
Peng Lyu 已提交
262
				let newComment = await this.commandService.executeCommand(action.id, this._commentThread.threadId, this.editor.getModel().uri, lineNumber, textArea.value);
P
Peng Lyu 已提交
263 264
				if (newComment) {
					textArea.value = '';
P
Peng Lyu 已提交
265
					this._commentThread.comments.push(newComment);
P
Peng Lyu 已提交
266
					let newCommentNode = new CommentNode(newComment);
P
Peng Lyu 已提交
267 268
					this._commentElements.push(newCommentNode);
					this._commentsElement.appendChild(newCommentNode.domNode);
P
Peng Lyu 已提交
269
					let secondaryHeading = this._commentThread.comments.filter(arrays.uniqueFilter(comment => comment.userName)).map(comment => `@${comment.userName}`).join(', ');
P
Peng Lyu 已提交
270
					$(this._secondaryHeading).safeInnerHtml(secondaryHeading);
P
Peng Lyu 已提交
271
				}
272 273 274 275
			};
			button.textContent = action.title;
		}

P
Peng Lyu 已提交
276
		this._resizeObserver = new ResizeObserver(entries => {
277
			if (entries[0].target === this._bodyElement) {
P
Peng Lyu 已提交
278 279
				const lineHeight = this.editor.getConfiguration().lineHeight;
				const arrowHeight = Math.round(lineHeight / 3);
280
				const computedLinesNumber = Math.ceil((headHeight + entries[0].contentRect.height + arrowHeight) / lineHeight);
P
Peng Lyu 已提交
281 282 283 284
				this._relayout(computedLinesNumber);
			}
		});

285 286 287 288 289 290 291 292 293
		this._resizeObserver.observe(this._bodyElement);
	}

	private _applyTheme(theme: ITheme) {
		let borderColor = theme.getColor(peekViewBorder) || Color.transparent;
		this.style({
			arrowColor: borderColor,
			frameColor: borderColor
		});
P
Peng Lyu 已提交
294 295 296 297
	}

	dispose() {
		super.dispose();
298 299 300 301
		if (this._resizeObserver) {
			this._resizeObserver.disconnect();
			this._resizeObserver = null;
		}
302 303 304 305 306
		this._onDidClose.fire();
	}

}

P
Peng Lyu 已提交
307 308 309 310 311
export class ReviewController implements IEditorContribution {
	private globalToDispose: IDisposable[];
	private localToDispose: IDisposable[];
	private editor: ICodeEditor;
	private decorationIDs: string[];
P
Peng Lyu 已提交
312
	private newCommentHintDecoration: string[];
P
Peng Lyu 已提交
313
	private _zoneWidget: ReviewZoneWidget;
314
	private _zoneWidgets: ReviewZoneWidget[];
P
Peng Lyu 已提交
315
	private _reviewPanelVisible: IContextKey<boolean>;
M
Matt Bierner 已提交
316
	private _commentThreads: modes.CommentThread[];
P
Peng Lyu 已提交
317
	private _newCommentActions: modes.NewCommentAction[];
318
	private _reviewModel: ReviewModel;
P
Peng Lyu 已提交
319 320 321

	constructor(
		editor: ICodeEditor,
322
		@IContextKeyService contextKeyService: IContextKeyService,
323 324
		@IThemeService private themeService: IThemeService,
		@ICommandService private commandService: ICommandService,
325
		@ICommentService private commentService: ICommentService
P
Peng Lyu 已提交
326 327 328 329 330
	) {
		this.editor = editor;
		this.globalToDispose = [];
		this.localToDispose = [];
		this.decorationIDs = [];
P
Peng Lyu 已提交
331
		this.newCommentHintDecoration = [];
P
Peng Lyu 已提交
332
		this.mouseDownInfo = null;
M
Matt Bierner 已提交
333
		this._commentThreads = [];
P
Peng Lyu 已提交
334
		this._newCommentActions = [];
335
		this._zoneWidgets = [];
336
		this._zoneWidget = null;
P
Peng Lyu 已提交
337 338

		this._reviewPanelVisible = ctxReviewPanelVisible.bindTo(contextKeyService);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
		this._reviewModel = new ReviewModel();

		this._reviewModel.onDidChangeStyle(style => {
			if (style === ReviewStyle.Gutter) {
				this._zoneWidgets.forEach(zone => {
					zone.dispose();
				});
				this._zoneWidgets = [];

				this.editor.changeDecorations(accessor => {
					this.decorationIDs = accessor.deltaDecorations(this.decorationIDs, this._commentThreads.map(thread => ({
						range: thread.range,
						options: REVIEW_DECORATION
					})));
				});
			} else {
				this.editor.changeDecorations(accessor => {
					this.decorationIDs = accessor.deltaDecorations(this.decorationIDs, []);
				});

				if (this._zoneWidget) {
					this._zoneWidget.dispose();
					this._zoneWidget = null;
				}

				this._zoneWidgets.forEach(zone => {
					zone.dispose();
				});

				this._commentThreads.forEach(thread => {
P
Peng Lyu 已提交
369 370
					let zoneWidget = new ReviewZoneWidget(this.editor, thread, {}, this.themeService, this.commandService);
					zoneWidget.display(thread.range.startLineNumber);
371 372 373 374
					this._zoneWidgets.push(zoneWidget);
				});
			}
		});
P
Peng Lyu 已提交
375

P
Peng Lyu 已提交
376
		this.globalToDispose.push(this.commentService.onDidSetResourceCommentThreads(e => {
377 378 379 380
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (editorURI && editorURI.toString() === e.resource.toString()) {
				this.setComments(e.commentThreads);
			}
P
Peng Lyu 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
		}));

		this.globalToDispose.push(this.commentService.onDidUpdateCommentThreads(e => {
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (!editorURI) {
				return;
			}
			let added = e.added.filter(thread => thread.resource.toString() === editorURI.toString());
			let removed = e.removed.filter(thread => thread.resource.toString() === editorURI.toString());
			let changed = e.changed.filter(thread => thread.resource.toString() === editorURI.toString());

			removed.forEach(thread => {
				let matchedZones = this._zoneWidgets.filter(zoneWidget => zoneWidget.commentThread.threadId === thread.threadId);
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
					let index = this._zoneWidgets.indexOf(matchedZone);
					this._zoneWidgets.splice(index, 1);
				}
			});

			changed.forEach(thread => {
				let matchedZones = this._zoneWidgets.filter(zoneWidget => zoneWidget.commentThread.threadId === thread.threadId);
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
					matchedZone.update(thread);
				}
			});
			added.forEach(thread => {
				let zoneWidget = new ReviewZoneWidget(this.editor, thread, {}, this.themeService, this.commandService);
				zoneWidget.display(thread.range.startLineNumber);
				this._zoneWidgets.push(zoneWidget);
				this._commentThreads.push(thread);
			});
		}));
415

P
Peng Lyu 已提交
416 417 418 419 420 421 422
		this.globalToDispose.push(this.editor.onDidChangeModel(() => this.onModelChanged()));
	}

	public static get(editor: ICodeEditor): ReviewController {
		return editor.getContribution<ReviewController>(ID);
	}

423
	public revealCommentThread(threadId: string): void {
P
Peng Lyu 已提交
424
		const commentThreadWidget = this._zoneWidgets.filter(widget => widget.commentThread.threadId === threadId);
425 426 427 428 429
		if (commentThreadWidget.length === 1) {
			commentThreadWidget[0].reveal();
		}
	}

P
Peng Lyu 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	getId(): string {
		return ID;
	}
	dispose(): void {
		this.globalToDispose = dispose(this.globalToDispose);
		this.localToDispose = dispose(this.localToDispose);

		if (this._zoneWidget) {
			this._zoneWidget.dispose();
			this._zoneWidget = null;
		}
		this.editor = null;
	}

	public onModelChanged(): void {
		this.localToDispose = dispose(this.localToDispose);
P
Peng Lyu 已提交
446 447 448 449 450
		if (this._zoneWidget) {
			// todo store view state.
			this._zoneWidget.dispose();
			this._zoneWidget = null;
		}
451 452 453 454 455

		this._zoneWidgets.forEach(zone => {
			zone.dispose();
		});
		this._zoneWidgets = [];
456

P
Peng Lyu 已提交
457 458
		this.localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this.localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
P
Peng Lyu 已提交
459
		this.localToDispose.push(this.editor.onMouseMove(e => this.onEditorMouseMove(e)));
P
Peng Lyu 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	}

	private mouseDownInfo: { lineNumber: number, iconClicked: boolean };

	private onEditorMouseDown(e: IEditorMouseEvent): void {
		if (!e.event.leftButton) {
			return;
		}

		let range = e.target.range;
		if (!range) {
			return;
		}

		let iconClicked = false;
		switch (e.target.type) {
476
			case MouseTargetType.GUTTER_GLYPH_MARGIN:
P
Peng Lyu 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
				iconClicked = true;
				break;
			default:
				return;
		}

		this.mouseDownInfo = { lineNumber: range.startLineNumber, iconClicked };
	}

	private onEditorMouseUp(e: IEditorMouseEvent): void {
		if (!this.mouseDownInfo) {
			return;
		}
		let lineNumber = this.mouseDownInfo.lineNumber;
		let iconClicked = this.mouseDownInfo.iconClicked;

		let range = e.target.range;
		if (!range || range.startLineNumber !== lineNumber) {
			return;
		}

		if (iconClicked) {
499
			if (e.target.type !== MouseTargetType.GUTTER_GLYPH_MARGIN) {
P
Peng Lyu 已提交
500 501 502 503 504 505 506 507
				return;
			}
		}

		if (this._zoneWidget && this._zoneWidget.position.lineNumber === lineNumber) {
			return;
		}

P
Peng Lyu 已提交
508
		if (this.marginFreeFromCommentHintDecorations(lineNumber)) {
P
Peng Lyu 已提交
509 510 511 512 513 514 515
			let newCommentAction = this.getNewCommentAction(lineNumber);
			if (!newCommentAction) {
				return;
			}

			// add new comment
			this._reviewPanelVisible.set(true);
P
Peng Lyu 已提交
516 517 518 519 520 521 522 523 524 525 526 527
			this._zoneWidget = new ReviewZoneWidget(this.editor, {
				threadId: null,
				resource: null,
				comments: [],
				range: {
					startLineNumber: lineNumber,
					startColumn: 0,
					endLineNumber: lineNumber,
					endColumn: 0
				},
				actions: newCommentAction.actions
			}, {}, this.themeService, this.commandService);
P
Peng Lyu 已提交
528 529 530
			this._zoneWidget.onDidClose(e => {
				this._zoneWidget = null;
			});
P
Peng Lyu 已提交
531
			this._zoneWidget.display(lineNumber);
P
use $.  
Peng Lyu 已提交
532
			this._zoneWidget.toggleExpand();
533
		}
P
Peng Lyu 已提交
534 535
	}

P
Peng Lyu 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
	private onEditorMouseMove(e: IEditorMouseEvent): void {
		let showNewCommentHintAtLineNumber = -1;
		if (e.target.type === MouseTargetType.GUTTER_GLYPH_MARGIN
			&& this.marginFreeFromCommentHintDecorations(e.target.position.lineNumber)) {
			const data = e.target.detail as IMarginData;
			if (!data.isAfterLines) {
				showNewCommentHintAtLineNumber = e.target.position.lineNumber;
			}
		}
		this.ensureNewCommentHintDecoration(showNewCommentHintAtLineNumber);
	}

	ensureNewCommentHintDecoration(showNewCommentHintAtLineNumber: number) {
		const newDecoration: IModelDeltaDecoration[] = [];
		if (showNewCommentHintAtLineNumber !== -1) {
			newDecoration.push({
				options: NEW_COMMENT_DECORATION,
				range: {
					startLineNumber: showNewCommentHintAtLineNumber,
					startColumn: 1,
					endLineNumber: showNewCommentHintAtLineNumber,
					endColumn: 1
				}
			});
		}

		this.newCommentHintDecoration = this.editor.deltaDecorations(this.newCommentHintDecoration, newDecoration);
	}

P
Peng Lyu 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	getNewCommentAction(line: number): modes.NewCommentAction {
		let allowNewComment = false;

		for (let i = 0; i < this._newCommentActions.length; i++) {
			let newCommentAction = this._newCommentActions[i];

			for (let j = 0; j < newCommentAction.ranges.length; j++) {
				if (newCommentAction.ranges[j].startLineNumber <= line && newCommentAction.ranges[j].endLineNumber >= line) {
					allowNewComment = true;
					break;
				}
			}

			if (allowNewComment) {
				return newCommentAction;
			}
		}

		return null;
	}

P
Peng Lyu 已提交
586 587 588
	marginFreeFromCommentHintDecorations(line: number): boolean {
		let allowNewComment = false;

P
Peng Lyu 已提交
589 590 591 592 593 594 595 596
		for (let i = 0; i < this._newCommentActions.length; i++) {
			let newCommentAction = this._newCommentActions[i];

			for (let j = 0; j < newCommentAction.ranges.length; j++) {
				if (newCommentAction.ranges[j].startLineNumber <= line && newCommentAction.ranges[j].endLineNumber >= line) {
					allowNewComment = true;
					break;
				}
P
Peng Lyu 已提交
597 598
			}

P
Peng Lyu 已提交
599 600 601
			if (allowNewComment) {
				break;
			}
P
Peng Lyu 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
		}

		if (!allowNewComment) {
			return false;
		}

		const decorations = this.editor.getLineDecorations(line);
		if (decorations) {
			for (const { options } of decorations) {
				if (options.glyphMarginClassName && options.glyphMarginClassName.indexOf('review') > -1) {
					return false;
				}
			}
		}

		return true;
	}

620
	getCommentThread(line: number): modes.CommentThread | undefined {
M
Matt Bierner 已提交
621 622
		for (let i = 0; i < this._commentThreads.length; i++) {
			if (this._commentThreads[i].range.startLineNumber === line) {
623
				return this._commentThreads[i];
M
Matt Bierner 已提交
624 625 626
			}
		}

627
		return undefined;
M
Matt Bierner 已提交
628 629
	}

P
Peng Lyu 已提交
630 631 632 633
	setNewCommentActions(newCommentActions: modes.NewCommentAction[]) {
		this._newCommentActions = newCommentActions;
	}

M
Matt Bierner 已提交
634 635
	setComments(commentThreads: modes.CommentThread[]): void {
		this._commentThreads = commentThreads;
636

637 638 639 640
		if (this._commentThreads.length === 0) {
			return;
		}

641 642 643 644 645 646 647 648 649 650 651 652
		if (this._reviewModel.style === ReviewStyle.Gutter) {
			this.editor.changeDecorations(accessor => {
				this.decorationIDs = accessor.deltaDecorations(this.decorationIDs, commentThreads.map(thread => ({
					range: thread.range,
					options: REVIEW_DECORATION
				})));
			});
		} else {
			// create viewzones
			this._zoneWidgets.forEach(zone => {
				zone.dispose();
			});
653

654
			this._commentThreads.forEach(thread => {
P
Peng Lyu 已提交
655 656
				let zoneWidget = new ReviewZoneWidget(this.editor, thread, {}, this.themeService, this.commandService);
				zoneWidget.display(thread.range.startLineNumber);
657 658 659
				this._zoneWidgets.push(zoneWidget);
			});
		}
M
Matt Bierner 已提交
660 661 662
	}


P
Peng Lyu 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
	public closeWidget(): void {
		this._reviewPanelVisible.reset();

		if (this._zoneWidget) {
			this._zoneWidget.dispose();
			this._zoneWidget = null;
		}

		this.editor.focus();
	}
}

registerEditorContribution(ReviewController);


KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: 'closeReviewPanel',
	weight: KeybindingsRegistry.WEIGHT.editorContrib(),
	primary: KeyCode.Escape,
	secondary: [KeyMod.Shift | KeyCode.Escape],
	when: ctxReviewPanelVisible,
	handler: closeReviewPanel
});

export function getOuterEditor(accessor: ServicesAccessor): ICodeEditor {
	let editor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
	if (editor instanceof EmbeddedCodeEditorWidget) {
		return editor.getParentEditor();
	}
	return editor;
}

function closeReviewPanel(accessor: ServicesAccessor, args: any) {
	var outerEditor = getOuterEditor(accessor);
	if (!outerEditor) {
		return;
	}

	let controller = ReviewController.get(outerEditor);

	if (!controller) {
		return;
	}

	controller.closeWidget();
708 709 710 711
}


registerThemingParticipant((theme, collector) => {
P
Peng Lyu 已提交
712 713
	let peekViewBackground = theme.getColor(peekViewEditorBackground);
	if (peekViewBackground) {
714 715 716
		collector.addRule(
			`.monaco-editor .review-widget,` +
			`.monaco-editor .review-widget {` +
P
Peng Lyu 已提交
717
			`	background-color: ${peekViewBackground};` +
718 719
			`}`);
	}
P
Peng Lyu 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737

	let monacoEditorBackground = theme.getColor(editorBackground);
	if (monacoEditorBackground) {
		collector.addRule(
			`.monaco-editor .review-widget .body textarea {` +
			`	background-color: ${monacoEditorBackground}` +
			`}`
		);
	}

	let monacoEditorForeground = theme.getColor(editorForeground);
	if (monacoEditorForeground) {
		collector.addRule(
			`.monaco-editor .review-widget .body textarea {` +
			`	color: ${monacoEditorForeground}` +
			`}`
		);
	}
738
});