workspaces.ts 4.8 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
8
import { localize } from 'vs/nls';
M
Matt Bierner 已提交
9
import { Event } from 'vs/base/common/event';
10
import { IWorkspaceFolder, IWorkspace } from 'vs/platform/workspace/common/workspace';
11
import { URI } from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
12 13 14 15

export const IWorkspacesMainService = createDecorator<IWorkspacesMainService>('workspacesMainService');
export const IWorkspacesService = createDecorator<IWorkspacesService>('workspacesService');

B
Benjamin Pasero 已提交
16
export const WORKSPACE_EXTENSION = 'code-workspace';
17
export const WORKSPACE_FILTER = [{ name: localize('codeWorkspace', "Code Workspace"), extensions: [WORKSPACE_EXTENSION] }];
18
export const UNTITLED_WORKSPACE_NAME = 'workspace.json';
B
Benjamin Pasero 已提交
19

20 21 22
/**
 * A single folder workspace identifier is just the path to the folder.
 */
23
export type ISingleFolderWorkspaceIdentifier = URI;
24

25 26
export interface IWorkspaceIdentifier {
	id: string;
27
	configPath: string;
B
Benjamin Pasero 已提交
28 29
}

J
Johannes Rieken 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
export function isStoredWorkspaceFolder(thing: any): thing is IStoredWorkspaceFolder {
	return isRawFileWorkspaceFolder(thing) || isRawUriWorkspaceFolder(thing);
}

export function isRawFileWorkspaceFolder(thing: any): thing is IRawFileWorkspaceFolder {
	return thing
		&& typeof thing === 'object'
		&& typeof thing.path === 'string'
		&& (!thing.name || typeof thing.name === 'string');
}

export function isRawUriWorkspaceFolder(thing: any): thing is IRawUriWorkspaceFolder {
	return thing
		&& typeof thing === 'object'
		&& typeof thing.uri === 'string'
		&& (!thing.name || typeof thing.name === 'string');
}

export interface IRawFileWorkspaceFolder {
49
	path: string;
50
	name?: string;
51 52
}

J
Johannes Rieken 已提交
53 54 55 56 57 58 59
export interface IRawUriWorkspaceFolder {
	uri: string;
	name?: string;
}

export type IStoredWorkspaceFolder = IRawFileWorkspaceFolder | IRawUriWorkspaceFolder;

60
export interface IResolvedWorkspace extends IWorkspaceIdentifier {
S
Sandeep Somavarapu 已提交
61
	folders: IWorkspaceFolder[];
B
Benjamin Pasero 已提交
62 63
}

J
Johannes Rieken 已提交
64 65 66 67
export interface IStoredWorkspace {
	folders: IStoredWorkspaceFolder[];
}

68 69 70 71 72
export interface IWorkspaceSavedEvent {
	workspace: IWorkspaceIdentifier;
	oldConfigPath: string;
}

73 74 75 76 77
export interface IWorkspaceFolderCreationData {
	uri: URI;
	name?: string;
}

B
Benjamin Pasero 已提交
78 79 80
export interface IWorkspacesMainService extends IWorkspacesService {
	_serviceBrand: any;

81
	onWorkspaceSaved: Event<IWorkspaceSavedEvent>;
82
	onUntitledWorkspaceDeleted: Event<IWorkspaceIdentifier>;
83

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

86
	createWorkspaceSync(folders?: IWorkspaceFolderCreationData[]): IWorkspaceIdentifier;
87

M
Matt Bierner 已提交
88
	resolveWorkspace(path: string): TPromise<IResolvedWorkspace | null>;
89

M
Matt Bierner 已提交
90
	resolveWorkspaceSync(path: string): IResolvedWorkspace | null;
B
Benjamin Pasero 已提交
91

B
Benjamin Pasero 已提交
92
	isUntitledWorkspace(workspace: IWorkspaceIdentifier): boolean;
93

94
	deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void;
95 96

	getUntitledWorkspacesSync(): IWorkspaceIdentifier[];
97 98

	getWorkspaceId(workspacePath: string): string;
B
Benjamin Pasero 已提交
99 100 101 102 103
}

export interface IWorkspacesService {
	_serviceBrand: any;

104
	createWorkspace(folders?: IWorkspaceFolderCreationData[]): TPromise<IWorkspaceIdentifier>;
105 106
}

107
export function isSingleFolderWorkspaceIdentifier(obj: any): obj is ISingleFolderWorkspaceIdentifier {
108
	return obj instanceof URI;
109 110 111 112 113 114
}

export function isWorkspaceIdentifier(obj: any): obj is IWorkspaceIdentifier {
	const workspaceIdentifier = obj as IWorkspaceIdentifier;

	return workspaceIdentifier && typeof workspaceIdentifier.id === 'string' && typeof workspaceIdentifier.configPath === 'string';
J
Johannes Rieken 已提交
115
}
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

export function toWorkspaceIdentifier(workspace: IWorkspace): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined {
	if (workspace.configuration) {
		return {
			configPath: workspace.configuration.fsPath,
			id: workspace.id
		};
	}
	if (workspace.folders.length === 1) {
		return workspace.folders[0].uri;
	}

	// Empty workspace
	return undefined;
}
131 132 133 134 135 136 137 138 139

export type IMultiFolderWorkspaceInitializationPayload = IWorkspaceIdentifier;
export interface ISingleFolderWorkspaceInitializationPayload { id: string; folder: ISingleFolderWorkspaceIdentifier; }
export interface IEmptyWorkspaceInitializationPayload { id: string; }
export type IWorkspaceInitializationPayload = IMultiFolderWorkspaceInitializationPayload | ISingleFolderWorkspaceInitializationPayload | IEmptyWorkspaceInitializationPayload;

export function isSingleFolderWorkspaceInitializationPayload(obj: any): obj is ISingleFolderWorkspaceInitializationPayload {
	return isSingleFolderWorkspaceIdentifier((obj.folder as ISingleFolderWorkspaceIdentifier));
}