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

Correctly get range of do/end blocks that have eoe #122

Merged
merged 3 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
42 changes: 42 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,48 @@ 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
{:bar, _, _} -> true
_ -> false
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
Loading