From 5dcdb7002dd99138c053fb1f8aefd1e8c19ba5fe Mon Sep 17 00:00:00 2001 From: Mitchell Hanberg Date: Sun, 10 Mar 2024 13:24:44 -0400 Subject: [PATCH] Correctly get range of do/end blocks that have eoe --- lib/sourceror/range.ex | 21 ++++++++++++++----- test/range_test.exs | 47 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/lib/sourceror/range.ex b/lib/sourceror/range.ex index ee12aff..8e0197d 100644 --- a/lib/sourceror/range.ex +++ b/lib/sourceror/range.ex @@ -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) + + dbg(meta) + 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) + %{start: start_position, end: end_position} end diff --git a/test/range_test.exs b/test/range_test.exs index b2f9390..09c90e2 100644 --- a/test/range_test.exs +++ b/test/range_test.exs @@ -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