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

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

P
Peng Lyu 已提交
41 42 43 44 45 46 47 48 49 50 51
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;

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

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

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

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

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

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

P
Peng Lyu 已提交
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 102
	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);
	}
}
103 104 105 106 107 108 109
class CommentingRangeDecorator {

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

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

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

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

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

P
Peng Lyu 已提交
132 133 134 135 136 137 138
		let commentingRangeDecorations = [];
		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));
			});
		}
139

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

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

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

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

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

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

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

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 196
		this._commentWidgets = [];
		this._newCommentWidget = null;
197
		// this._hasSetComments = false;
P
Peng Lyu 已提交
198 199

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

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

			this.getComments();
		}));

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

219
		this.globalToDispose.push(this.commentService.onDidSetDataProvider(_ => this.getComments()));
220

P
Peng Lyu 已提交
221
		this.globalToDispose.push(this.editor.onDidChangeModel(() => this.onModelChanged()));
P
Peng Lyu 已提交
222
		this.codeEditorService.registerDecorationType(COMMENTEDITOR_DECORATION_KEY, {});
P
Peng Lyu 已提交
223 224
	}

225 226 227 228 229 230 231 232 233 234
	private getComments(): void {
		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 已提交
235 236 237 238
	public static get(editor: ICodeEditor): ReviewController {
		return editor.getContribution<ReviewController>(ID);
	}

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

P
Peng Lyu 已提交
246 247 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
	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 已提交
296 297 298
	getId(): string {
		return ID;
	}
299

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

304 305
		this._commentWidgets.forEach(widget => widget.dispose());

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

	public onModelChanged(): void {
		this.localToDispose = dispose(this.localToDispose);
R
Rachel Macfarlane 已提交
315
		if (this._newCommentWidget) {
B
Benjamin Pasero 已提交
316
			// todo@peng store view state.
R
Rachel Macfarlane 已提交
317 318
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
319
		}
320

R
Rachel Macfarlane 已提交
321
		this._commentWidgets.forEach(zone => {
322 323
			zone.dispose();
		});
R
Rachel Macfarlane 已提交
324
		this._commentWidgets = [];
325

326 327
		this.localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this.localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
328 329
		this.localToDispose.push(this.editor.onDidChangeModelContent(() => {
		}));
P
Peng Lyu 已提交
330 331 332 333 334
		this.localToDispose.push(this.commentService.onDidUpdateCommentThreads(e => {
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (!editorURI) {
				return;
			}
335 336 337 338 339

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

P
Peng Lyu 已提交
340 341 342 343 344
			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 已提交
345
				let matchedZones = this._commentWidgets.filter(zoneWidget => zoneWidget.owner === e.owner && zoneWidget.commentThread.threadId === thread.threadId);
P
Peng Lyu 已提交
346 347
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
R
Rachel Macfarlane 已提交
348 349
					let index = this._commentWidgets.indexOf(matchedZone);
					this._commentWidgets.splice(index, 1);
P
Peng Lyu 已提交
350 351 352 353
				}
			});

			changed.forEach(thread => {
R
Rachel Macfarlane 已提交
354
				let matchedZones = this._commentWidgets.filter(zoneWidget => zoneWidget.owner === e.owner && zoneWidget.commentThread.threadId === thread.threadId);
P
Peng Lyu 已提交
355 356 357 358 359 360
				if (matchedZones.length) {
					let matchedZone = matchedZones[0];
					matchedZone.update(thread);
				}
			});
			added.forEach(thread => {
361
				let zoneWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.editor, e.owner, thread, {});
362
				zoneWidget.display(thread.range.startLineNumber, this._commentingRangeDecorator.commentsOptions);
R
Rachel Macfarlane 已提交
363
				this._commentWidgets.push(zoneWidget);
364
				this._commentInfos.filter(info => info.owner === e.owner)[0].threads.push(thread);
P
Peng Lyu 已提交
365 366
			});
		}));
P
Peng Lyu 已提交
367 368
	}

369
	private addComment(lineNumber: number) {
370 371 372 373 374
		if (this._newCommentWidget !== null) {
			this.notificationService.warn(`Please submit the comment at line ${this._newCommentWidget.position.lineNumber} before creating a new one.`);
			return;
		}

P
Peng Lyu 已提交
375
		let newCommentInfo = this._commentingRangeDecorator.getMatchedCommentAction(lineNumber);
376 377
		if (!newCommentInfo) {
			return;
P
Peng Lyu 已提交
378
		}
379 380 381

		// add new comment
		this._reviewPanelVisible.set(true);
382
		const { replyCommand, ownerId } = newCommentInfo;
383
		this._newCommentWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.editor, ownerId, {
384 385 386 387 388 389 390 391 392
			threadId: null,
			resource: null,
			comments: [],
			range: {
				startLineNumber: lineNumber,
				startColumn: 0,
				endLineNumber: lineNumber,
				endColumn: 0
			},
393
			reply: replyCommand,
394
			collapsibleState: CommentThreadCollapsibleState.Expanded,
395
		}, {});
396

397
		this.localToDispose.push(this._newCommentWidget.onDidClose(e => {
R
Rachel Macfarlane 已提交
398
			this._newCommentWidget = null;
399 400 401 402 403 404 405 406 407
		}));

		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;
		}));

