markers.contribution.ts 14.7 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

6 7
import 'vs/workbench/contrib/markers/browser/markersFileDecorations';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
8 9 10
import { Extensions, IConfigurationRegistry } from 'vs/platform/configuration/common/configurationRegistry';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
11
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
12 13
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { localize } from 'vs/nls';
S
Sandeep Somavarapu 已提交
14
import { Marker, RelatedInformation } from 'vs/workbench/contrib/markers/browser/markersModel';
S
Sandeep Somavarapu 已提交
15
import { MarkersView, getMarkersView } from 'vs/workbench/contrib/markers/browser/markersView';
16
import { MenuId, MenuRegistry, SyncActionDescriptor, registerAction2, Action2 } from 'vs/platform/actions/common/actions';
17
import { TogglePanelAction } from 'vs/workbench/browser/panel';
18
import { Registry } from 'vs/platform/registry/common/platform';
S
Sandeep Somavarapu 已提交
19
import { ShowProblemsPanelAction } from 'vs/workbench/contrib/markers/browser/markersViewActions';
S
Sandeep Somavarapu 已提交
20 21
import Constants from 'vs/workbench/contrib/markers/browser/constants';
import Messages from 'vs/workbench/contrib/markers/browser/messages';
22
import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions, IWorkbenchContribution } from 'vs/workbench/common/contributions';
S
Sandeep Somavarapu 已提交
23
import { IMarkersWorkbenchService, MarkersWorkbenchService, ActivityUpdater } from 'vs/workbench/contrib/markers/browser/markers';
S
Sandeep Somavarapu 已提交
24 25
import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
S
Sandeep Somavarapu 已提交
26
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
27
import { ActivePanelContext } from 'vs/workbench/common/panel';
28
import { Disposable } from 'vs/base/common/lifecycle';
29
import { IStatusbarEntryAccessor, IStatusbarService, StatusbarAlignment, IStatusbarEntry } from 'vs/workbench/services/statusbar/common/statusbar';
30
import { IMarkerService, MarkerStatistics } from 'vs/platform/markers/common/markers';
31
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
32
import { ViewContainer, IViewContainersRegistry, Extensions as ViewContainerExtensions, ViewContainerLocation, IViewsRegistry } from 'vs/workbench/common/views';
S
Sandeep Somavarapu 已提交
33
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
S
Sandeep Somavarapu 已提交
34
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
S
Sandeep Somavarapu 已提交
35
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
36
import type { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
37

S
Sandeep Somavarapu 已提交
38 39
registerSingleton(IMarkersWorkbenchService, MarkersWorkbenchService, false);

40 41
KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: Constants.MARKER_OPEN_SIDE_ACTION_ID,
42
	weight: KeybindingWeight.WorkbenchContrib,
43 44 45 46 47 48
	when: ContextKeyExpr.and(Constants.MarkerFocusContextKey),
	primary: KeyMod.CtrlCmd | KeyCode.Enter,
	mac: {
		primary: KeyMod.WinCtrl | KeyCode.Enter
	},
	handler: (accessor, args: any) => {
S
Sandeep Somavarapu 已提交
49 50
		const markersView = getMarkersView(accessor.get(IPanelService))!;
		markersView.openFileAtElement(markersView.getFocusElement(), false, true, true);
51 52 53 54 55
	}
});

KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: Constants.MARKER_SHOW_PANEL_ID,
56
	weight: KeybindingWeight.WorkbenchContrib,
57 58 59 60 61 62 63
	when: undefined,
	primary: undefined,
	handler: (accessor, args: any) => {
		accessor.get(IPanelService).openPanel(Constants.MARKERS_PANEL_ID);
	}
});

S
Sandeep Somavarapu 已提交
64 65 66 67 68 69
KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: Constants.MARKER_SHOW_QUICK_FIX,
	weight: KeybindingWeight.WorkbenchContrib,
	when: Constants.MarkerFocusContextKey,
	primary: KeyMod.CtrlCmd | KeyCode.US_DOT,
	handler: (accessor, args: any) => {
S
Sandeep Somavarapu 已提交
70 71
		const markersView = getMarkersView(accessor.get(IPanelService))!;
		const focusedElement = markersView.getFocusElement();
S
Sandeep Somavarapu 已提交
72
		if (focusedElement instanceof Marker) {
S
Sandeep Somavarapu 已提交
73
			markersView.showQuickFixes(focusedElement);
S
Sandeep Somavarapu 已提交
74 75 76 77
		}
	}
});

