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

P
Peng Lyu 已提交
6
import 'vs/css!./media/review';
P
Peng Lyu 已提交
7
import * as nls from 'vs/nls';
8
import { $ } from 'vs/base/browser/dom';
P
Peng Lyu 已提交
9
import { findFirstInSorted } from 'vs/base/common/arrays';
10
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
P
Peng Lyu 已提交
11
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
12
import { ICodeEditor, IEditorMouseEvent, IViewZone, MouseTargetType } from 'vs/editor/browser/editorBrowser';
P
Peng Lyu 已提交
13
import { registerEditorContribution, EditorAction, registerEditorAction } from 'vs/editor/browser/editorExtensions';
P
Peng Lyu 已提交
14 15
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { EmbeddedCodeEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
R
rebornix 已提交
16
import { IEditorContribution, IModelChangedEvent } from 'vs/editor/common/editorCommon';
P
Peng Lyu 已提交
17
import { IRange } from 'vs/editor/common/core/range';
18
import * as modes from 'vs/editor/common/modes';
M
Miguel Solorio 已提交
19
import { peekViewResultsBackground, peekViewResultsSelectionBackground, peekViewTitleBackground } from 'vs/editor/contrib/referenceSearch/referencesWidget';
20
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
P
Peng Lyu 已提交
21
import { ServicesAccessor, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
22
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
23
import { editorForeground, registerColor } from 'vs/platform/theme/common/colorRegistry';
24
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
25
import { CommentThreadCollapsibleState } from 'vs/workbench/api/node/extHostTypes';
P
Peng Lyu 已提交
26
import { ReviewZoneWidget, COMMENTEDITOR_DECORATION_KEY } from 'vs/workbench/parts/comments/electron-browser/commentThreadWidget';
27
import { ICommentService } from 'vs/workbench/parts/comments/electron-browser/commentService';
P
Peng Lyu 已提交
28 29
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
30
import { IOpenerService } from 'vs/platform/opener/common/opener';
31 32 33 34
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
import { IModelDecorationOptions } from 'vs/editor/common/model';
import { Color, RGBA } from 'vs/base/common/color';
import { IMarginData } from 'vs/editor/browser/controller/mouseTarget';
35
import { INotificationService } from 'vs/platform/notification/common/notification';
36
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
P
Peng Lyu 已提交
37 38

export const ctxReviewPanelVisible = new RawContextKey<boolean>('reviewPanelVisible', false);
39

P
Peng Lyu 已提交
40 41 42 43 44 45 46 47 48 49 50
export const ID = 'editor.contrib.review';

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;

51
		this.domNode = $('.review-viewzone');
P
Peng Lyu 已提交
52 53 54 55 56 57 58
	}

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

P
Peng Lyu 已提交
59
const overviewRulerDefault = new Color(new RGBA(197, 197, 197, 1));
60

61
export const overviewRulerCommentingRangeForeground = registerColor('editorGutter.commentRangeForeground', { dark: overviewRulerDefault, light: overviewRulerDefault, hc: overviewRulerDefault }, nls.localize('editorGutterCommentRangeForeground', 'Editor gutter decoration color for commenting ranges.'));
62

P
Peng Lyu 已提交
63 64 65
class CommentingRangeDecoration {
	private _decorationId: string;

66 67 68 69
	get id(): string {
		return this._decorationId;
	}

P
Peng Lyu 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
	constructor(private _editor: ICodeEditor, private _ownerId: number, private _range: IRange, private _reply: modes.Command, commentingOptions: ModelDecorationOptions) {
		const startLineNumber = _range.startLineNumber;
		const endLineNumber = _range.endLineNumber;
		let commentingRangeDecorations = [{
			range: {
				startLineNumber: startLineNumber, startColumn: 1,
				endLineNumber: endLineNumber, endColumn: 1
			},
			options: commentingOptions
		}];

		let model = this._editor.getModel();
		if (model) {
			this._decorationId = model.deltaDecorations([this._decorationId], commentingRangeDecorations)[0];
		}
	}

	getCommentAction(): { replyCommand: modes.Command, ownerId: number } {
		return {
			replyCommand: this._reply,
			ownerId: this._ownerId
		};
	}

	getOriginalRange() {
		return this._range;
	}

	getActiveRange() {
		return this._editor.getModel().getDecorationRange(this._decorationId);
	}
}
102 103 104 105 106 107 108
class CommentingRangeDecorator {

	static createDecoration(className: string, foregroundColor: string, options: { gutter: boolean, overview: boolean }): ModelDecorationOptions {
		const decorationOptions: IModelDecorationOptions = {
			isWholeLine: true,
		};

109
		decorationOptions.linesDecorationsClassName = `comment-range-glyph ${className}`;
110 111 112 113
		return ModelDecorationOptions.createDynamic(decorationOptions);
	}

	private commentingOptions: ModelDecorationOptions;
114
	public commentsOptions: ModelDecorationOptions;
P
Peng Lyu 已提交
115
	private commentingRangeDecorations: CommentingRangeDecoration[] = [];
116 117 118 119 120 121
	private disposables: IDisposable[] = [];

	constructor(
	) {
		const options = { gutter: true, overview: false };
		this.commentingOptions = CommentingRangeDecorator.createDecoration('comment-diff-added', overviewRulerCommentingRangeForeground, options);
122
		this.commentsOptions = CommentingRangeDecorator.createDecoration('comment-thread', overviewRulerCommentingRangeForeground, options);
123 124
	}

P
Peng Lyu 已提交
125
	update(editor: ICodeEditor, commentInfos: modes.CommentInfo[]) {
126 127 128 129 130
		let model = editor.getModel();
		if (!model) {
			return;
		}

M
Matt Bierner 已提交
131
		let commentingRangeDecorations: CommentingRangeDecoration[] = [];
P
Peng Lyu 已提交
132 133 134 135 136 137
		for (let i = 0; i < commentInfos.length; i++) {
			let info = commentInfos[i];
			info.commentingRanges.forEach(range => {
				commentingRangeDecorations.push(new CommentingRangeDecoration(editor, info.owner, range, info.reply, this.commentingOptions));
			});
		}
138

139 140 141
		let oldDecorations = this.commentingRangeDecorations.map(decoration => decoration.id);
		editor.deltaDecorations(oldDecorations, []);

P
Peng Lyu 已提交
142
		this.commentingRangeDecorations = commentingRangeDecorations;
143 144
	}

P
Peng Lyu 已提交
145
	getMatchedCommentAction(line: number) {
146
		for (let i = 0; i < this.commentingRangeDecorations.length; i++) {
P
Peng Lyu 已提交
147 148 149 150 151
			let range = this.commentingRangeDecorations[i].getActiveRange();

			if (range.startLineNumber <= line && line <= range.endLineNumber) {
				return this.commentingRangeDecorations[i].getCommentAction();
			}
152 153
		}

P
Peng Lyu 已提交
154
		return null;
155 156 157 158
	}

	dispose(): void {
		this.disposables = dispose(this.disposables);
159
		this.commentingRangeDecorations = [];
160 161 162
	}
}

P
Peng Lyu 已提交
163 164 165 166
export class ReviewController implements IEditorContribution {
	private globalToDispose: IDisposable[];
	private localToDispose: IDisposable[];
	private editor: ICodeEditor;
R
Rachel Macfarlane 已提交
167 168
	private _newCommentWidget: ReviewZoneWidget;
	private _commentWidgets: ReviewZoneWidget[];
P
Peng Lyu 已提交
169
	private _reviewPanelVisible: IContextKey<boolean>;
170
	private _commentInfos: modes.CommentInfo[];
171 172
	private _commentingRangeDecorator: CommentingRangeDecorator;
	private mouseDownInfo: { lineNumber: number } | null = null;
173
	private _commentingRangeSpaceReserved = false;
174

175
	private _pendingCommentCache: { [key: number]: { [key: string]: string } };
R
rebornix 已提交
176
	private _pendingNewCommentCache: { [key: string]: { lineNumber: number, replyCommand: modes.Command, ownerId: number, pendingComment: string } };
P
Peng Lyu 已提交
177 178 179

	constructor(
		editor: ICodeEditor,
180
		@IContextKeyService contextKeyService: IContextKeyService,
181
		@IThemeService private themeService: IThemeService,
182
		@ICommentService private commentService: ICommentService,
183
		@INotificationService private notificationService: INotificationService,
P
Peng Lyu 已提交
184 185 186 187
		@IInstantiationService private instantiationService: IInstantiationService,
		@IModeService private modeService: IModeService,
		@IModelService private modelService: IModelService,
		@ICodeEditorService private codeEditorService: ICodeEditorService,
188 189
		@IOpenerService private openerService: IOpenerService,
		@IDialogService private dialogService: IDialogService
P
Peng Lyu 已提交
190 191 192 193
	) {
		this.editor = editor;
		this.globalToDispose = [];
		this.localToDispose = [];
194
		this._commentInfos = [];
R
Rachel Macfarlane 已提交
195
		this._commentWidgets = [];
196
		this._pendingCommentCache = {};
R
rebornix 已提交
197
		this._pendingNewCommentCache = {};
R
Rachel Macfarlane 已提交
198
		this._newCommentWidget = null;
P
Peng Lyu 已提交
199 200

		this._reviewPanelVisible = ctxReviewPanelVisible.bindTo(contextKeyService);
201
		this._commentingRangeDecorator = new CommentingRangeDecorator();
202

203
		this.globalToDispose.push(this.commentService.onDidDeleteDataProvider(ownerId => {
204
			// Remove new comment widget and glyph, refresh comments
205
			if (this._newCommentWidget && this._newCommentWidget.owner === ownerId) {
206 207 208 209
				this._newCommentWidget.dispose();
				this._newCommentWidget = null;
			}

R
rebornix 已提交
210
			delete this._pendingNewCommentCache[ownerId];
211
			delete this._pendingCommentCache[ownerId];
212
			this.recomputeComments();
213
		}));
214
		this.globalToDispose.push(this.commentService.onDidSetDataProvider(_ => this.recomputeComments()));
215

216
		this.globalToDispose.push(this.commentService.onDidSetResourceCommentInfos(e => {
217 218
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (editorURI && editorURI.toString() === e.resource.toString()) {
219
				this.setComments(e.commentInfos.filter(commentInfo => commentInfo !== null));
220
			}
P
Peng Lyu 已提交
221 222
		}));

R
rebornix 已提交
223
		this.globalToDispose.push(this.editor.onDidChangeModel(e => this.onModelChanged(e)));
P
Peng Lyu 已提交
224
		this.codeEditorService.registerDecorationType(COMMENTEDITOR_DECORATION_KEY, {});
P
Peng Lyu 已提交
225 226
	}

227
	private recomputeComments(): void {
228 229 230 231 232 233 234 235 236
		const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;

		if (editorURI) {
			this.commentService.getComments(editorURI).then(commentInfos => {
				this.setComments(commentInfos.filter(commentInfo => commentInfo !== null));
			}, error => console.log(error));
		}
	}

P
Peng Lyu 已提交
237 238 239 240
	public static get(editor: ICodeEditor): ReviewController {
		return editor.getContribution<ReviewController>(ID);
	}

P
Peng Lyu 已提交
241
	public revealCommentThread(threadId: string, commentId?: string): void {
R
Rachel Macfarlane 已提交
242
		const commentThreadWidget = this._commentWidgets.filter(widget => widget.commentThread.threadId === threadId);
243
		if (commentThreadWidget.length === 1) {
P
Peng Lyu 已提交
244
			commentThreadWidget[0].reveal(commentId);
245 246 247
		}
	}

P
Peng Lyu 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	public nextCommentThread(): void {
		if (!this._commentWidgets.length) {
			return;
		}

		const after = this.editor.getSelection().getEndPosition();
		const sortedWidgets = this._commentWidgets.sort((a, b) => {
			if (a.commentThread.range.startLineNumber < b.commentThread.range.startLineNumber) {
				return -1;
			}

			if (a.commentThread.range.startLineNumber > b.commentThread.range.startLineNumber) {
				return 1;
			}

			if (a.commentThread.range.startColumn < b.commentThread.range.startColumn) {
				return -1;
			}

			if (a.commentThread.range.startColumn > b.commentThread.range.startColumn) {
				return 1;
			}

			return 0;
		});

		let idx = findFirstInSorted(sortedWidgets, widget => {
			if (widget.commentThread.range.startLineNumber > after.lineNumber) {
				return true;
			}

			if (widget.commentThread.range.startLineNumber < after.lineNumber) {
				return false;
			}

			if (widget.commentThread.range.startColumn > after.column) {
				return true;
			}
			return false;
		});

		if (idx === this._commentWidgets.length) {
			this._commentWidgets[0].reveal();
			this.editor.setSelection(this._commentWidgets[0].commentThread.range);
		} else {
			sortedWidgets[idx].reveal();
			this.editor.setSelection(sortedWidgets[idx].commentThread.range);
		}
	}

P
Peng Lyu 已提交
298 299 300
	getId(): string {
		return ID;
	}
301

P
Peng Lyu 已提交
302 303 304 305
	dispose(): void {
		this.globalToDispose = dispose(this.globalToDispose);
		this.localToDispose = dispose(this.localToDispose);

306 307
		this._commentWidgets.forEach(widget => widget.dispose());

R
Rachel Macfarlane 已提交
308 309 310
		if (this._newCommentWidget) {
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
311 312 313 314
		}
		this.editor = null;
	}

R
rebornix 已提交
315
	public onModelChanged(e: IModelChangedEvent): void {
P
Peng Lyu 已提交
316
		this.localToDispose = dispose(this.localToDispose);
R
Rachel Macfarlane 已提交
317
		if (this._newCommentWidget) {
R
rebornix 已提交
318 319 320 321 322 323 324 325 326 327 328 329
			let pendingNewComment = this._newCommentWidget.getPendingComment();

			if (pendingNewComment && e.oldModelUrl) {
				// we can't fetch zone widget's position as the model is already gone
				this._pendingNewCommentCache[e.oldModelUrl.toString()] = {
					lineNumber: this._newCommentWidget.position.lineNumber,
					ownerId: this._newCommentWidget.owner,
					replyCommand: this._newCommentWidget.commentThread.reply,
					pendingComment: pendingNewComment
				};
			}

R
Rachel Macfarlane 已提交
330 331
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
332
		}
333

334
		this.removeCommentWidgetsAndStoreCache();
335

R
rebornix 已提交
336 337 338 339 340
		if (e.newModelUrl && this._pendingNewCommentCache[e.newModelUrl.toString()]) {
			let newCommentCache = this._pendingNewCommentCache[e.newModelUrl.toString()];
			this.addComment(newCommentCache.lineNumber, newCommentCache.replyCommand, newCommentCache.ownerId, newCommentCache.pendingComment);
		}

341 342
		this.localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this.localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
343 344
		this.localToDispose.push(this.editor.onDidChangeModelContent(() => {
		}));
P
Peng Lyu 已提交
345 346 347 348 349
		this.localToDispose.push(this.commentService.onDidUpdateCommentThreads(e => {
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (!editorURI) {
				return;
			}
350 351 352 353 354

			if (!this._commentInfos.some(info => info.owner === e.owner)) {
				return;
			}

P
Peng Lyu 已提交
355 356 357 358 359
			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 => {
R
Rachel Macfarlane 已提交
360
				let matchedZones = this._commentWidgets.filter(zoneWidget => zoneWidget.owner === e.owner && zoneWidget.commentThread.threadId === thread.threadId);
P
Peng Lyu 已提交
361 362
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
R
Rachel Macfarlane 已提交
363 364
					let index = this._commentWidgets.indexOf(matchedZone);
					this._commentWidgets.splice(index, 1);
P
Peng Lyu 已提交
365 366 367 368
				}
			});

			changed.forEach(thread => {
R
Rachel Macfarlane 已提交
369
				let matchedZones = this._commentWidgets.filter(zoneWidget => zoneWidget.owner === e.owner && zoneWidget.commentThread.threadId === thread.threadId);
P
Peng Lyu 已提交
370 371 372 373 374 375
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
					matchedZone.update(thread);
				}
			});
			added.forEach(thread => {
376
				let zoneWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.notificationService, this.editor, e.owner, thread, null, {});
377
				zoneWidget.display(thread.range.startLineNumber, this._commentingRangeDecorator.commentsOptions);
R
Rachel Macfarlane 已提交
378
				this._commentWidgets.push(zoneWidget);
379
				this._commentInfos.filter(info => info.owner === e.owner)[0].threads.push(thread);
P
Peng Lyu 已提交
380 381
			});
		}));
