workspaceEditingService.ts 3.2 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/

'use strict';

B
Benjamin Pasero 已提交
8 9 10
import { IWorkspaceEditingService } from 'vs/workbench/services/workspace/common/workspaceEditing';
import URI from 'vs/base/common/uri';
import { equals, distinct } from 'vs/base/common/arrays';
11
import { TPromise } from "vs/base/common/winjs.base";
B
Benjamin Pasero 已提交
12
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
13
import { IWindowsService } from 'vs/platform/windows/common/windows';
B
Benjamin Pasero 已提交
14
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
15
import { IJSONEditingService } from 'vs/workbench/services/configuration/common/jsonEditing';
B
Benjamin Pasero 已提交
16
import { IWorkspacesService } from 'vs/platform/workspaces/common/workspaces';
17 18 19 20 21 22

export class WorkspaceEditingService implements IWorkspaceEditingService {

	public _serviceBrand: any;

	constructor(
23
		@IJSONEditingService private jsonEditingService: IJSONEditingService,
B
Benjamin Pasero 已提交
24
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
25 26 27
		@IEnvironmentService private environmentService: IEnvironmentService,
		@IWindowsService private windowsService: IWindowsService,
		@IWorkspacesService private workspacesService: IWorkspacesService
28 29 30
	) {
	}

B
Benjamin Pasero 已提交
31
	public addRoots(rootsToAdd: URI[]): TPromise<void> {
32
		if (!this.isSupported()) {
33 34 35
			return TPromise.as(void 0); // we need a workspace to begin with
		}

B
Benjamin Pasero 已提交
36
		const roots = this.contextService.getWorkspace().roots;
37 38 39 40 41

		return this.doSetRoots([...roots, ...rootsToAdd]);
	}

	public removeRoots(rootsToRemove: URI[]): TPromise<void> {
42
		if (!this.isSupported()) {
43 44 45
			return TPromise.as(void 0); // we need a workspace to begin with
		}

B
Benjamin Pasero 已提交
46
		const roots = this.contextService.getWorkspace().roots;
47 48 49 50 51
		const rootsToRemoveRaw = rootsToRemove.map(root => root.toString());

		return this.doSetRoots(roots.filter(root => rootsToRemoveRaw.indexOf(root.toString()) === -1));
	}

52
	private isSupported(): boolean {
53
		// TODO@Ben multi root
F
Felix Becker 已提交
54 55 56 57
		return (
			this.environmentService.appQuality !== 'stable'  // not yet enabled in stable
			&& this.contextService.hasMultiFolderWorkspace() // we need a multi folder workspace to begin with
		);
58 59
	}

B
Benjamin Pasero 已提交
60
	private doSetRoots(newRoots: URI[]): TPromise<void> {
61 62 63
		const workspace = this.contextService.getWorkspace();
		const currentWorkspaceRoots = this.contextService.getWorkspace().roots.map(root => root.fsPath);
		const newWorkspaceRoots = this.validateRoots(newRoots);
64 65 66 67 68 69 70

		// See if there are any changes
		if (equals(currentWorkspaceRoots, newWorkspaceRoots)) {
			return TPromise.as(void 0);
		}

		// Apply to config
B
Benjamin Pasero 已提交
71
		if (newWorkspaceRoots.length) {
72
			return this.jsonEditingService.write(workspace.configuration, { key: 'folders', value: newWorkspaceRoots }, true);
B
Benjamin Pasero 已提交
73
		} else {
74
			// TODO: Sandeep - Removing all roots?
B
Benjamin Pasero 已提交
75
		}
76

77
		return TPromise.as(null);
78
	}
B
Benjamin Pasero 已提交
79

80
	private validateRoots(roots: URI[]): string[] {
B
Benjamin Pasero 已提交
81 82 83 84 85
		if (!roots) {
			return [];
		}

		// Prevent duplicates
86
		const validatedRoots = distinct(roots.map(root => root.toString(true /* skip encoding */)));
87 88
		return validatedRoots;
	}
F
Felix Becker 已提交
89
}