File tree Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Expand file tree Collapse file tree 3 files changed +40
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Copyright 2017 Palantir Technologies, Inc.
2
2
import logging
3
+ import json
3
4
from pyls import hookimpl , _utils
4
5
5
6
log = logging .getLogger (__name__ )
@@ -17,4 +18,9 @@ def pyls_hover(document, position):
17
18
# :(
18
19
return {'contents' : '' }
19
20
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
+ }
Original file line number Diff line number Diff line change @@ -178,6 +178,28 @@ def offset_at_position(self, position):
178
178
"""Return the byte-offset pointed at by the given position."""
179
179
return position ['character' ] + len ('' .join (self .lines [:position ['line' ]]))
180
180
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
+
181
203
def word_at_position (self , position ):
182
204
"""Get the word under the cursor returning the start and end positions."""
183
205
if position ['line' ] >= len (self .lines ):
Original file line number Diff line number Diff line change @@ -21,7 +21,17 @@ def test_hover():
21
21
doc = Document (DOC_URI , DOC )
22
22
23
23
assert {
24
- 'contents' : 'main()\n \n hello world'
24
+ 'contents' : 'main()\n \n hello world' ,
25
+ 'range' : {
26
+ 'start' : {
27
+ 'line' : 2 ,
28
+ 'character' : 4 ,
29
+ },
30
+ 'end' : {
31
+ 'line' : 2 ,
32
+ 'character' : 8 ,
33
+ },
34
+ },
25
35
} == pyls_hover (doc , hov_position )
26
36
27
37
assert {'contents' : '' } == pyls_hover (doc , no_hov_position )
You can’t perform that action at this time.
0 commit comments