window.ts 21.4 KB
Newer Older
E
Erich Gamma 已提交
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
import * as nls from 'vs/nls';
7
import { URI } from 'vs/base/common/uri';
8
import * as errors from 'vs/base/common/errors';
9
import { equals, deepClone, assign } from 'vs/base/common/objects';
10
import * as DOM from 'vs/base/browser/dom';
11 12
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
import { IAction, Action } from 'vs/base/common/actions';
S
SteVen Batten 已提交
13
import { IFileService } from 'vs/platform/files/common/files';
B
Benjamin Pasero 已提交
14
import { toResource, IUntitledResourceInput } from 'vs/workbench/common/editor';
15
import { IEditorService, IResourceEditor } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
16
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
17
import { IWindowsService, IWindowService, IWindowSettings, IOpenFileRequest, IWindowsConfiguration, IAddFoldersRequest, IRunActionInWindowRequest, IPathData, IRunKeybindingInWindowRequest } from 'vs/platform/windows/common/windows';
18
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
B
Benjamin Pasero 已提交
19
import { ITitleService } from 'vs/workbench/services/title/common/titleService';
20
import { IWorkbenchThemeService, VS_HC_THEME } from 'vs/workbench/services/themes/common/workbenchThemeService';
21 22
import * as browser from 'vs/base/browser/browser';
import { ICommandService } from 'vs/platform/commands/common/commands';
B
Benjamin Pasero 已提交
23
import { IResourceInput } from 'vs/platform/editor/common/editor';
A
Alex Dima 已提交
24
import { KeyboardMapperFactory } from 'vs/workbench/services/keybinding/electron-browser/keybindingService';
25
import { ipcRenderer as ipc, webFrame, crashReporter, Event } from 'electron';
26
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
27 28
import { IMenuService, MenuId, IMenu, MenuItemAction, ICommandAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
29
import { fillInActionBarActions } from 'vs/platform/actions/browser/menuItemActionItem';
30
import { RunOnceScheduler } from 'vs/base/common/async';
31
import { IDisposable, dispose, Disposable } from 'vs/base/common/lifecycle';
32
import { LifecyclePhase, ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
33
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
34
import { IIntegrityService } from 'vs/workbench/services/integrity/common/integrity';
35
import { isRootUser, isWindows, isMacintosh, isLinux } from 'vs/base/common/platform';
36 37
import product from 'vs/platform/product/node/product';
import pkg from 'vs/platform/product/node/package';
38
import { INotificationService } from 'vs/platform/notification/common/notification';
B
Benjamin Pasero 已提交
39
import { EditorServiceImpl } from 'vs/workbench/browser/parts/editor/editor';
40
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
41
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
42
import { IAccessibilityService, AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
43
import { WorkbenchState, IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
B
Benjamin Pasero 已提交
44
import { coalesce } from 'vs/base/common/arrays';
45
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
E
Erich Gamma 已提交
46

47
const TextInputActions: IAction[] = [
48 49
	new Action('undo', nls.localize('undo', "Undo"), undefined, true, () => Promise.resolve(document.execCommand('undo'))),
	new Action('redo', nls.localize('redo', "Redo"), undefined, true, () => Promise.resolve(document.execCommand('redo'))),
50
	new Separator(),
51 52 53
	new Action('editor.action.clipboardCutAction', nls.localize('cut', "Cut"), undefined, true, () => Promise.resolve(document.execCommand('cut'))),
	new Action('editor.action.clipboardCopyAction', nls.localize('copy', "Copy"), undefined, true, () => Promise.resolve(document.execCommand('copy'))),
	new Action('editor.action.clipboardPasteAction', nls.localize('paste', "Paste"), undefined, true, () => Promise.resolve(document.execCommand('paste'))),
54
	new Separator(),
55
	new Action('editor.action.selectAll', nls.localize('selectAll', "Select All"), undefined, true, () => Promise.resolve(document.execCommand('selectAll')))
56 57
];

58 59
export class ElectronWindow extends Disposable {

60
	private touchBarMenu?: IMenu;
61
	private touchBarUpdater: RunOnceScheduler;
62 63 64 65 66
	private touchBarDisposables: IDisposable[];
	private lastInstalledTouchedBar: ICommandAction[][];

	private previousConfiguredZoomLevel: number;

67
	private addFoldersScheduler: RunOnceScheduler;
68
	private pendingFoldersToAdd: URI[];
69

70 71
	private closeEmptyWindowScheduler: RunOnceScheduler = this._register(new RunOnceScheduler(() => this.onAllEditorsClosed(), 50));

E
Erich Gamma 已提交
72
	constructor(
73 74 75
		@IEditorService private readonly editorService: EditorServiceImpl,
		@IWindowsService private readonly windowsService: IWindowsService,
		@IWindowService private readonly windowService: IWindowService,
76
		@IConfigurationService private readonly configurationService: IConfigurationService,
77
		@ITitleService private readonly titleService: ITitleService,
78
		@IWorkbenchThemeService protected themeService: IWorkbenchThemeService,
79 80 81 82 83 84 85 86 87
		@INotificationService private readonly notificationService: INotificationService,
		@ICommandService private readonly commandService: ICommandService,
		@IKeybindingService private readonly keybindingService: IKeybindingService,
		@IContextMenuService private readonly contextMenuService: IContextMenuService,
		@ITelemetryService private readonly telemetryService: ITelemetryService,
		@IWorkspaceEditingService private readonly workspaceEditingService: IWorkspaceEditingService,
		@IFileService private readonly fileService: IFileService,
		@IMenuService private readonly menuService: IMenuService,
		@ILifecycleService private readonly lifecycleService: ILifecycleService,
88
		@IIntegrityService private readonly integrityService: IIntegrityService,
89
		@IEnvironmentService private readonly environmentService: IEnvironmentService,
90 91
		@IAccessibilityService private readonly accessibilityService: IAccessibilityService,
		@IWorkspaceContextService private readonly contextService: IWorkspaceContextService
E
Erich Gamma 已提交
92
	) {
93
		super();
94

95 96
		this.touchBarDisposables = [];

97
		this.pendingFoldersToAdd = [];
B
Benjamin Pasero 已提交
98
		this.addFoldersScheduler = this._register(new RunOnceScheduler(() => this.doAddFolders(), 100));
99

E
Erich Gamma 已提交
100
		this.registerListeners();
101
		this.create();
E
Erich Gamma 已提交
102 103 104 105
	}

	private registerListeners(): void {

106
		// React to editor input changes
B
Benjamin Pasero 已提交
107
		this._register(this.editorService.onDidActiveEditorChange(() => this.updateTouchbarMenu()));
E
Erich Gamma 已提交
108

109
		// prevent opening a real URL inside the shell
110 111 112 113
		[DOM.EventType.DRAG_OVER, DOM.EventType.DROP].forEach(event => {
			window.document.body.addEventListener(event, (e: DragEvent) => {
				DOM.EventHelper.stop(e);
			});
E
Erich Gamma 已提交
114 115
		});

116
		// Support runAction event
117
		ipc.on('vscode:runAction', (event: Event, request: IRunActionInWindowRequest) => {
118
			const args: any[] = request.args || [];
119 120 121 122

			// If we run an action from the touchbar, we fill in the currently active resource
			// as payload because the touch bar items are context aware depending on the editor
			if (request.from === 'touchbar') {
B
Benjamin Pasero 已提交
123
				const activeEditor = this.editorService.activeEditor;
124
				if (activeEditor) {
B
Benjamin Pasero 已提交
125
					const resource = toResource(activeEditor, { supportSideBySide: true });
126 127 128 129 130 131 132 133
					if (resource) {
						args.push(resource);
					}
				}
			} else {
				args.push({ from: request.from }); // TODO@telemetry this is a bit weird to send this to every action?
			}

134
			this.commandService.executeCommand(request.id, ...args).then(_ => {
K
kieferrm 已提交
135
				/* __GDPR__
K
kieferrm 已提交
136 137 138 139 140
					"commandExecuted" : {
						"id" : { "classification": "SystemMetaData", "purpose": "FeatureInsight" },
						"from": { "classification": "SystemMetaData", "purpose": "FeatureInsight" }
					}
				*/
141
				this.telemetryService.publicLog('commandExecuted', { id: request.id, from: request.from });
J
Johannes Rieken 已提交
142
			}, err => {
143
				this.notificationService.error(err);
J
Johannes Rieken 已提交
144
			});
145 146
		});

147
		// Support runKeybinding event
148
		ipc.on('vscode:runKeybinding', (event: Event, request: IRunKeybindingInWindowRequest) => {
149 150 151
			if (document.activeElement) {
				this.keybindingService.dispatchByUserSettingsLabel(request.userSettingsLabel, document.activeElement);
			}
152 153
		});

B
Benjamin Pasero 已提交
154
		// Error reporting from main
155
		ipc.on('vscode:reportError', (event: Event, error: string) => {
156
			if (error) {
157
				errors.onUnexpectedError(JSON.parse(error));
158 159 160 161
			}
		});

		// Support openFiles event for existing and new files
162
		ipc.on('vscode:openFiles', (event: Event, request: IOpenFileRequest) => this.onOpenFiles(request));
163

164
		// Support addFolders event if we have a workspace opened
165
		ipc.on('vscode:addFolders', (event: Event, request: IAddFoldersRequest) => this.onAddFoldersRequest(request));
166

167
		// Message support
168
		ipc.on('vscode:showInfoMessage', (event: Event, message: string) => {
169
			this.notificationService.info(message);
170 171 172
		});

		// Fullscreen Events
173
		ipc.on('vscode:enterFullScreen', () => {
B
Benjamin Pasero 已提交
174
			this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
175 176 177 178
				browser.setFullscreen(true);
			});
		});

179
		ipc.on('vscode:leaveFullScreen', () => {
B
Benjamin Pasero 已提交
180
			this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
181 182 183 184 185
				browser.setFullscreen(false);
			});
		});

		// High Contrast Events
186
		ipc.on('vscode:enterHighContrast', () => {
187
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
188
			if (windowConfig && windowConfig.autoDetectHighContrast) {
B
Benjamin Pasero 已提交
189
				this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
190
					this.themeService.setColorTheme(VS_HC_THEME, undefined);
191 192
				});
			}
193 194
		});

