Skip to content

Commit

Permalink
current_word_range function added to pygls_utils
Browse files Browse the repository at this point in the history
Makes it possible to determine the range of the word under the cursor.
  • Loading branch information
pappasam committed May 17, 2020
1 parent 73b9522 commit d66c402
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion jedi_language_server/pygls_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@


import functools
from typing import Optional

from pygls.types import Position
from pygls.types import Position, Range
from pygls.workspace import Document

_SENTINEL = object()
Expand Down Expand Up @@ -58,3 +59,25 @@ def char_before_cursor(
return document.lines[position.line][position.character - 1]
except IndexError:
return default


def current_word_range(
document: Document, position: Position
) -> Optional[Range]:
"""Get the range of the word under the cursor"""
word = document.word_at_position(position)
word_len = len(word)
line: str = document.lines[position.line]
start = 0
for _ in range(1000): # prevent infinite hanging in case we hit edge case
begin = line.find(word, start)
if begin == -1:
return None
end = begin + word_len
if begin <= position.character <= end:
return Range(
start=Position(line=position.line, character=begin),
end=Position(line=position.line, character=end),
)
start = end
return None

0 comments on commit d66c402

Please sign in to comment.