From bafce03d1dad89d90465b00a444bb9fd5c5e45d1 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 7 Aug 2017 14:13:30 +0200 Subject: [PATCH 1/4] Handle windows paths in workspace.get_uri_like + test --- pyls/workspace.py | 2 ++ test/test_workspace.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/pyls/workspace.py b/pyls/workspace.py index 10e6f500..f35f07b9 100644 --- a/pyls/workspace.py +++ b/pyls/workspace.py @@ -181,5 +181,7 @@ def get_uri_like(doc_uri, path): unicode objects. """ parts = list(urlparse(doc_uri)) + if path[0] != '/': # fix path for windows + path = '/' + path.replace('\\', '/') parts[2] = path return urlunparse([str(p) for p in parts]) diff --git a/test/test_workspace.py b/test/test_workspace.py index 37cbf2db..70083da3 100644 --- a/test/test_workspace.py +++ b/test/test_workspace.py @@ -36,6 +36,10 @@ def test_bad_get_document(pyls): def test_uri_like(): assert workspace.get_uri_like('file:///some-path', '/my/path') == 'file:///my/path' + win_doc_uri = r'file:///D:/helloworld.py' + win_doc_path = r'D:\helloworld.py' + win_uri = workspace.get_uri_like(win_doc_uri, win_doc_path) + assert win_uri == win_doc_uri def test_non_root_project(pyls): From 413cf2c59165be02d1f256db9187fb27d26c89d9 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 7 Aug 2017 14:36:03 +0200 Subject: [PATCH 2/4] Guard against module_path=None being passed into get_uri_like The assumption is that the definition points to the current file instead --- pyls/plugins/definition.py | 3 ++- pyls/plugins/references.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pyls/plugins/definition.py b/pyls/plugins/definition.py index cd1e4dd3..0e79a795 100644 --- a/pyls/plugins/definition.py +++ b/pyls/plugins/definition.py @@ -12,8 +12,9 @@ def pyls_definitions(document, position): d for d in definitions if d.is_definition() and d.line is not None and d.column is not None ] + return [{ - 'uri': workspace.get_uri_like(document.uri, d.module_path), + 'uri': workspace.get_uri_like(document.uri, d.module_path) if d.module_path else document.uri, 'range': { 'start': {'line': d.line - 1, 'character': d.column}, 'end': {'line': d.line - 1, 'character': d.column + len(d.name)} diff --git a/pyls/plugins/references.py b/pyls/plugins/references.py index ad338bdf..c77e26c9 100644 --- a/pyls/plugins/references.py +++ b/pyls/plugins/references.py @@ -15,7 +15,7 @@ def pyls_references(document, position, exclude_declaration=False): usages = [d for d in usages if not d.is_definition()] return [{ - 'uri': workspace.get_uri_like(document.uri, d.module_path), + 'uri': workspace.get_uri_like(document.uri, d.module_path) if d.module_path else document.uri, 'range': { 'start': {'line': d.line - 1, 'character': d.column}, 'end': {'line': d.line - 1, 'character': d.column + len(d.name)} From a21a5fa8126c11fe8087248e4e65d74519a4de20 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 7 Aug 2017 16:12:00 +0200 Subject: [PATCH 3/4] Also quote outgoing uris --- pyls/workspace.py | 7 +++++-- test/test_workspace.py | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyls/workspace.py b/pyls/workspace.py index f35f07b9..7ece3900 100644 --- a/pyls/workspace.py +++ b/pyls/workspace.py @@ -4,7 +4,7 @@ import os import re import sys -from urllib.parse import urlparse, urlunparse, unquote +from urllib.parse import urlparse, urlunparse, quote, unquote import jedi @@ -182,6 +182,9 @@ def get_uri_like(doc_uri, path): """ parts = list(urlparse(doc_uri)) if path[0] != '/': # fix path for windows - path = '/' + path.replace('\\', '/') + drivespec, path = path.split(':', 1) + path = '/' + drivespec + ':' + quote(path.replace('\\', '/')) + else: + path = quote(path) parts[2] = path return urlunparse([str(p) for p in parts]) diff --git a/test/test_workspace.py b/test/test_workspace.py index 70083da3..a66c1927 100644 --- a/test/test_workspace.py +++ b/test/test_workspace.py @@ -36,8 +36,8 @@ def test_bad_get_document(pyls): def test_uri_like(): assert workspace.get_uri_like('file:///some-path', '/my/path') == 'file:///my/path' - win_doc_uri = r'file:///D:/helloworld.py' - win_doc_path = r'D:\helloworld.py' + win_doc_uri = r'file:///D:/hello%20world.py' + win_doc_path = r'D:\hello world.py' win_uri = workspace.get_uri_like(win_doc_uri, win_doc_path) assert win_uri == win_doc_uri From 4947f799e2aff6ebfec92546599bd23c813e9a48 Mon Sep 17 00:00:00 2001 From: Tom van Ommeren Date: Mon, 7 Aug 2017 23:08:13 +0200 Subject: [PATCH 4/4] Make sure a colon exists before trying to split a windows path --- pyls/workspace.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyls/workspace.py b/pyls/workspace.py index 7ece3900..4d41418b 100644 --- a/pyls/workspace.py +++ b/pyls/workspace.py @@ -181,7 +181,7 @@ def get_uri_like(doc_uri, path): unicode objects. """ parts = list(urlparse(doc_uri)) - if path[0] != '/': # fix path for windows + if path[0] != '/' and ':' in path: # fix path for windows drivespec, path = path.split(':', 1) path = '/' + drivespec + ':' + quote(path.replace('\\', '/')) else: