Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize Windows path to fix jump to definition with Pyright #1024

Merged
merged 1 commit into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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