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

'use strict';

import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { TPromise } from 'vs/base/common/winjs.base';
B
Benjamin Pasero 已提交
10 11 12 13 14
import { isParent } from 'vs/platform/files/common/files';
import { localize } from 'vs/nls';
import { basename, dirname, join } from 'vs/base/common/paths';
import { isLinux } from 'vs/base/common/platform';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
M
Matt Bierner 已提交
15
import { Event } from 'vs/base/common/event';
B
Benjamin Pasero 已提交
16
import { tildify, getPathLabel } from 'vs/base/common/labels';
S
Sandeep Somavarapu 已提交
17
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
18
import URI from 'vs/base/common/uri';
B
Benjamin Pasero 已提交
19 20 21 22

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

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

27 28 29 30 31
/**
 * A single folder workspace identifier is just the path to the folder.
 */
export type ISingleFolderWorkspaceIdentifier = string;

32 33
export interface IWorkspaceIdentifier {
	id: string;
34
	configPath: string;
B
Benjamin Pasero 已提交
35 36
}

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

J
Johannes Rieken 已提交
60 61 62 63 64 65 66
export interface IRawUriWorkspaceFolder {
	uri: string;
	name?: string;
}

export type IStoredWorkspaceFolder = IRawFileWorkspaceFolder | IRawUriWorkspaceFolder;

67
export interface IResolvedWorkspace extends IWorkspaceIdentifier {
S
Sandeep Somavarapu 已提交
68
	folders: IWorkspaceFolder[];
B
Benjamin Pasero 已提交
69 70
}

J
Johannes Rieken 已提交
71 72 73 74
export interface IStoredWorkspace {
	folders: IStoredWorkspaceFolder[];
}

75 76 77 78 79
export interface IWorkspaceSavedEvent {
	workspace: IWorkspaceIdentifier;
	oldConfigPath: string;
}

80 81 82 83 84
export interface IWorkspaceFolderCreationData {
	uri: URI;
	name?: string;
}

B
Benjamin Pasero 已提交
85 86 87
export interface IWorkspacesMainService extends IWorkspacesService {
	_serviceBrand: any;

88
	onWorkspaceSaved: Event<IWorkspaceSavedEvent>;
89
	onUntitledWorkspaceDeleted: Event<IWorkspaceIdentifier>;
90

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

93
	createWorkspaceSync(folders?: IWorkspaceFolderCreationData[]): IWorkspaceIdentifier;
94 95

	resolveWorkspaceSync(path: string): IResolvedWorkspace;
B
Benjamin Pasero 已提交
96

B
Benjamin Pasero 已提交
97
	isUntitledWorkspace(workspace: IWorkspaceIdentifier): boolean;
98

99
	deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void;
100 101

	getUntitledWorkspacesSync(): IWorkspaceIdentifier[];
102 103

	getWorkspaceId(workspacePath: string): string;
B
Benjamin Pasero 已提交
104 105 106 107 108
}

export interface IWorkspacesService {
	_serviceBrand: any;

109
	createWorkspace(folders?: IWorkspaceFolderCreationData[]): TPromise<IWorkspaceIdentifier>;
110 111
}

B
Benjamin Pasero 已提交
112 113 114 115 116 117 118 119
export function getWorkspaceLabel(workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier), environmentService: IEnvironmentService, options?: { verbose: boolean }): string {

	// Workspace: Single Folder
	if (isSingleFolderWorkspaceIdentifier(workspace)) {
		return tildify(workspace, environmentService.userHome);
	}

	// Workspace: Untitled
120
	if (isParent(workspace.configPath, environmentService.workspacesHome, !isLinux /* ignore case */)) {
B
Benjamin Pasero 已提交
121
		return localize('untitledWorkspace', "Untitled (Workspace)");
122 123
	}

B
Benjamin Pasero 已提交
124
	// Workspace: Saved
B
Benjamin Pasero 已提交
125 126
	const filename = basename(workspace.configPath);
	const workspaceName = filename.substr(0, filename.length - WORKSPACE_EXTENSION.length - 1);
B
Benjamin Pasero 已提交
127
	if (options && options.verbose) {
128
		return localize('workspaceNameVerbose', "{0} (Workspace)", getPathLabel(join(dirname(workspace.configPath), workspaceName), environmentService));
B
Benjamin Pasero 已提交
129
	}
B
Benjamin Pasero 已提交
130

B
Benjamin Pasero 已提交
131
	return localize('workspaceName', "{0} (Workspace)", workspaceName);
B
Benjamin Pasero 已提交
132 133 134 135
}

export function isSingleFolderWorkspaceIdentifier(obj: any): obj is ISingleFolderWorkspaceIdentifier {
	return typeof obj === 'string';
136 137 138 139 140 141
}

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 已提交
142
}