P
Peng Lyu 已提交
382 383
	}

R
rebornix 已提交
384
	private addComment(lineNumber: number, replyCommand: modes.Command, ownerId: number, pendingComment: string) {
385 386 387 388 389
		if (this._newCommentWidget !== null) {
			this.notificationService.warn(`Please submit the comment at line ${this._newCommentWidget.position.lineNumber} before creating a new one.`);
			return;
		}

390 391
		// add new comment
		this._reviewPanelVisible.set(true);
392
		this._newCommentWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.notificationService, this.editor, ownerId, {
393 394 395 396 397 398 399 400 401
			threadId: null,
			resource: null,
			comments: [],
			range: {
				startLineNumber: lineNumber,
				startColumn: 0,
				endLineNumber: lineNumber,
				endColumn: 0
			},
402
			reply: replyCommand,
403
			collapsibleState: CommentThreadCollapsibleState.Expanded,
R
rebornix 已提交
404
		}, pendingComment, {});
405

406
		this.localToDispose.push(this._newCommentWidget.onDidClose(e => {
R
Rachel Macfarlane 已提交
407
			this._newCommentWidget = null;
408 409 410 411 412 413 414 415 416
		}));

		this.localToDispose.push(this._newCommentWidget.onDidCreateThread(commentWidget => {
			const thread = commentWidget.commentThread;
			this._commentWidgets.push(commentWidget);
			this._commentInfos.filter(info => info.owner === commentWidget.owner)[0].threads.push(thread);
			this._newCommentWidget = null;
		}));

