Skip to content

Commit

Permalink
Normalize windows path in uriToContentsPath (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
krassowski committed Nov 26, 2023
1 parent 14b12f7 commit 663f187
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
40 changes: 38 additions & 2 deletions packages/jupyterlab-lsp/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { collapseToDotted, escapeMarkdown, urisEqual } from './utils';
import {
collapseToDotted,
escapeMarkdown,
uriToContentsPath,
urisEqual
} from './utils';

describe('uris_equal', () => {
describe('urisEqual', () => {
it('should workaround Windows paths/Pyright issues', () => {
const result = urisEqual(
'file:///d%3A/a/jupyterlab-lsp/jupyterlab-lsp/atest/output/windows_39_4/home/n%C3%B6te%20b%C3%B2%C3%B3ks/example.py',
Expand All @@ -10,6 +15,37 @@ describe('uris_equal', () => {
});
});

describe('uriToContentsPath', () => {
it('should decode special characters', () => {
const result = uriToContentsPath(
'/node_modules/%40organization/package/lib/file.d.ts',
''
);
expect(result).toBe('/node_modules/@organization/package/lib/file.d.ts');
});

it('should remove shared prefix', () => {
const result = uriToContentsPath(
'file:///home/user/project/.virtual_documents/test.ipynb',
'file:///home/user/project'
);
expect(result).toBe('/.virtual_documents/test.ipynb');
});

it('should workaround Windows paths/Pyright issues', () => {
let result = uriToContentsPath(
'file:///d%3A/user/project/.virtual_documents/test.ipynb',
'file:///d:/user/project'
);
expect(result).toBe('/.virtual_documents/test.ipynb');
result = uriToContentsPath(
'file:///d%3A/user/project/.virtual_documents/test.ipynb',
'file:///d%3A/user/project'
);
expect(result).toBe('/.virtual_documents/test.ipynb');
});
});

describe('collapseToDotted', () => {
it('collapses simple objects', () => {
expect(
Expand Down
5 changes: 5 additions & 0 deletions packages/jupyterlab-lsp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export function uriToContentsPath(child: string, parent?: string) {
if (parent == null) {
return null;
}
const winPaths = isWinPath(parent) && isWinPath(child);
if (winPaths) {
parent = normalizeWinPath(parent);
child = normalizeWinPath(child);
}
if (child.startsWith(parent)) {
// 'decodeURIComponent' is needed over 'decodeURI' for '@' in TS/JS paths
return decodeURIComponent(child.replace(parent, ''));
Expand Down

0 comments on commit 663f187

Please sign in to comment.