195
		ipc.on('vscode:leaveHighContrast', () => {
196
			const windowConfig = this.configurationService.getValue<IWindowSettings>('window');
197
			if (windowConfig && windowConfig.autoDetectHighContrast) {
B
Benjamin Pasero 已提交
198
				this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
199
					this.themeService.restoreColorTheme();
200 201
				});
			}
202 203
		});

A
Alex Dima 已提交
204
		// keyboard layout changed event
205
		ipc.on('vscode:keyboardLayoutChanged', () => {
206
			KeyboardMapperFactory.INSTANCE._onKeyboardLayoutChanged();
A
Alex Dima 已提交
207 208
		});

209
		// keyboard layout changed event
210
		ipc.on('vscode:accessibilitySupportChanged', (event: Event, accessibilitySupportEnabled: boolean) => {
211
			this.accessibilityService.setAccessibilitySupport(accessibilitySupportEnabled ? AccessibilitySupport.Enabled : AccessibilitySupport.Disabled);
212 213
		});

S
Sandeep Somavarapu 已提交
214 215
		// Zoom level changes
		this.updateWindowZoomLevel();
B
Benjamin Pasero 已提交
216
		this._register(this.configurationService.onDidChangeConfiguration(e => {
S
Sandeep Somavarapu 已提交
217 218 219 220
			if (e.affectsConfiguration('window.zoomLevel')) {
				this.updateWindowZoomLevel();
			}
		}));
