提交 a1543727 编写于 作者: J Johannes Rieken

fix #31553

上级 281cf393
......@@ -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.
......
......@@ -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);
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册