Skip to content

Commit

Permalink
Handle windows paths in workspace.get_uri_like + test (#87)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tomv564 authored and gatesn committed Aug 7, 2017
1 parent 396693c commit a78885d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pyls/plugins/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion pyls/plugins/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
7 changes: 6 additions & 1 deletion pyls/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -181,5 +181,10 @@ def get_uri_like(doc_uri, path):
unicode objects.
"""
parts = list(urlparse(doc_uri))
if path[0] != '/' and ':' in path: # fix path for windows
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])
4 changes: 4 additions & 0 deletions test/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:/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


def test_non_root_project(pyls):
Expand Down

0 comments on commit a78885d

Please sign in to comment.