提交 c22bcdb8 编写于 作者: M Martin Aeschlimann

resources must not use path library

上级 fbb088bb
......@@ -9,6 +9,7 @@ import URI from 'vs/base/common/uri';
import { equalsIgnoreCase } from 'vs/base/common/strings';
import { Schemas } from 'vs/base/common/network';
import { isLinux } from 'vs/base/common/platform';
import { CharCode } from 'vs/base/common/charCode';
export function getComparisonKey(resource: URI): string {
return hasToIgnoreCase(resource) ? resource.toString().toLowerCase() : resource.toString();
......@@ -21,7 +22,7 @@ export function hasToIgnoreCase(resource: URI): boolean {
}
export function basenameOrAuthority(resource: URI): string {
return paths.basename(resource.path) || resource.authority;
return basename_urlpath(resource.path) || resource.authority;
}
export function isEqualOrParent(resource: URI, candidate: URI, ignoreCase?: boolean): boolean {
......@@ -53,21 +54,45 @@ export function isEqual(first: URI, second: URI, ignoreCase?: boolean): boolean
return first.toString() === second.toString();
}
export function basename(resource: URI): string {
if (resource.scheme === 'file') {
return paths.basename(resource.fsPath);
}
return basename_urlpath(resource.path);
}
export function dirname(resource: URI): URI {
const dirname = paths.dirname(resource.path);
if (resource.authority && dirname && !paths.isAbsolute(dirname)) {
if (resource.scheme === 'file') {
return URI.file(paths.dirname(resource.fsPath));
}
let dirname = dirname_urlpath(resource.path);
if (resource.authority && dirname.length && dirname.charCodeAt(0) !== CharCode.Slash) {
return null; // If a URI contains an authority component, then the path component must either be empty or begin with a slash ("/") character
}
return resource.with({
path: dirname
});
}
export function joinPath(resource: URI, pathFragment: string): URI {
const joinedPath = paths.join(resource.path || '/', pathFragment);
if (resource.scheme === 'file') {
return URI.file(paths.join(resource.path || '/', pathFragment));
}
let path = resource.path || '';
let last = path.charCodeAt(path.length - 1);
let next = pathFragment.charCodeAt(0);
if (last !== CharCode.Slash) {
if (next !== CharCode.Slash) {
path += '/';
}
} else {
if (next === CharCode.Slash) {
pathFragment = pathFragment.substr(1);
}
}
return resource.with({
path: joinedPath
path: path + pathFragment
});
}
......@@ -90,3 +115,27 @@ export function distinctParents<T>(items: T[], resourceAccessor: (item: T) => UR
return distinctParents;
}
function dirname_urlpath(path: string): string {
const idx = ~path.lastIndexOf('/');
if (idx === 0) {
return '';
} else if (~idx === 0) {
return path[0];
} else if (~idx === path.length - 1) {
return dirname_urlpath(path.substring(0, path.length - 1));
} else {
return path.substring(0, ~idx);
}
}
function basename_urlpath(path: string): string {
const idx = ~path.lastIndexOf('/');
if (idx === 0) {
return path;
} else if (~idx === path.length - 1) {
return basename_urlpath(path.substring(0, path.length - 1));
} else {
return path.substr(~idx + 1);
}
}
......@@ -5,8 +5,7 @@
'use strict';
import * as assert from 'assert';
import { normalize } from 'vs/base/common/paths';
import { dirname, distinctParents, joinPath, isEqual, isEqualOrParent, hasToIgnoreCase } from 'vs/base/common/resources';
import { dirname, basename, distinctParents, joinPath, isEqual, isEqualOrParent, hasToIgnoreCase } from 'vs/base/common/resources';
import URI from 'vs/base/common/uri';
import { isWindows } from 'vs/base/common/platform';
......@@ -44,26 +43,62 @@ suite('Resources', () => {
});
test('dirname', () => {
const f = URI.file('/some/file/test.txt');
const d = dirname(f);
assert.equal(d.fsPath, normalize('/some/file', true));
if (isWindows) {
assert.equal(dirname(URI.file('c:\\some\\file\\test.txt')).toString(), 'file:///c%3A/some/file');
assert.equal(dirname(URI.file('c:\\some\\file')).toString(), 'file:///c%3A/some');
assert.equal(dirname(URI.file('c:\\some\\file\\')).toString(), 'file:///c%3A/some');
assert.equal(dirname(URI.file('c:\\some')).toString(), 'file:///c%3A/');
} else {
assert.equal(dirname(URI.file('/some/file/test.txt')).toString(), 'file:///some/file');
assert.equal(dirname(URI.file('/some/file/')).toString(), 'file:///some');
assert.equal(dirname(URI.file('/some/file')).toString(), 'file:///some');
assert.equal(dirname(URI.file('/some/file')).toString(), 'file:///some');
}
assert.equal(dirname(URI.parse('foo://a/some/file/test.txt')).toString(), 'foo://a/some/file');
assert.equal(dirname(URI.parse('foo://a/some/file/')).toString(), 'foo://a/some');
assert.equal(dirname(URI.parse('foo://a/some/file')).toString(), 'foo://a/some');
assert.equal(dirname(URI.parse('foo://a/some')).toString(), 'foo://a/');
// does not explode (https://github.com/Microsoft/vscode/issues/41987)
dirname(URI.from({ scheme: 'file', authority: '/users/someone/portal.h' }));
});
test('joinPath', () => {
assert.equal(
joinPath(URI.file('/foo/bar'), '/file.js').toString(),
'file:///foo/bar/file.js');
test('basename', () => {
if (isWindows) {
assert.equal(basename(URI.file('c:\\some\\file\\test.txt')).toString(), 'test.txt');
assert.equal(basename(URI.file('c:\\some\\file')).toString(), 'file');
assert.equal(basename(URI.file('c:\\some\\file\\')).toString(), 'file');
} else {
assert.equal(basename(URI.file('/some/file/test.txt')).toString(), 'test.txt');
assert.equal(basename(URI.file('/some/file/')).toString(), 'file');
assert.equal(basename(URI.file('/some/file')).toString(), 'file');
assert.equal(basename(URI.file('/some')).toString(), 'some');
}
assert.equal(basename(URI.parse('foo://a/some/file/test.txt')).toString(), 'test.txt');
assert.equal(basename(URI.parse('foo://a/some/file/')).toString(), 'file');
assert.equal(basename(URI.parse('foo://a/some/file')).toString(), 'file');
assert.equal(basename(URI.parse('foo://a/some')).toString(), 'some');
assert.equal(
joinPath(URI.file('/foo/bar/'), '/file.js').toString(),
'file:///foo/bar/file.js');
// does not explode (https://github.com/Microsoft/vscode/issues/41987)
dirname(URI.from({ scheme: 'file', authority: '/users/someone/portal.h' }));
});
assert.equal(
joinPath(URI.file('/'), '/file.js').toString(),
'file:///file.js');
test('joinPath', () => {
if (isWindows) {
assert.equal(joinPath(URI.file('c:\\foo\\bar'), '/file.js').toString(), 'file:///c%3A/foo/bar/file.js');
assert.equal(joinPath(URI.file('c:\\foo\\bar\\'), 'file.js').toString(), 'file:///c%3A/foo/bar/file.js');
assert.equal(joinPath(URI.file('c:\\foo\\bar\\'), '/file.js').toString(), 'file:///c%3A/foo/bar/file.js');
assert.equal(joinPath(URI.file('c:\\'), '/file.js').toString(), 'file:///c%3A/file.js');
} else {
assert.equal(joinPath(URI.file('/foo/bar'), '/file.js').toString(), 'file:///foo/bar/file.js');
assert.equal(joinPath(URI.file('/foo/bar'), 'file.js').toString(), 'file:///foo/bar/file.js');
assert.equal(joinPath(URI.file('/foo/bar/'), '/file.js').toString(), 'file:///foo/bar/file.js');
assert.equal(joinPath(URI.file('/'), '/file.js').toString(), 'file:///file.js');
}
assert.equal(joinPath(URI.parse('foo://a/foo/bar'), '/file.js').toString(), 'foo://a/foo/bar/file.js');
assert.equal(joinPath(URI.parse('foo://a/foo/bar'), 'file.js').toString(), 'foo://a/foo/bar/file.js');
assert.equal(joinPath(URI.parse('foo://a/foo/bar/'), '/file.js').toString(), 'foo://a/foo/bar/file.js');
assert.equal(joinPath(URI.parse('foo://a/'), '/file.js').toString(), 'foo://a/file.js');
assert.equal(
joinPath(URI.from({ scheme: 'myScheme', authority: 'authority', path: '/path', query: 'query', fragment: 'fragment' }), '/file.js').toString(),
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册