Skip to content

Commit

Permalink
Fix get_range/1 for binaries (#120)
Browse files Browse the repository at this point in the history
* Add test showing failure

* Fix bug regarding delimiters in strings

It seems escaped double quotes are not counted in `Sourceror.get_range/1`:

```elixir
~S"""
"key: \"value\""
"""
|> Sourceror.parse_string!()
|> Sourceror.get_range()
|> dbg
```
  • Loading branch information
rrrene authored Jan 18, 2024
1 parent f8617bc commit 8ebc68b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/sourceror/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ defmodule Sourceror.Range do
if meta[:delimiter] in [~S/"""/, ~S/'''/] do
meta[:column] + String.length(meta[:delimiter])
else
count = meta[:column] + String.length(last_line) + String.length(meta[:delimiter])
delimiter_count =
if String.contains?(string, meta[:delimiter]) do
~r/#{meta[:delimiter]}/ |> Regex.scan(string) |> length()
else
0
end

count =
meta[:column] + String.length(last_line) + String.length(meta[:delimiter]) +
delimiter_count

if end_line == meta[:line] do
count + 1
Expand Down
3 changes: 3 additions & 0 deletions test/range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ defmodule SourcerorTest.RangeTest do
code = ~S/"fo\no"/
assert decorate(code, to_range(code)) == \"fo\\no\"»"

code = ~S/"key: \"value\""/
assert decorate(code, to_range(code)) == \"key: \\\"value\\\"\"»"

code = ~S'''
"""
foo
Expand Down

0 comments on commit 8ebc68b

Please sign in to comment.