Skip to content

Commit

Permalink
more adoption of IWorkspace2
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Jun 19, 2017
1 parent 77bf512 commit f2e1911
Show file tree
Hide file tree
Showing 33 changed files with 213 additions and 224 deletions.
26 changes: 5 additions & 21 deletions src/vs/base/browser/ui/iconLabel/iconLabel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { HighlightedLabel } from 'vs/base/browser/ui/highlightedlabel/highlighte
import { IMatch } from 'vs/base/common/filters';
import uri from 'vs/base/common/uri';
import paths = require('vs/base/common/paths');
import types = require('vs/base/common/types');
import { IWorkspaceProvider, getPathLabel, IUserHomeProvider } from 'vs/base/common/labels';
import { IRootProvider, getPathLabel, IUserHomeProvider } from 'vs/base/common/labels';

export interface IIconLabelCreationOptions {
supportHighlights?: boolean;
Expand Down Expand Up @@ -99,30 +98,15 @@ export class IconLabel {

export class FileLabel extends IconLabel {

constructor(container: HTMLElement, file: uri, provider: IWorkspaceProvider, userHome?: IUserHomeProvider) {
constructor(container: HTMLElement, file: uri, provider: IRootProvider, userHome?: IUserHomeProvider) {
super(container);

this.setFile(file, provider, userHome);
}

public setFile(file: uri, provider: IWorkspaceProvider, userHome: IUserHomeProvider): void {
const path = getPath(file);
const parent = paths.dirname(path);
public setFile(file: uri, provider: IRootProvider, userHome: IUserHomeProvider): void {
const parent = paths.dirname(file.fsPath);

this.setValue(paths.basename(path), parent && parent !== '.' ? getPathLabel(parent, provider, userHome) : '', { title: path });
this.setValue(paths.basename(file.fsPath), parent && parent !== '.' ? getPathLabel(parent, provider, userHome) : '', { title: file.fsPath });
}
}

function getPath(arg1: uri | IWorkspaceProvider): string {
if (!arg1) {
return null;
}

if (types.isFunction((<IWorkspaceProvider>arg1).getWorkspace)) {
const ws = (<IWorkspaceProvider>arg1).getWorkspace();

return ws ? ws.resource.fsPath : void 0;
}

return (<uri>arg1).fsPath;
}
64 changes: 31 additions & 33 deletions src/vs/base/common/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import URI from 'vs/base/common/uri';
import platform = require('vs/base/common/platform');
import types = require('vs/base/common/types');
import { nativeSep, normalize, isEqualOrParent, isEqual } from 'vs/base/common/paths';
import { nativeSep, normalize, isEqualOrParent, isEqual, basename, join } from 'vs/base/common/paths';
import { endsWith, ltrim } from 'vs/base/common/strings';

export interface ILabelProvider {
Expand All @@ -18,61 +17,60 @@ export interface ILabelProvider {
getLabel(element: any): string;
}

export interface IWorkspaceProvider {
getWorkspace(): {
resource: URI;
export interface IRootProvider {
getRoot(resource: URI): URI;
getWorkspace2(): {
roots: URI[];
};
}

export interface IUserHomeProvider {
userHome: string;
}

export function getPathLabel(resource: URI | string, basePathProvider?: URI | string | IWorkspaceProvider, userHomeProvider?: IUserHomeProvider): string {
const absolutePath = getPath(resource);
if (!absolutePath) {
export function getPathLabel(resource: URI | string, rootProvider?: IRootProvider, userHomeProvider?: IUserHomeProvider): string {
if (!resource) {
return null;
}

const basepath = basePathProvider && getPath(basePathProvider);
if (typeof resource === 'string') {
resource = URI.file(resource);
}

// return early if we can resolve a relative path label from the root
const baseResource = rootProvider ? rootProvider.getRoot(resource) : null;
if (baseResource) {
const hasMultipleRoots = rootProvider.getWorkspace2().roots.length > 1;

let pathLabel: string;
if (isEqual(baseResource.fsPath, resource.fsPath, !platform.isLinux /* ignorecase */)) {
pathLabel = ''; // no label if pathes are identical
} else {
pathLabel = normalize(ltrim(resource.fsPath.substr(baseResource.fsPath.length), nativeSep), true);
}

if (basepath && isEqualOrParent(absolutePath, basepath, !platform.isLinux /* ignorecase */)) {
if (isEqual(basepath, absolutePath, !platform.isLinux /* ignorecase */)) {
return ''; // no label if pathes are identical
if (hasMultipleRoots) {
const rootName = basename(baseResource.fsPath);
pathLabel = pathLabel ? join(rootName, pathLabel) : rootName; // always show root basename if there are multiple
}

return normalize(ltrim(absolutePath.substr(basepath.length), nativeSep), true);
return pathLabel;
}

if (platform.isWindows && absolutePath && absolutePath[1] === ':') {
return normalize(absolutePath.charAt(0).toUpperCase() + absolutePath.slice(1), true); // convert c:\something => C:\something
// convert c:\something => C:\something
if (platform.isWindows && resource.fsPath && resource.fsPath[1] === ':') {
return normalize(resource.fsPath.charAt(0).toUpperCase() + resource.fsPath.slice(1), true);
}

let res = normalize(absolutePath, true);
// normalize and tildify (macOS, Linux only)
let res = normalize(resource.fsPath, true);
if (!platform.isWindows && userHomeProvider) {
res = tildify(res, userHomeProvider.userHome);
}

return res;
}

function getPath(arg1: URI | string | IWorkspaceProvider): string {
if (!arg1) {
return null;
}

if (typeof arg1 === 'string') {
return arg1;
}

if (types.isFunction((<IWorkspaceProvider>arg1).getWorkspace)) {
const ws = (<IWorkspaceProvider>arg1).getWorkspace();
return ws ? ws.resource.fsPath : void 0;
}

return (<URI>arg1).fsPath;
}

export function tildify(path: string, userHome: string): string {
if (path && (platform.isMacintosh || platform.isLinux) && isEqualOrParent(path, userHome, !platform.isLinux /* ignorecase */)) {
path = `~${path.substr(userHome.length)}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Event, { Emitter } from 'vs/base/common/event';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { IExtensionManagementService, DidUninstallExtensionEvent, IExtensionEnablementService } from 'vs/platform/extensionManagement/common/extensionManagement';
import { adoptToGalleryExtensionId, getIdAndVersionFromLocalExtensionId } from 'vs/platform/extensionManagement/common/extensionManagementUtil';
import { IWorkspaceContextService, ILegacyWorkspace } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';

Expand All @@ -34,8 +34,8 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
extensionManagementService.onDidUninstallExtension(this.onDidUninstallExtension, this, this.disposables);
}

private get workspace(): ILegacyWorkspace {
return this.contextService.getWorkspace();
private get hasWorkspace(): boolean {
return this.contextService.hasWorkspace();
}

public getGloballyDisabledExtensions(): string[] {
Expand All @@ -60,7 +60,7 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
}

public setEnablement(identifier: string, enable: boolean, workspace: boolean = false): TPromise<boolean> {
if (workspace && !this.workspace) {
if (workspace && !this.hasWorkspace) {
return TPromise.wrapError<boolean>(localize('noWorkspace', "No workspace."));
}

Expand Down Expand Up @@ -106,7 +106,7 @@ export class ExtensionEnablementService implements IExtensionEnablementService {
}

private getDisabledExtensions(scope: StorageScope): string[] {
if (scope === StorageScope.WORKSPACE && !this.workspace) {
if (scope === StorageScope.WORKSPACE && !this.hasWorkspace) {
return [];
}
const value = this.storageService.get(DISABLED_EXTENSIONS_STORAGE_PATH, scope, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ suite('ExtensionEnablementService Test', () => {
test('test when no extensions are disabled for workspace when there is no workspace', (done) => {
testObject.setEnablement('pub.a', false, true)
.then(() => {
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', null);
instantiationService.stub(IWorkspaceContextService, 'hasWorkspace', false);
assert.deepEqual([], testObject.getWorkspaceDisabledExtensions());
})
.then(done, done);
Expand Down Expand Up @@ -174,7 +174,7 @@ suite('ExtensionEnablementService Test', () => {
});

test('test disable an extension for workspace when there is no workspace throws error', (done) => {
instantiationService.stub(IWorkspaceContextService, 'getWorkspace', null);
instantiationService.stub(IWorkspaceContextService, 'hasWorkspace', false);
testObject.setEnablement('pub.a', false, true)
.then(() => assert.fail('should throw an error'), error => assert.ok(error))
.then(done, done);
Expand Down
30 changes: 12 additions & 18 deletions src/vs/platform/storage/common/storageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import types = require('vs/base/common/types');
import errors = require('vs/base/common/errors');
import strings = require('vs/base/common/strings');
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import URI from "vs/base/common/uri";
import { IWorkspace } from "vs/platform/workspace/common/workspace";

// Browser localStorage interface
export interface IStorage {
Expand All @@ -20,11 +20,6 @@ export interface IStorage {
removeItem(key: string): void;
}

export interface IWorkspaceStorageIdentifier {
resource: URI;
uid?: number;
}

export class StorageService implements IStorageService {

public _serviceBrand: any;
Expand All @@ -43,36 +38,35 @@ export class StorageService implements IStorageService {
constructor(
globalStorage: IStorage,
workspaceStorage: IStorage,
workspaceIdentifier?: IWorkspaceStorageIdentifier
workspace?: IWorkspace,
legacyWorkspaceId?: number
) {
this.globalStorage = globalStorage;
this.workspaceStorage = workspaceStorage || globalStorage;

// Calculate workspace storage key
this.workspaceKey = this.getWorkspaceKey(workspaceIdentifier ? workspaceIdentifier.resource : void 0);
this.workspaceKey = this.getWorkspaceKey(workspace ? workspace.id : void 0);

// Make sure to delete all workspace storage if the workspace has been recreated meanwhile
// which is only possible if a UID property is provided that we can check on
if (workspaceIdentifier && types.isNumber(workspaceIdentifier.uid)) {
this.cleanupWorkspaceScope(workspaceIdentifier.uid);
// which is only possible if a id property is provided that we can check on
if (workspace && types.isNumber(legacyWorkspaceId)) {
this.cleanupWorkspaceScope(legacyWorkspaceId);
}
}

private getWorkspaceKey(workspaceId?: URI): string {
if (!workspaceId) {
private getWorkspaceKey(id?: string): string {
if (!id) {
return StorageService.NO_WORKSPACE_IDENTIFIER;
}

let workspaceIdStr = workspaceId.toString();

// Special case file:// URIs: strip protocol from key to produce shorter key
const fileProtocol = 'file:///';
if (workspaceIdStr.indexOf(fileProtocol) === 0) {
workspaceIdStr = workspaceIdStr.substr(fileProtocol.length);
if (id.indexOf(fileProtocol) === 0) {
id = id.substr(fileProtocol.length);
}

// Always end with "/"
return `${strings.rtrim(workspaceIdStr, '/')}/`;
return `${strings.rtrim(id, '/')}/`;
}

private cleanupWorkspaceScope(workspaceUid: number): void {
Expand Down
24 changes: 10 additions & 14 deletions src/vs/platform/storage/test/storageService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@
import * as assert from 'assert';
import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock';
import { StorageScope } from 'vs/platform/storage/common/storage';
import { IWorkspaceContextService, LegacyWorkspace } from 'vs/platform/workspace/common/workspace';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { StorageService, InMemoryLocalStorage } from 'vs/platform/storage/common/storageService';
import { TestContextService } from 'vs/workbench/test/workbenchTestServices';
import { TestWorkspace } from 'vs/platform/workspace/test/common/testWorkspace';

suite('Workbench StorageSevice', () => {

let contextService, instantiationService;
let contextService: IWorkspaceContextService;
let instantiationService: TestInstantiationService;

setup(() => {
instantiationService = new TestInstantiationService();
contextService = instantiationService.stub(IWorkspaceContextService, new TestContextService());
});

test('Swap Data with undefined default value', () => {
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace());
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace2());

s.swap('Monaco.IDE.Core.Storage.Test.swap', 'foobar', 'barfoo');
assert.strictEqual('foobar', s.get('Monaco.IDE.Core.Storage.Test.swap'));
Expand All @@ -34,7 +33,7 @@ suite('Workbench StorageSevice', () => {
});

test('Remove Data', () => {
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace());
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace2());
s.store('Monaco.IDE.Core.Storage.Test.remove', 'foobar');
assert.strictEqual('foobar', s.get('Monaco.IDE.Core.Storage.Test.remove'));

Expand All @@ -43,7 +42,7 @@ suite('Workbench StorageSevice', () => {
});

test('Get Data, Integer, Boolean', () => {
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace());
let s = new StorageService(new InMemoryLocalStorage(), null, contextService.getWorkspace2());

assert.strictEqual(s.get('Monaco.IDE.Core.Storage.Test.get', StorageScope.GLOBAL, 'foobar'), 'foobar');
assert.strictEqual(s.get('Monaco.IDE.Core.Storage.Test.get', StorageScope.GLOBAL, ''), '');
Expand Down Expand Up @@ -77,16 +76,15 @@ suite('Workbench StorageSevice', () => {

test('StorageSevice cleans up when workspace changes', () => {
let storageImpl = new InMemoryLocalStorage();
let ws = contextService.getWorkspace();
ws.uid = new Date().getTime();
let s = new StorageService(storageImpl, null, ws);
let time = new Date().getTime();
let s = new StorageService(storageImpl, null, contextService.getWorkspace2(), time);

s.store('key1', 'foobar');
s.store('key2', 'something');
s.store('wkey1', 'foo', StorageScope.WORKSPACE);
s.store('wkey2', 'foo2', StorageScope.WORKSPACE);

s = new StorageService(storageImpl, null, contextService.getWorkspace());
s = new StorageService(storageImpl, null, contextService.getWorkspace2(), time);

assert.strictEqual(s.get('key1', StorageScope.GLOBAL), 'foobar');
assert.strictEqual(s.get('key1', StorageScope.WORKSPACE, null), null);
Expand All @@ -95,9 +93,7 @@ suite('Workbench StorageSevice', () => {
assert.strictEqual(s.get('wkey1', StorageScope.WORKSPACE), 'foo');
assert.strictEqual(s.get('wkey2', StorageScope.WORKSPACE), 'foo2');

ws = new LegacyWorkspace(TestWorkspace.resource, Date.now());
ws.uid = new Date().getTime() + 100;
s = new StorageService(storageImpl, null, ws);
s = new StorageService(storageImpl, null, contextService.getWorkspace2(), time + 100);

assert.strictEqual(s.get('key1', StorageScope.GLOBAL), 'foobar');
assert.strictEqual(s.get('key1', StorageScope.WORKSPACE, null), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { resolveWorkbenchCommonProperties } from 'vs/platform/telemetry/node/wor
import { StorageService, InMemoryLocalStorage } from 'vs/platform/storage/common/storageService';
import { TestWorkspace } from 'vs/platform/workspace/test/common/testWorkspace';


suite('Telemetry - common properties', function () {

const commit = void 0;
Expand Down
2 changes: 1 addition & 1 deletion src/vs/platform/windows/common/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface IWindowsService {
getWindowCount(): TPromise<number>;
log(severity: string, ...messages: string[]): TPromise<void>;
// TODO@joao: what?
closeExtensionHostWindow(extensionDevelopmentPath: string): TPromise<void>;
closeExtensionHostWindow(extensionDevelopmentPaths: string[]): TPromise<void>;
showItemInFolder(path: string): TPromise<void>;

// This needs to be handled from browser process to prevent
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/windows/common/windowsIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface IWindowsChannel extends IChannel {
call(command: 'whenSharedProcessReady'): TPromise<void>;
call(command: 'toggleSharedProcess'): TPromise<void>;
call(command: 'log', arg: [string, string[]]): TPromise<void>;
call(command: 'closeExtensionHostWindow', arg: string): TPromise<void>;
call(command: 'closeExtensionHostWindow', arg: string[]): TPromise<void>;
call(command: 'showItemInFolder', arg: string): TPromise<void>;
call(command: 'openExternal', arg: string): TPromise<boolean>;
call(command: 'startCrashReporter', arg: Electron.CrashReporterStartOptions): TPromise<void>;
Expand Down Expand Up @@ -235,8 +235,8 @@ export class WindowsChannelClient implements IWindowsService {
return this.channel.call('log', [severity, messages]);
}

closeExtensionHostWindow(extensionDevelopmentPath: string): TPromise<void> {
return this.channel.call('closeExtensionHostWindow', extensionDevelopmentPath);
closeExtensionHostWindow(extensionDevelopmentPaths: string[]): TPromise<void> {
return this.channel.call('closeExtensionHostWindow', extensionDevelopmentPaths);
}

showItemInFolder(path: string): TPromise<void> {
Expand Down
Loading

0 comments on commit f2e1911

Please sign in to comment.