|
4 | 4 | import sys |
5 | 5 | import json |
6 | 6 | import traceback |
| 7 | +import platform |
7 | 8 |
|
8 | 9 | WORD_RE = re.compile(r'\w') |
9 | 10 | jediPreview = False |
@@ -33,6 +34,17 @@ class JediCompletion(object): |
33 | 34 | def __init__(self): |
34 | 35 | self.default_sys_path = sys.path |
35 | 36 | self._input = io.open(sys.stdin.fileno(), encoding='utf-8') |
| 37 | + if (os.path.sep == '/') and (platform.uname()[2].find('Microsoft') > -1): |
| 38 | + # WSL; does not support UNC paths |
| 39 | + self.drive_mount = '/mnt/' |
| 40 | + elif sys.platform == 'cygwin': |
| 41 | + # cygwin |
| 42 | + self.drive_mount = '/cygdrive/' |
| 43 | + else: |
| 44 | + # Do no normalization, e.g. Windows build of Python. |
| 45 | + # Could add additional test: ((os.path.sep == '/') and os.path.isdir('/mnt/c')) |
| 46 | + # However, this may have more false positives trying to identify Windows/*nix hybrids |
| 47 | + self.drive_mount = '' |
36 | 48 |
|
37 | 49 | def _get_definition_type(self, definition): |
38 | 50 | is_built_in = definition.in_builtin_module |
@@ -533,13 +545,33 @@ def _set_request_config(self, config): |
533 | 545 | if path and path not in sys.path: |
534 | 546 | sys.path.insert(0, path) |
535 | 547 |
|
| 548 | + def _normalize_request_path(self, request): |
| 549 | + """Normalize any Windows paths received by a *nix build of |
| 550 | + Python. Does not alter the reverse os.path.sep=='\\', |
| 551 | + i.e. *nix paths received by a Windows build of Python. |
| 552 | + """ |
| 553 | + if 'path' in request: |
| 554 | + if not self.drive_mount: |
| 555 | + return |
| 556 | + newPath = request['path'].replace('\\', '/') |
| 557 | + if newPath[0:1] == '/': |
| 558 | + # is absolute path with no drive letter |
| 559 | + request['path'] = newPath |
| 560 | + elif newPath[1:2] == ':': |
| 561 | + # is path with drive letter, only absolute can be mapped |
| 562 | + request['path'] = self.drive_mount + newPath[0:1].lower() + newPath[2:] |
| 563 | + else: |
| 564 | + # is relative path |
| 565 | + request['path'] = newPath |
| 566 | + |
536 | 567 | def _process_request(self, request): |
537 | 568 | """Accept serialized request from VSCode and write response. |
538 | 569 | """ |
539 | 570 | request = self._deserialize(request) |
540 | 571 |
|
541 | 572 | self._set_request_config(request.get('config', {})) |
542 | 573 |
|
| 574 | + self._normalize_request_path(request) |
543 | 575 | path = self._get_top_level_module(request.get('path', '')) |
544 | 576 | if path not in sys.path: |
545 | 577 | sys.path.insert(0, path) |
|
0 commit comments