8000 Implement 'range' property for hover response (#485) · palantir/python-language-server@d318fb3 · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit d318fb3

Browse files
vhakulinengatesn
authored andcommitted
Implement 'range' property for hover response (#485)
1 parent dace6e6 commit d318fb3

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

pyls/plugins/hover.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright 2017 Palantir Technologies, Inc.
22
import logging
3+
import json
34
from pyls import hookimpl, _utils
45

56
log = logging.getLogger(__name__)
@@ -17,4 +18,9 @@ def pyls_hover(document, position):
1718
# :(
1819
return {'contents': ''}
1920

20-
return {'contents': _utils.format_docstring(definitions[0].docstring()) or ""}
21+
pos = document.word_range_at_position(position)
22+
23+
return {
24+
'contents': _utils.format_docstring(definitions[0].docstring()) or "",
25+
'range': pos,
26+
}

pyls/workspace.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ def offset_at_position(self, position):
178178
"""Return the byte-offset pointed at by the given position."""
179179
return position['character'] + len(''.join(self.lines[:position['line']]))
180180

181+
def word_range_at_position(self, position):
182+
"""Get the range of a word at given position."""
183+
if position['line'] >= len(self.lines):
184+
return None
185+
186+
line = self.lines[position['line']]
187+
i = position['character']
188+
# Split word in two
189+
start = line[:i]
190+
end = line[i:]
191+
192+
# Take end of start and start of end to find word
193+
# These are guaranteed to match, even if they match the empty string
194+
m_start = RE_START_WORD.findall(start)
195+
m_end = RE_END_WORD.findall(end)
196+
197+
start = { 'line': position['line'], 'character': i - len(m_start[0]) }
198+
end = { 'line': position['line'], 'character': i + len(m_end[-1]) }
199+
200+
return { 'start': start, 'end': end }
201+
202+
181203
def word_at_position(self, position):
182204
"""Get the word under the cursor returning the start and end positions."""
183205
if position['line'] >= len(self.lines):

test/plugins/test_hover.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ def test_hover():
2121
doc = Document(DOC_URI, DOC)
2222

2323
assert {
24-
'contents': 'main()\n\nhello world'
24+
'contents': 'main()\n\nhello world',
25+
'range': {
26+
'start': {
27+
'line': 2,
28+
'character': 4,
29+
},
30+
'end': {
31+
'line': 2,
32+
'character': 8,
33+
},
34+
},
2535
} == pyls_hover(doc, hov_position)
2636

2737
assert {'contents': ''} == pyls_hover(doc, no_hov_position)

0 commit comments

Comments
 (0)
0