From a154372764081ebe5d95b4c2be893047c1ba2734 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 28 Jul 2017 11:46:43 +0200 Subject: [PATCH] fix #31553 --- src/vs/vscode.d.ts | 7 ++++-- src/vs/workbench/api/node/extHost.api.impl.ts | 4 ++-- src/vs/workbench/api/node/extHostWorkspace.ts | 8 +++++-- .../api/extHostWorkspace.test.ts | 22 +++++++++++++++++++ 4 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/vs/vscode.d.ts b/src/vs/vscode.d.ts index a6431c3a134..c7d1065567b 100644 --- a/src/vs/vscode.d.ts +++ b/src/vs/vscode.d.ts @@ -4739,12 +4739,15 @@ declare module 'vscode' { * 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. + * is not contained in them, the input is returned. * * @param pathOrUri A path or uri. When a uri is given its [fsPath](#Uri.fsPath) is used. + * @param includeWorkspaceFolder When `true` and when the given path is contained inside a + * workspace folder the name of the workspace is prepended. Defaults to `true` when there are + * multiple workspace folders and `false` otherwise. * @return A path relative to the root or the input. */ - export function asRelativePath(pathOrUri: string | Uri): string; + export function asRelativePath(pathOrUri: string | Uri, includeWorkspaceFolder?: boolean): string; /** * Creates a file system watcher. diff --git a/src/vs/workbench/api/node/extHost.api.impl.ts b/src/vs/workbench/api/node/extHost.api.impl.ts index 4782c97be95..142d3d896ed 100644 --- a/src/vs/workbench/api/node/extHost.api.impl.ts +++ b/src/vs/workbench/api/node/extHost.api.impl.ts @@ -386,8 +386,8 @@ export function createApiFactory( apiUsage.publicLog('workspace#onDidChangeWorkspaceFolders'); return extHostWorkspace.onDidChangeWorkspace(listener, thisArgs, disposables); }, - asRelativePath: (pathOrUri) => { - return extHostWorkspace.getRelativePath(pathOrUri); + asRelativePath: (pathOrUri, includeWorkspace) => { + return extHostWorkspace.getRelativePath(pathOrUri, includeWorkspace); }, findFiles: (include, exclude, maxResults?, token?) => { return extHostWorkspace.findFiles(include, exclude, maxResults, token); diff --git a/src/vs/workbench/api/node/extHostWorkspace.ts b/src/vs/workbench/api/node/extHostWorkspace.ts index 97cc141dc64..c4e78b98bb8 100644 --- a/src/vs/workbench/api/node/extHostWorkspace.ts +++ b/src/vs/workbench/api/node/extHostWorkspace.ts @@ -117,7 +117,7 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { return roots[0].fsPath; } - getRelativePath(pathOrUri: string | vscode.Uri): string { + getRelativePath(pathOrUri: string | vscode.Uri, includeWorkspace?: boolean): string { let path: string; if (typeof pathOrUri === 'string') { @@ -139,8 +139,12 @@ export class ExtHostWorkspace extends ExtHostWorkspaceShape { return normalize(path); } + if (typeof includeWorkspace === 'undefined') { + includeWorkspace = this.workspace.roots.length > 1; + } + let result = relative(folder.uri.fsPath, path); - if (this.workspace.roots.length > 1) { + if (includeWorkspace) { result = `${folder.name}/${result}`; } return normalize(result); 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 275e78a22a8..af39c3d2e50 100644 --- a/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts +++ b/src/vs/workbench/test/electron-browser/api/extHostWorkspace.test.ts @@ -49,6 +49,28 @@ suite('ExtHostWorkspace', function () { assert.equal(ws.getRelativePath('/Coding/Two2/files/out.txt'), '/Coding/Two2/files/out.txt'); }); + test('slightly inconsistent behaviour of asRelativePath and getWorkspaceFolder, #31553', function () { + const mrws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', roots: [URI.file('/Coding/One'), URI.file('/Coding/Two')], name: 'Test' }); + + assert.equal(mrws.getRelativePath('/Coding/One/file.txt'), 'One/file.txt'); + assert.equal(mrws.getRelativePath('/Coding/One/file.txt', true), 'One/file.txt'); + assert.equal(mrws.getRelativePath('/Coding/One/file.txt', false), 'file.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two/files/out.txt'), 'Two/files/out.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two/files/out.txt', true), 'Two/files/out.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two/files/out.txt', false), 'files/out.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two2/files/out.txt'), '/Coding/Two2/files/out.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two2/files/out.txt', true), '/Coding/Two2/files/out.txt'); + assert.equal(mrws.getRelativePath('/Coding/Two2/files/out.txt', false), '/Coding/Two2/files/out.txt'); + + const srws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', roots: [URI.file('/Coding/One')], name: 'Test' }); + assert.equal(srws.getRelativePath('/Coding/One/file.txt'), 'file.txt'); + assert.equal(srws.getRelativePath('/Coding/One/file.txt', false), 'file.txt'); + assert.equal(srws.getRelativePath('/Coding/One/file.txt', true), 'One/file.txt'); + assert.equal(srws.getRelativePath('/Coding/Two2/files/out.txt'), '/Coding/Two2/files/out.txt'); + assert.equal(srws.getRelativePath('/Coding/Two2/files/out.txt', true), '/Coding/Two2/files/out.txt'); + assert.equal(srws.getRelativePath('/Coding/Two2/files/out.txt', false), '/Coding/Two2/files/out.txt'); + }); + test('getPath, legacy', function () { let ws = new ExtHostWorkspace(new TestThreadService(), { id: 'foo', name: 'Test', roots: [] }); assert.equal(ws.getPath(), undefined); -- GitLab