Skip to content

Commit

Permalink
Fix #34699
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken authored and sandy081 committed Sep 20, 2017
1 parent c031ee1 commit 7c7eb9b
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 83 deletions.
21 changes: 12 additions & 9 deletions src/vs/code/electron-main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { TPromise } from 'vs/base/common/winjs.base';
import { IWorkspacesMainService, IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, WORKSPACE_FILTER, isSingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { mnemonicButtonLabel } from 'vs/base/common/labels';
import { Schemas } from 'vs/base/common/network';

enum WindowError {
UNRESPONSIVE,
Expand Down Expand Up @@ -1813,18 +1814,20 @@ class WorkspacesManager {
}

private getWorkspaceDialogDefaultPath(workspace?: IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier): string {
let defaultPath: string;
if (workspace) {
if (isSingleFolderWorkspaceIdentifier(workspace)) {
defaultPath = dirname(workspace);
} else {
const resolvedWorkspace = this.workspacesService.resolveWorkspaceSync(workspace.configPath);
if (resolvedWorkspace && resolvedWorkspace.folders.length > 0) {
defaultPath = dirname(resolvedWorkspace.folders[0].uri.fsPath);
return dirname(workspace);
}

const resolvedWorkspace = this.workspacesService.resolveWorkspaceSync(workspace.configPath);
if (resolvedWorkspace && resolvedWorkspace.folders.length > 0) {
for (const folder of resolvedWorkspace.folders) {
if (folder.uri.scheme === Schemas.file) {
return dirname(folder.uri.fsPath);
}
}
}
}

return defaultPath;
return void 0;
}
}
}
5 changes: 3 additions & 2 deletions src/vs/code/node/windowsFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as platform from 'vs/base/common/platform';
import * as paths from 'vs/base/common/paths';
import { OpenContext } from 'vs/platform/windows/common/windows';
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, IResolvedWorkspace } from 'vs/platform/workspaces/common/workspaces';
import { Schemas } from 'vs/base/common/network';

export interface ISimpleWindow {
openedWorkspace?: IWorkspaceIdentifier;
Expand Down Expand Up @@ -62,7 +63,7 @@ function findWindowOnFilePath<W extends ISimpleWindow>(windows: W[], filePath: s
for (let i = 0; i < workspaceWindows.length; i++) {
const window = workspaceWindows[i];
const resolvedWorkspace = workspaceResolver(window.openedWorkspace);
if (resolvedWorkspace && resolvedWorkspace.folders.some(folder => paths.isEqualOrParent(filePath, folder.uri.fsPath, !platform.isLinux /* ignorecase */))) {
if (resolvedWorkspace && resolvedWorkspace.folders.some(folder => folder.uri.scheme === Schemas.file && paths.isEqualOrParent(filePath, folder.uri.fsPath, !platform.isLinux /* ignorecase */))) {
return window;
}
}
Expand Down Expand Up @@ -164,4 +165,4 @@ export function findWindowOnWorkspaceOrFolderPath<W extends ISimpleWindow>(windo

return false;
})[0];
}
}
14 changes: 12 additions & 2 deletions src/vs/platform/workspace/common/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as paths from 'vs/base/common/paths';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { TrieMap } from 'vs/base/common/map';
import Event from 'vs/base/common/event';
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier, IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier, IStoredWorkspaceFolder, isRawFileWorkspaceFolder, isRawUriWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { coalesce, distinct } from 'vs/base/common/arrays';
import { isLinux } from 'vs/base/common/platform';