221

222 223
		// Context menu support in input/textarea
		window.document.addEventListener('contextmenu', e => this.onContextMenu(e));
224 225 226 227 228 229 230

		// Listen to visible editor changes
		this._register(this.editorService.onDidVisibleEditorsChange(() => this.onDidVisibleEditorsChange()));

		// Listen to editor closing (if we run with --wait)
		const filesToWait = this.windowService.getConfiguration().filesToWait;
		if (filesToWait) {
B
Benjamin Pasero 已提交
231
			const resourcesToWaitFor = coalesce(filesToWait.paths.map(p => p.fileUri));
232 233 234 235 236 237 238 239 240 241 242 243 244 245
			const waitMarkerFile = URI.file(filesToWait.waitMarkerFilePath);
			const listenerDispose = this.editorService.onDidCloseEditor(() => this.onEditorClosed(listenerDispose, resourcesToWaitFor, waitMarkerFile));

			this._register(listenerDispose);
		}
	}

	private onDidVisibleEditorsChange(): void {

		// Close when empty: check if we should close the window based on the setting
		// Overruled by: window has a workspace opened or this window is for extension development
		// or setting is disabled. Also enabled when running with --wait from the command line.
		const visibleEditors = this.editorService.visibleControls;
		if (visibleEditors.length === 0 && this.contextService.getWorkbenchState() === WorkbenchState.EMPTY && !this.environmentService.isExtensionDevelopment) {
246
			const closeWhenEmpty = this.configurationService.getValue<boolean>('window.closeWhenEmpty');
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
			if (closeWhenEmpty || this.environmentService.args.wait) {
				this.closeEmptyWindowScheduler.schedule();
			}
		}
	}

	private onAllEditorsClosed(): void {
		const visibleEditors = this.editorService.visibleControls.length;
		if (visibleEditors === 0) {
			this.windowService.closeWindow();
		}
	}

	private onEditorClosed(listenerDispose: IDisposable, resourcesToWaitFor: URI[], waitMarkerFile: URI): void {

		// In wait mode, listen to changes to the editors and wait until the files
		// are closed that the user wants to wait for. When this happens we delete
		// the wait marker file to signal to the outside that editing is done.
		if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
			listenerDispose.dispose();
			this.fileService.del(waitMarkerFile);
		}
