customEditors.ts 23.1 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 { coalesce, distinct } from 'vs/base/common/arrays';
import { Emitter, Event } from 'vs/base/common/event';
8
import { Lazy } from 'vs/base/common/lazy';
9
import { Disposable, IDisposable, toDisposable } from 'vs/base/common/lifecycle';
10
import { basename, extname, isEqual } from 'vs/base/common/resources';
11 12 13 14
import { URI } from 'vs/base/common/uri';
import { generateUuid } from 'vs/base/common/uuid';
import * as nls from 'vs/nls';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
15
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
16
import { EditorActivation, IEditorOptions, ITextEditorOptions } from 'vs/platform/editor/common/editor';
17
import { FileOperation, IFileService } from 'vs/platform/files/common/files';
18 19
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
M
Matt Bierner 已提交
20 21
import * as colorRegistry from 'vs/platform/theme/common/colorRegistry';
import { registerThemingParticipant } from 'vs/platform/theme/common/themeService';
22
import { EditorServiceImpl } from 'vs/workbench/browser/parts/editor/editor';
23
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
24
import { EditorInput, EditorOptions, GroupIdentifier, IEditorInput, IEditorPane } from 'vs/workbench/common/editor';
25
import { DiffEditorInput } from 'vs/workbench/common/editor/diffEditorInput';
26
import { CONTEXT_CUSTOM_EDITORS, CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE, CustomEditorCapabilities, CustomEditorInfo, CustomEditorInfoCollection, CustomEditorPriority, ICustomEditorService } from 'vs/workbench/contrib/customEditor/common/customEditor';
27
import { CustomEditorModelManager } from 'vs/workbench/contrib/customEditor/common/customEditorModelManager';
28
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
29
import { IWebviewService, webviewHasOwnEditFunctionsContext } from 'vs/workbench/contrib/webview/browser/webview';
30
import { CustomEditorAssociation, CustomEditorsAssociations, customEditorsAssociationsSettingId } from 'vs/workbench/services/editor/common/editorAssociationsSetting';
31
import { IEditorGroup, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
32
import { ICustomEditorInfo, ICustomEditorViewTypesHandler, IEditorService, IOpenEditorOverride, IOpenEditorOverrideEntry } from 'vs/workbench/services/editor/common/editorService';
33
import { ContributedCustomEditors, defaultCustomEditor } from '../common/contributedCustomEditors';
34
import { CustomEditorInput } from './customEditorInput';
35

36
export class CustomEditorService extends Disposable implements ICustomEditorService, ICustomEditorViewTypesHandler {
37 38
	_serviceBrand: any;

39
	private readonly _contributedEditors = this._register(new ContributedCustomEditors());
40
	private readonly _editorCapabilities = new Map<string, CustomEditorCapabilities>();
41

42
	private readonly _models = new CustomEditorModelManager();
43

44
	private readonly _customEditorContextKey: IContextKey<string>;
45
	private readonly _focusedCustomEditorIsEditable: IContextKey<boolean>;
46
	private readonly _webviewHasOwnEditFunctions: IContextKey<boolean>;
47 48
	private readonly _onDidChangeViewTypes = new Emitter<void>();
	onDidChangeViewTypes: Event<void> = this._onDidChangeViewTypes.event;
49 50

	constructor(
51
		@IContextKeyService contextKeyService: IContextKeyService,
52
		@IFileService fileService: IFileService,
53
		@IConfigurationService private readonly configurationService: IConfigurationService,
54
		@IEditorService private readonly editorService: IEditorService,
55
		@IEditorGroupsService private readonly editorGroupService: IEditorGroupsService,
56 57
		@IInstantiationService private readonly instantiationService: IInstantiationService,
		@IQuickInputService private readonly quickInputService: IQuickInputService,
58
		@IWebviewService private readonly webviewService: IWebviewService,
59
	) {
60 61
		super();

62
		this._customEditorContextKey = CONTEXT_CUSTOM_EDITORS.bindTo(contextKeyService);
63
		this._focusedCustomEditorIsEditable = CONTEXT_FOCUSED_CUSTOM_EDITOR_IS_EDITABLE.bindTo(contextKeyService);
64
		this._webviewHasOwnEditFunctions = webviewHasOwnEditFunctionsContext.bindTo(contextKeyService);
65

66
		this._register(this.editorService.registerCustomEditorViewTypesHandler('Custom Editor', this));
67
		this._register(this._contributedEditors.onChange(() => {
68
			this.updateContexts();
69
			this._onDidChangeViewTypes.fire();
70
		}));
71
		this._register(this.editorService.onDidActiveEditorChange(() => this.updateContexts()));
72

73 74 75 76 77 78
		this._register(fileService.onDidRunOperation(e => {
			if (e.isOperation(FileOperation.MOVE)) {
				this.handleMovedFileInOpenedFileEditors(e.resource, e.target.resource);
			}
		}));

79 80 81
		this.updateContexts();
	}

82 83 84 85
	getViewTypes(): ICustomEditorInfo[] {
		return [...this._contributedEditors];
	}

86 87
	public get models() { return this._models; }

88
	public getCustomEditor(viewType: string): CustomEditorInfo | undefined {
89
		return this._contributedEditors.get(viewType);
90 91
	}

92
	public getContributedCustomEditors(resource: URI): CustomEditorInfoCollection {
93
		return new CustomEditorInfoCollection(this._contributedEditors.getContributedEditors(resource));
94 95
	}

96
	public getUserConfiguredCustomEditors(resource: URI): CustomEditorInfoCollection {
97
		const rawAssociations = this.configurationService.getValue<CustomEditorsAssociations>(customEditorsAssociationsSettingId) || [];
98 99 100
		return new CustomEditorInfoCollection(
			coalesce(rawAssociations
				.filter(association => CustomEditorInfo.selectorMatches(association, resource))
101
				.map(association => this._contributedEditors.get(association.viewType))));
102 103
	}

104 105 106 107 108 109 110
	public getAllCustomEditors(resource: URI): CustomEditorInfoCollection {
		return new CustomEditorInfoCollection([
			...this.getUserConfiguredCustomEditors(resource).allEditors,
			...this.getContributedCustomEditors(resource).allEditors,
		]);
	}

111 112 113 114
	public async promptOpenWith(
		resource: URI,
		options?: ITextEditorOptions,
		group?: IEditorGroup,
115
	): Promise<IEditorPane | undefined> {
116 117 118 119 120 121 122 123 124 125 126 127
		const pick = await this.showOpenWithPrompt(resource, group);
		if (!pick) {
			return;
		}

		return this.openWith(resource, pick, options, group);
	}

	private showOpenWithPrompt(
		resource: URI,
		group?: IEditorGroup,
	): Promise<string | undefined> {
128
		const customEditors = new CustomEditorInfoCollection([
129
			defaultCustomEditor,
130
			...this.getAllCustomEditors(resource).allEditors,
131
		]);
132

133 134
		let currentlyOpenedEditorType: undefined | string;
		for (const editor of group ? group.editors : []) {
135
			if (editor.resource && isEqual(editor.resource, resource)) {
136
				currentlyOpenedEditorType = editor instanceof CustomEditorInput ? editor.viewType : defaultCustomEditor.id;
137 138 139 140
				break;
			}
		}

141 142
		const resourceExt = extname(resource);

143
		const items = customEditors.allEditors.map((editorDescriptor): IQuickPickItem => ({
144 145 146 147
			label: editorDescriptor.displayName,
			id: editorDescriptor.id,
			description: editorDescriptor.id === currentlyOpenedEditorType
				? nls.localize('openWithCurrentlyActive', "Currently Active")
148
				: undefined,
149
			detail: editorDescriptor.providerDisplayName,
150 151 152 153
			buttons: resourceExt ? [{
				iconClass: 'codicon-settings-gear',
				tooltip: nls.localize('promptOpenWith.setDefaultTooltip', "Set as default editor for '{0}' files", resourceExt)
			}] : undefined
154
		}));
155 156 157 158 159

		const picker = this.quickInputService.createQuickPick();
		picker.items = items;
		picker.placeholder = nls.localize('promptOpenWith.placeHolder', "Select editor to use for '{0}'...", basename(resource));

160
		return new Promise<string | undefined>(resolve => {
161 162 163 164 165 166 167 168 169 170 171 172
			picker.onDidAccept(() => {
				resolve(picker.selectedItems.length === 1 ? picker.selectedItems[0].id : undefined);
				picker.dispose();
			});
			picker.onDidTriggerItemButton(e => {
				const pick = e.item.id;
				resolve(pick); // open the view
				picker.dispose();

				// And persist the setting
				if (pick) {
					const newAssociation: CustomEditorAssociation = { viewType: pick, filenamePattern: '*' + resourceExt };
173
					const currentAssociations = [...this.configurationService.getValue<CustomEditorsAssociations>(customEditorsAssociationsSettingId)];
174 175 176 177 178 179

					// First try updating existing association
					for (let i = 0; i < currentAssociations.length; ++i) {
						const existing = currentAssociations[i];
						if (existing.filenamePattern === newAssociation.filenamePattern) {
							currentAssociations.splice(i, 1, newAssociation);
180
							this.configurationService.updateValue(customEditorsAssociationsSettingId, currentAssociations);
181 182 183 184 185 186
							return;
						}
					}

					// Otherwise, create a new one
					currentAssociations.unshift(newAssociation);
187
					this.configurationService.updateValue(customEditorsAssociationsSettingId, currentAssociations);
188 189 190
				}
			});
			picker.show();
191 192 193
		});
	}

194
	public async openWith(
195 196 197 198
		resource: URI,
		viewType: string,
		options?: ITextEditorOptions,
		group?: IEditorGroup,
199
	): Promise<IEditorPane | undefined> {
200
		if (viewType === defaultCustomEditor.id) {
201 202
			const fileEditorInput = this.editorService.createEditorInput({ resource, forceFile: true });
			return this.openEditorForResource(resource, fileEditorInput, { ...options, ignoreOverrides: true }, group);
203 204
		}

205
		if (!this._contributedEditors.get(viewType)) {
206 207 208
			return this.promptOpenWith(resource, options, group);
		}

209 210 211 212 213 214 215 216
		const capabilities = this.getCustomEditorCapabilities(viewType) || {};
		if (!capabilities.supportsMultipleEditorsPerResource) {
			const movedEditor = await this.tryRevealExistingEditorForResourceInGroup(resource, viewType, options, group);
			if (movedEditor) {
				return movedEditor;
			}
		}

217
		const input = this.createInput(resource, viewType, group?.id);
218 219 220 221 222 223
		return this.openEditorForResource(resource, input, options, group);
	}

	public createInput(
		resource: URI,
		viewType: string,
224
		group: GroupIdentifier | undefined,
225
		options?: { readonly customClasses: string; },
226
	): IEditorInput {
227
		if (viewType === defaultCustomEditor.id) {
228
			return this.editorService.createEditorInput({ resource, forceFile: true });
M
Matt Bierner 已提交
229 230
		}

231
		const id = generateUuid();
232
		const webview = new Lazy(() => {
233
			return this.webviewService.createWebviewOverlay(id, { customClasses: options?.customClasses }, {});
234
		});
235
		const input = this.instantiationService.createInstance(CustomEditorInput, resource, viewType, id, webview, {});
236 237
		if (typeof group !== 'undefined') {
			input.updateGroup(group);
238
		}
239
		return input;
240 241 242 243 244 245 246
	}

	private async openEditorForResource(
		resource: URI,
		input: IEditorInput,
		options?: IEditorOptions,
		group?: IEditorGroup
247
	): Promise<IEditorPane | undefined> {
248 249
		const targetGroup = group || this.editorGroupService.activeGroup;

250 251 252 253
		if (options && typeof options.activation === 'undefined') {
			options = { ...options, activation: options.preserveFocus ? EditorActivation.RESTORE : undefined };
		}

254
		// Try to replace existing editors for resource
255
		const existingEditors = targetGroup.editors.filter(editor => editor.resource && isEqual(editor.resource, resource));
256 257 258 259 260 261 262 263 264
		if (existingEditors.length) {
			const existing = existingEditors[0];
			if (!input.matches(existing)) {
				await this.editorService.replaceEditors([{
					editor: existing,
					replacement: input,
					options: options ? EditorOptions.create(options) : undefined,
				}], targetGroup);

265
				if (existing instanceof CustomEditorInput) {
266
					existing.dispose();
267
				}
268 269
			}
		}
270

271 272
		return this.editorService.openEditor(input, options, group);
	}
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287
	public registerCustomEditorCapabilities(viewType: string, options: CustomEditorCapabilities): IDisposable {
		if (this._editorCapabilities.has(viewType)) {
			throw new Error(`Capabilities for ${viewType} already set`);
		}
		this._editorCapabilities.set(viewType, options);
		return toDisposable(() => {
			this._editorCapabilities.delete(viewType);
		});
	}

	private getCustomEditorCapabilities(viewType: string): CustomEditorCapabilities | undefined {
		return this._editorCapabilities.get(viewType);
	}

288
	private updateContexts() {
289
		const activeEditorPane = this.editorService.activeEditorPane;
290
		const resource = activeEditorPane?.input?.resource;
291
		if (!resource) {
292
			this._customEditorContextKey.reset();
293
			this._focusedCustomEditorIsEditable.reset();
294
			this._webviewHasOwnEditFunctions.reset();
295 296
			return;
		}
297

298 299
		const possibleEditors = this.getAllCustomEditors(resource).allEditors;

300
		this._customEditorContextKey.set(possibleEditors.map(x => x.id).join(','));
301
		this._focusedCustomEditorIsEditable.set(activeEditorPane?.input instanceof CustomEditorInput);
302
		this._webviewHasOwnEditFunctions.set(possibleEditors.length > 0);
303
	}
304

305 306 307 308 309
	private async handleMovedFileInOpenedFileEditors(oldResource: URI, newResource: URI): Promise<void> {
		if (extname(oldResource) === extname(newResource)) {
			return;
		}

310 311 312 313
		const possibleEditors = this.getAllCustomEditors(newResource);

		// See if we have any non-optional custom editor for this resource
		if (!possibleEditors.allEditors.some(editor => editor.priority !== CustomEditorPriority.option)) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
			return;
		}

		// If so, check all editors to see if there are any file editors open for the new resource
		const editorsToReplace = new Map<GroupIdentifier, IEditorInput[]>();
		for (const group of this.editorGroupService.groups) {
			for (const editor of group.editors) {
				if (editor instanceof FileEditorInput
					&& !(editor instanceof CustomEditorInput)
					&& isEqual(editor.resource, newResource)
				) {
					let entry = editorsToReplace.get(group.id);
					if (!entry) {
						entry = [];
						editorsToReplace.set(group.id, entry);
					}
					entry.push(editor);
				}
			}
		}

		if (!editorsToReplace.size) {
			return;
		}

339 340 341 342 343 344
		let viewType: string | undefined;
		if (possibleEditors.defaultEditor) {
			viewType = possibleEditors.defaultEditor.id;
		} else {
			// If there is, show a single prompt for all editors to see if the user wants to re-open them
			//
345
			// TODO: instead of prompting eagerly, it'd likely be better to replace all the editors with
346 347 348 349 350 351
			// ones that would prompt when they first become visible
			await new Promise(resolve => setTimeout(resolve, 50));
			viewType = await this.showOpenWithPrompt(newResource);
		}

		if (!viewType) {
352 353 354 355 356
			return;
		}

		for (const [group, entries] of editorsToReplace) {
			this.editorService.replaceEditors(entries.map(editor => {
357
				const replacement = this.createInput(newResource, viewType!, group);
358 359 360 361 362 363 364 365 366 367
				return {
					editor,
					replacement,
					options: {
						preserveFocus: true,
					}
				};
			}), group);
		}
	}
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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	private async tryRevealExistingEditorForResourceInGroup(
		resource: URI,
		viewType: string,
		options?: ITextEditorOptions,
		group?: IEditorGroup,
	): Promise<IEditorPane | undefined> {
		const editorInfoForResource = this.findExistingEditorsForResource(resource, viewType);
		if (!editorInfoForResource.length) {
			return undefined;
		}

		const editorToUse = editorInfoForResource[0];

		// Replace all other editors
		for (const { editor, group } of editorInfoForResource) {
			if (editor !== editorToUse.editor) {
				group.closeEditor(editor);
			}
		}

		const targetGroup = group || this.editorGroupService.activeGroup;
		const newEditor = await this.openEditorForResource(resource, editorToUse.editor, { ...options, ignoreOverrides: true }, targetGroup);
		if (targetGroup.id !== editorToUse.group.id) {
			editorToUse.group.closeEditor(editorToUse.editor);
		}
		return newEditor;
	}

	private findExistingEditorsForResource(
		resource: URI,
		viewType: string,
	): Array<{ editor: IEditorInput, group: IEditorGroup }> {
		const out: Array<{ editor: IEditorInput, group: IEditorGroup }> = [];
		const orderedGroups = distinct([
			this.editorGroupService.activeGroup,
			...this.editorGroupService.groups,
		]);

		for (const group of orderedGroups) {
			for (const editor of group.editors) {
				if (isMatchingCustomEditor(editor, viewType, resource)) {
					out.push({ editor, group });
				}
			}
		}
		return out;
	}
416
}
417

