Skip to content

Commit

Permalink
Fix edge case for code actions
Browse files Browse the repository at this point in the history
Resolves #96
  • Loading branch information
pappasam committed Mar 20, 2021
1 parent e0dc207 commit d1b316b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
9 changes: 8 additions & 1 deletion jedi_language_server/text_edit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,20 @@ def get_opcode_position_lookup(
structure within which one can look up opcode-friendly values. It
relies on the `RangeDict` above, which lets you look up a value
within a range of linear values
Range contains padding at the start and the end to accommodate edge cases
within editors.
"""
original_lines = code.splitlines(keepends=True)
line_lookup = RangeDict()
last_line_number = len(original_lines) - 1
start = 0
for line, code_line in enumerate(original_lines):
end = start + len(code_line)
key = range(start, end)
key = range(
start if line != 0 else start - 1,
end if line != last_line_number else end + 1,
)
line_lookup[key] = LinePosition(start, end, line, code_line)
start = end
return line_lookup
51 changes: 51 additions & 0 deletions tests/lsp_tests/test_refactoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,54 @@ def test_lsp_code_action() -> None:
actual[1]["edit"]["documentChanges"][0]["edits"] = []

assert_that(actual, is_(expected))


def test_lsp_code_action2() -> None:
"""Tests edge case for code actions.
Identified in: https://github.com/pappasam/jedi-language-server/issues/96
"""

with session.LspSession() as ls_session:
ls_session.initialize()
uri = as_uri((REFACTOR_TEST_ROOT / "code_action_test2.py"))
actual = ls_session.text_document_code_action(
{
"textDocument": {"uri": uri},
"range": {
"start": {"line": 2, "character": 6},
"end": {"line": 2, "character": 6},
},
"context": {"diagnostics": []},
}
)

expected = [
{
"title": StringPattern(
r"Extract expression into function 'func_\w+'"
),
"kind": "refactor.extract",
"edit": {
"documentChanges": [
{
"textDocument": {
"uri": uri,
"version": 0,
},
"edits": [],
}
]
},
},
]

# Cannot use hamcrest directly for this due to unpredictable
# variations in how the text edits are generated.

assert_that(len(actual), is_(len(expected)))

# Remove the edits
actual[0]["edit"]["documentChanges"][0]["edits"] = []

assert_that(actual, is_(expected))
3 changes: 3 additions & 0 deletions tests/test_data/refactoring/code_action_test2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import random

random.gauss

0 comments on commit d1b316b

Please sign in to comment.