diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index bb260eb969a..a01185a1131 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -4681,7 +4681,7 @@ declare module 'vscode' { export function getWorkspaceFolder(uri: Uri): WorkspaceFolder | undefined; /** - * Returns a path that is relative to the workspace root. + * Returns a path that is relative to the workspace folder or folders. * * When there are no [workspace folders](#workspace.workspaceFolders) or when the path * is not a child of them, the input is returned. diff --git a/src/vs/workbench/api/node/extHostWorkspace.ts b/src/vs/workbench/api/node/extHostWorkspace.ts index f42eb06cfa5..b98c5d0b198 100644 --- a/src/vs/workbench/api/node/extHostWorkspace.ts +++ b/src/vs/workbench/api/node/extHostWorkspace.ts @@ -7,7 +7,7 @@ import URI from 'vs/base/common/uri'; import Event, { Emitter } from 'vs/base/common/event'; import { normalize } from 'vs/base/common/paths'; -import { isFalsyOrEmpty, delta } from 'vs/base/common/arrays'; +import { delta } from 'vs/base/common/arrays'; import { relative, basename } from 'path'; import { Workspace } from 'vs/platform/workspace/common/workspace'; import { IThreadService } from 'vs/workbench/services/thread/common/threadService'; @@ -112,12 +112,7 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { if (roots.length === 0) { return undefined; } - // if (roots.length === 1) { return roots[0].fsPath; - // } - // return `undefined` when there no or more than 1 - // root folder. - // return undefined; } getRelativePath(pathOrUri: string | vscode.Uri): string { @@ -133,19 +128,20 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { return path; } - if (!this._workspace || isFalsyOrEmpty(this._workspace.roots)) { + const folder = this.getWorkspaceFolder(typeof pathOrUri === 'string' + ? URI.file(pathOrUri) + : pathOrUri + ); + + if (!folder) { return normalize(path); } - for (const { fsPath } of this._workspace.roots) { - let result = relative(fsPath, path); - if (!result || result.indexOf('..') === 0) { - continue; - } - return normalize(result); + let result = relative(folder.uri.fsPath, path); + if (this.workspace.roots.length > 1) { + result = `${folder.name}/${result}`; } - - return normalize(path); + return normalize(result); } $acceptWorkspaceData(data: IWorkspaceData): void { diff --git a/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts b/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts index bd4c1109352..275e78a22a8 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts @@ -44,8 +44,8 @@ suite('ExtHostWorkspace', function () { test('asRelativePath, multiple folders', function () { const ws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', roots: [URI.file('/Coding/One'), URI.file('/Coding/Two')], name: 'Test' }); - assert.equal(ws.getRelativePath('/Coding/One/file.txt'), 'file.txt'); - assert.equal(ws.getRelativePath('/Coding/Two/files/out.txt'), 'files/out.txt'); + assert.equal(ws.getRelativePath('/Coding/One/file.txt'), 'One/file.txt'); + assert.equal(ws.getRelativePath('/Coding/Two/files/out.txt'), 'Two/files/out.txt'); assert.equal(ws.getRelativePath('/Coding/Two2/files/out.txt'), '/Coding/Two2/files/out.txt'); }); @@ -59,8 +59,8 @@ suite('ExtHostWorkspace', function () { ws = new ExtHostWorkspace(new TestThreadService(), undefined); assert.equal(ws.getPath(), undefined); - // ws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', name: 'Test', roots: [URI.file('Folder'), URI.file('Another/Folder')] }); - // assert.equal(ws.getPath(), undefined); + ws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', name: 'Test', roots: [URI.file('Folder'), URI.file('Another/Folder')] }); + assert.equal(ws.getPath().replace(/\\/g, '/'), '/Folder'); ws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', name: 'Test', roots: [URI.file('/Folder')] }); assert.equal(ws.getPath().replace(/\\/g, '/'), '/Folder');