78 79 80 81 82 83 84 85 86 87 88
// configuration
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
	'id': 'problems',
	'order': 101,
	'title': Messages.PROBLEMS_PANEL_CONFIGURATION_TITLE,
	'type': 'object',
	'properties': {
		'problems.autoReveal': {
			'description': Messages.PROBLEMS_PANEL_CONFIGURATION_AUTO_REVEAL,
			'type': 'boolean',
			'default': true
89 90 91 92 93
		},
		'problems.showCurrentInStatus': {
			'description': Messages.PROBLEMS_PANEL_CONFIGURATION_SHOW_CURRENT_STATUS,
			'type': 'boolean',
			'default': false
94 95 96 97
		}
	}
});

S
Sandeep Somavarapu 已提交
98 99 100 101 102 103 104 105 106 107 108 109
class ToggleMarkersPanelAction extends TogglePanelAction {

	public static readonly ID = 'workbench.actions.view.problems';
	public static readonly LABEL = Messages.MARKERS_PANEL_TOGGLE_LABEL;

	constructor(id: string, label: string,
		@IWorkbenchLayoutService layoutService: IWorkbenchLayoutService,
		@IPanelService panelService: IPanelService
	) {
		super(id, label, Constants.MARKERS_PANEL_ID, panelService, layoutService);
	}
}
110 111 112 113 114

// markers view container
const VIEW_CONTAINER: ViewContainer = Registry.as<IViewContainersRegistry>(ViewContainerExtensions.ViewContainersRegistry).registerViewContainer({
	id: Constants.MARKERS_PANEL_ID,
	name: Messages.MARKERS_PANEL_TITLE_PROBLEMS,
S
Sandeep Somavarapu 已提交
115
	ctorDescriptor: new SyncDescriptor(ViewPaneContainer, [Constants.MARKERS_PANEL_ID, Constants.MARKERS_PANEL_STORAGE_ID, { mergeViewWithContainerWhenSingleView: true, donotShowContainerTitleWhenMergedWithContainer: true }]),
116 117 118 119 120 121 122 123 124 125 126
	focusCommand: {
		id: ToggleMarkersPanelAction.ID, keybindings: {
			primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_M
		}
	}
}, ViewContainerLocation.Panel);

Registry.as<IViewsRegistry>(ViewContainerExtensions.ViewsRegistry).registerViews([{
	id: Constants.MARKERS_VIEW_ID,
	name: Messages.MARKERS_PANEL_TITLE_PROBLEMS,
	canToggleVisibility: false,
S
Sandeep Somavarapu 已提交
127
	ctorDescriptor: new SyncDescriptor(MarkersView),
128
}], VIEW_CONTAINER);
129

S
Sandeep Somavarapu 已提交
130 131
// workbench
const workbenchRegistry = Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench);
B
Benjamin Pasero 已提交
132
workbenchRegistry.registerWorkbenchContribution(ActivityUpdater, LifecyclePhase.Restored);
S
Sandeep Somavarapu 已提交
133

134 135
// actions
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
136
registry.registerWorkbenchAction(SyncActionDescriptor.create(ToggleMarkersPanelAction, ToggleMarkersPanelAction.ID, ToggleMarkersPanelAction.LABEL, {
137 138
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_M
}), 'View: Toggle Problems (Errors, Warnings, Infos)', Messages.MARKERS_PANEL_VIEW_CATEGORY);
139
registry.registerWorkbenchAction(SyncActionDescriptor.create(ShowProblemsPanelAction, ShowProblemsPanelAction.ID, ShowProblemsPanelAction.LABEL), 'View: Focus Problems (Errors, Warnings, Infos)', Messages.MARKERS_PANEL_VIEW_CATEGORY);
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.MARKER_COPY_ACTION_ID,
			title: { value: localize('copyMarker', "Copy"), original: 'Copy' },
			menu: {
				id: MenuId.ProblemsPanelContext,
				when: Constants.MarkerFocusContextKey,
				group: 'navigation'
			},
			keybinding: {
				weight: KeybindingWeight.WorkbenchContrib,
				primary: KeyMod.CtrlCmd | KeyCode.KEY_C,
				when: Constants.MarkerFocusContextKey
			},
		});
	}
	async run(accessor: ServicesAccessor) {
158
		await copyMarker(accessor.get(IPanelService), accessor.get(IClipboardService));
159
	}