417
		this._newCommentWidget.display(lineNumber, this._commentingRangeDecorator.commentsOptions);
P
Peng Lyu 已提交
418 419
	}

420 421 422 423 424 425
	private onEditorMouseDown(e: IEditorMouseEvent): void {
		this.mouseDownInfo = null;

		const range = e.target.range;

		if (!range) {
426 427 428
			return;
		}

429 430 431 432 433 434 435 436 437 438 439
		if (!e.event.leftButton) {
			return;
		}

		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
			return;
		}

		const data = e.target.detail as IMarginData;
		const gutterOffsetX = data.offsetX - data.glyphMarginWidth - data.lineNumbersWidth - data.glyphMarginLeft;

440
		// don't collide with folding and git decorations
441
		if (gutterOffsetX > 14) {
442 443 444 445 446 447 448 449 450 451
			return;
		}

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

	private onEditorMouseUp(e: IEditorMouseEvent): void {
		if (!this.mouseDownInfo) {
			return;
		}
452

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
		const { lineNumber } = this.mouseDownInfo;
		this.mouseDownInfo = null;

		const range = e.target.range;

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

		if (e.target.type !== MouseTargetType.GUTTER_LINE_DECORATIONS) {
			return;
		}

		if (!e.target.element) {
			return;
		}

470
		if (e.target.element.className.indexOf('comment-diff-added') >= 0) {
R
Rachel Macfarlane 已提交
471
			const lineNumber = e.target.position.lineNumber;
R
rebornix 已提交
472 473 474 475 476 477
			let newCommentInfo = this._commentingRangeDecorator.getMatchedCommentAction(lineNumber);
			if (!newCommentInfo) {
				return;
			}
			const { replyCommand, ownerId } = newCommentInfo;
			this.addComment(lineNumber, replyCommand, ownerId, null);
478
		}
P
Peng Lyu 已提交
479 480
	}