269
	}
270

271
	private onContextMenu(e: MouseEvent): void {
272 273 274
		if (e.target instanceof HTMLElement) {
			const target = <HTMLElement>e.target;
			if (target.nodeName && (target.nodeName.toLowerCase() === 'input' || target.nodeName.toLowerCase() === 'textarea')) {
B
Benjamin Pasero 已提交
275
				DOM.EventHelper.stop(e, true);
276

277 278
				this.contextMenuService.showContextMenu({
					getAnchor: () => e,
279
					getActions: () => TextInputActions,
B
Benjamin Pasero 已提交
280
					onHide: () => target.focus() // fixes https://github.com/Microsoft/vscode/issues/52948
281
				});
282
			}
283 284 285
		}
	}

S
Sandeep Somavarapu 已提交
286
	private updateWindowZoomLevel(): void {
287
		const windowConfig: IWindowsConfiguration = this.configurationService.getValue<IWindowsConfiguration>();
288 289 290 291

		let newZoomLevel = 0;
		if (windowConfig.window && typeof windowConfig.window.zoomLevel === 'number') {
			newZoomLevel = windowConfig.window.zoomLevel;
292

293 294 295
			// Leave early if the configured zoom level did not change (https://github.com/Microsoft/vscode/issues/1536)
			if (this.previousConfiguredZoomLevel === newZoomLevel) {
				return;
296
			}
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

			this.previousConfiguredZoomLevel = newZoomLevel;
		}

		if (webFrame.getZoomLevel() !== newZoomLevel) {
			webFrame.setZoomLevel(newZoomLevel);
			browser.setZoomFactor(webFrame.getZoomFactor());
			// See https://github.com/Microsoft/vscode/issues/26151
			// Cannot be trusted because the webFrame might take some time
			// until it really applies the new zoom level
			browser.setZoomLevel(webFrame.getZoomLevel(), /*isTrusted*/false);
		}
	}

	private create(): void {

		// Handle window.open() calls
		const $this = this;
315
		window.open = function (url: string, target: string, features: string, replace: boolean): Window | null {
316 317 318 319 320
			$this.windowsService.openExternal(url);

			return null;
		};

B
Benjamin Pasero 已提交
321 322 323
		// Emit event when vscode is ready
		this.lifecycleService.when(LifecyclePhase.Ready).then(() => {
			ipc.send('vscode:workbenchReady', this.windowService.getCurrentWindowId());
324 325
		});

326 327 328 329
		// Integrity warning
		this.integrityService.isPure().then(res => this.titleService.updateProperties({ isPure: res.isPure }));

		// Root warning
330
		this.lifecycleService.when(LifecyclePhase.Restored).then(() => {
331 332 333 334
			let isAdminPromise: Promise<boolean>;
			if (isWindows) {
				isAdminPromise = import('native-is-elevated').then(isElevated => isElevated());
			} else {
A
Alex Dima 已提交
335
				isAdminPromise = Promise.resolve(isRootUser());
336 337 338 339 340 341 342
			}

			return isAdminPromise.then(isAdmin => {

				// Update title
				this.titleService.updateProperties({ isAdmin });

343 344
				// Show warning message (unix only)
				if (isAdmin && !isWindows) {
345
					this.notificationService.warn(nls.localize('runningAsRoot', "It is not recommended to run {0} as root user.", product.nameShort));
346 347 348
				}
			});
		});
349 350 351

		// Touchbar menu (if enabled)
		this.updateTouchbarMenu();
352 353 354 355 356

		// Crash reporter (if enabled)
		if (!this.environmentService.disableCrashReporter && product.crashReporter && product.hockeyApp && this.configurationService.getValue('telemetry.enableCrashReporter')) {
			this.setupCrashReporter();
		}
357 358 359
	}

	private updateTouchbarMenu(): void {
360 361 362 363 364
		if (
			!isMacintosh || // macOS only
			!this.configurationService.getValue<boolean>('keyboard.touchbar.enabled') // disabled via setting
		) {
			return;
365 366
		}

367 368
		// Dispose old
		this.touchBarDisposables = dispose(this.touchBarDisposables);
R
Rob Lourens 已提交
369
		this.touchBarMenu = undefined;
370

371 372 373 374
		// Create new (delayed)
		this.touchBarUpdater = new RunOnceScheduler(() => this.doUpdateTouchbarMenu(), 300);
		this.touchBarDisposables.push(this.touchBarUpdater);
		this.touchBarUpdater.schedule();
375 376
	}