160
});
161 162 163 164 165 166 167 168 169 170 171 172 173
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.MARKER_COPY_MESSAGE_ACTION_ID,
			title: { value: localize('copyMessage', "Copy Message"), original: 'Copy Message' },
			menu: {
				id: MenuId.ProblemsPanelContext,
				when: Constants.MarkerFocusContextKey,
				group: 'navigation'
			},
		});
	}
	async run(accessor: ServicesAccessor) {
S
Sandeep Somavarapu 已提交
174
		await copyMessage(accessor.get(IPanelService), accessor.get(IClipboardService));
175
	}
176
});
177 178 179 180 181 182 183 184 185 186 187 188 189
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.RELATED_INFORMATION_COPY_MESSAGE_ACTION_ID,
			title: { value: localize('copyMessage', "Copy Message"), original: 'Copy Message' },
			menu: {
				id: MenuId.ProblemsPanelContext,
				when: Constants.RelatedInformationFocusContextKey,
				group: 'navigation'
			}
		});
	}
	async run(accessor: ServicesAccessor) {
190
		await copyRelatedInformationMessage(accessor.get(IPanelService), accessor.get(IClipboardService));
S
Sandeep Somavarapu 已提交
191 192
	}
});
193 194 195 196 197 198 199 200 201 202 203 204 205
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.FOCUS_PROBLEMS_FROM_FILTER,
			title: localize('focusProblemsList', "Focus problems view"),
			keybinding: {
				when: Constants.MarkerPanelFilterFocusContextKey,
				weight: KeybindingWeight.WorkbenchContrib,
				primary: KeyMod.CtrlCmd | KeyCode.DownArrow
			}
		});
	}
	run(accessor: ServicesAccessor) {
206
		focusProblemsView(accessor.get(IPanelService));
S
Sandeep Somavarapu 已提交
207 208
	}
});
209 210 211 212 213 214 215 216 217 218 219 220 221
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.MARKERS_PANEL_FOCUS_FILTER,
			title: localize('focusProblemsFilter', "Focus problems filter"),
			keybinding: {
				when: Constants.MarkerPanelFocusContextKey,
				weight: KeybindingWeight.WorkbenchContrib,
				primary: KeyMod.CtrlCmd | KeyCode.KEY_F
			}
		});
	}
	run(accessor: ServicesAccessor) {
222
		focusProblemsFilter(accessor.get(IPanelService));
S
Sandeep Somavarapu 已提交
223 224
	}
});
225 226 227 228 229 230 231 232 233 234 235 236 237
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.MARKERS_PANEL_SHOW_MULTILINE_MESSAGE,
			title: { value: localize('show multiline', "Show message in multiple lines"), original: 'Problems: Show message in multiple lines' },
			category: localize('problems', "Problems"),
			menu: {
				id: MenuId.CommandPalette,
				when: ActivePanelContext.isEqualTo(Constants.MARKERS_PANEL_ID)
			}
		});
	}
	run(accessor: ServicesAccessor) {
S
Sandeep Somavarapu 已提交
238 239 240
		const markersView = getMarkersView(accessor.get(IPanelService));
		if (markersView) {
			markersView.markersViewModel.multiline = true;
S
#1927  
Sandeep Somavarapu 已提交
241 242 243
		}
	}
});
244 245 246 247 248 249 250 251 252 253 254 255 256
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.MARKERS_PANEL_SHOW_SINGLELINE_MESSAGE,
			title: { value: localize('show singleline', "Show message in single line"), original: 'Problems: Show message in single line' },
			category: localize('problems', "Problems"),
			menu: {
				id: MenuId.CommandPalette,
				when: ActivePanelContext.isEqualTo(Constants.MARKERS_PANEL_ID)
			}
		});
	}
	run(accessor: ServicesAccessor) {
S
Sandeep Somavarapu 已提交
257 258 259
		const markersView = getMarkersView(accessor.get(IPanelService));
		if (markersView) {
			markersView.markersViewModel.multiline = false;
S
#1927  
Sandeep Somavarapu 已提交
260 261 262
		}
	}
});
263

S
Sandeep Somavarapu 已提交
264 265 266 267
async function copyMarker(panelService: IPanelService, clipboardService: IClipboardService) {
	const markersView = getMarkersView(panelService);
	if (markersView) {
		const element = markersView.getFocusElement();
268
		if (element instanceof Marker) {
269
			await clipboardService.writeText(`${element}`);
270 271 272 273
		}
	}
}

S
Sandeep Somavarapu 已提交
274 275 276 277
async function copyMessage(panelService: IPanelService, clipboardService: IClipboardService) {
	const markersView = getMarkersView(panelService);
	if (markersView) {
		const element = markersView.getFocusElement();
278
		if (element instanceof Marker) {
279
			await clipboardService.writeText(element.marker.message);
S
Sandeep Somavarapu 已提交
280 281 282 283
		}
	}
}

