Skip to content

Commit a78885d

Browse files
tomv564gatesn
authored andcommitted
Handle windows paths in workspace.get_uri_like + test (#87)
* Handle windows paths in workspace.get_uri_like + test * Guard against module_path=None being passed into get_uri_like The assumption is that the definition points to the current file instead * Also quote outgoing uris * Make sure a colon exists before trying to split a windows path
1 parent 396693c commit a78885d

File tree

4 files changed

+13
-3
lines changed

4 files changed

+13
-3
lines changed

pyls/plugins/definition.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def pyls_definitions(document, position):
1212
d for d in definitions
1313
if d.is_definition() and d.line is not None and d.column is not None
1414
]
15+
1516
return [{
16-
'uri': workspace.get_uri_like(document.uri, d.module_path),
17+
'uri': workspace.get_uri_like(document.uri, d.module_path) if d.module_path else document.uri,
1718
'range': {
1819
'start': {'line': d.line - 1, 'character': d.column},
1920
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}

pyls/plugins/references.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def pyls_references(document, position, exclude_declaration=False):
1515
usages = [d for d in usages if not d.is_definition()]
1616

1717
return [{
18-
'uri': workspace.get_uri_like(document.uri, d.module_path),
18+
'uri': workspace.get_uri_like(document.uri, d.module_path) if d.module_path else document.uri,
1919
'range': {
2020
'start': {'line': d.line - 1, 'character': d.column},
2121
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}

pyls/workspace.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55
import re
66
import sys
7-
from urllib.parse import urlparse, urlunparse, unquote
7+
from urllib.parse import urlparse, urlunparse, quote, unquote
88

99
import jedi
1010

@@ -181,5 +181,10 @@ def get_uri_like(doc_uri, path):
181181
unicode objects.
182182
"""
183183
parts = list(urlparse(doc_uri))
184+
if path[0] != '/' and ':' in path: # fix path for windows
185+
drivespec, path = path.split(':', 1)
186+
path = '/' + drivespec + ':' + quote(path.replace('\\', '/'))
187+
else:
188+
path = quote(path)
184189
parts[2] = path
185190
return urlunparse([str(p) for p in parts])

test/test_workspace.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def test_bad_get_document(pyls):
3636

3737
def test_uri_like():
3838
assert workspace.get_uri_like('file:///some-path', '/my/path') == 'file:///my/path'
39+
win_doc_uri = r'file:///D:/hello%20world.py'
40+
win_doc_path = r'D:\hello world.py'
41+
win_uri = workspace.get_uri_like(win_doc_uri, win_doc_path)
42+
assert win_uri == win_doc_uri
3943

4044

4145
def test_non_root_project(pyls):

0 commit comments

Comments
 (0)