Skip to content

Commit 1a8241e

Browse files
committed
refactor: improvements on cursor position to selection conversion
1 parent 78a9264 commit 1a8241e

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

lib/refactorex.ex

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,19 @@ defmodule Refactorex do
143143

144144
case Parser.parse_inputs(original, range) do
145145
{:ok, zipper, selection} ->
146-
if Refactor.rename_available?(zipper, selection),
147-
do: {:ok, Response.send_rename_range(range)},
148-
else: {:ok, nil}
146+
cond do
147+
not Refactor.rename_available?(zipper, selection) ->
148+
{:ok, nil}
149+
150+
match?({:@, _, _}, selection) ->
151+
# this is done so that the placeholder
152+
# for RenameConstant doesn't include the @
153+
range = update_in(range.start.character, &(&1 + 1))
154+
{:ok, Response.send_rename_range(range)}
155+
156+
true ->
157+
{:ok, Response.send_rename_range(range)}
158+
end
149159

150160
{:error, :parse_error} ->
151161
{:ok, nil}

lib/refactorex/parser.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ defmodule Refactorex.Parser do
2727
|> String.split(~r/(?<!\\)\n/)
2828
|> Enum.at(line)
2929
|> String.split("")
30-
|> Enum.split(character)
30+
|> Enum.split(character + 1)
3131

3232
%{
3333
start: %{
3434
line: line,
35-
character: character - 1 - count_while_name(Enum.reverse(left))
35+
character: character - count_while_name(Enum.reverse(left))
3636
},
3737
end: %{
3838
line: line,
39-
character: character - 1 + count_while_name(right)
39+
character: character + count_while_name(right)
4040
}
4141
}
4242
end
@@ -89,7 +89,7 @@ defmodule Refactorex.Parser do
8989

9090
defp count_while_name(characters) do
9191
Enum.reduce_while(characters, 0, fn i, count ->
92-
if String.match?(i, ~r/^[a-zA-Z0-9_?!]+$/),
92+
if String.match?(i, ~r/^[a-zA-Z0-9_?!@]+$/),
9393
do: {:cont, count + 1},
9494
else: {:halt, count}
9595
end)

test/parser_test.exs

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,66 @@ defmodule Refactorex.ParserTest do
44
alias Refactorex.Parser
55

66
describe "position_to_range/2" do
7-
test "parse the selected node when passed a position" do
7+
test "parse the selected node when passed a position in the beginning" do
88
original = """
99
defmodule Foo do
10+
# v
1011
def do_bar?(arg) do
1112
end
1213
end
1314
"""
1415

15-
position = %{line: 1, character: 10}
16+
%{start: %{line: 2, character: 6} = position} = range_from_markers(original)
1617

1718
assert %{
18-
start: %{line: 1, character: 6},
19-
end: %{line: 1, character: 13}
19+
start: %{line: 2, character: 6},
20+
end: %{line: 2, character: 13}
21+
} = range = Parser.position_to_range(original, position)
22+
23+
assert {
24+
:ok,
25+
%Sourceror.Zipper{},
26+
{:do_bar?, _, nil}
27+
} = Parser.parse_inputs(original, range)
28+
end
29+
30+
test "parse the selected node when passed a position in the middle" do
31+
original = """
32+
defmodule Foo do
33+
# v
34+
def do_bar?(arg) do
35+
end
36+
end
37+
"""
38+
39+
%{start: %{line: 2, character: 7} = position} = range_from_markers(original)
40+
41+
assert %{
42+
start: %{line: 2, character: 6},
43+
end: %{line: 2, character: 13}
44+
} = range = Parser.position_to_range(original, position)
45+
46+
assert {
47+
:ok,
48+
%Sourceror.Zipper{},
49+
{:do_bar?, _, nil}
50+
} = Parser.parse_inputs(original, range)
51+
end
52+
53+
test "parse the selected node when passed a position in the end" do
54+
original = """
55+
defmodule Foo do
56+
# v
57+
def do_bar?(arg) do
58+
end
59+
end
60+
"""
61+
62+
%{start: %{line: 2, character: 13} = position} = range_from_markers(original)
63+
64+
assert %{
65+
start: %{line: 2, character: 6},
66+
end: %{line: 2, character: 13}
2067
} = range = Parser.position_to_range(original, position)
2168

2269
assert {

test/refactorex_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ defmodule RefactorexTest do
224224
jsonrpc: "2.0",
225225
params: %{
226226
textDocument: %{uri: file_uri},
227-
position: %{line: 1, character: 5}
227+
position: %{line: 1, character: 2}
228228
}
229229
})
230230

@@ -245,7 +245,7 @@ defmodule RefactorexTest do
245245
params: %{
246246
newName: "bar",
247247
textDocument: %{uri: file_uri},
248-
position: %{line: 1, character: 5}
248+
position: %{line: 1, character: 2}
249249
}
250250
})
251251

0 commit comments

Comments
 (0)