481 482
	setComments(commentInfos: modes.CommentInfo[]): void {
		this._commentInfos = commentInfos;
483 484 485 486 487
		let lineDecorationsWidth: number = this.editor.getConfiguration().layoutInfo.decorationsWidth;

		if (this._commentInfos.some(info => Boolean(info.commentingRanges && info.commentingRanges.length))) {
			if (!this._commentingRangeSpaceReserved) {
				this._commentingRangeSpaceReserved = true;
M
Matt Bierner 已提交
488
				let extraEditorClassName: string[] = [];
489 490 491 492 493 494 495 496 497 498 499 500 501
				if (this.editor.getRawConfiguration().extraEditorClassName) {
					extraEditorClassName = this.editor.getRawConfiguration().extraEditorClassName.split(' ');
				}

				if (this.editor.getConfiguration().contribInfo.folding) {
					lineDecorationsWidth -= 16;
				}
				lineDecorationsWidth += 9;
				extraEditorClassName.push('inline-comment');
				this.editor.updateOptions({
					extraEditorClassName: extraEditorClassName.join(' '),
					lineDecorationsWidth: lineDecorationsWidth
				});
502 503 504 505 506 507 508 509

				// we only update the lineDecorationsWidth property but keep the width of the whole editor.
				const originalLayoutInfo = this.editor.getLayoutInfo();

				this.editor.layout({
					width: originalLayoutInfo.width,
					height: originalLayoutInfo.height
				});
510 511 512
			}
		}

513
		// create viewzones
514
		this.removeCommentWidgetsAndStoreCache();
515

516
		this._commentInfos.forEach(info => {
517
			let providerCacheStore = this._pendingCommentCache[info.owner];
518
			info.threads.forEach(thread => {
519 520 521
				let pendingComment: string = null;
				if (providerCacheStore) {
					pendingComment = providerCacheStore[thread.threadId];
R
rebornix 已提交
522 523 524
				}

				if (pendingComment) {
525
					thread.collapsibleState = modes.CommentThreadCollapsibleState.Expanded;
526
				}
R
rebornix 已提交
527

528
				let zoneWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.notificationService, this.editor, info.owner, thread, pendingComment, {});
529
				zoneWidget.display(thread.range.startLineNumber, this._commentingRangeDecorator.commentsOptions);
R
Rachel Macfarlane 已提交
530
				this._commentWidgets.push(zoneWidget);
531 532
			});
		});
