Skip to content

Commit

Permalink
more supplements
Browse files Browse the repository at this point in the history
  • Loading branch information
sailingKieler committed Feb 21, 2025
1 parent ec492d3 commit 02fd044
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/langium/src/utils/uri-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ export namespace UriUtils {
export const joinPath = Utils.joinPath;
export const resolvePath = Utils.resolvePath;

const isWindows = process?.platform === 'win32';
const isWindows = typeof process === 'object' && process?.platform === 'win32';

export function equals(a?: URI | string, b?: URI | string): boolean {
return a?.toString() === b?.toString();
}

export function relative(from: URI | string, to: URI | string): string {
const fromPath = typeof from === 'string' ? from : from.path;
const toPath = typeof to === 'string' ? to : to.path;
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);
const toParts = toPath.split('/').filter(e => e.length > 0);

if (isWindows) {
const upperCaseDriveLetter = /^[A-Z]:$/;
Expand All @@ -36,6 +36,10 @@ export namespace UriUtils {
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;
Expand Down
6 changes: 6 additions & 0 deletions packages/langium/test/utils/uri-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ describe('URI Utils', () => {
expect(UriUtils.relative(from, to)).toBe('../c/d.txt');
});

test.skipIf(process.platform !== 'win32')('different win32 drive letters', () => {
const from = URI.file('c:\\a\\b');
const to = URI.file('D:\\a\\c\\d.txt');
expect(UriUtils.relative(from, to)).toBe('D:/a/c/d.txt');
});

test('Equal uris are equal', () => {
const uri1 = 'file:///a/b';
const uri2 = 'file:///a/b';
Expand Down

0 comments on commit 02fd044

Please sign in to comment.