UNPKG

langium

Version:

A language engineering tool for the Language Server Protocol

54 lines 2.52 kB
/****************************************************************************** * Copyright 2022 TypeFox GmbH * This program and the accompanying materials are made available under the * terms of the MIT License, which is available in the project root. ******************************************************************************/ import { URI, Utils } from 'vscode-uri'; export { URI }; export var UriUtils; (function (UriUtils) { UriUtils.basename = Utils.basename; UriUtils.dirname = Utils.dirname; UriUtils.extname = Utils.extname; UriUtils.joinPath = Utils.joinPath; UriUtils.resolvePath = Utils.resolvePath; const isWindows = typeof process === 'object' && (process === null || process === void 0 ? void 0 : process.platform) === 'win32'; function equals(a, b) { return (a === null || a === void 0 ? void 0 : a.toString()) === (b === null || b === void 0 ? void 0 : b.toString()); } UriUtils.equals = equals; function relative(from, to) { const fromPath = typeof from === 'string' ? URI.parse(from).path : from.path; const toPath = typeof to === 'string' ? URI.parse(to).path : to.path; const fromParts = fromPath.split('/').filter(e => e.length > 0); const toParts = toPath.split('/').filter(e => e.length > 0); if (isWindows) { const upperCaseDriveLetter = /^[A-Z]:$/; if (fromParts[0] && upperCaseDriveLetter.test(fromParts[0])) { fromParts[0] = fromParts[0].toLowerCase(); } if (toParts[0] && upperCaseDriveLetter.test(toParts[0])) { toParts[0] = toParts[0].toLowerCase(); } if (fromParts[0] !== toParts[0]) { // in case of different drive letters, we cannot compute a relative path, so... return toPath.substring(1); // fall back to full 'to' path, drop the leading '/', keep everything else as is for good comparability } } let i = 0; for (; i < fromParts.length; i++) { if (fromParts[i] !== toParts[i]) { break; } } const backPart = '../'.repeat(fromParts.length - i); const toPart = toParts.slice(i).join('/'); return backPart + toPart; } UriUtils.relative = relative; function normalize(uri) { return URI.parse(uri.toString()).toString(); } UriUtils.normalize = normalize; })(UriUtils || (UriUtils = {})); //# sourceMappingURL=uri-utils.js.map