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/builder';
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';
27
import { ReviewModel } from 'vs/workbench/parts/comments/common/reviewModel';
P
Peng Lyu 已提交
28
import { ReviewZoneWidget, COMMENTEDITOR_DECORATION_KEY } from 'vs/workbench/parts/comments/electron-browser/commentThreadWidget';
29
import { ICommentService } from 'vs/workbench/parts/comments/electron-browser/commentService';
P
Peng Lyu 已提交
30 31
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
32
import { IOpenerService } from 'vs/platform/opener/common/opener';
33 34 35 36
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';
37
import { INotificationService } from 'vs/platform/notification/common/notification';
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;

P
use $.  
Peng Lyu 已提交
52
		this.domNode = $('.review-viewzone').getHTMLElement();
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
	private _reviewModel: ReviewModel;
173 174 175
	// private _hasSetComments: boolean;
	private _commentingRangeDecorator: CommentingRangeDecorator;
	private mouseDownInfo: { lineNumber: number } | null = null;
176
	private _commentingRangeSpaceReserved = false;
177

P
Peng Lyu 已提交
178 179 180

	constructor(
		editor: ICodeEditor,
181
		@IContextKeyService contextKeyService: IContextKeyService,
182
		@IThemeService private themeService: IThemeService,
183
		@ICommentService private commentService: ICommentService,
184
		@INotificationService private notificationService: INotificationService,
P
Peng Lyu 已提交
185 186 187 188
		@IInstantiationService private instantiationService: IInstantiationService,
		@IModeService private modeService: IModeService,
		@IModelService private modelService: IModelService,
		@ICodeEditorService private codeEditorService: ICodeEditorService,
189
		@IOpenerService private openerService: IOpenerService
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._reviewModel = new ReviewModel();
201
		this._commentingRangeDecorator = new CommentingRangeDecorator();
202 203

		this._reviewModel.onDidChangeStyle(style => {
R
Rachel Macfarlane 已提交
204 205 206
			if (this._newCommentWidget) {
				this._newCommentWidget.dispose();
				this._newCommentWidget = null;
207
			}
208

R
Rachel Macfarlane 已提交
209
			this._commentWidgets.forEach(zone => {
210 211
				zone.dispose();
			});
212

213 214
			this._commentInfos.forEach(info => {
				info.threads.forEach(thread => {
215
					let zoneWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.editor, info.owner, thread, {});
216
					zoneWidget.display(thread.range.startLineNumber, this._commentingRangeDecorator.commentsOptions);
R
Rachel Macfarlane 已提交
217
					this._commentWidgets.push(zoneWidget);
218
				});
219
			});
220
		});
P
Peng Lyu 已提交
221

222 223 224 225 226 227 228 229 230 231
		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();
		}));

232
		this.globalToDispose.push(this.commentService.onDidSetResourceCommentInfos(e => {
233 234
			const editorURI = this.editor && this.editor.getModel() && this.editor.getModel().uri;
			if (editorURI && editorURI.toString() === e.resource.toString()) {
235
				this.setComments(e.commentInfos.filter(commentInfo => commentInfo !== null));
236
			}
P
Peng Lyu 已提交
237 238
		}));

239
		this.globalToDispose.push(this.commentService.onDidSetDataProvider(_ => this.getComments()));
240

P
Peng Lyu 已提交
241
		this.globalToDispose.push(this.editor.onDidChangeModel(() => this.onModelChanged()));
P
Peng Lyu 已提交
242
		this.codeEditorService.registerDecorationType(COMMENTEDITOR_DECORATION_KEY, {});
P
Peng Lyu 已提交
243 244
	}

245 246 247 248 249 250 251 252 253 254
	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 已提交
255 256 257 258
	public static get(editor: ICodeEditor): ReviewController {
		return editor.getContribution<ReviewController>(ID);
	}

