Skip to content

Commit

Permalink
make createAndEnterWorkspace fit for any resources and optional name …
Browse files Browse the repository at this point in the history
…(for #35407)
  • Loading branch information
bpasero committed Oct 26, 2017
1 parent f43ae13 commit 7f1379f
Show file tree
Hide file tree
Showing 20 changed files with 184 additions and 132 deletions.
13 changes: 7 additions & 6 deletions src/vs/code/electron-main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ import { IWindowsMainService, IOpenConfiguration, IWindowsCountChangedEvent } fr
import { IHistoryMainService } from 'vs/platform/history/common/history';
import { IProcessEnvironment, isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
import { TPromise } from 'vs/base/common/winjs.base';
import { IWorkspacesMainService, IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, WORKSPACE_FILTER, isSingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspacesMainService, IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, WORKSPACE_FILTER, isSingleFolderWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { Schemas } from 'vs/base/common/network';
import { normalizeNFC } from 'vs/base/common/strings';
import URI from 'vs/base/common/uri';

enum WindowError {
UNRESPONSIVE,
Expand Down Expand Up @@ -795,7 +796,7 @@ export class WindowsManager implements IWindowsMainService {
if (!openConfig.addMode && isCommandLineOrAPICall) {
const foldersToOpen = windowsToOpen.filter(path => !!path.folderPath);
if (foldersToOpen.length > 1) {
const workspace = this.workspacesService.createWorkspaceSync(foldersToOpen.map(folder => folder.folderPath));
const workspace = this.workspacesService.createWorkspaceSync(foldersToOpen.map(folder => ({ uri: URI.file(folder.folderPath) })));

// Add workspace and remove folders thereby
windowsToOpen.push({ workspace });
Expand Down Expand Up @@ -1324,8 +1325,8 @@ export class WindowsManager implements IWindowsMainService {
return this.workspacesManager.saveAndEnterWorkspace(win, path).then(result => this.doEnterWorkspace(win, result));
}

public createAndEnterWorkspace(win: CodeWindow, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.workspacesManager.createAndEnterWorkspace(win, folderPaths, path).then(result => this.doEnterWorkspace(win, result));
public createAndEnterWorkspace(win: CodeWindow, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.workspacesManager.createAndEnterWorkspace(win, folders, path).then(result => this.doEnterWorkspace(win, result));
}

private doEnterWorkspace(win: CodeWindow, result: IEnterWorkspaceResult): IEnterWorkspaceResult {
Expand Down Expand Up @@ -1686,12 +1687,12 @@ class WorkspacesManager {
return this.doSaveAndOpenWorkspace(window, window.openedWorkspace, path);
}

public createAndEnterWorkspace(window: CodeWindow, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult> {
public createAndEnterWorkspace(window: CodeWindow, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult> {
if (!window || !window.win || window.readyState !== ReadyState.READY || !this.isValidTargetWorkspacePath(window, path)) {
return TPromise.as(null); // return early if the window is not ready or disposed
}

return this.workspacesService.createWorkspace(folderPaths).then(workspace => {
return this.workspacesService.createWorkspace(folders).then(workspace => {
return this.doSaveAndOpenWorkspace(window, workspace, path);
});
}
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/windows/common/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Event from 'vs/base/common/event';
import { ITelemetryData } from 'vs/platform/telemetry/common/telemetry';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { ParsedArgs } from 'vs/platform/environment/common/environment';
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { IRecentlyOpened } from 'vs/platform/history/common/history';
import { ICommandAction } from 'vs/platform/actions/common/actions';

Expand Down Expand Up @@ -48,7 +48,7 @@ export interface IWindowsService {
openDevTools(windowId: number): TPromise<void>;
toggleDevTools(windowId: number): TPromise<void>;
closeWorkspace(windowId: number): TPromise<void>;
createAndEnterWorkspace(windowId: number, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult>;
createAndEnterWorkspace(windowId: number, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult>;
saveAndEnterWorkspace(windowId: number, path: string): TPromise<IEnterWorkspaceResult>;
toggleFullScreen(windowId: number): TPromise<void>;
setRepresentedFilename(windowId: number, fileName: string): TPromise<void>;
Expand Down Expand Up @@ -121,7 +121,7 @@ export interface IWindowService {
toggleDevTools(): TPromise<void>;
closeWorkspace(): TPromise<void>;
updateTouchBar(items: ICommandAction[][]): TPromise<void>;
createAndEnterWorkspace(folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult>;
createAndEnterWorkspace(folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult>;
saveAndEnterWorkspace(path: string): TPromise<IEnterWorkspaceResult>;
toggleFullScreen(): TPromise<void>;
setRepresentedFilename(fileName: string): TPromise<void>;
Expand Down
24 changes: 19 additions & 5 deletions src/vs/platform/windows/common/windowsIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { TPromise } from 'vs/base/common/winjs.base';
import Event, { buffer } from 'vs/base/common/event';
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
import { IWindowsService, INativeOpenDialogOptions, IEnterWorkspaceResult } from './windows';
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { IRecentlyOpened } from 'vs/platform/history/common/history';
import { ICommandAction } from 'vs/platform/actions/common/actions';
import URI from 'vs/base/common/uri';

export interface IWindowsChannel extends IChannel {
call(command: 'event:onWindowOpen'): TPromise<number>;
Expand All @@ -24,7 +25,7 @@ export interface IWindowsChannel extends IChannel {
call(command: 'reloadWindow', arg: number): TPromise<void>;
call(command: 'toggleDevTools', arg: number): TPromise<void>;
call(command: 'closeWorkspace', arg: number): TPromise<void>;
call(command: 'createAndEnterWorkspace', arg: [number, string[], string]): TPromise<IEnterWorkspaceResult>;
call(command: 'createAndEnterWorkspace', arg: [number, IWorkspaceFolderCreationData[], string]): TPromise<IEnterWorkspaceResult>;
call(command: 'saveAndEnterWorkspace', arg: [number, string]): TPromise<IEnterWorkspaceResult>;
call(command: 'toggleFullScreen', arg: number): TPromise<void>;
call(command: 'setRepresentedFilename', arg: [number, string]): TPromise<void>;
Expand Down Expand Up @@ -87,7 +88,20 @@ export class WindowsChannel implements IWindowsChannel {
case 'openDevTools': return this.service.openDevTools(arg);
case 'toggleDevTools': return this.service.toggleDevTools(arg);
case 'closeWorkspace': return this.service.closeWorkspace(arg);
case 'createAndEnterWorkspace': return this.service.createAndEnterWorkspace(arg[0], arg[1], arg[2]);
case 'createAndEnterWorkspace': {
const rawFolders: IWorkspaceFolderCreationData[] = arg[1];
let folders: IWorkspaceFolderCreationData[];
if (Array.isArray(rawFolders)) {
folders = rawFolders.map(rawFolder => {
return {
uri: URI.revive(rawFolder.uri), // convert raw URI back to real URI
name: rawFolder.name
} as IWorkspaceFolderCreationData;
});
}

return this.service.createAndEnterWorkspace(arg[0], folders, arg[2]);
};
case 'saveAndEnterWorkspace': return this.service.saveAndEnterWorkspace(arg[0], arg[1]);
case 'toggleFullScreen': return this.service.toggleFullScreen(arg);
case 'setRepresentedFilename': return this.service.setRepresentedFilename(arg[0], arg[1]);
Expand Down Expand Up @@ -174,8 +188,8 @@ export class WindowsChannelClient implements IWindowsService {
return this.channel.call('closeWorkspace', windowId);
}

createAndEnterWorkspace(windowId: number, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.channel.call('createAndEnterWorkspace', [windowId, folderPaths, path]);
createAndEnterWorkspace(windowId: number, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.channel.call('createAndEnterWorkspace', [windowId, folders, path]);
}

saveAndEnterWorkspace(windowId: number, path: string): TPromise<IEnterWorkspaceResult> {
Expand Down
5 changes: 3 additions & 2 deletions src/vs/platform/windows/electron-browser/windowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IRecentlyOpened } from 'vs/platform/history/common/history';
import { ICommandAction } from 'vs/platform/actions/common/actions';
import { isMacintosh } from 'vs/base/common/platform';
import { normalizeNFC } from 'vs/base/common/strings';
import { IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';

export class WindowService implements IWindowService {

Expand Down Expand Up @@ -73,8 +74,8 @@ export class WindowService implements IWindowService {
return this.windowsService.closeWorkspace(this.windowId);
}

createAndEnterWorkspace(folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.windowsService.createAndEnterWorkspace(this.windowId, folderPaths, path);
createAndEnterWorkspace(folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult> {
return this.windowsService.createAndEnterWorkspace(this.windowId, folders, path);
}

saveAndEnterWorkspace(path: string): TPromise<IEnterWorkspaceResult> {
Expand Down
4 changes: 2 additions & 2 deletions src/vs/platform/windows/electron-main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ParsedArgs } from 'vs/platform/environment/common/environment';
import Event from 'vs/base/common/event';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IProcessEnvironment } from 'vs/base/common/platform';
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { ICommandAction } from 'vs/platform/actions/common/actions';

export interface ICodeWindow {
Expand Down Expand Up @@ -60,7 +60,7 @@ export interface IWindowsMainService {
// methods
ready(initialUserEnv: IProcessEnvironment): void;
reload(win: ICodeWindow, cli?: ParsedArgs): void;
createAndEnterWorkspace(win: ICodeWindow, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult>;
createAndEnterWorkspace(win: ICodeWindow, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult>;
saveAndEnterWorkspace(win: ICodeWindow, path: string): TPromise<IEnterWorkspaceResult>;
closeWorkspace(win: ICodeWindow): void;
open(openConfig: IOpenConfiguration): ICodeWindow[];
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/windows/electron-main/windowsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { IURLService } from 'vs/platform/url/common/url';
import { ILifecycleService } from 'vs/platform/lifecycle/electron-main/lifecycleMain';
import { IWindowsMainService, ISharedProcess } from 'vs/platform/windows/electron-main/windows';
import { IHistoryMainService, IRecentlyOpened } from 'vs/platform/history/common/history';
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { ICommandAction } from 'vs/platform/actions/common/actions';

export class WindowsService implements IWindowsService, IDisposable {
Expand Down Expand Up @@ -131,11 +131,11 @@ export class WindowsService implements IWindowsService, IDisposable {
return TPromise.as(null);
}

createAndEnterWorkspace(windowId: number, folderPaths?: string[], path?: string): TPromise<IEnterWorkspaceResult> {
createAndEnterWorkspace(windowId: number, folders?: IWorkspaceFolderCreationData[], path?: string): TPromise<IEnterWorkspaceResult> {
const codeWindow = this.windowsMainService.getWindowById(windowId);

if (codeWindow) {
return this.windowsMainService.createAndEnterWorkspace(codeWindow, folderPaths, path);
return this.windowsMainService.createAndEnterWorkspace(codeWindow, folders, path);
}

return TPromise.as(null);
Expand Down
11 changes: 7 additions & 4 deletions src/vs/platform/workspaces/common/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export interface IWorkspaceSavedEvent {
oldConfigPath: string;
}

export interface IWorkspaceFolderCreationData {
uri: URI;
name?: string;
}

export interface IWorkspacesMainService extends IWorkspacesService {
_serviceBrand: any;

Expand All @@ -85,8 +90,7 @@ export interface IWorkspacesMainService extends IWorkspacesService {

saveWorkspace(workspace: IWorkspaceIdentifier, target: string): TPromise<IWorkspaceIdentifier>;

createWorkspaceSync(folderPaths?: string[]): IWorkspaceIdentifier;
createWorkspaceSync(folderResources?: URI[]): IWorkspaceIdentifier;
createWorkspaceSync(folders?: IWorkspaceFolderCreationData[]): IWorkspaceIdentifier;

resolveWorkspace(path: string): TPromise<IResolvedWorkspace>;
resolveWorkspaceSync(path: string): IResolvedWorkspace;
Expand All @@ -103,8 +107,7 @@ export interface IWorkspacesMainService extends IWorkspacesService {
export interface IWorkspacesService {
_serviceBrand: any;

createWorkspace(folderPaths?: string[]): TPromise<IWorkspaceIdentifier>;
createWorkspace(folderResources?: URI[]): TPromise<IWorkspaceIdentifier>;
createWorkspace(folders?: IWorkspaceFolderCreationData[]): TPromise<IWorkspaceIdentifier>;
}

export function getWorkspaceLabel(workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier), environmentService: IEnvironmentService, options?: { verbose: boolean }): string {
Expand Down
26 changes: 12 additions & 14 deletions src/vs/platform/workspaces/common/workspacesIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel } from 'vs/base/parts/ipc/common/ipc';
import { IWorkspacesService, IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspacesService, IWorkspaceIdentifier, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import URI from 'vs/base/common/uri';

export interface IWorkspacesChannel extends IChannel {
call(command: 'createWorkspace', arg: [(string | URI)[]]): TPromise<string>;
call(command: 'createWorkspace', arg: [IWorkspaceFolderCreationData[]]): TPromise<string>;
call(command: string, arg?: any): TPromise<any>;
}

Expand All @@ -22,14 +22,14 @@ export class WorkspacesChannel implements IWorkspacesChannel {
public call(command: string, arg?: any): TPromise<any> {
switch (command) {
case 'createWorkspace': {
let folders: any[];
if (Array.isArray(arg)) {
folders = arg.map(folder => {
if (typeof folder === 'string') {
return folder;
}

return URI.revive(folder);
const rawFolders: IWorkspaceFolderCreationData[] = arg;
let folders: IWorkspaceFolderCreationData[];
if (Array.isArray(rawFolders)) {
folders = rawFolders.map(rawFolder => {
return {
uri: URI.revive(rawFolder.uri), // convert raw URI back to real URI
name: rawFolder.name
} as IWorkspaceFolderCreationData;
});
}

Expand All @@ -47,9 +47,7 @@ export class WorkspacesChannelClient implements IWorkspacesService {

constructor(private channel: IWorkspacesChannel) { }

public createWorkspace(folderPaths?: string[]): TPromise<IWorkspaceIdentifier>;
public createWorkspace(folderResources?: URI[]): TPromise<IWorkspaceIdentifier>;
public createWorkspace(arg1?: any[]): TPromise<IWorkspaceIdentifier> {
return this.channel.call('createWorkspace', arg1);
public createWorkspace(folders?: IWorkspaceFolderCreationData[]): TPromise<IWorkspaceIdentifier> {
return this.channel.call('createWorkspace', folders);
}
}
37 changes: 19 additions & 18 deletions src/vs/platform/workspaces/electron-main/workspacesMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import { IWorkspacesMainService, IWorkspaceIdentifier, WORKSPACE_EXTENSION, IWorkspaceSavedEvent, UNTITLED_WORKSPACE_NAME, IResolvedWorkspace, IStoredWorkspaceFolder, isRawFileWorkspaceFolder, isStoredWorkspaceFolder, IRawFileWorkspaceFolder, IRawUriWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspacesMainService, IWorkspaceIdentifier, WORKSPACE_EXTENSION, IWorkspaceSavedEvent, UNTITLED_WORKSPACE_NAME, IResolvedWorkspace, IStoredWorkspaceFolder, isRawFileWorkspaceFolder, isStoredWorkspaceFolder, IWorkspaceFolderCreationData } from 'vs/platform/workspaces/common/workspaces';
import { TPromise } from 'vs/base/common/winjs.base';
import { isParent } from 'vs/platform/files/common/files';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
Expand Down Expand Up @@ -127,20 +127,16 @@ export class WorkspacesMainService implements IWorkspacesMainService {
return isParent(path, this.environmentService.workspacesHome, !isLinux /* ignore case */);
}

public createWorkspace(folders: string[]): TPromise<IWorkspaceIdentifier>;
public createWorkspace(resources: URI[]): TPromise<IWorkspaceIdentifier>;
public createWorkspace(arg1: string[] | URI[]): TPromise<IWorkspaceIdentifier> {
const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(arg1);
public createWorkspace(folders?: IWorkspaceFolderCreationData[]): TPromise<IWorkspaceIdentifier> {
const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(folders);

return mkdirp(configParent).then(() => {
return writeFile(workspace.configPath, JSON.stringify(storedWorkspace, null, '\t')).then(() => workspace);
});
}

public createWorkspaceSync(folders: string[]): IWorkspaceIdentifier;
public createWorkspaceSync(resources: URI[]): IWorkspaceIdentifier;
public createWorkspaceSync(arg1: string[] | URI[]): IWorkspaceIdentifier {
const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(arg1);
public createWorkspaceSync(folders?: IWorkspaceFolderCreationData[]): IWorkspaceIdentifier {
const { workspace, configParent, storedWorkspace } = this.createUntitledWorkspace(folders);

if (!existsSync(this.workspacesHome)) {
mkdirSync(this.workspacesHome);
Expand All @@ -153,26 +149,31 @@ export class WorkspacesMainService implements IWorkspacesMainService {
return workspace;
}

private createUntitledWorkspace(folders: (string | URI)[]): { workspace: IWorkspaceIdentifier, configParent: string, storedWorkspace: IStoredWorkspace } {
private createUntitledWorkspace(folders: IWorkspaceFolderCreationData[] = []): { workspace: IWorkspaceIdentifier, configParent: string, storedWorkspace: IStoredWorkspace } {
const randomId = (Date.now() + Math.round(Math.random() * 1000)).toString();
const untitledWorkspaceConfigFolder = join(this.workspacesHome, randomId);
const untitledWorkspaceConfigPath = join(untitledWorkspaceConfigFolder, UNTITLED_WORKSPACE_NAME);

const storedWorkspace: IStoredWorkspace = {
folders: folders.map(folder => {

// File path
if (typeof folder === 'string') {
return { path: massageFolderPathForWorkspace(folder, untitledWorkspaceConfigFolder, []) } as IRawFileWorkspaceFolder;
}
const folderResource = folder.uri;
let storedWorkspace: IStoredWorkspaceFolder;

// File URI
else if (folder.scheme === 'file') {
return { path: massageFolderPathForWorkspace(folder.fsPath, untitledWorkspaceConfigFolder, []) } as IRawFileWorkspaceFolder;
if (folderResource.scheme === 'file') {
storedWorkspace = { path: massageFolderPathForWorkspace(folderResource.fsPath, untitledWorkspaceConfigFolder, []) };
}

// Any URI
return { uri: folder.toString(true) } as IRawUriWorkspaceFolder;
else {
storedWorkspace = { uri: folderResource.toString(true) };
}

if (folder.name) {
storedWorkspace.name = folder.name;
}

return storedWorkspace;
})
};

Expand Down
Loading

0 comments on commit 7f1379f

Please sign in to comment.