S
Sandeep Somavarapu 已提交
284 285 286 287
async function copyRelatedInformationMessage(panelService: IPanelService, clipboardService: IClipboardService) {
	const markersView = getMarkersView(panelService);
	if (markersView) {
		const element = markersView.getFocusElement();
S
Sandeep Somavarapu 已提交
288
		if (element instanceof RelatedInformation) {
289
			await clipboardService.writeText(element.raw.message);
290 291 292 293
		}
	}
}

S
Sandeep Somavarapu 已提交
294
function focusProblemsView(panelService: IPanelService) {
S
Sandeep Somavarapu 已提交
295 296 297
	const markersView = getMarkersView(panelService);
	if (markersView) {
		markersView.focus();
S
Sandeep Somavarapu 已提交
298 299 300
	}
}

301
function focusProblemsFilter(panelService: IPanelService): void {
S
Sandeep Somavarapu 已提交
302 303 304
	const markersView = getMarkersView(panelService);
	if (markersView) {
		markersView.focusFilter();
S
Sandeep Somavarapu 已提交
305 306 307
	}
}

308 309 310 311 312 313 314 315
MenuRegistry.appendMenuItem(MenuId.MenubarViewMenu, {
	group: '4_panels',
	command: {
		id: ToggleMarkersPanelAction.ID,
		title: localize({ key: 'miMarker', comment: ['&& denotes a mnemonic'] }, "&&Problems")
	},
	order: 4
});
316 317 318 319 320

CommandsRegistry.registerCommand('workbench.actions.view.toggleProblems', accessor => {
	const panelService = accessor.get(IPanelService);
	const panel = accessor.get(IPanelService).getActivePanel();
	if (panel && panel.getId() === Constants.MARKERS_PANEL_ID) {
321
		panelService.hideActivePanel();
322 323 324 325 326 327 328 329 330 331 332 333 334 335
	} else {
		panelService.openPanel(Constants.MARKERS_PANEL_ID, true);
	}
});

class MarkersStatusBarContributions extends Disposable implements IWorkbenchContribution {

	private markersStatusItem: IStatusbarEntryAccessor;

	constructor(
		@IMarkerService private readonly markerService: IMarkerService,
		@IStatusbarService private readonly statusbarService: IStatusbarService
	) {
		super();
336
		this.markersStatusItem = this._register(this.statusbarService.addEntry(this.getMarkersItem(), 'status.problems', localize('status.problems', "Problems"), StatusbarAlignment.LEFT, 50 /* Medium Priority */));
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
		this.markerService.onMarkerChanged(() => this.markersStatusItem.update(this.getMarkersItem()));
	}

	private getMarkersItem(): IStatusbarEntry {
		const markersStatistics = this.markerService.getStatistics();
		return {
			text: this.getMarkersText(markersStatistics),
			tooltip: this.getMarkersTooltip(markersStatistics),
			command: 'workbench.actions.view.toggleProblems'
		};
	}

	private getMarkersTooltip(stats: MarkerStatistics): string {
		const errorTitle = (n: number) => localize('totalErrors', "{0} Errors", n);
		const warningTitle = (n: number) => localize('totalWarnings', "{0} Warnings", n);
		const infoTitle = (n: number) => localize('totalInfos', "{0} Infos", n);

		const titles: string[] = [];

		if (stats.errors > 0) {
			titles.push(errorTitle(stats.errors));
		}

		if (stats.warnings > 0) {
			titles.push(warningTitle(stats.warnings));
		}

		if (stats.infos > 0) {
			titles.push(infoTitle(stats.infos));
		}

		if (titles.length === 0) {
			return localize('noProblems', "No Problems");
		}

		return titles.join(', ');
	}

	private getMarkersText(stats: MarkerStatistics): string {
		const problemsText: string[] = [];

		// Errors
		problemsText.push('$(error) ' + this.packNumber(stats.errors));

		// Warnings
		problemsText.push('$(warning) ' + this.packNumber(stats.warnings));

		// Info (only if any)
		if (stats.infos > 0) {
			problemsText.push('$(info) ' + this.packNumber(stats.infos));
		}

		return problemsText.join(' ');
	}

	private packNumber(n: number): string {
		const manyProblems = localize('manyProblems', "10K+");
		return n > 9999 ? manyProblems : n > 999 ? n.toString().charAt(0) + 'K' : n.toString();
	}
}

S
Sandeep Somavarapu 已提交
398
workbenchRegistry.registerWorkbenchContribution(MarkersStatusBarContributions, LifecyclePhase.Restored);