418
export class CustomEditorContribution extends Disposable implements IWorkbenchContribution {
419
	constructor(
420
		@IEditorService private readonly editorService: EditorServiceImpl,
421 422
		@ICustomEditorService private readonly customEditorService: ICustomEditorService,
	) {
423 424
		super();

R
rebornix 已提交
425
		this._register(this.editorService.overrideOpenEditor({
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
			open: (editor, options, group, id) => {
				return this.onEditorOpening(editor, options, group, id);
			},
			getEditorOverrides: (editor: IEditorInput, _options: IEditorOptions | undefined, _group: IEditorGroup | undefined): IOpenEditorOverrideEntry[] => {
				const resource = editor.resource;
				if (!resource) {
					return [];
				}

				const customEditors = this.customEditorService.getAllCustomEditors(resource);
				return customEditors.allEditors.map(entry => {
					return {
						id: entry.id,
						active: editor instanceof CustomEditorInput && editor.viewType === entry.id,
						label: entry.displayName,
						detail: entry.providerDisplayName,
					};
				});
R
rebornix 已提交
444
			}
445 446 447
		}));

		this._register(this.editorService.onDidCloseEditor(({ editor }) => {
448
			if (!(editor instanceof CustomEditorInput)) {
449 450 451 452 453 454 455
				return;
			}

			if (!this.editorService.editors.some(other => other === editor)) {
				editor.dispose();
			}
		}));
456 457 458 459 460
	}

