From 3163863d5f59edab2b7adf6a7e0bc77f85ccc044 Mon Sep 17 00:00:00 2001 From: doorgan Date: Fri, 31 May 2024 01:37:53 -0300 Subject: [PATCH] Fix heredocs ranges --- lib/sourceror/range.ex | 4 +++- test/range_test.exs | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/sourceror/range.ex b/lib/sourceror/range.ex index 5951191..8cf6b3a 100644 --- a/lib/sourceror/range.ex +++ b/lib/sourceror/range.ex @@ -108,7 +108,9 @@ defmodule Sourceror.Range do end_column = if meta[:delimiter] in [~S/"""/, ~S/'''/] do - meta[:column] + String.length(meta[:delimiter]) + delimiter_count = String.length(meta[:delimiter]) + 1 + indentation = meta[:indentation] || 0 + indentation + delimiter_count else delimiter_count = if String.contains?(string, meta[:delimiter]) do diff --git a/test/range_test.exs b/test/range_test.exs index 5c91f6a..62aab59 100644 --- a/test/range_test.exs +++ b/test/range_test.exs @@ -228,6 +228,33 @@ defmodule SourcerorTest.RangeTest do |> String.trim_trailing() end + test "heredocs" do + code = ~S''' + @moduledoc """ + This is my funny moduledoc + """ + ''' + + zipper = + code + |> Sourceror.parse_string!() + |> Sourceror.Zipper.zip() + + heredoc = + zipper + |> Sourceror.Zipper.down() + |> Sourceror.Zipper.down() + |> Sourceror.Zipper.node() + + assert decorate(code, Sourceror.get_range(heredoc)) == + ~S''' + @moduledoc «""" + This is my funny moduledoc + """» + ''' + |> String.trim_trailing() + end + test "charlists" do code = ~S/'foo'/ assert decorate(code, to_range(code)) == "«'foo'»"