markers.contribution.ts 15.0 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';
15
import { MarkersView } 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 { Disposable } from 'vs/base/common/lifecycle';
28
import { IStatusbarEntryAccessor, IStatusbarService, StatusbarAlignment, IStatusbarEntry } from 'vs/workbench/services/statusbar/common/statusbar';
29
import { IMarkerService, MarkerStatistics } from 'vs/platform/markers/common/markers';
30
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
31
import { ViewContainer, IViewContainersRegistry, Extensions as ViewContainerExtensions, ViewContainerLocation, IViewsRegistry, IViewsService } from 'vs/workbench/common/views';
S
Sandeep Somavarapu 已提交
32
import { ViewPaneContainer } from 'vs/workbench/browser/parts/views/viewPaneContainer';
S
Sandeep Somavarapu 已提交
33
import { IWorkbenchLayoutService } from 'vs/workbench/services/layout/browser/layoutService';
S
Sandeep Somavarapu 已提交
34
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
35
import type { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
36

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

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

KeybindingsRegistry.registerCommandAndKeybindingRule({
	id: Constants.MARKER_SHOW_PANEL_ID,
55
	weight: KeybindingWeight.WorkbenchContrib,
56 57
	when: undefined,
	primary: undefined,
S
SteVen Batten 已提交
58
	handler: async (accessor, args: any) => {
59
		await accessor.get(IViewsService).openView(Constants.MARKERS_VIEW_ID);
60 61 62
	}
});

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

77 78 79 80 81 82 83 84 85 86 87
// 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
88 89 90 91 92
		},
		'problems.showCurrentInStatus': {
			'description': Messages.PROBLEMS_PANEL_CONFIGURATION_SHOW_CURRENT_STATUS,
			'type': 'boolean',
			'default': false
93 94 95 96
		}
	}
});

S
Sandeep Somavarapu 已提交
97 98 99 100 101 102 103 104 105
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
	) {
106
		super(id, label, Constants.MARKERS_CONTAINER_ID, panelService, layoutService);
S
Sandeep Somavarapu 已提交
107 108
	}
}
109 110 111

// markers view container
const VIEW_CONTAINER: ViewContainer = Registry.as<IViewContainersRegistry>(ViewContainerExtensions.ViewContainersRegistry).registerViewContainer({
112
	id: Constants.MARKERS_CONTAINER_ID,
113
	name: Messages.MARKERS_PANEL_TITLE_PROBLEMS,
114
	ctorDescriptor: new SyncDescriptor(ViewPaneContainer, [Constants.MARKERS_CONTAINER_ID, Constants.MARKERS_VIEW_STORAGE_ID, { mergeViewWithContainerWhenSingleView: true, donotShowContainerTitleWhenMergedWithContainer: true }]),
115 116 117 118 119 120 121 122 123 124 125
	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 已提交
126
	ctorDescriptor: new SyncDescriptor(MarkersView),
127
}], VIEW_CONTAINER);
128

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

133 134
// actions
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
135
registry.registerWorkbenchAction(SyncActionDescriptor.create(ToggleMarkersPanelAction, ToggleMarkersPanelAction.ID, ToggleMarkersPanelAction.LABEL, {
136 137
	primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_M
}), 'View: Toggle Problems (Errors, Warnings, Infos)', Messages.MARKERS_PANEL_VIEW_CATEGORY);
138
registry.registerWorkbenchAction(SyncActionDescriptor.create(ShowProblemsPanelAction, ShowProblemsPanelAction.ID, ShowProblemsPanelAction.LABEL), 'View: Focus Problems (Errors, Warnings, Infos)', Messages.MARKERS_PANEL_VIEW_CATEGORY);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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) {
157
		await copyMarker(accessor.get(IViewsService), accessor.get(IClipboardService));
158
	}
159
});
160 161 162 163 164 165 166 167 168 169 170 171 172
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) {
173
		await copyMessage(accessor.get(IViewsService), accessor.get(IClipboardService));
174
	}