377 378 379 380 381 382 383
	private doUpdateTouchbarMenu(): void {
		if (!this.touchBarMenu) {
			this.touchBarMenu = this.editorService.invokeWithinEditorContext(accessor => this.menuService.createMenu(MenuId.TouchBarContext, accessor.get(IContextKeyService)));
			this.touchBarDisposables.push(this.touchBarMenu);
			this.touchBarDisposables.push(this.touchBarMenu.onDidChange(() => this.touchBarUpdater.schedule()));
		}

384
		const actions: Array<MenuItemAction | Separator> = [];
385 386

		// Fill actions into groups respecting order
R
Rob Lourens 已提交
387
		fillInActionBarActions(this.touchBarMenu, undefined, actions);
388 389 390 391

		// Convert into command action multi array
		const items: ICommandAction[][] = [];
		let group: ICommandAction[] = [];
392
		for (const action of actions) {
393 394 395 396 397 398 399 400 401 402

			// Command
			if (action instanceof MenuItemAction) {
				group.push(action.item);
			}

			// Separator
			else if (action instanceof Separator) {
				if (group.length) {
					items.push(group);
403
				}
404 405

				group = [];
406
			}
407 408 409 410 411 412 413
		}

		if (group.length) {
			items.push(group);
		}

		// Only update if the actions have changed
414
		if (!equals(this.lastInstalledTouchedBar, items)) {
415 416 417
			this.lastInstalledTouchedBar = items;
			this.windowService.updateTouchBar(items);
		}
418 419
	}

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	private setupCrashReporter(): void {

		// base options with product info
		const options = {
			companyName: product.crashReporter.companyName,
			productName: product.crashReporter.productName,
			submitURL: isWindows ? product.hockeyApp[`win32-${process.arch}`] : isLinux ? product.hockeyApp[`linux-${process.arch}`] : product.hockeyApp.darwin,
			extra: {
				vscode_version: pkg.version,
				vscode_commit: product.commit
			}
		};

		// mixin telemetry info
		this.telemetryService.getTelemetryInfo()
			.then(info => {
				assign(options.extra, {
					vscode_sessionId: info.sessionId
				});

				// start crash reporter right here
				crashReporter.start(deepClone(options));

				// start crash reporter in the main process
				return this.windowsService.startCrashReporter(options);
			});
	}

