Skip to content

Commit

Permalink
Correctly get range of do/end blocks that have eoe
Browse files Browse the repository at this point in the history
  • Loading branch information
mhanberg committed Mar 10, 2024
1 parent 52ba8f2 commit d2031d0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
15 changes: 10 additions & 5 deletions lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,16 @@ defmodule Sourceror.Range do
end_position = Sourceror.get_end_position(quoted)

end_position =
if Keyword.has_key?(meta, :end) do
Keyword.update!(end_position, :column, &(&1 + 3))
else
# If it doesn't have an end token, then it has either a ), a ] or a }
Keyword.update!(end_position, :column, &(&1 + 1))
cond do
Keyword.has_key?(meta, :end_of_expression) ->
Keyword.update!(end_position, :column, &(&1 + 1))

Keyword.has_key?(meta, :end) ->
Keyword.update!(end_position, :column, &(&1 + 3))

true ->
# If it doesn't have an end token, then it has either a ), a ] or a }
Keyword.update!(end_position, :column, &(&1 + 1))
end

%{start: start_position, end: end_position}
Expand Down
47 changes: 47 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,53 @@ defmodule SourcerorTest.RangeTest do
|> String.trim_trailing()
end

test "do/end blocks that also have :end_of_expression" do
alias Sourceror.Zipper, as: Z

code = ~S"""
foo do
x ->
bar do
a -> b
c, d -> e
end
:foo
end
"""

value =
code
|> Sourceror.parse_string!()
|> Z.zip()
|> Z.find(fn node ->
case node do
{:bar, _, _} ->
true

_ ->
false
end
end)
|> Z.node()

range = Sourceror.Range.get_range(value)

assert decorate(code, range) ==
~S"""
foo do
x ->
«bar do
a -> b
c, d -> e
end»
:foo
end
"""
|> String.trim_trailing()
end

test "stabs" do
alias Sourceror.Zipper, as: Z

Expand Down

0 comments on commit d2031d0

Please sign in to comment.