533

M
Matt Bierner 已提交
534
		const commentingRanges: IRange[] = [];
535 536 537
		this._commentInfos.forEach(info => {
			commentingRanges.push(...info.commentingRanges);
		});
P
Peng Lyu 已提交
538
		this._commentingRangeDecorator.update(this.editor, this._commentInfos);
M
Matt Bierner 已提交
539 540
	}

P
Peng Lyu 已提交
541 542 543
	public closeWidget(): void {
		this._reviewPanelVisible.reset();

R
Rachel Macfarlane 已提交
544 545 546
		if (this._newCommentWidget) {
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
547 548
		}

R
Rachel Macfarlane 已提交
549
		if (this._commentWidgets) {
550
			this._commentWidgets.forEach(widget => widget.hide());
551 552
		}

P
Peng Lyu 已提交
553
		this.editor.focus();
554
		this.editor.revealRangeInCenter(this.editor.getSelection());
P
Peng Lyu 已提交
555
	}
556 557 558 559 560

	removeCommentWidgetsAndStoreCache() {
		if (this._commentWidgets) {
			this._commentWidgets.forEach(zone => {
				let pendingComment = zone.getPendingComment();
R
rebornix 已提交
561
				let providerCacheStore = this._pendingCommentCache[zone.owner];
562

R
rebornix 已提交
563
				if (pendingComment) {
564 565 566 567
					if (!providerCacheStore) {
						this._pendingCommentCache[zone.owner] = {};
					}

R
rebornix 已提交
568 569 570 571 572
					this._pendingCommentCache[zone.owner][zone.commentThread.threadId] = pendingComment;
				} else {
					if (providerCacheStore) {
						delete providerCacheStore[zone.commentThread.threadId];
					}
573 574 575 576 577 578 579 580
				}

				zone.dispose();
			});
		}

		this._commentWidgets = [];
	}
