8000 Fixes for word_at_position (#182) · python-lsp/python-lsp-server@722b710 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 722b710

Browse files
authored
Fixes for word_at_position (#182)
* Implement sopme fixes for rope completions * Bug fixes for word_at_position
1 parent c4fd7a2 commit 722b710

File tree

5 files changed

+13
-4
lines changed

5 files changed

+13
-4
lines changed

pyls/_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ def race_hooks(hook_caller, pool, **kwargs):
4444
return None
4545

4646
def _apply(impl):
47-
return impl, impl.function(**kwargs)
47+
try:
48+
return impl, impl.function(**kwargs)
49+
except Exception:
50+
log.exception("Failed to run hook %s", impl.plugin_name)
51+
raise
4852

4953
# imap unordered gives us an iterator over the items in the order they finish.
5054
# We have to be careful to set chunksize to 1 to ensure hooks each get their own thread.

pyls/plugins/rope_completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def pyls_completions(document, position):
1616
word = document.word_at_position({
1717
# The -1 should really be trying to look at the previous word, but that might be quite expensive
1818
# So we only skip import completions when the cursor is one space after `import`
19-
'line': position['line'], 'character': position['character'] - 1,
19+
'line': position['line'], 'character': max(position['character'] - 1, 0),
2020
})
2121
if word == 'import':
2222
return None

pyls/workspace.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ def offset_at_position(self, position):
234234

235235
def word_at_position(self, position):
236236
"""Get the word under the cursor returning the start and end positions."""
237+
if position['line'] >= len(self.lines):
238+
return ''
239+
237240
line = self.lines[position['line']]
238241
i = position['character']
239242
# Split word in two

test/test_document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Copyright 2017 Palantir Technologies, Inc.
2-
import os
32
import pytest
43
from pyls import uris
54
from pyls.workspace import Document
@@ -32,6 +31,7 @@ def test_offset_at_position(doc):
3231
assert doc.offset_at_position({'line': 1, 'character': 5}) == 16
3332
assert doc.offset_at_position({'line': 2, 'character': 0}) == 12
3433
assert doc.offset_at_position({'line': 2, 'character': 4}) == 16
34+
assert doc.offset_at_position({'line': 4, 'character': 0}) == 51
3535

3636

3737
def test_word_at_position(doc):
@@ -44,6 +44,8 @@ def test_word_at_position(doc):
4444
assert doc.word_at_position({'line': 1, 'character': 5}) == ''
4545
# def main():
4646
assert doc.word_at_position({'line': 2, 'character': 0}) == 'def'
47+
# Past end of file
48+
assert doc.word_at_position({'line': 4, 'character': 0}) == ''
4749

4850

4951
def test_document_empty_edit():

test/test_workspace.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import os
33
import pytest
4-
from pyls import uris, workspace
4+
from pyls import uris
55

66
DOC_URI = uris.from_fs_path(__file__)
77

0 commit comments

Comments
 (0)
0