Skip to content

Commit

Permalink
Fix heredocs ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
doorgan committed May 31, 2024
1 parent ac5e59d commit 3163863
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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'»"
Expand Down

0 comments on commit 3163863

Please sign in to comment.