Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Charles/review #11

Merged
merged 13 commits into from
Dec 9, 2023
Prev Previous commit
Next Next commit
add utils tests preparing for refactor
z80dev committed Dec 6, 2023
commit 548868f6e862076ae1725a0b25ef0b838b233cb8
26 changes: 26 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from vyper_lsp import utils


def test_get_word_at_cursor():
text = "self.foo = 123"
assert utils.get_word_at_cursor(text, 0) == "self"
assert utils.get_word_at_cursor(text, 1) == "self"
assert utils.get_word_at_cursor(text, 5) == "foo"
assert utils.get_word_at_cursor(text, 12) == "123"

text = "foo_bar = 123"
assert utils.get_word_at_cursor(text, 0) == "foo_bar"
assert utils.get_word_at_cursor(text, 4) == "foo_bar"


def test_get_expression_at_cursor():
text = "self.foo = 123"
assert utils.get_expression_at_cursor(text, 0) == "self.foo"
assert utils.get_expression_at_cursor(text, 1) == "self.foo"
assert utils.get_expression_at_cursor(text, 5) == "self.foo"
assert utils.get_expression_at_cursor(text, 12) == "123"

text = "foo_bar = self.baz(1,2,3)"
assert utils.get_expression_at_cursor(text, 0) == "foo_bar"
assert utils.get_expression_at_cursor(text, 4) == "foo_bar"
assert utils.get_expression_at_cursor(text, 11) == "self.baz(1"
10 changes: 4 additions & 6 deletions vyper_lsp/utils.py
Original file line number Diff line number Diff line change
@@ -129,16 +129,14 @@ def get_expression_at_cursor(sentence: str, cursor_index: int) -> str:

# Find the start of the word
# REVIEW: maybe sentence[start - 1] in `_WORD_CHARS + ".[]()"`
while (
start > 0
and is_word_char(sentence[start - 1])
or sentence[start - 1] in ".[]()"
while start > 0 and (
is_word_char(sentence[start - 1]) or sentence[start - 1] in ".[]()"
):
start -= 1

# Find the end of the word
while (
end < len(sentence) and is_word_char(sentence[end]) or sentence[end] in ".[]()"
while end < len(sentence) and (
is_word_char(sentence[end]) or sentence[end] in ".[]()"
):
end += 1