175
});
176 177 178 179 180 181 182 183 184 185 186 187 188
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) {
189
		await copyRelatedInformationMessage(accessor.get(IViewsService), accessor.get(IClipboardService));
S
Sandeep Somavarapu 已提交
190 191
	}
});
192 193 194 195 196 197
registerAction2(class extends Action2 {
	constructor() {
		super({
			id: Constants.FOCUS_PROBLEMS_FROM_FILTER,
			title: localize('focusProblemsList', "Focus problems view"),
			keybinding: {
198
				when: Constants.MarkerViewFilterFocusContextKey,
199 200 201 202 203 204
				weight: KeybindingWeight.WorkbenchContrib,
				primary: KeyMod.CtrlCmd | KeyCode.DownArrow
			}
		});
	}
	run(accessor: ServicesAccessor) {
205
		focusProblemsView(accessor.get(IViewsService));
S
Sandeep Somavarapu 已提交
206 207
	}
});
208 209 210
registerAction2(class extends Action2 {
	constructor() {
		super({
211
			id: Constants.MARKERS_VIEW_FOCUS_FILTER,
212 213
			title: localize('focusProblemsFilter', "Focus problems filter"),
			keybinding: {
214
				when: Constants.MarkerViewFocusContextKey,
215 216 217 218 219 220
				weight: KeybindingWeight.WorkbenchContrib,
				primary: KeyMod.CtrlCmd | KeyCode.KEY_F
			}
		});
	}
	run(accessor: ServicesAccessor) {
221
		focusProblemsFilter(accessor.get(IViewsService));
S
Sandeep Somavarapu 已提交
222 223
	}
});
224 225 226
registerAction2(class extends Action2 {
	constructor() {
		super({
227
			id: Constants.MARKERS_VIEW_SHOW_MULTILINE_MESSAGE,
228 229 230 231
			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,
232
				when: ContextKeyExpr.has(`${Constants.MARKERS_VIEW_ID}.visible`)
233 234 235 236
			}
		});
	}
	run(accessor: ServicesAccessor) {
237
		const markersView = accessor.get(IViewsService).getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID)!;
S
Sandeep Somavarapu 已提交
238 239
		if (markersView) {
			markersView.markersViewModel.multiline = true;
S
#1927  
Sandeep Somavarapu 已提交
240 241 242
		}
	}
});
243 244 245
registerAction2(class extends Action2 {
	constructor() {
		super({
246
			id: Constants.MARKERS_VIEW_SHOW_SINGLELINE_MESSAGE,
247 248 249 250
			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,
251
				when: ContextKeyExpr.has(`${Constants.MARKERS_VIEW_ID}.visible`)
252 253 254 255
			}
		});
	}
	run(accessor: ServicesAccessor) {
256
		const markersView = accessor.get(IViewsService).getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
257 258
		if (markersView) {
			markersView.markersViewModel.multiline = false;
S
#1927  
Sandeep Somavarapu 已提交
259 260 261
		}
	}
});
262

263 264
async function copyMarker(viewsService: IViewsService, clipboardService: IClipboardService) {
	const markersView = viewsService.getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
265 266
	if (markersView) {
		const element = markersView.getFocusElement();
267
		if (element instanceof Marker) {
268
			await clipboardService.writeText(`${element}`);
269 270 271 272
		}
	}
}

273 274
async function copyMessage(viewsService: IViewsService, clipboardService: IClipboardService) {
	const markersView = viewsService.getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
275 276
	if (markersView) {
		const element = markersView.getFocusElement();
277
		if (element instanceof Marker) {
278
			await clipboardService.writeText(element.marker.message);
S
Sandeep Somavarapu 已提交
279 280 281 282
		}
	}
}

283 284
async function copyRelatedInformationMessage(viewsService: IViewsService, clipboardService: IClipboardService) {
	const markersView = viewsService.getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
285 286
	if (markersView) {
		const element = markersView.getFocusElement();
S
Sandeep Somavarapu 已提交
287
		if (element instanceof RelatedInformation) {
288
			await clipboardService.writeText(element.raw.message);
289 290 291 292
		}
	}
}

293 294
function focusProblemsView(viewsService: IViewsService) {
	const markersView = viewsService.getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
295 296
	if (markersView) {
		markersView.focus();
S
Sandeep Somavarapu 已提交
297 298 299
	}
}

300 301
function focusProblemsFilter(viewsService: IViewsService): void {
	const markersView = viewsService.getActiveViewWithId<MarkersView>(Constants.MARKERS_VIEW_ID);
S
Sandeep Somavarapu 已提交
302 303
	if (markersView) {
		markersView.focusFilter();
S
Sandeep Somavarapu 已提交
304 305 306
	}
}

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

316 317 318 319
CommandsRegistry.registerCommand(Constants.TOGGLE_MARKERS_VIEW_ACTION_ID, async (accessor) => {
	const viewsService = accessor.get(IViewsService);
	if (viewsService.isViewVisible(Constants.MARKERS_VIEW_ID)) {
		viewsService.closeView(Constants.MARKERS_VIEW_ID);
320
	} else {
321
		viewsService.openView(Constants.MARKERS_VIEW_ID, true);
322 323 324 325 326 327 328 329 330 331 332 333
	}
});

class MarkersStatusBarContributions extends Disposable implements IWorkbenchContribution {

	private markersStatusItem: IStatusbarEntryAccessor;

	constructor(
		@IMarkerService private readonly markerService: IMarkerService,
		@IStatusbarService private readonly statusbarService: IStatusbarService
	) {
		super();
334
		this.markersStatusItem = this._register(this.statusbarService.addEntry(this.getMarkersItem(), 'status.problems', localize('status.problems', "Problems"), StatusbarAlignment.LEFT, 50 /* Medium Priority */));
335 336 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
		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 已提交
396
workbenchRegistry.registerWorkbenchContribution(MarkersStatusBarContributions, LifecyclePhase.Restored);