	private onEditorOpening(
		editor: IEditorInput,
		options: ITextEditorOptions | undefined,
461 462
		group: IEditorGroup,
		id?: string,
463
	): IOpenEditorOverride | undefined {
464
		if (editor instanceof CustomEditorInput) {
465
			if (editor.group === group.id && (editor.viewType === id || typeof id !== 'string')) {
466
				// No need to do anything
467
				return undefined;
468 469 470 471 472
			} else {
				// Create a copy of the input.
				// Unlike normal editor inputs, we do not want to share custom editor inputs
				// between multiple editors / groups.
				return {
473
					override: this.customEditorService.openWith(editor.resource, id ?? editor.viewType, options, group)
474
				};
475
			}
476
		}
477 478

		if (editor instanceof DiffEditorInput) {
M
Matt Bierner 已提交
479
			return this.onDiffEditorOpening(editor, options, group);
480
		}
481

482
		const resource = editor.resource;
483 484
		if (!resource) {
			return undefined;
485
		}
486 487 488 489 490 491 492 493 494 495 496 497

		if (id) {
			if (editor instanceof FileEditorInput && id === defaultCustomEditor.id) {
				return undefined;
			}

			return {
				override: this.customEditorService.openWith(resource, id, { ...options, ignoreOverrides: true }, group)
			};
		}

		return this.onResourceEditorOpening(resource, editor, options, group);
M
Matt Bierner 已提交
498
	}
499

M
Matt Bierner 已提交
500 501 502 503
	private onResourceEditorOpening(
		resource: URI,
		editor: IEditorInput,
		options: ITextEditorOptions | undefined,
504
		group: IEditorGroup,
M
Matt Bierner 已提交
505
	): IOpenEditorOverride | undefined {
506 507 508 509 510 511
		const userConfiguredEditors = this.customEditorService.getUserConfiguredCustomEditors(resource);
		const contributedEditors = this.customEditorService.getContributedCustomEditors(resource);
		if (!userConfiguredEditors.length && !contributedEditors.length) {
			return;
		}

512 513
		// Check to see if there already an editor for the resource in the group.
		// If there is, we want to open that instead of creating a new editor.
514
		// This ensures that we preserve whatever type of editor was previously being used
515
		// when the user switches back to it.
516
		const existingEditorForResource = group.editors.find(editor => isEqual(resource, editor.resource));
517
		if (existingEditorForResource) {
518 519 520 521
			if (editor === existingEditorForResource) {
				return;
			}

522
			return {
523 524 525 526 527
				override: this.editorService.openEditor(existingEditorForResource, {
					...options,
					ignoreOverrides: true,
					activation: options?.preserveFocus ? EditorActivation.RESTORE : undefined,
				}, group)
528 529 530
			};
		}

M
Matt Bierner 已提交
531 532
		if (userConfiguredEditors.length) {
			return {
533
				override: this.customEditorService.openWith(resource, userConfiguredEditors.allEditors[0].id, options, group),
M
Matt Bierner 已提交
534
			};
535 536
		}

M
Matt Bierner 已提交
537 538
		if (!contributedEditors.length) {
			return;
539 540
		}

541
		const defaultEditor = contributedEditors.defaultEditor;
M
Matt Bierner 已提交
542
		if (defaultEditor) {
543
			return {
M
Matt Bierner 已提交
544
				override: this.customEditorService.openWith(resource, defaultEditor.id, options, group),
545 546 547
			};
		}

548
		// If we have all optional editors, then open VS Code's standard editor
549
		if (contributedEditors.allEditors.every(editor => editor.priority === CustomEditorPriority.option)) {
550 551 552
			return;
		}

M
Matt Bierner 已提交
553
		// Open VS Code's standard editor but prompt user to see if they wish to use a custom one instead
554 555 556
		return {
			override: (async () => {
				const standardEditor = await this.editorService.openEditor(editor, { ...options, ignoreOverrides: true }, group);
557 558 559
				// Give a moment to make sure the editor is showing.
				// Otherwise the focus shift can cause the prompt to be dismissed right away.
				await new Promise(resolve => setTimeout(resolve, 20));
560 561 562 563 564 565 566 567 568 569 570 571 572
				const selectedEditor = await this.customEditorService.promptOpenWith(resource, options, group);
				if (selectedEditor && selectedEditor.input) {
					await group.replaceEditors([{
						editor,
						replacement: selectedEditor.input
					}]);
					return selectedEditor;
				}

				return standardEditor;
			})()
		};
	}
M
Matt Bierner 已提交
573 574 575 576 577 578 579

