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

Fix get_range for interpolation nodes #129

Merged
merged 2 commits into from
Apr 12, 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
23 changes: 23 additions & 0 deletions lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,14 @@ defmodule Sourceror.Range do
%{start: start_pos, end: end_pos}
end

# Charlist interpolation node
defp do_get_range({{:., _, [Kernel, :to_string]}, meta, _}) do
start_pos = Keyword.take(meta, [:line, :column])
end_pos = Keyword.update!(meta[:closing], :column, &(&1 + 1))

%{start: start_pos, end: end_pos}
end

# Qualified call
defp do_get_range({{:., _, [_left, right]}, _meta, []} = quoted) when is_atom(right) do
get_range_for_qualified_call_without_arguments(quoted)
Expand Down Expand Up @@ -333,6 +341,21 @@ defmodule Sourceror.Range do
end
end

# Interpolation
defp do_get_range(
{:"::", meta,
[
{{:., _, [Kernel, :to_string]}, end_meta, _},
_
]}
) do
start_pos = [line: meta[:line], column: meta[:column]]

end_pos = Keyword.update!(end_meta[:closing], :column, &(&1 + 1))

%{start: start_pos, end: end_pos}
end

# Binary operators
defp do_get_range({op, _, [left, right]}) when is_binary_op(op) do
get_range_for_pair(left, right)
Expand Down
53 changes: 53 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ defmodule SourcerorTest.RangeTest do
|> String.trim_trailing()
end

test "child of string with interpolations" do
code = ~S"""
"foo#{
2
}bar"
"""

string =
code
|> Sourceror.parse_string!()
|> Sourceror.Zipper.zip()

interpolation =
string |> Sourceror.Zipper.down() |> Sourceror.Zipper.right() |> Sourceror.Zipper.node()

assert decorate(code, Sourceror.get_range(interpolation)) ==
~S"""
"foo«#{
2
}»bar"
"""
|> String.trim_trailing()
end

test "charlists" do
code = ~S/'foo'/
assert decorate(code, to_range(code)) == "«'foo'»"
Expand Down Expand Up @@ -298,6 +322,35 @@ defmodule SourcerorTest.RangeTest do
|> String.trim_trailing()
end

test "child of charlists with interpolations" do
code = ~S"""
'foo#{
2
}bar'
"""

charlist =
code
|> Sourceror.parse_string!()
|> Sourceror.Zipper.zip()

interpolation =
charlist
|> Sourceror.Zipper.down()
|> Sourceror.Zipper.right()
|> Sourceror.Zipper.down()
|> Sourceror.Zipper.right()
|> Sourceror.Zipper.node()

assert decorate(code, Sourceror.get_range(interpolation)) ==
~S"""
'foo«#{
2
}»bar'
"""
|> String.trim_trailing()
end

test "atoms" do
code = ~S/:foo/
assert decorate(code, to_range(code)) == "«:foo»"
Expand Down
Loading