diff --git a/src/vs/workbench/api/node/extHostWorkspace.ts b/src/vs/workbench/api/node/extHostWorkspace.ts index c56bf79591b..7ec394f6913 100644 --- a/src/vs/workbench/api/node/extHostWorkspace.ts +++ b/src/vs/workbench/api/node/extHostWorkspace.ts @@ -19,19 +19,19 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { private static _requestIdPool = 0; - private _proxy: MainThreadWorkspaceShape; - private _workspacePath: string; + private readonly _proxy: MainThreadWorkspaceShape; + private _workspaceFolders: URI[]; constructor(threadService: IThreadService, folders: URI[]) { super(); this._proxy = threadService.get(MainContext.MainThreadWorkspace); - this._workspacePath = isFalsyOrEmpty(folders) ? undefined : folders[0].fsPath; + this._workspaceFolders = folders; } // --- workspace --- getPath(): string { - return this._workspacePath; + return isFalsyOrEmpty(this._workspaceFolders) ? undefined : this._workspaceFolders[0].fsPath; } getRelativePath(pathOrUri: string | vscode.Uri): string { @@ -47,20 +47,24 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { return path; } - if (!this._workspacePath) { + if (isFalsyOrEmpty(this._workspaceFolders)) { return normalize(path); } - let result = relative(this._workspacePath, path); - if (!result || result.indexOf('..') === 0) { - return normalize(path); + for (const { fsPath } of this._workspaceFolders) { + let result = relative(fsPath, path); + if (!result || result.indexOf('..') === 0) { + continue; + } + return normalize(result); } - return normalize(result); + return normalize(path); } - $acceptWorkspaceData(folders: URI[]): void { - // todo@joh do something, align with ctor URI[] vs IWorkspace + $acceptWorkspaceData(workspaceFolders: URI[]): void { + //TODO@joh equality-check, emit event etc. + this._workspaceFolders = workspaceFolders; } // --- search --- 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 413e9f14fb2..36430d52357 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts @@ -22,6 +22,7 @@ suite('ExtHostWorkspace', function () { assert.equal(ws.getRelativePath(''), ''); assert.equal(ws.getRelativePath('/foo/bar'), '/foo/bar'); + assert.equal(ws.getRelativePath('in/out'), 'in/out'); }); test('asRelativePath, same paths, #11402', function () { @@ -33,6 +34,18 @@ suite('ExtHostWorkspace', function () { const input2 = '/home/aeschli/workspaces/samples/docker/a.file'; assert.equal(ws.getRelativePath(input2), 'a.file'); + }); + test('asRelativePath, no workspace', function () { + const ws = new ExtHostWorkspace(new TestThreadService(), null); + assert.equal(ws.getRelativePath(''), ''); + assert.equal(ws.getRelativePath('/foo/bar'), '/foo/bar'); + }); + + test('asRelativePath, multiple folders', function () { + const ws = new ExtHostWorkspace(new TestThreadService(), [URI.file('/Coding/One'), URI.file('/Coding/Two')]); + 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/Two2/files/out.txt'), '/Coding/Two2/files/out.txt'); }); });