commentsView.ts 10.8 KB
Newer Older
R
Rachel Macfarlane 已提交
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.
 *--------------------------------------------------------------------------------------------*/

R
Rachel Macfarlane 已提交
6
import 'vs/css!./media/panel';
7
import * as nls from 'vs/nls';
8
import * as dom from 'vs/base/browser/dom';
9
import { basename, isEqual } from 'vs/base/common/resources';
10 11
import { IAction, Action } from 'vs/base/common/actions';
import { CollapseAllAction } from 'vs/base/browser/ui/tree/treeDefaults';
12
import { isCodeEditor } from 'vs/editor/browser/editorBrowser';
13
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
R
Rachel Macfarlane 已提交
14
import { IThemeService } from 'vs/platform/theme/common/themeService';
15
import { CommentNode, CommentsModel, ResourceWithCommentThreads, ICommentThreadChangedEvent } from 'vs/workbench/contrib/comments/common/commentModel';
16
import { CommentController } from 'vs/workbench/contrib/comments/browser/commentsEditorContribution';
17
import { IWorkspaceCommentThreadsEvent, ICommentService } from 'vs/workbench/contrib/comments/browser/commentService';
R
rebornix 已提交
18
import { IEditorService, ACTIVE_GROUP, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
19
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
20
import { textLinkForeground, textLinkActiveForeground, focusBorder, textPreformatForeground } from 'vs/platform/theme/common/colorRegistry';
B
Benjamin Pasero 已提交
21
import { ResourceLabels } from 'vs/workbench/browser/labels';
22 23 24 25 26 27 28 29
import { CommentsList, COMMENTS_VIEW_ID, COMMENTS_VIEW_TITLE } from 'vs/workbench/contrib/comments/browser/commentsTreeViewer';
import { ViewPane, IViewPaneOptions } from 'vs/workbench/browser/parts/views/viewPaneContainer';
import { IViewDescriptorService, IViewsService } from 'vs/workbench/common/views';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IOpenerService } from 'vs/platform/opener/common/opener';
30
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
R
Rachel Macfarlane 已提交
31

