workspaces.ts 4.7 KB
Newer Older
B
Benjamin Pasero 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  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';
B
Benjamin Pasero 已提交
7
import { localize } from 'vs/nls';
M
Matt Bierner 已提交
8
import { Event } from 'vs/base/common/event';
9
import { IWorkspaceFolder, IWorkspace } from 'vs/platform/workspace/common/workspace';
10
import { URI } from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
11 12 13 14

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

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

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

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

J
Johannes Rieken 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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 {
48
	path: string;
49
	name?: string;
50 51
}

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

export type IStoredWorkspaceFolder = IRawFileWorkspaceFolder | IRawUriWorkspaceFolder;

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

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

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

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

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

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

J
Johannes Rieken 已提交
83
	saveWorkspace(workspace: IWorkspaceIdentifier, target: string): Promise<IWorkspaceIdentifier>;
84

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

J
Johannes Rieken 已提交
87
	resolveWorkspace(path: string): Promise<IResolvedWorkspace | null>;
88

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

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

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

	getUntitledWorkspacesSync(): IWorkspaceIdentifier[];
96 97

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

export interface IWorkspacesService {
	_serviceBrand: any;

J
Johannes Rieken 已提交
103
	createWorkspace(folders?: IWorkspaceFolderCreationData[]): Promise<IWorkspaceIdentifier>;
104 105
}

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

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 已提交
114
}
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

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;
}
130 131 132 133

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

135 136 137 138
export type IWorkspaceInitializationPayload = IMultiFolderWorkspaceInitializationPayload | ISingleFolderWorkspaceInitializationPayload | IEmptyWorkspaceInitializationPayload;

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