Skip to content

Commit 663f187

Browse files
authored
Normalize windows path in uriToContentsPath (#1024)
1 parent 14b12f7 commit 663f187

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/jupyterlab-lsp/src/utils.spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { collapseToDotted, escapeMarkdown, urisEqual } from './utils';
1+
import {
2+
collapseToDotted,
3+
escapeMarkdown,
4+
uriToContentsPath,
5+
urisEqual
6+
} from './utils';
27

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

18+
describe('uriToContentsPath', () => {
19+
it('should decode special characters', () => {
20+
const result = uriToContentsPath(
21+
'/node_modules/%40organization/package/lib/file.d.ts',
22+
''
23+
);
24+
expect(result).toBe('/node_modules/@organization/package/lib/file.d.ts');
25+
});
26+
27+
it('should remove shared prefix', () => {
28+
const result = uriToContentsPath(
29+
'file:///home/user/project/.virtual_documents/test.ipynb',
30+
'file:///home/user/project'
31+
);
32+
expect(result).toBe('/.virtual_documents/test.ipynb');
33+
});
34+
35+
it('should workaround Windows paths/Pyright issues', () => {
36+
let result = uriToContentsPath(
37+
'file:///d%3A/user/project/.virtual_documents/test.ipynb',
38+
'file:///d:/user/project'
39+
);
40+
expect(result).toBe('/.virtual_documents/test.ipynb');
41+
result = uriToContentsPath(
42+
'file:///d%3A/user/project/.virtual_documents/test.ipynb',
43+
'file:///d%3A/user/project'
44+
);
45+
expect(result).toBe('/.virtual_documents/test.ipynb');
46+
});
47+
});
48+
1349
describe('collapseToDotted', () => {
1450
it('collapses simple objects', () => {
1551
expect(

packages/jupyterlab-lsp/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ export function uriToContentsPath(child: string, parent?: string) {
132132
if (parent == null) {
133133
return null;
134134
}
135+
const winPaths = isWinPath(parent) && isWinPath(child);
136+
if (winPaths) {
137+
parent = normalizeWinPath(parent);
138+
child = normalizeWinPath(child);
139+
}
135140
if (child.startsWith(parent)) {
136141
// 'decodeURIComponent' is needed over 'decodeURI' for '@' in TS/JS paths
137142
return decodeURIComponent(child.replace(parent, ''));

0 commit comments

Comments
 (0)