32
export class CommentsPanel extends ViewPane {
33 34 35 36 37 38 39
	private treeLabels!: ResourceLabels;
	private tree!: CommentsList;
	private treeContainer!: HTMLElement;
	private messageBoxContainer!: HTMLElement;
	private messageBox!: HTMLElement;
	private commentsModel!: CommentsModel;
	private collapseAllAction?: IAction;
R
Rachel Macfarlane 已提交
40

41 42
	readonly onDidChangeVisibility = this.onDidChangeBodyVisibility;

R
Rachel Macfarlane 已提交
43
	constructor(
44 45 46
		options: IViewPaneOptions,
		@IInstantiationService readonly instantiationService: IInstantiationService,
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
47
		@IEditorService private readonly editorService: IEditorService,
48 49 50 51 52
		@IConfigurationService configurationService: IConfigurationService,
		@IContextKeyService contextKeyService: IContextKeyService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IKeybindingService keybindingService: IKeybindingService,
		@IOpenerService openerService: IOpenerService,
B
Benjamin Pasero 已提交
53
		@IThemeService themeService: IThemeService,
54 55
		@ICommentService private readonly commentService: ICommentService,
		@ITelemetryService telemetryService: ITelemetryService,
R
Rachel Macfarlane 已提交
56
	) {
57
		super(options, keybindingService, contextMenuService, configurationService, contextKeyService, viewDescriptorService, instantiationService, openerService, themeService, telemetryService);
R
Rachel Macfarlane 已提交
58 59
	}

60 61
	public renderBody(container: HTMLElement): void {
		super.renderBody(container);
R
Rachel Macfarlane 已提交
62

R
rebornix 已提交
63
		container.classList.add('comments-panel');
R
Rachel Macfarlane 已提交
64

65 66
		let domContainer = dom.append(container, dom.$('.comments-panel-container'));
		this.treeContainer = dom.append(domContainer, dom.$('.tree-container'));
67
		this.commentsModel = new CommentsModel();
R
Rachel Macfarlane 已提交
68 69

		this.createTree();
70
		this.createMessageBox(domContainer);
R
Rachel Macfarlane 已提交
71

72 73
		this._register(this.commentService.onDidSetAllCommentThreads(this.onAllCommentsChanged, this));
		this._register(this.commentService.onDidUpdateCommentThreads(this.onCommentsUpdated, this));
R
Rachel Macfarlane 已提交
74

75
		const styleElement = dom.createStyleSheet(container);
76
		this.applyStyles(styleElement);
M
Martin Aeschlimann 已提交
77
		this._register(this.themeService.onDidColorThemeChange(_ => this.applyStyles(styleElement)));
78

79
		this._register(this.onDidChangeBodyVisibility(visible => {
80 81 82 83
			if (visible) {
				this.refresh();
			}
		}));
84

85
		this.renderComments();
R
Rachel Macfarlane 已提交
86 87
	}

88 89 90
	private applyStyles(styleElement: HTMLStyleElement) {
		const content: string[] = [];

M
Martin Aeschlimann 已提交
91
		const theme = this.themeService.getColorTheme();
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
		const linkColor = theme.getColor(textLinkForeground);
		if (linkColor) {
			content.push(`.comments-panel .comments-panel-container a { color: ${linkColor}; }`);
		}

		const linkActiveColor = theme.getColor(textLinkActiveForeground);
		if (linkActiveColor) {
			content.push(`.comments-panel .comments-panel-container a:hover, a:active { color: ${linkActiveColor}; }`);
		}

		const focusColor = theme.getColor(focusBorder);
		if (focusColor) {
			content.push(`.comments-panel .commenst-panel-container a:focus { outline-color: ${focusColor}; }`);
		}

107 108 109 110 111
		const codeTextForegroundColor = theme.getColor(textPreformatForeground);
		if (codeTextForegroundColor) {
			content.push(`.comments-panel .comments-panel-container .text code { color: ${codeTextForegroundColor}; }`);
		}

112
		styleElement.textContent = content.join('\n');
113 114
	}

115
	private async renderComments(): Promise<void> {
116
		this.treeContainer.classList.toggle('hidden', !this.commentsModel.hasCommentThreads());
S
Sandeep Somavarapu 已提交
117 118
		await this.tree.setInput(this.commentsModel);
		this.renderMessage();
R
Rachel Macfarlane 已提交
119 120
	}

121 122
	public getActions(): IAction[] {
		if (!this.collapseAllAction) {
123
			this.collapseAllAction = new Action('vs.tree.collapse', nls.localize('collapseAll', "Collapse All"), 'collapse-all', true, () => this.tree ? new CollapseAllAction<any, any>(this.tree, true).run() : Promise.resolve());
B
Benjamin Pasero 已提交
124
			this._register(this.collapseAllAction);
125 126 127 128 129
		}

		return [this.collapseAllAction];
	}

130
	public layoutBody(height: number, width: number): void {
J
João Moreno 已提交
131
		super.layoutBody(height, width);
132
		this.tree.layout(height, width);
R
Rachel Macfarlane 已提交
133 134 135
	}

	public getTitle(): string {
136
		return COMMENTS_VIEW_TITLE;
R
Rachel Macfarlane 已提交
137 138
	}

139 140 141 142 143 144 145 146
	private createMessageBox(parent: HTMLElement): void {
		this.messageBoxContainer = dom.append(parent, dom.$('.message-box-container'));
		this.messageBox = dom.append(this.messageBoxContainer, dom.$('span'));
		this.messageBox.setAttribute('tabindex', '0');
	}

