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 5dcdb70
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,25 @@ defmodule Sourceror.Range do
start_position = Sourceror.get_start_position(quoted)
end_position = Sourceror.get_end_position(quoted)

dbg(end_position)

Check failure on line 474 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.12.3, 24.3, true)

** (CompileError) lib/sourceror/range.ex:474: undefined function dbg/1

Check failure on line 474 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 24.3)

** (CompileError) lib/sourceror/range.ex:474: undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

Check failure on line 474 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 25.0.2)

** (CompileError) lib/sourceror/range.ex:474: undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

dbg(meta)

Check warning on line 476 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.12.3, 24.3, true)

undefined function dbg/1

Check warning on line 476 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 24.3)

undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

Check warning on line 476 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 25.0.2)

undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

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

dbg(end_position)

Check warning on line 491 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.12.3, 24.3, true)

undefined function dbg/1

Check warning on line 491 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 24.3)

undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

Check warning on line 491 in lib/sourceror/range.ex

View workflow job for this annotation

GitHub Actions / ci (1.13.4, 25.0.2)

undefined function dbg/1 (expected Sourceror.Range to define such a function or for it to be imported, but none are available)

%{start: start_position, end: end_position}
end

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 5dcdb70

Please sign in to comment.