Merge from vscode 2b0b9136329c181a9e381463a1f7dc3a2d105a34 (#4880)

This commit is contained in:
Karl Burtram
2019-04-05 10:09:18 -07:00
committed by GitHub
parent 9bd7e30d18
commit cb5bcf2248
433 changed files with 8915 additions and 8361 deletions

View File

@@ -4,9 +4,9 @@
*--------------------------------------------------------------------------------------------*/
import { isWindows } from 'vs/base/common/platform';
import { startsWithIgnoreCase, equalsIgnoreCase } from 'vs/base/common/strings';
import { startsWithIgnoreCase, equalsIgnoreCase, endsWith, rtrim } from 'vs/base/common/strings';
import { CharCode } from 'vs/base/common/charCode';
import { sep, posix } from 'vs/base/common/path';
import { sep, posix, isAbsolute, join, normalize } from 'vs/base/common/path';
function isPathSeparator(code: number) {
return code === CharCode.Slash || code === CharCode.Backslash;
@@ -227,4 +227,56 @@ export function isEqualOrParent(path: string, candidate: string, ignoreCase?: bo
export function isWindowsDriveLetter(char0: number): boolean {
return char0 >= CharCode.A && char0 <= CharCode.Z || char0 >= CharCode.a && char0 <= CharCode.z;
}
export function sanitizeFilePath(candidate: string, cwd: string): string {
// Special case: allow to open a drive letter without trailing backslash
if (isWindows && endsWith(candidate, ':')) {
candidate += sep;
}
// Ensure absolute
if (!isAbsolute(candidate)) {
candidate = join(cwd, candidate);
}
// Ensure normalized
candidate = normalize(candidate);
// Ensure no trailing slash/backslash
if (isWindows) {
candidate = rtrim(candidate, sep);
// Special case: allow to open drive root ('C:\')
if (endsWith(candidate, ':')) {
candidate += sep;
}
} else {
candidate = rtrim(candidate, sep);
// Special case: allow to open root ('/')
if (!candidate) {
candidate = sep;
}
}
return candidate;
}
export function isRootOrDriveLetter(path: string): boolean {
const pathNormalized = normalize(path);
if (isWindows) {
if (path.length > 3) {
return false;
}
return isWindowsDriveLetter(pathNormalized.charCodeAt(0))
&& pathNormalized.charCodeAt(1) === CharCode.Colon
&& (path.length === 2 || pathNormalized.charCodeAt(2) === CharCode.Backslash);
}
return pathNormalized === posix.sep;
}