	private renderMessage(): void {
		this.messageBox.textContent = this.commentsModel.getMessage();
147
		this.messageBoxContainer.classList.toggle('hidden', this.commentsModel.hasCommentThreads());
148 149
	}

R
Rachel Macfarlane 已提交
150
	private createTree(): void {
151
		this.treeLabels = this._register(this.instantiationService.createInstance(ResourceLabels, this));
152 153 154
		this.tree = this._register(this.instantiationService.createInstance(CommentsList, this.treeLabels, this.treeContainer, {
			overrideStyles: { listBackground: this.getBackgroundColor() },
			openOnFocus: true,
R
rebornix 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
			accessibilityProvider: {
				getAriaLabel(element: any): string {
					if (element instanceof CommentsModel) {
						return nls.localize('rootCommentsLabel', "Comments for current workspace");
					}
					if (element instanceof ResourceWithCommentThreads) {
						return nls.localize('resourceWithCommentThreadsLabel', "Comments in {0}, full path {1}", basename(element.resource), element.resource.fsPath);
					}
					if (element instanceof CommentNode) {
						return nls.localize('resourceWithCommentLabel',
							"Comment from ${0} at line {1} column {2} in {3}, source: {4}",
							element.comment.userName,
							element.range.startLineNumber,
							element.range.startColumn,
							basename(element.resource),
							element.comment.body.value
						);
					}
					return '';
				},
				getWidgetAriaLabel(): string {
					return COMMENTS_VIEW_TITLE;
				}
178 179
			}
		}));
B
Benjamin Pasero 已提交
180

181
		this._register(this.tree.onDidOpen(e => {
182
			this.openFile(e.element, e.editorOptions.pinned, e.editorOptions.preserveFocus, e.sideBySide);
183
		}));
R
Rachel Macfarlane 已提交
184
	}
185

186
	private openFile(element: any, pinned?: boolean, preserveFocus?: boolean, sideBySide?: boolean): boolean {
187
		if (!element) {
188 189 190
			return false;
		}

191
		if (!(element instanceof ResourceWithCommentThreads || element instanceof CommentNode)) {
192 193 194 195
			return false;
		}

		const range = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].range : element.range;
P
Peng Lyu 已提交
196

B
Benjamin Pasero 已提交
197
		const activeEditor = this.editorService.activeEditor;
198
		let currentActiveResource = activeEditor ? activeEditor.resource : undefined;
199
		if (currentActiveResource && isEqual(currentActiveResource, element.resource)) {
P
Peng Lyu 已提交
200
			const threadToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].threadId : element.threadId;
P
Peng Lyu 已提交
201
			const commentToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].comment.uniqueIdInThread : element.comment.uniqueIdInThread;
202
			const control = this.editorService.activeTextEditorControl;
P
Peng Lyu 已提交
203
			if (threadToReveal && isCodeEditor(control)) {
204
				const controller = CommentController.get(control);
205
				controller.revealCommentThread(threadToReveal, commentToReveal, false);
P
Peng Lyu 已提交
206
			}
R
Rachel Macfarlane 已提交
207 208

			return true;
P
Peng Lyu 已提交
209 210
		}

211 212 213
		const threadToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].threadId : element.threadId;
		const commentToReveal = element instanceof ResourceWithCommentThreads ? element.commentThreads[0].comment : element.comment;

214 215 216 217 218 219 220 221 222 223 224
		this.editorService.openEditor({
			resource: element.resource,
			options: {
				pinned: pinned,
				preserveFocus: preserveFocus,
				selection: range
			}
		}, sideBySide ? SIDE_GROUP : ACTIVE_GROUP).then(editor => {
			if (editor) {
				const control = editor.getControl();
				if (threadToReveal && isCodeEditor(control)) {
225
					const controller = CommentController.get(control);
P
Peng Lyu 已提交
226
					controller.revealCommentThread(threadToReveal, commentToReveal.uniqueIdInThread, true);
227
				}
228 229
			}
		});
230

231 232
		return true;
	}
233 234 235

	private refresh(): void {
		if (this.isVisible()) {
236 237 238
			if (this.collapseAllAction) {
				this.collapseAllAction.enabled = this.commentsModel.hasCommentThreads();
			}
239

240
			this.treeContainer.classList.toggle('hidden', !this.commentsModel.hasCommentThreads());
241
			this.tree.updateChildren().then(() => {
242 243 244 245 246 247 248
				this.renderMessage();
			}, (e) => {
				console.log(e);
			});
		}
	}

249 250
	private onAllCommentsChanged(e: IWorkspaceCommentThreadsEvent): void {
		this.commentsModel.setCommentThreads(e.ownerId, e.commentThreads);
251 252 253
		this.refresh();
	}

254
	private onCommentsUpdated(e: ICommentThreadChangedEvent): void {
255 256 257 258
		const didUpdate = this.commentsModel.updateCommentThreads(e);
		if (didUpdate) {
			this.refresh();
		}
259
	}
R
Rachel Macfarlane 已提交
260
}
261 262 263

CommandsRegistry.registerCommand({
	id: 'workbench.action.focusCommentsPanel',
S
SteVen Batten 已提交
264
	handler: async (accessor) => {
265 266
		const viewsService = accessor.get(IViewsService);
		viewsService.openView(COMMENTS_VIEW_ID, true);
267
	}
268
});