P
Peng Lyu 已提交
581 582
}

P
Peng Lyu 已提交
583
export class NextCommentThreadAction extends EditorAction {
P
Peng Lyu 已提交
584

P
Peng Lyu 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	constructor() {
		super({
			id: 'editor.action.nextCommentThreadAction',
			label: nls.localize('nextCommentThreadAction', "Go to Next Comment Thread"),
			alias: 'Go to Next Comment Thread',
			precondition: null,
		});
	}

	public run(accessor: ServicesAccessor, editor: ICodeEditor): void {
		let controller = ReviewController.get(editor);
		if (controller) {
			controller.nextCommentThread();
		}
	}
}

registerEditorContribution(ReviewController);
registerEditorAction(NextCommentThreadAction);
P
Peng Lyu 已提交
604 605 606

KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: 'closeReviewPanel',
607
	weight: KeybindingWeight.EditorContrib,
P
Peng Lyu 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	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();
635 636 637 638
}


registerThemingParticipant((theme, collector) => {
M
Matt Bierner 已提交
639
	const peekViewBackground = theme.getColor(peekViewResultsBackground);
P
Peng Lyu 已提交
640
	if (peekViewBackground) {
641 642 643
		collector.addRule(
			`.monaco-editor .review-widget,` +
			`.monaco-editor .review-widget {` +
P
Peng Lyu 已提交
644
			`	background-color: ${peekViewBackground};` +
645 646
			`}`);
	}
P
Peng Lyu 已提交
647

M
Matt Bierner 已提交
648
	const monacoEditorBackground = theme.getColor(peekViewTitleBackground);
P
Peng Lyu 已提交
649 650
	if (monacoEditorBackground) {
		collector.addRule(
P
Peng Lyu 已提交
651 652
			`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button {` +
			`	background-color: ${monacoEditorBackground}` +
P
Peng Lyu 已提交
653 654 655 656
			`}`
		);
	}

M
Matt Bierner 已提交
657
	const monacoEditorForeground = theme.getColor(editorForeground);
P
Peng Lyu 已提交
658 659
	if (monacoEditorForeground) {
		collector.addRule(
P
Peng Lyu 已提交
660
			`.monaco-editor .review-widget .body .monaco-editor {` +
P
Peng Lyu 已提交
661
			`	color: ${monacoEditorForeground}` +
P
Peng Lyu 已提交
662 663 664
			`}` +
			`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button {` +
			`	color: ${monacoEditorForeground}` +
P
Peng Lyu 已提交
665 666 667
			`}`
		);
	}
P
Peng Lyu 已提交
668

M
Matt Bierner 已提交
669
	const selectionBackground = theme.getColor(peekViewResultsSelectionBackground);
P
Peng Lyu 已提交
670 671 672 673 674 675 676 677 678 679 680 681

	if (selectionBackground) {
		collector.addRule(
			`@keyframes monaco-review-widget-focus {` +
			`	0% { background: ${selectionBackground}; }` +
			`	100% { background: transparent; }` +
			`}` +
			`.monaco-editor .review-widget .body .review-comment.focus {` +
			`	animation: monaco-review-widget-focus 3s ease 0s;` +
			`}`
		);
	}
682 683 684 685 686 687 688 689 690 691

	const commentingRangeForeground = theme.getColor(overviewRulerCommentingRangeForeground);
	if (commentingRangeForeground) {
		collector.addRule(`
			.monaco-editor .comment-diff-added {
				border-left: 3px solid ${commentingRangeForeground};
			}
			.monaco-editor .comment-diff-added:before {
				background: ${commentingRangeForeground};
			}
692 693 694 695 696 697
			.monaco-editor .comment-thread {
				border-left: 3px solid ${commentingRangeForeground};
			}
			.monaco-editor .comment-thread:before {
				background: ${commentingRangeForeground};
			}
698 699
		`);
	}
700
});