workspaces.ts 4.8 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';
M
Martin Aeschlimann 已提交
10
import { URI, UriComponents } 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;
M
Martin Aeschlimann 已提交
26
	configPath: URI;
B
Benjamin Pasero 已提交
27 28
}

M
Martin Aeschlimann 已提交
29 30 31 32
export function reviveWorkspaceIdentifier(workspace: { id: string, configPath: UriComponents; }) {
	return { id: workspace.id, configPath: URI.revive(workspace.configPath) };
}

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

J
Johannes Rieken 已提交
56 57 58 59 60 61 62
export interface IRawUriWorkspaceFolder {
	uri: string;
	name?: string;
}

export type IStoredWorkspaceFolder = IRawFileWorkspaceFolder | IRawUriWorkspaceFolder;

63
export interface IResolvedWorkspace extends IWorkspaceIdentifier {
S
Sandeep Somavarapu 已提交
64
	folders: IWorkspaceFolder[];
B
Benjamin Pasero 已提交
65 66
}

J
Johannes Rieken 已提交
67 68 69 70
export interface IStoredWorkspace {
	folders: IStoredWorkspaceFolder[];
}

71 72 73 74 75
export interface IWorkspaceSavedEvent {
	workspace: IWorkspaceIdentifier;
	oldConfigPath: string;
}

76 77 78 79 80
export interface IWorkspaceFolderCreationData {
	uri: URI;
	name?: string;
}

B
Benjamin Pasero 已提交
81 82 83
export interface IWorkspacesMainService extends IWorkspacesService {
	_serviceBrand: any;

84
	onUntitledWorkspaceDeleted: Event<IWorkspaceIdentifier>;
85

86
	saveWorkspaceAs(workspace: IWorkspaceIdentifier, target: string): Promise<IWorkspaceIdentifier>;
87

88
	createUntitledWorkspaceSync(folders?: IWorkspaceFolderCreationData[]): IWorkspaceIdentifier;
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

M
Martin Aeschlimann 已提交
98
	getWorkspaceIdentifier(workspacePath: URI): IWorkspaceIdentifier;
B
Benjamin Pasero 已提交
99 100 101 102 103
}

export interface IWorkspacesService {
	_serviceBrand: any;

104
	createUntitledWorkspace(folders?: IWorkspaceFolderCreationData[]): Promise<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

export function toWorkspaceIdentifier(workspace: IWorkspace): IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | undefined {
	if (workspace.configuration) {
		return {
M
Martin Aeschlimann 已提交
120
			configPath: workspace.configuration,
121 122 123 124 125 126 127 128 129 130
			id: workspace.id
		};
	}
	if (workspace.folders.length === 1) {
		return workspace.folders[0].uri;
	}

	// Empty workspace
	return undefined;
}
131 132 133 134

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

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

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