Skip to content

Commit

Permalink
editors - make resource a required property of untitled untyped inp…
Browse files Browse the repository at this point in the history
…uts (#127792)
  • Loading branch information
bpasero committed Jul 7, 2021
1 parent d6cfa5f commit 4c2c3ca
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions src/vs/workbench/browser/dnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export class ResourcesDropHandler {
const targetGroup = resolveTargetGroup();
await this.editorService.openEditors(editors.map(editor => ({
...editor,
resource: editor.resource,
options: {
...editor.options,
pinned: true,
Expand Down
8 changes: 4 additions & 4 deletions src/vs/workbench/browser/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { onDidChangeFullscreen, isFullscreen } from 'vs/base/browser/browser';
import { IWorkingCopyBackupService } from 'vs/workbench/services/workingCopy/common/workingCopyBackup';
import { Registry } from 'vs/platform/registry/common/platform';
import { isWindows, isLinux, isMacintosh, isWeb, isNative, isIOS } from 'vs/base/common/platform';
import { IResourceDiffEditorInput, IUntypedEditorInput, pathsToEditors } from 'vs/workbench/common/editor';
import { IUntypedEditorInput, pathsToEditors } from 'vs/workbench/common/editor';
import { SideBySideEditorInput } from 'vs/workbench/common/editor/sideBySideEditorInput';
import { SidebarPart } from 'vs/workbench/browser/parts/sidebar/sidebarPart';
import { PanelPart } from 'vs/workbench/browser/parts/panel/panelPart';
Expand Down Expand Up @@ -596,12 +596,12 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
this.state.editor.restoreEditors = !!forceRestoreEditors || initialFilesToOpen === undefined;

// Files to open, diff or create
if (initialFilesToOpen !== undefined) {
if (initialFilesToOpen) {

// Files to diff is exclusive
return pathsToEditors(initialFilesToOpen.filesToDiff, fileService).then(filesToDiff => {
if (filesToDiff.length === 2) {
const diffEditorInput: IResourceDiffEditorInput[] = [{
const diffEditorInput: IUntypedEditorInput[] = [{
original: { resource: filesToDiff[0].resource },
modified: { resource: filesToDiff[1].resource },
options: { pinned: true },
Expand All @@ -627,7 +627,7 @@ export abstract class Layout extends Disposable implements IWorkbenchLayoutServi
return []; // do not open any empty untitled file if we have backups to restore
}

return [Object.create(null)]; // open empty untitled file
return [{ resource: undefined }]; // open empty untitled file
});
}

Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/browser/parts/editor/editorGroupView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ export class EditorGroupView extends Themable implements IEditorGroupView {
EventHelper.stop(e);

this.editorService.openEditor({
resource: undefined,
forceUntitled: true,
options: {
pinned: true,
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/browser/parts/editor/tabsTitleControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export class TabsTitleControl extends TitleControl {
EventHelper.stop(e);

this.editorService.openEditor({
resource: undefined,
forceUntitled: true,
options: {
pinned: true,
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export interface IUntitledTextResourceEditorInput extends IBaseTextResourceEdito
* force use the provided resource as associated path. As such, the resource will be used when saving
* the untitled editor.
*/
readonly resource?: URI;
readonly resource: URI | undefined;
}

export interface IResourceDiffEditorInput extends IBaseResourceEditorInput {
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/common/editor/diffEditorInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class DiffEditorInput extends SideBySideEditorInput {
const originalResourceEditorInput = this.secondary.toUntyped(group, context);
const modifiedResourceEditorInput = this.primary.toUntyped(group, context);

if (originalResourceEditorInput && modifiedResourceEditorInput) {
if (originalResourceEditorInput && modifiedResourceEditorInput && !isResourceDiffEditorInput(originalResourceEditorInput) && !isResourceDiffEditorInput(modifiedResourceEditorInput)) {
return {
label: this.name,
description: this.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class InspectKeyMap extends EditorAction {
const keybindingService = accessor.get(IKeybindingService);
const editorService = accessor.get(IEditorService);

editorService.openEditor({ contents: keybindingService._dumpDebugInfo(), options: { pinned: true } });
editorService.openEditor({ resource: undefined, contents: keybindingService._dumpDebugInfo(), options: { pinned: true } });
}
}

Expand All @@ -47,7 +47,7 @@ class InspectKeyMapJSON extends Action2 {
const editorService = accessor.get(IEditorService);
const keybindingService = accessor.get(IKeybindingService);

await editorService.openEditor({ contents: keybindingService._dumpDebugInfoJSON(), options: { pinned: true } });
await editorService.openEditor({ resource: undefined, contents: keybindingService._dumpDebugInfoJSON(), options: { pinned: true } });
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/files/browser/fileCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,9 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
const editorGroupsService = accessor.get(IEditorGroupsService);

const group = editorGroupsService.activeGroup;
await editorService.openEditor({ options: { override: args.viewType, pinned: true } }, group);
await editorService.openEditor({ resource: undefined, options: { override: args.viewType, pinned: true } }, group);
} else {
await editorService.openEditor({ options: { pinned: true } }); // untitled are always pinned
await editorService.openEditor({ resource: undefined, options: { pinned: true } }); // untitled are always pinned
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ suite('Files - TextFileEditorTracker', () => {
test('dirty untitled text file model opens as editor', async function () {
const accessor = await createTracker();

const untitledTextEditor = accessor.editorService.createEditorInput({ forceUntitled: true }) as UntitledTextEditorInput;
const untitledTextEditor = accessor.editorService.createEditorInput({ resource: undefined, forceUntitled: true }) as UntitledTextEditorInput;
const model = disposables.add(await untitledTextEditor.resolve());

assert.ok(!accessor.editorService.isOpened(untitledTextEditor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class GenerateColorThemeAction extends Action {
}, null, '\t');
contents = contents.replace(/\"__/g, '//"');

return this.editorService.openEditor({ contents, mode: 'jsonc', options: { pinned: true } });
return this.editorService.openEditor({ resource: undefined, contents, mode: 'jsonc', options: { pinned: true } });
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/vs/workbench/electron-sandbox/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IWorkbenchThemeService } from 'vs/workbench/services/themes/common/work
import { applyZoom } from 'vs/platform/windows/electron-sandbox/window';
import { setFullscreen, getZoomLevel } from 'vs/base/browser/browser';
import { ICommandService, CommandsRegistry } from 'vs/platform/commands/common/commands';
import { IBaseResourceEditorInput, IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
import { ipcRenderer } from 'vs/base/parts/sandbox/electron-sandbox/globals';
import { env } from 'vs/base/common/process';
import { IWorkspaceEditingService } from 'vs/workbench/services/workspaces/common/workspaceEditing';
Expand Down Expand Up @@ -631,7 +631,7 @@ export class NativeWindow extends Disposable {
}

private async onOpenFiles(request: INativeOpenFileRequest): Promise<void> {
const inputs: IUntypedEditorInput[] = [];
const inputs: Array<IResourceEditorInput | IUntitledTextResourceEditorInput> = [];
const diffMode = !!(request.filesToDiff && (request.filesToDiff.length === 2));

if (!diffMode && request.filesToOpenOrCreate) {
Expand Down Expand Up @@ -664,7 +664,7 @@ export class NativeWindow extends Disposable {
}

private async openResources(resources: Array<IResourceEditorInput | IUntitledTextResourceEditorInput>, diffMode: boolean): Promise<unknown> {
const editors: IBaseResourceEditorInput[] = [];
const editors: IUntypedEditorInput[] = [];

// In diffMode we open 2 resources as diff
if (diffMode && resources.length === 2 && resources[0].resource && resources[1].resource) {
Expand Down
12 changes: 8 additions & 4 deletions src/vs/workbench/services/editor/browser/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
//#region openEditor()

openEditor(editor: IEditorInput, options?: IEditorOptions, group?: OpenInEditorGroup): Promise<IEditorPane | undefined>;
openEditor(editor: IUntypedEditorInput, group?: OpenInEditorGroup): Promise<IEditorPane | undefined>;
openEditor(editor: IResourceEditorInput, group?: OpenInEditorGroup): Promise<IEditorPane | undefined>;
openEditor(editor: ITextResourceEditorInput | IUntitledTextResourceEditorInput, group?: OpenInEditorGroup): Promise<ITextEditorPane | undefined>;
openEditor(editor: IResourceDiffEditorInput, group?: OpenInEditorGroup): Promise<ITextDiffEditorPane | undefined>;
Expand Down Expand Up @@ -642,8 +643,10 @@ export class EditorService extends Disposable implements EditorServiceImpl {
return [group, activation];
}

private doFindTargetGroup(editor: IEditorInputWithOptions | IUntypedEditorInput, preferredGroup: OpenInEditorGroup | undefined): IEditorGroup {
private doFindTargetGroup(input: IEditorInputWithOptions | IUntypedEditorInput, preferredGroup: OpenInEditorGroup | undefined): IEditorGroup {
let group: IEditorGroup | undefined;
let editor = isEditorInputWithOptions(input) ? input.editor : input;
let options = input.options;

// Group: Instance of Group
if (preferredGroup && typeof preferredGroup !== 'number') {
Expand All @@ -661,11 +664,11 @@ export class EditorService extends Disposable implements EditorServiceImpl {
}

// Group: Unspecified without a specific index to open
else if (!editor.options || typeof editor.options.index !== 'number') {
else if (!options || typeof options.index !== 'number') {
const groupsByLastActive = this.editorGroupService.getGroups(GroupsOrder.MOST_RECENTLY_ACTIVE);

// Respect option to reveal an editor if it is already visible in any group
if (editor.options?.revealIfVisible) {
if (options?.revealIfVisible) {
for (const lastActiveGroup of groupsByLastActive) {
if (lastActiveGroup.isActive(editor)) {
group = lastActiveGroup;
Expand All @@ -677,7 +680,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {
// Respect option to reveal an editor if it is open (not necessarily visible)
// Still prefer to reveal an editor in a group where the editor is active though.
if (!group) {
if (editor.options?.revealIfOpened || this.configurationService.getValue<boolean>('workbench.editor.revealIfOpen')) {
if (options?.revealIfOpened || this.configurationService.getValue<boolean>('workbench.editor.revealIfOpen')) {
let groupWithInputActive: IEditorGroup | undefined = undefined;
let groupWithInputOpened: IEditorGroup | undefined = undefined;

Expand Down Expand Up @@ -728,6 +731,7 @@ export class EditorService extends Disposable implements EditorServiceImpl {

openEditors(editors: IEditorInputWithOptions[], group?: OpenInEditorGroup, options?: IOpenEditorsOptions): Promise<IEditorPane[]>;
openEditors(editors: IUntypedEditorInput[], group?: OpenInEditorGroup, options?: IOpenEditorsOptions): Promise<IEditorPane[]>;
openEditors(editors: Array<IEditorInputWithOptions | IUntypedEditorInput>, group?: OpenInEditorGroup, options?: IOpenEditorsOptions): Promise<IEditorPane[]>;
async openEditors(editors: Array<IEditorInputWithOptions | IUntypedEditorInput>, preferredGroup?: OpenInEditorGroup, options?: IOpenEditorsOptions): Promise<IEditorPane[]> {

// Pass all editors to trust service to determine if
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/services/editor/common/editorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export interface IEditorService {
openEditor(editor: IResourceEditorInput, group?: IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE): Promise<IEditorPane | undefined>;
openEditor(editor: ITextResourceEditorInput | IUntitledTextResourceEditorInput, group?: IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE): Promise<ITextEditorPane | undefined>;
openEditor(editor: IResourceDiffEditorInput, group?: IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE): Promise<ITextDiffEditorPane | undefined>;
openEditor(editor: IUntypedEditorInput, group?: IEditorGroup | GroupIdentifier | SIDE_GROUP_TYPE | ACTIVE_GROUP_TYPE): Promise<IEditorPane | undefined>;

/**
* Open editors in an editor group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ suite('EditorService', () => {
{
// untyped untitled editor, no options, no group
{
let untypedEditor: IUntitledTextResourceEditorInput = { options: { override: TEST_EDITOR_INPUT_ID } };
let untypedEditor: IUntitledTextResourceEditorInput = { resource: undefined, options: { override: TEST_EDITOR_INPUT_ID } };
let pane = await openEditor(untypedEditor);

assert.strictEqual(pane?.group, rootGroup);
Expand All @@ -733,7 +733,7 @@ suite('EditorService', () => {

// untyped untitled editor, no options, SIDE_GROUP
{
let untypedEditor: IUntitledTextResourceEditorInput = { options: { override: TEST_EDITOR_INPUT_ID } };
let untypedEditor: IUntitledTextResourceEditorInput = { resource: undefined, options: { override: TEST_EDITOR_INPUT_ID } };
let pane = await openEditor(untypedEditor, SIDE_GROUP);

assert.strictEqual(accessor.editorGroupService.groups.length, 2);
Expand Down Expand Up @@ -780,7 +780,7 @@ suite('EditorService', () => {

// untyped untitled editor, options (sticky: true, preserveFocus: true), no group
{
let untypedEditor: IUntitledTextResourceEditorInput = { options: { sticky: true, preserveFocus: true, override: TEST_EDITOR_INPUT_ID } };
let untypedEditor: IUntitledTextResourceEditorInput = { resource: undefined, options: { sticky: true, preserveFocus: true, override: TEST_EDITOR_INPUT_ID } };
let pane = await openEditor(untypedEditor);

assert.strictEqual(pane?.group, rootGroup);
Expand Down Expand Up @@ -1301,7 +1301,7 @@ suite('EditorService', () => {
assert.strictEqual(contentInput.getPreferredMode(), 'text');

// Untyped Input (untitled)
input = service.createEditorInput({ options: { selection: { startLineNumber: 1, startColumn: 1 } } });
input = service.createEditorInput({ resource: undefined, options: { selection: { startLineNumber: 1, startColumn: 1 } } });
assert(input instanceof UntitledTextEditorInput);

// Untyped Input (untitled with contents)
Expand All @@ -1313,7 +1313,7 @@ suite('EditorService', () => {
assert.strictEqual(model.textEditorModel?.getValue(), 'Hello Untitled');

// Untyped Input (untitled with mode)
input = service.createEditorInput({ mode, options: { selection: { startLineNumber: 1, startColumn: 1 } } });
input = service.createEditorInput({ resource: undefined, mode, options: { selection: { startLineNumber: 1, startColumn: 1 } } });
assert(input instanceof UntitledTextEditorInput);
model = await input.resolve() as UntitledTextEditorModel;
assert.strictEqual(model.getMode(), mode);
Expand Down Expand Up @@ -1725,7 +1725,7 @@ suite('EditorService', () => {
const [, service] = await createEditorService();

// Open untitled input
let editor = await service.openEditor({});
let editor = await service.openEditor({ resource: undefined });

assert.strictEqual(service.activeEditorPane, editor);
assert.strictEqual(service.activeTextEditorControl, editor?.getControl());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ registerAction2(class MeasureExtHostLatencyAction extends Action2 {
const editorService = accessor.get(IEditorService);

const measurements = await Promise.all(getLatencyTestProviders().map(provider => provider.measure()));
editorService.openEditor({ contents: measurements.map(MeasureExtHostLatencyAction._print).join('\n\n'), options: { pinned: true } });
editorService.openEditor({ resource: undefined, contents: measurements.map(MeasureExtHostLatencyAction._print).join('\n\n'), options: { pinned: true } });
}

private static _print(m: ExtHostLatencyResult | null): string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ suite('WorkingCopyBackupTracker (browser)', function () {
return { accessor, part, tracker, workingCopyBackupService: workingCopyBackupService, instantiationService, cleanup: () => disposables.dispose() };
}

async function untitledBackupTest(untitled: IUntitledTextResourceEditorInput = {}): Promise<void> {
async function untitledBackupTest(untitled: IUntitledTextResourceEditorInput = { resource: undefined }): Promise<void> {
const { accessor, cleanup, workingCopyBackupService } = await createTracker();

const untitledTextEditor = (await accessor.editorService.openEditor(untitled))?.input as UntitledTextEditorInput;
Expand Down Expand Up @@ -137,7 +137,7 @@ suite('WorkingCopyBackupTracker (browser)', function () {
});

test('Track backups (untitled with initial contents)', function () {
return untitledBackupTest({ contents: 'Foo Bar' });
return untitledBackupTest({ resource: undefined, contents: 'Foo Bar' });
});

test('Track backups (custom)', async function () {
Expand Down

0 comments on commit 4c2c3ca

Please sign in to comment.