Expand Down Expand Up @@ -232,7 +232,17 @@ export function toWorkspaceFolders(configuredFolders: IStoredWorkspaceFolder[],

function parseWorkspaceFolders(configuredFolders: IStoredWorkspaceFolder[], relativeTo: URI): WorkspaceFolder[] {
return configuredFolders.map((configuredFolder, index) => {
const uri = toUri(configuredFolder.path, relativeTo);
let uri: URI;
if (isRawFileWorkspaceFolder(configuredFolder)) {
uri = toUri(configuredFolder.path, relativeTo);
} else if (isRawUriWorkspaceFolder(configuredFolder)) {
try {
uri = URI.parse(configuredFolder.uri);
} catch (e) {
console.warn(e);
// ignore
}
}
if (!uri) {
return void 0;
}
Expand Down
44 changes: 23 additions & 21 deletions src/vs/platform/workspace/test/common/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as assert from 'assert';
import { Workspace, toWorkspaceFolders, WorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import URI from 'vs/base/common/uri';
import { IRawFileWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';

suite('Workspace', () => {

Expand Down Expand Up @@ -51,7 +52,7 @@ suite('Workspace', () => {

assert.equal(actual.length, 1);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test').fsPath);
assert.equal(actual[0].raw.path, '/src/test');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test');
});
Expand All @@ -61,7 +62,7 @@ suite('Workspace', () => {

assert.equal(actual.length, 1);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test').fsPath);
assert.equal(actual[0].raw.path, './test');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, './test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test');
});
Expand All @@ -70,8 +71,9 @@ suite('Workspace', () => {
const actual = toWorkspaceFolders([{ path: '/src/test', name: 'hello' }]);

assert.equal(actual.length, 1);

assert.equal(actual[0].uri.fsPath, URI.file('/src/test').fsPath);
assert.equal(actual[0].raw.path, '/src/test');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'hello');
});
Expand All @@ -81,17 +83,17 @@ suite('Workspace', () => {

assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/src/test3').fsPath);
assert.equal(actual[1].raw.path, '/src/test3');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test3');

assert.equal(actual[2].uri.fsPath, URI.file('/src/test1').fsPath);
assert.equal(actual[2].raw.path, '/src/test1');
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/src/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
Expand All @@ -101,17 +103,17 @@ suite('Workspace', () => {

assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/src/test3').fsPath);
assert.equal(actual[1].raw.path, '/src/test3');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');

assert.equal(actual[2].uri.fsPath, URI.file('/src/test1').fsPath);
assert.equal(actual[2].raw.path, '/src/test1');
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/src/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
Expand All @@ -121,17 +123,17 @@ suite('Workspace', () => {

assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/abc/test3').fsPath);
assert.equal(actual[1].raw.path, '/abc/test3');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/abc/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');

assert.equal(actual[2].uri.fsPath, URI.file('/src/test1').fsPath);
assert.equal(actual[2].raw.path, './test1');
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, './test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
Expand All @@ -141,12 +143,12 @@ suite('Workspace', () => {

assert.equal(actual.length, 2);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/src/test1').fsPath);
assert.equal(actual[1].raw.path, '/src/test1');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test1');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test1');
});
Expand All @@ -156,17 +158,17 @@ suite('Workspace', () => {

assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/src/test3').fsPath);
assert.equal(actual[1].raw.path, '/src/test3');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, '/src/test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'noName');

assert.equal(actual[2].uri.fsPath, URI.file('/abc/test1').fsPath);
assert.equal(actual[2].raw.path, '/abc/test1');
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/abc/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
Expand All @@ -176,18 +178,18 @@ suite('Workspace', () => {

assert.equal(actual.length, 3);
assert.equal(actual[0].uri.fsPath, URI.file('/src/test2').fsPath);
assert.equal(actual[0].raw.path, '/src/test2');
assert.equal((<IRawFileWorkspaceFolder>actual[0].raw).path, '/src/test2');
assert.equal(actual[0].index, 0);
assert.equal(actual[0].name, 'test2');

assert.equal(actual[1].uri.fsPath, URI.file('/src/test3').fsPath);
assert.equal(actual[1].raw.path, './test3');
assert.equal((<IRawFileWorkspaceFolder>actual[1].raw).path, './test3');
assert.equal(actual[1].index, 1);
assert.equal(actual[1].name, 'test3');

assert.equal(actual[2].uri.fsPath, URI.file('/abc/test1').fsPath);
assert.equal(actual[2].raw.path, '/abc/test1');
assert.equal((<IRawFileWorkspaceFolder>actual[2].raw).path, '/abc/test1');
assert.equal(actual[2].index, 2);
assert.equal(actual[2].name, 'test1');
});
});
});
33 changes: 31 additions & 2 deletions src/vs/platform/workspaces/common/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,44 @@ export interface IWorkspaceIdentifier {
configPath: string;
}

export interface IStoredWorkspaceFolder {
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 {
path: string;
name?: string;
}

export interface IRawUriWorkspaceFolder {
uri: string;
name?: string;
}

export type IStoredWorkspaceFolder = IRawFileWorkspaceFolder | IRawUriWorkspaceFolder;

export interface IResolvedWorkspace extends IWorkspaceIdentifier {
folders: IWorkspaceFolder[];
}

export interface IStoredWorkspace {
folders: IStoredWorkspaceFolder[];
}

export interface IWorkspaceSavedEvent {
workspace: IWorkspaceIdentifier;
oldConfigPath: string;
Expand Down Expand Up @@ -104,4 +133,4 @@ export function isWorkspaceIdentifier(obj: any): obj is IWorkspaceIdentifier {
const workspaceIdentifier = obj as IWorkspaceIdentifier;

return workspaceIdentifier && typeof workspaceIdentifier.id === 'string' && typeof workspaceIdentifier.configPath === 'string';
}
}
22 changes: 9 additions & 13 deletions src/vs/platform/workspaces/electron-main/workspacesMainService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import { IWorkspacesMainService, IWorkspaceIdentifier, WORKSPACE_EXTENSION, IWorkspaceSavedEvent, UNTITLED_WORKSPACE_NAME, IResolvedWorkspace, IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { IWorkspacesMainService, IWorkspaceIdentifier, WORKSPACE_EXTENSION, IWorkspaceSavedEvent, UNTITLED_WORKSPACE_NAME, IResolvedWorkspace, IStoredWorkspaceFolder, isRawFileWorkspaceFolder, isStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { TPromise } from 'vs/base/common/winjs.base';
import { isParent } from 'vs/platform/files/common/files';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
Expand All @@ -24,6 +24,7 @@ import * as jsonEdit from 'vs/base/common/jsonEdit';
import { applyEdit } from 'vs/base/common/jsonFormatter';
import { massageFolderPathForWorkspace } from 'vs/platform/workspaces/node/workspaces';
import { toWorkspaceFolders } from 'vs/platform/workspace/common/workspace';
import URI from 'vs/base/common/uri';

export interface IStoredWorkspace {
folders: IStoredWorkspaceFolder[];
Expand Down Expand Up @@ -80,17 +81,10 @@ export class WorkspacesMainService implements IWorkspacesMainService {
try {
const workspace = this.doParseStoredWorkspace(path, contents);

// relative paths get resolved against the workspace location
workspace.folders.forEach(folder => {
if (!isAbsolute(folder.path)) {
folder.path = resolve(dirname(path), folder.path);
}
});

return {
id: this.getWorkspaceId(path),
configPath: path,
folders: toWorkspaceFolders(workspace.folders)
folders: toWorkspaceFolders(workspace.folders, URI.file(dirname(path)))
};
} catch (error) {
this.logService.log(error.toString());
Expand All @@ -111,7 +105,7 @@ export class WorkspacesMainService implements IWorkspacesMainService {

// Filter out folders which do not have a path set
if (Array.isArray(storedWorkspace.folders)) {
storedWorkspace.folders = storedWorkspace.folders.filter(folder => !!folder.path);
storedWorkspace.folders = storedWorkspace.folders.filter(folder => isStoredWorkspaceFolder(folder));
}

// Validate
Expand Down Expand Up @@ -205,11 +199,13 @@ export class WorkspacesMainService implements IWorkspacesMainService {
// is a parent of the location of the workspace file itself. Otherwise keep
// using absolute paths.
storedWorkspace.folders.forEach(folder => {
if (!isAbsolute(folder.path)) {
folder.path = resolve(sourceConfigFolder, folder.path); // relative paths get resolved against the workspace location
if (isRawFileWorkspaceFolder(folder)) {
if (!isAbsolute(folder.path)) {
folder.path = resolve(sourceConfigFolder, folder.path); // relative paths get resolved against the workspace location
}
folder.path = massageFolderPathForWorkspace(folder.path, targetConfigFolder, storedWorkspace.folders);
}

folder.path = massageFolderPathForWorkspace(folder.path, targetConfigFolder, storedWorkspace.folders);
});

// Preserve as much of the existing workspace as possible by using jsonEdit
Expand Down
6 changes: 3 additions & 3 deletions src/vs/platform/workspaces/node/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict';

import { IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { IStoredWorkspaceFolder, isRawFileWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces';
import { isWindows, isLinux } from 'vs/base/common/platform';
import { isAbsolute, relative } from 'path';
import { isEqualOrParent, normalize } from 'vs/base/common/paths';
Expand Down Expand Up @@ -56,11 +56,11 @@ function shouldUseSlashForPath(storedFolders: IStoredWorkspaceFolder[]): boolean
let useSlashesForPath = !isWindows;
if (isWindows) {
storedFolders.forEach(folder => {
if (!useSlashesForPath && folder.path.indexOf(SLASH) >= 0) {
if (isRawFileWorkspaceFolder(folder) && !useSlashesForPath && folder.path.indexOf(SLASH) >= 0) {
useSlashesForPath = true;
}
});
}

return useSlashesForPath;
}
}
Loading

0 comments on commit 7c7eb9b

Please sign in to comment.