Skip to content

Commit

Permalink
File: reopen with...
Browse files Browse the repository at this point in the history
  • Loading branch information
rebornix committed Apr 9, 2020
1 parent 8b46fd0 commit dc1746e
Show file tree
Hide file tree
Showing 14 changed files with 221 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/vs/workbench/browser/parts/editor/baseEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export abstract class BaseEditor extends Composite implements IEditorPane {
get input(): EditorInput | undefined { return this._input; }

protected _options: EditorOptions | undefined;
get options(): EditorOptions | undefined { return this._options; }

private _group?: IEditorGroup;
get group(): IEditorGroup | undefined { return this._group; }
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export interface IEditorPane extends IComposite {
*/
readonly input: IEditorInput | undefined;

/**
* The assigned options of the editor.
*/
readonly options: EditorOptions | undefined;

/**
* The assigned group this editor is showing in.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,10 @@ export class CustomEditorContribution extends Disposable implements IWorkbenchCo
) {
super();

this._register(this.editorService.overrideOpenEditor((editor, options, group) => {
return this.onEditorOpening(editor, options, group);
this._register(this.editorService.overrideOpenEditor({
open: (editor, options, group) => {
return this.onEditorOpening(editor, options, group);
}
}));

this._register(this.editorService.onDidCloseEditor(({ editor }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as nls from 'vs/nls';
import { Registry } from 'vs/platform/registry/common/platform';
import { ToggleAutoSaveAction, GlobalNewUntitledFileAction, FocusFilesExplorer, GlobalCompareResourcesAction, SaveAllAction, ShowActiveFileInExplorer, CollapseExplorerView, RefreshExplorerView, CompareWithClipboardAction, NEW_FILE_COMMAND_ID, NEW_FILE_LABEL, NEW_FOLDER_COMMAND_ID, NEW_FOLDER_LABEL, TRIGGER_RENAME_LABEL, MOVE_FILE_TO_TRASH_LABEL, COPY_FILE_LABEL, PASTE_FILE_LABEL, FileCopiedContext, renameHandler, moveFileToTrashHandler, copyFileHandler, pasteFileHandler, deleteFileHandler, cutFileHandler, DOWNLOAD_COMMAND_ID, openFilePreserveFocusHandler, DOWNLOAD_LABEL, ShowOpenedFileInNewWindow } from 'vs/workbench/contrib/files/browser/fileActions';
import { ToggleAutoSaveAction, GlobalNewUntitledFileAction, FocusFilesExplorer, GlobalCompareResourcesAction, SaveAllAction, ShowActiveFileInExplorer, CollapseExplorerView, RefreshExplorerView, CompareWithClipboardAction, NEW_FILE_COMMAND_ID, NEW_FILE_LABEL, NEW_FOLDER_COMMAND_ID, NEW_FOLDER_LABEL, TRIGGER_RENAME_LABEL, MOVE_FILE_TO_TRASH_LABEL, COPY_FILE_LABEL, PASTE_FILE_LABEL, FileCopiedContext, renameHandler, moveFileToTrashHandler, copyFileHandler, pasteFileHandler, deleteFileHandler, cutFileHandler, DOWNLOAD_COMMAND_ID, openFilePreserveFocusHandler, DOWNLOAD_LABEL, ShowOpenedFileInNewWindow, ReopenResourcesAction } from 'vs/workbench/contrib/files/browser/fileActions';
import { revertLocalChangesCommand, acceptLocalChangesCommand, CONFLICT_RESOLUTION_CONTEXT } from 'vs/workbench/contrib/files/browser/editors/textFileSaveErrorHandler';
import { SyncActionDescriptor, MenuId, MenuRegistry, ILocalizedString } from 'vs/platform/actions/common/actions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
Expand Down Expand Up @@ -36,6 +36,7 @@ const category = { value: nls.localize('filesCategory', "File"), original: 'File
const registry = Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions);
registry.registerWorkbenchAction(SyncActionDescriptor.create(SaveAllAction, SaveAllAction.ID, SaveAllAction.LABEL, { primary: undefined, mac: { primary: KeyMod.CtrlCmd | KeyMod.Alt | KeyCode.KEY_S }, win: { primary: KeyChord(KeyMod.CtrlCmd | KeyCode.KEY_K, KeyCode.KEY_S) } }), 'File: Save All', category.value);
registry.registerWorkbenchAction(SyncActionDescriptor.create(GlobalCompareResourcesAction, GlobalCompareResourcesAction.ID, GlobalCompareResourcesAction.LABEL), 'File: Compare Active File With...', category.value);
registry.registerWorkbenchAction(SyncActionDescriptor.create(ReopenResourcesAction, ReopenResourcesAction.ID, ReopenResourcesAction.LABEL), 'File: Reopen With...', category.value);
registry.registerWorkbenchAction(SyncActionDescriptor.create(FocusFilesExplorer, FocusFilesExplorer.ID, FocusFilesExplorer.LABEL), 'File: Focus on Files Explorer', category.value);
registry.registerWorkbenchAction(SyncActionDescriptor.create(ShowActiveFileInExplorer, ShowActiveFileInExplorer.ID, ShowActiveFileInExplorer.LABEL), 'File: Reveal Active File in Side Bar', category.value);
registry.registerWorkbenchAction(SyncActionDescriptor.create(CollapseExplorerView, CollapseExplorerView.ID, CollapseExplorerView.LABEL), 'File: Collapse Folders in Explorer', category.value);
Expand Down
124 changes: 106 additions & 18 deletions src/vs/workbench/contrib/files/browser/fileActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import { dispose, IDisposable } from 'vs/base/common/lifecycle';
import { VIEWLET_ID, IExplorerService, IFilesConfiguration } from 'vs/workbench/contrib/files/common/files';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IFileService } from 'vs/platform/files/common/files';
import { toResource, SideBySideEditor } from 'vs/workbench/common/editor';
import { toResource, SideBySideEditor, IEditorInput } from 'vs/workbench/common/editor';
import { ExplorerViewPaneContainer } from 'vs/workbench/contrib/files/browser/explorerViewlet';
import { IQuickInputService, ItemActivation } from 'vs/platform/quickinput/common/quickInput';
import { IQuickInputService, ItemActivation, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet';
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { ITextModel } from 'vs/editor/common/model';
Expand All @@ -34,7 +34,7 @@ import { RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { Schemas } from 'vs/base/common/network';
import { IDialogService, IConfirmationResult, getFileNamesMessage, IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditorService, IOpenEditorOverrideHandler } from 'vs/workbench/services/editor/common/editorService';
import { Constants } from 'vs/base/common/uint';
import { CLOSE_EDITORS_AND_GROUP_COMMAND_ID } from 'vs/workbench/browser/parts/editor/editorCommands';
import { coalesce } from 'vs/base/common/arrays';
Expand All @@ -47,6 +47,9 @@ import { IWorkingCopyService, IWorkingCopy } from 'vs/workbench/services/working
import { sequence, timeout } from 'vs/base/common/async';
import { IWorkingCopyFileService } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
import { once } from 'vs/base/common/functional';
import { IEditorOptions } from 'vs/platform/editor/common/editor';
import { IEditorGroup } from 'vs/workbench/services/editor/common/editorGroupsService';
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';

export const NEW_FILE_COMMAND_ID = 'explorer.newFile';
export const NEW_FILE_LABEL = nls.localize('newFile', "New File");
Expand Down Expand Up @@ -480,23 +483,27 @@ export class GlobalCompareResourcesAction extends Action {
if (activeResource) {

// Compare with next editor that opens
const toDispose = this.editorService.overrideOpenEditor(editor => {

// Only once!
toDispose.dispose();
const toDispose = this.editorService.overrideOpenEditor({
getEditorOverrides: (editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
return [];
},
open: editor => {
// Only once!
toDispose.dispose();

// Open editor as diff
const resource = editor.resource;
if (resource) {
return {
override: this.editorService.openEditor({
leftResource: activeResource,
rightResource: resource
})
};
}

// Open editor as diff
const resource = editor.resource;
if (resource) {
return {
override: this.editorService.openEditor({
leftResource: activeResource,
rightResource: resource
})
};
return undefined;
}

return undefined;
});

once(this.quickInputService.onHide)((async () => {
Expand All @@ -512,6 +519,87 @@ export class GlobalCompareResourcesAction extends Action {
}
}

const builtinProviderDisplayName = nls.localize('builtinProviderDisplayName', "Built-in");
export class ReopenResourcesAction extends Action {

static readonly ID = 'workbench.files.action.reopenWithEditor';
static readonly LABEL = nls.localize('workbench.files.action.reopenWithEditor', "Reopen With...");

constructor(
id: string,
label: string,
@IQuickInputService private readonly quickInputService: IQuickInputService,
@IEditorService private readonly editorService: IEditorService,
) {
super(id, label);
}

async run(): Promise<void> {
const activeInput = this.editorService.activeEditor;
const activeEditorPane = this.editorService.activeEditorPane;
if (!activeEditorPane) {
return;
}

const options = activeEditorPane.options;
const group = activeEditorPane.group;
const activeResource = activeInput ? activeInput.resource : undefined;

if (!activeResource) {
return;
}

const overrides = this.editorService.getEditorOverrides(activeInput!, options, group);
const items: (IQuickPickItem & { handler?: IOpenEditorOverrideHandler })[] = overrides.map((override) => {
return {
handler: override[0],
id: override[1].id,
label: override[1].label,
description: override[1].active ? 'Currently Active' : undefined,
detail: override[1].detail
};
});

if (!items.length) {
return;
}

items.unshift({
id: 'default',
label: nls.localize('promptOpenWith.defaultEditor.displayName', "Text Editor"),
description: activeInput instanceof FileEditorInput ? 'Currently Active' : undefined,
detail: builtinProviderDisplayName
});

const picker = this.quickInputService.createQuickPick<(IQuickPickItem & { handler?: IOpenEditorOverrideHandler })>();
picker.items = items;
picker.placeholder = nls.localize('promptOpenWith.placeHolder', "Select editor to use for '{0}'...", resources.basename(activeResource));

const pickedItem = await new Promise<(IQuickPickItem & { handler?: IOpenEditorOverrideHandler }) | undefined>(resolve => {
picker.onDidAccept(() => {
resolve(picker.selectedItems.length === 1 ? picker.selectedItems[0] : undefined);
picker.dispose();
});

picker.show();
});

if (!pickedItem) {
return;
}

if (pickedItem.id === 'default') {
const fileEditorInput = this.editorService.createEditorInput({ resource: activeResource!, forceFile: true });
const textOptions = options ? { ...options, ignoreOverrides: true } : { ignoreOverrides: true };

await this.editorService.openEditor(fileEditorInput, textOptions, group);
return;
}

await pickedItem.handler!.open(activeInput!, options, group, pickedItem.id);

This comment has been minimized.

Copy link
@bpasero

bpasero Apr 23, 2020

Member

@rebornix @mjbvz this await does nothing, did you mean to await on the resulting override?

}
}

export class ToggleAutoSaveAction extends Action {
static readonly ID = 'workbench.action.toggleAutoSave';
static readonly LABEL = nls.localize('toggleAutoSave', "Toggle Auto Save");
Expand Down
44 changes: 35 additions & 9 deletions src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,26 @@ export class NotebookContribution implements IWorkbenchContribution {
@IInstantiationService private readonly instantiationService: IInstantiationService

) {
this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
this.editorService.overrideOpenEditor({
getEditorOverrides: (editor: IEditorInput, options: IEditorOptions | undefined, group: IEditorGroup | undefined) => {
let resource = editor.resource;
if (!resource) {
return [];
}

const infos = notebookService.getContributedNotebookProviders(resource);

return infos.map(info => {
return {
label: info.displayName,
id: info.id,
active: editor instanceof NotebookEditorInput && editor.viewType === info.id,
detail: info.providerDisplayName
};
});
},
open: (editor, options, group, id) => this.onEditorOpening(editor, options, group, id)
});

this.editorService.onDidActiveEditorChange(() => {
if (this.editorService.activeEditor && this.editorService.activeEditor! instanceof NotebookEditorInput) {
Expand All @@ -104,23 +123,30 @@ export class NotebookContribution implements IWorkbenchContribution {
});
}

private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup): IOpenEditorOverride | undefined {
private onEditorOpening(originalInput: IEditorInput, options: IEditorOptions | ITextEditorOptions | undefined, group: IEditorGroup, id: string | undefined): IOpenEditorOverride | undefined {
let resource = originalInput.resource;
if (!resource) {
return undefined;
}

let info: NotebookProviderInfo | undefined;
const data = CellUri.parse(resource);
if (data && (info = getFirstNotebookInfo(this.notebookService, data.notebook))) {
// cell-uri -> open (container) notebook
const name = basename(data.notebook);
const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, info.id);
this._resourceMapping.set(resource, input);
return { override: this.editorService.openEditor(input, new NotebookEditorOptions({ ...options, forceReload: true, cellOptions: { resource, options } }), group) };
if (data) {
const infos = this.notebookService.getContributedNotebookProviders(data.notebook);

if (infos.length) {
const info = id === undefined ? infos[0] : (infos.find(info => info.id === id) || infos[0]);
// cell-uri -> open (container) notebook
const name = basename(data.notebook);
const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, info.id);
this._resourceMapping.set(resource, input);
return { override: this.editorService.openEditor(input, new NotebookEditorOptions({ ...options, forceReload: true, cellOptions: { resource, options } }), group) };
}
}

info = getFirstNotebookInfo(this.notebookService, resource);
const infos = this.notebookService.getContributedNotebookProviders(resource);
info = id === undefined ? infos[0] : infos.find(info => info.id === id);

if (!info) {
return undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions src/vs/workbench/contrib/notebook/browser/notebookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as nls from 'vs/nls';
import { Disposable, IDisposable, DisposableStore } from 'vs/base/common/lifecycle';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { URI } from 'vs/base/common/uri';
Expand Down Expand Up @@ -143,6 +144,7 @@ export class NotebookService extends Disposable implements INotebookService {
id: notebookContribution.viewType,
displayName: notebookContribution.displayName,
selector: notebookContribution.selector || [],
providerDisplayName: extension.description.isBuiltin ? nls.localize('builtinProviderDisplayName', "Built-in") : extension.description.displayName || extension.description.identifier.value,
}));
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/vs/workbench/contrib/notebook/common/notebookProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ export class NotebookProviderInfo {
readonly id: string;
readonly displayName: string;
readonly selector: readonly NotebookSelector[];
readonly providerDisplayName: string;

constructor(descriptor: {
readonly id: string;
readonly displayName: string;
readonly selector: readonly NotebookSelector[];
readonly providerDisplayName: string;
}) {
this.id = descriptor.id;
this.displayName = descriptor.displayName;
this.selector = descriptor.selector;
this.providerDisplayName = descriptor.providerDisplayName;
}

matches(resource: URI): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export class PreferencesContribution implements IWorkbenchContribution {

// install editor opening listener unless user has disabled this
if (!!this.configurationService.getValue(USE_SPLIT_JSON_SETTING)) {
this.editorOpeningListener = this.editorService.overrideOpenEditor((editor, options, group) => this.onEditorOpening(editor, options, group));
this.editorOpeningListener = this.editorService.overrideOpenEditor({
open: (editor, options, group) => this.onEditorOpening(editor, options, group)
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { EditorDescriptor, Extensions as EditorExtensions, IEditorRegistry } from 'vs/workbench/browser/editor';
import { Extensions as ActionExtensions, IWorkbenchActionRegistry } from 'vs/workbench/common/actions';
import { Extensions as WorkbenchExtensions, IWorkbenchContribution, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions';
import { ActiveEditorContext, Extensions as EditorInputExtensions, IEditorInputFactory, IEditorInputFactoryRegistry } from 'vs/workbench/common/editor';
import { Extensions as EditorInputExtensions, IEditorInputFactory, IEditorInputFactoryRegistry, ActiveEditorContext } from 'vs/workbench/common/editor';
import * as SearchConstants from 'vs/workbench/contrib/search/common/constants';
import * as SearchEditorConstants from 'vs/workbench/contrib/searchEditor/browser/constants';
import { SearchEditor } from 'vs/workbench/contrib/searchEditor/browser/searchEditor';
Expand Down Expand Up @@ -54,27 +54,29 @@ class SearchEditorContribution implements IWorkbenchContribution {
@IContextKeyService protected readonly contextKeyService: IContextKeyService,
) {

this.editorService.overrideOpenEditor((editor, options, group) => {
const resource = editor.resource;
if (!resource) { return undefined; }
this.editorService.overrideOpenEditor({
open: (editor, options, group) => {
const resource = editor.resource;
if (!resource) { return undefined; }

if (!endsWith(resource.path, '.code-search')) {
return undefined;
}
if (!endsWith(resource.path, '.code-search')) {
return undefined;
}

if (group.isOpened(editor)) {
return undefined;
}
if (group.isOpened(editor) && editor instanceof SearchEditorInput) {
return undefined;
}

this.telemetryService.publicLog2('searchEditor/openSavedSearchEditor');
this.telemetryService.publicLog2('searchEditor/openSavedSearchEditor');

return {
override: (async () => {
const { config } = await instantiationService.invokeFunction(parseSavedSearchEditor, resource);
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { backingUri: resource, config });
return editorService.openEditor(input, { ...options, ignoreOverrides: true }, group);
})()
};
return {
override: (async () => {
const { config } = await instantiationService.invokeFunction(parseSavedSearchEditor, resource);
const input = instantiationService.invokeFunction(getOrMakeSearchEditorInput, { backingUri: resource, config });
return editorService.openEditor(input, { ...options, ignoreOverrides: true }, group);
})()
};
}
});
}
}
Expand Down
Loading

0 comments on commit dc1746e

Please sign in to comment.