workspaces.ts 3.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';
15
import Event from 'vs/base/common/event';
B
Benjamin Pasero 已提交
16
import { tildify, getPathLabel } from 'vs/base/common/labels';
B
Benjamin Pasero 已提交
17 18 19 20

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

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

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

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

35 36 37 38
export interface IStoredWorkspaceFolder {
	path: string;
}

B
Benjamin Pasero 已提交
39
export interface IStoredWorkspace {
40
	folders: IStoredWorkspaceFolder[];
B
Benjamin Pasero 已提交
41 42
}

43 44
export interface IResolvedWorkspace extends IWorkspaceIdentifier, IStoredWorkspace { }

45 46 47 48 49
export interface IWorkspaceSavedEvent {
	workspace: IWorkspaceIdentifier;
	oldConfigPath: string;
}

B
Benjamin Pasero 已提交
50 51 52
export interface IWorkspacesMainService extends IWorkspacesService {
	_serviceBrand: any;

53
	onWorkspaceSaved: Event<IWorkspaceSavedEvent>;
54
	onUntitledWorkspaceDeleted: Event<IWorkspaceIdentifier>;
55

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

	resolveWorkspaceSync(path: string): IResolvedWorkspace;
B
Benjamin Pasero 已提交
59
	isUntitledWorkspace(workspace: IWorkspaceIdentifier): boolean;
60

61
	deleteUntitledWorkspaceSync(workspace: IWorkspaceIdentifier): void;
62 63

	getUntitledWorkspacesSync(): IWorkspaceIdentifier[];
64 65

	getWorkspaceId(workspacePath: string): string;
B
Benjamin Pasero 已提交
66 67 68 69 70
}

export interface IWorkspacesService {
	_serviceBrand: any;

71
	createWorkspace(folders?: string[]): TPromise<IWorkspaceIdentifier>;
72 73
}

B
Benjamin Pasero 已提交
74 75 76 77 78 79 80 81
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
82
	if (isParent(workspace.configPath, environmentService.workspacesHome, !isLinux /* ignore case */)) {
B
Benjamin Pasero 已提交
83
		return localize('untitledWorkspace', "Untitled (Workspace)");
84 85
	}

B
Benjamin Pasero 已提交
86
	// Workspace: Saved
B
Benjamin Pasero 已提交
87 88
	const filename = basename(workspace.configPath);
	const workspaceName = filename.substr(0, filename.length - WORKSPACE_EXTENSION.length - 1);
B
Benjamin Pasero 已提交
89
	if (options && options.verbose) {
B
Benjamin Pasero 已提交
90
		return localize('workspaceNameVerbose', "{0} (Workspace)", getPathLabel(join(dirname(workspace.configPath), workspaceName), null, environmentService));
B
Benjamin Pasero 已提交
91
	}
B
Benjamin Pasero 已提交
92

B
Benjamin Pasero 已提交
93
	return localize('workspaceName', "{0} (Workspace)", workspaceName);
B
Benjamin Pasero 已提交
94 95 96 97
}

export function isSingleFolderWorkspaceIdentifier(obj: any): obj is ISingleFolderWorkspaceIdentifier {
	return typeof obj === 'string';
98 99 100 101 102 103
}

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

	return workspaceIdentifier && typeof workspaceIdentifier.id === 'string' && typeof workspaceIdentifier.configPath === 'string';
B
Benjamin Pasero 已提交
104
}