408
		this._newCommentWidget.display(lineNumber, this._commentingRangeDecorator.commentsOptions);
P
Peng Lyu 已提交
409 410
	}

411 412 413 414 415 416
	private onEditorMouseDown(e: IEditorMouseEvent): void {
		this.mouseDownInfo = null;

		const range = e.target.range;

		if (!range) {
417 418 419
			return;
		}

420 421 422 423 424 425 426 427 428 429 430
		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;

431
		// don't collide with folding and git decorations
432
		if (gutterOffsetX > 14) {
433 434 435 436 437 438 439 440 441 442
			return;
		}

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

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

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
		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;
		}

461
		if (e.target.element.className.indexOf('comment-diff-added') >= 0) {
R
Rachel Macfarlane 已提交
462
			const lineNumber = e.target.position.lineNumber;
P
Peng Lyu 已提交
463
			this.addComment(lineNumber);
464
		}
P
Peng Lyu 已提交
465 466
	}

467 468
	setComments(commentInfos: modes.CommentInfo[]): void {
		this._commentInfos = commentInfos;
469 470 471 472 473 474 475 476 477 478 479 480 481 482 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;
				let extraEditorClassName = [];
				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
				});
488 489 490 491 492 493 494 495

				// 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
				});
496 497 498
			}
		}

499
		// this._hasSetComments = true;
500

501
		// create viewzones
R
Rachel Macfarlane 已提交
502
		this._commentWidgets.forEach(zone => {
503 504 505
			zone.dispose();
		});

506 507
		this._commentWidgets = [];

508 509
		this._commentInfos.forEach(info => {
			info.threads.forEach(thread => {
510
				let zoneWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.dialogService, this.editor, info.owner, thread, {});
511
				zoneWidget.display(thread.range.startLineNumber, this._commentingRangeDecorator.commentsOptions);
R
Rachel Macfarlane 已提交
512
				this._commentWidgets.push(zoneWidget);
513 514
			});
		});
515 516

		const commentingRanges = [];
517 518 519
		this._commentInfos.forEach(info => {
			commentingRanges.push(...info.commentingRanges);
		});
P
Peng Lyu 已提交
520
		this._commentingRangeDecorator.update(this.editor, this._commentInfos);
M
Matt Bierner 已提交
521 522
	}

P
Peng Lyu 已提交
523 524 525
	public closeWidget(): void {
		this._reviewPanelVisible.reset();

R
Rachel Macfarlane 已提交
526 527 528
		if (this._newCommentWidget) {
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
529 530
		}

R
Rachel Macfarlane 已提交
531
		if (this._commentWidgets) {
532
			this._commentWidgets.forEach(widget => widget.hide());
533 534
		}

P
Peng Lyu 已提交
535
		this.editor.focus();
536
		this.editor.revealRangeInCenter(this.editor.getSelection());
P
Peng Lyu 已提交
537 538 539
	}
}

P
Peng Lyu 已提交
540
export class NextCommentThreadAction extends EditorAction {
P
Peng Lyu 已提交
541

P
Peng Lyu 已提交
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	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 已提交
561 562 563

KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: 'closeReviewPanel',
564
	weight: KeybindingWeight.EditorContrib,
P
Peng Lyu 已提交
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	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();
592 593 594 595
}


registerThemingParticipant((theme, collector) => {
P
Peng Lyu 已提交
596
	let peekViewBackground = theme.getColor(peekViewResultsBackground);
P
Peng Lyu 已提交
597
	if (peekViewBackground) {
598 599 600
		collector.addRule(
			`.monaco-editor .review-widget,` +
			`.monaco-editor .review-widget {` +
P
Peng Lyu 已提交
601
			`	background-color: ${peekViewBackground};` +
602 603
			`}`);
	}
P
Peng Lyu 已提交
604

605
	let monacoEditorBackground = theme.getColor(peekViewTitleBackground);
P
Peng Lyu 已提交
606 607
	if (monacoEditorBackground) {
		collector.addRule(
P
Peng Lyu 已提交
608 609
			`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button {` +
			`	background-color: ${monacoEditorBackground}` +
P
Peng Lyu 已提交
610 611 612 613 614 615 616
			`}`
		);
	}

	let monacoEditorForeground = theme.getColor(editorForeground);
	if (monacoEditorForeground) {
		collector.addRule(
P
Peng Lyu 已提交
617
			`.monaco-editor .review-widget .body .monaco-editor {` +
P
Peng Lyu 已提交
618
			`	color: ${monacoEditorForeground}` +
P
Peng Lyu 已提交
619 620 621
			`}` +
			`.monaco-editor .review-widget .body .comment-form .review-thread-reply-button {` +
			`	color: ${monacoEditorForeground}` +
P
Peng Lyu 已提交
622 623 624
			`}`
		);
	}
P
Peng Lyu 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638

	let selectionBackground = theme.getColor(peekViewResultsSelectionBackground);

	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;` +
			`}`
		);
	}
639 640 641 642 643 644 645 646 647 648

	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};
			}
649 650 651 652 653 654
			.monaco-editor .comment-thread {
				border-left: 3px solid ${commentingRangeForeground};
			}
			.monaco-editor .comment-thread:before {
				background: ${commentingRangeForeground};
			}
655 656
		`);
	}
657
});