Skip to content

Commit

Permalink
skip invalid definition to fix #362 (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixystone authored and gatesn committed Jun 1, 2018
1 parent d8eb65f commit 077e1ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pyls/plugins/highlight.py
Expand Up @@ -9,6 +9,9 @@
def pyls_document_highlight(document, position):
usages = document.jedi_script(position).usages()

def is_valid(definition):
return definition.line is not None and definition.column is not None

def local_to_document(definition):
return not definition.module_path or uris.uri_with(document.uri, path=definition.module_path) == document.uri

Expand All @@ -18,4 +21,4 @@ def local_to_document(definition):
'end': {'line': d.line - 1, 'character': d.column + len(d.name)}
},
'kind': lsp.DocumentHighlightKind.Write if d.is_definition() else lsp.DocumentHighlightKind.Read
} for d in usages if local_to_document(d)]
} for d in usages if is_valid(d) and local_to_document(d)]
24 changes: 24 additions & 0 deletions test/plugins/test_highlight.py
Expand Up @@ -30,3 +30,27 @@ def test_highlight():
# The second usage is Read
'kind': lsp.DocumentHighlightKind.Read
}]


SYS_DOC = '''import sys
print sys.path
'''


def test_sys_highlight():
cursor_pos = {'line': 0, 'character': 8}

doc = Document(DOC_URI, SYS_DOC)
assert pyls_document_highlight(doc, cursor_pos) == [{
'range': {
'start': {'line': 0, 'character': 7},
'end': {'line': 0, 'character': 10}
},
'kind': lsp.DocumentHighlightKind.Write
}, {
'range': {
'start': {'line': 1, 'character': 6},
'end': {'line': 1, 'character': 9}
},
'kind': lsp.DocumentHighlightKind.Read
}]

0 comments on commit 077e1ae

Please sign in to comment.