	private onDiffEditorOpening(
		editor: DiffEditorInput,
		options: ITextEditorOptions | undefined,
		group: IEditorGroup
	): IOpenEditorOverride | undefined {
		const getCustomEditorOverrideForSubInput = (subInput: IEditorInput, customClasses: string): EditorInput | undefined => {
580
			if (subInput instanceof CustomEditorInput) {
M
Matt Bierner 已提交
581 582
				return undefined;
			}
583
			const resource = subInput.resource;
M
Matt Bierner 已提交
584 585 586 587
			if (!resource) {
				return undefined;
			}

588
			// Prefer default editors in the diff editor case but ultimatly always take the first editor
589 590 591 592 593 594 595
			const allEditors = new CustomEditorInfoCollection([
				...this.customEditorService.getUserConfiguredCustomEditors(resource).allEditors,
				...this.customEditorService.getContributedCustomEditors(resource).allEditors.filter(x => x.priority !== CustomEditorPriority.option),
			]);

			const bestAvailableEditor = allEditors.bestAvailableEditor;
			if (!bestAvailableEditor) {
M
Matt Bierner 已提交
596 597
				return undefined;
			}
598

599
			const input = this.customEditorService.createInput(resource, bestAvailableEditor.id, group.id, { customClasses });
600 601 602 603 604
			if (input instanceof EditorInput) {
				return input;
			}

			return undefined;
M
Matt Bierner 已提交
605 606 607 608 609 610 611 612
		};

		const modifiedOverride = getCustomEditorOverrideForSubInput(editor.modifiedInput, 'modified');
		const originalOverride = getCustomEditorOverrideForSubInput(editor.originalInput, 'original');

		if (modifiedOverride || originalOverride) {
			return {
				override: (async () => {
613
					const input = new DiffEditorInput(editor.getName(), editor.getDescription(), originalOverride || editor.originalInput, modifiedOverride || editor.modifiedInput, true);
M
Matt Bierner 已提交
614 615 616 617 618 619 620
					return this.editorService.openEditor(input, { ...options, ignoreOverrides: true }, group);
				})(),
			};
		}

		return undefined;
	}
621 622
}

623 624 625 626 627
function isMatchingCustomEditor(editor: IEditorInput, viewType: string, resource: URI): boolean {
	return editor instanceof CustomEditorInput
		&& editor.viewType === viewType
		&& isEqual(editor.resource, resource);
}
628

629 630 631 632 633 634
registerThemingParticipant((theme, collector) => {
	const shadow = theme.getColor(colorRegistry.scrollbarShadow);
	if (shadow) {
		collector.addRule(`.webview.modified { box-shadow: -6px 0 5px -5px ${shadow}; }`);
	}
});