448 449 450
	private onAddFoldersRequest(request: IAddFoldersRequest): void {

		// Buffer all pending requests
451
		this.pendingFoldersToAdd.push(...request.foldersToAdd.map(f => URI.revive(f)));
452 453 454 455 456 457 458 459 460 461

		// Delay the adding of folders a bit to buffer in case more requests are coming
		if (!this.addFoldersScheduler.isScheduled()) {
			this.addFoldersScheduler.schedule();
		}
	}

	private doAddFolders(): void {
		const foldersToAdd: IWorkspaceFolderCreationData[] = [];

462 463
		this.pendingFoldersToAdd.forEach(folder => {
			foldersToAdd.push(({ uri: folder }));
464 465 466
		});

		this.pendingFoldersToAdd = [];
467

468
		this.workspaceEditingService.addFolders(foldersToAdd);
469 470
	}

471
	private onOpenFiles(request: IOpenFileRequest): void {
B
Benjamin Pasero 已提交
472
		const inputs: IResourceEditor[] = [];
473
		const diffMode = !!(request.filesToDiff && (request.filesToDiff.length === 2));
474 475 476 477 478 479 480 481 482

		if (!diffMode && request.filesToOpen) {
			inputs.push(...this.toInputs(request.filesToOpen, false));
		}

		if (!diffMode && request.filesToCreate) {
			inputs.push(...this.toInputs(request.filesToCreate, true));
		}

483
		if (diffMode && request.filesToDiff) {
484 485 486 487
			inputs.push(...this.toInputs(request.filesToDiff, false));
		}

		if (inputs.length) {
488
			this.openResources(inputs, diffMode);
489
		}
490 491 492 493 494

		if (request.filesToWait && inputs.length) {
			// In wait mode, listen to changes to the editors and wait until the files
			// are closed that the user wants to wait for. When this happens we delete
			// the wait marker file to signal to the outside that editing is done.
M
Martin Aeschlimann 已提交
495
			const resourcesToWaitFor = request.filesToWait.paths.map(p => URI.revive(p.fileUri));
496
			const waitMarkerFile = URI.file(request.filesToWait.waitMarkerFilePath);
B
Benjamin Pasero 已提交
497 498
			const unbind = this.editorService.onDidCloseEditor(() => {
				if (resourcesToWaitFor.every(resource => !this.editorService.isOpen({ resource }))) {
499
					unbind.dispose();
500
					this.fileService.del(waitMarkerFile);
501 502 503
				}
			});
		}
504 505
	}

506
	private openResources(resources: Array<IResourceInput | IUntitledResourceInput>, diffMode: boolean): void {
J
Johannes Rieken 已提交
507
		this.lifecycleService.when(LifecyclePhase.Ready).then((): Promise<any> => {
N
Nick Snyder 已提交
508

509
			// In diffMode we open 2 resources as diff
510
			if (diffMode && resources.length === 2) {
511
				return this.editorService.openEditor({ leftResource: resources[0].resource!, rightResource: resources[1].resource!, options: { pinned: true } });
512 513 514 515 516 517 518 519
			}

			// For one file, just put it into the current active editor
			if (resources.length === 1) {
				return this.editorService.openEditor(resources[0]);
			}

			// Otherwise open all
B
Benjamin Pasero 已提交
520
			return this.editorService.openEditors(resources);
521 522 523
		});
	}

M
Martin Aeschlimann 已提交
524
	private toInputs(paths: IPathData[], isNew: boolean): IResourceEditor[] {
525
		return paths.map(p => {
M
Martin Aeschlimann 已提交
526
			const resource = URI.revive(p.fileUri);
527 528 529 530 531 532
			let input: IResourceInput | IUntitledResourceInput;
			if (isNew) {
				input = { filePath: resource.fsPath, options: { pinned: true } } as IUntitledResourceInput;
			} else {
				input = { resource, options: { pinned: true } } as IResourceInput;
			}
533

534 535
			if (!isNew && typeof p.lineNumber === 'number' && typeof p.columnNumber === 'number') {
				input.options!.selection = {
536 537 538 539 540 541 542 543 544
					startLineNumber: p.lineNumber,
					startColumn: p.columnNumber
				};
			}

			return input;
		});
	}

B
Benjamin Pasero 已提交
545
	dispose(): void {
546 547 548 549
		this.touchBarDisposables = dispose(this.touchBarDisposables);

		super.dispose();
	}
J
Johannes Rieken 已提交
550
}