P
Peng Lyu 已提交
259
	public revealCommentThread(threadId: string, commentId?: string): void {
R
Rachel Macfarlane 已提交
260
		const commentThreadWidget = this._commentWidgets.filter(widget => widget.commentThread.threadId === threadId);
261
		if (commentThreadWidget.length === 1) {
P
Peng Lyu 已提交
262
			commentThreadWidget[0].reveal(commentId);
263 264 265
		}
	}

P
Peng Lyu 已提交
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
	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 已提交
316 317 318
	getId(): string {
		return ID;
	}
319

P
Peng Lyu 已提交
320 321 322 323
	dispose(): void {
		this.globalToDispose = dispose(this.globalToDispose);
		this.localToDispose = dispose(this.localToDispose);

324 325
		this._commentWidgets.forEach(widget => widget.dispose());

R
Rachel Macfarlane 已提交
326 327 328
		if (this._newCommentWidget) {
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
329 330 331 332 333 334
		}
		this.editor = null;
	}

	public onModelChanged(): void {
		this.localToDispose = dispose(this.localToDispose);
R
Rachel Macfarlane 已提交
335
		if (this._newCommentWidget) {
B
Benjamin Pasero 已提交
336
			// todo@peng store view state.
R
Rachel Macfarlane 已提交
337 338
			this._newCommentWidget.dispose();
			this._newCommentWidget = null;
P
Peng Lyu 已提交
339
		}
340

R
Rachel Macfarlane 已提交
341
		this._commentWidgets.forEach(zone => {
342 343
			zone.dispose();
		});
R
Rachel Macfarlane 已提交
344
		this._commentWidgets = [];
345

346 347
		this.localToDispose.push(this.editor.onMouseDown(e => this.onEditorMouseDown(e)));
		this.localToDispose.push(this.editor.onMouseUp(e => this.onEditorMouseUp(e)));
348 349
		this.localToDispose.push(this.editor.onDidChangeModelContent(() => {
		}));
P
Peng Lyu 已提交
350 351 352 353 354 355 356 357 358 359
		this.localToDispose.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 => {
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.editor, e.owner, thread, {});
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
	}

384
	private addComment(lineNumber: number) {
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;
		}

P
Peng Lyu 已提交
390
		let newCommentInfo = this._commentingRangeDecorator.getMatchedCommentAction(lineNumber);
391 392
		if (!newCommentInfo) {
			return;
P
Peng Lyu 已提交
393
		}
394 395 396

		// add new comment
		this._reviewPanelVisible.set(true);
397
		const { replyCommand, ownerId } = newCommentInfo;
398
		this._newCommentWidget = new ReviewZoneWidget(this.instantiationService, this.modeService, this.modelService, this.themeService, this.commentService, this.openerService, this.editor, ownerId, {
399 400 401 402 403 404 405 406 407
			threadId: null,
			resource: null,
			comments: [],
			range: {
				startLineNumber: lineNumber,
				startColumn: 0,
				endLineNumber: lineNumber,
				endColumn: 0
			},
408
			reply: replyCommand,
409
			collapsibleState: CommentThreadCollapsibleState.Expanded,
410
		}, {});
411

R
Rachel Macfarlane 已提交
412 413
		this._newCommentWidget.onDidClose(e => {
			this._newCommentWidget = null;
414
		});
415
		this._newCommentWidget.display(lineNumber, this._commentingRangeDecorator.commentsOptions);
P
Peng Lyu 已提交
416 417
	}

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

		const range = e.target.range;

		if (!range) {
424 425 426
			return;
		}

427 428 429 430 431 432 433 434 435 436 437
		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;

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

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

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

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
		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;
		}

468
		if (e.target.element.className.indexOf('comment-diff-added') >= 0) {
R
Rachel Macfarlane 已提交
469
			const lineNumber = e.target.position.lineNumber;
P
Peng Lyu 已提交
470
			this.addComment(lineNumber);
471
		}
P
Peng Lyu 已提交
472 473
	}

474 475
	setComments(commentInfos: modes.CommentInfo[]): void {
		this._commentInfos = commentInfos;
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
		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
				});
				this.editor.layout();
			}
		}

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