Skip to content
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
7 changes: 5 additions & 2 deletions lib/makeup/lexers/elixir_lexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,11 @@ defmodule Makeup.Lexers.ElixirLexer do
[newline, first | postprocess_helper(rest)]
end

defp postprocess_helper([{:string_sigil, attrs, content} | tokens]) do
# content is a list of the format ["~", sigil_char, separator, ... sigil_content ..., end_separator]
defp postprocess_helper([{:string_sigil, attrs, ["~" | _] = content} | tokens]) do
# content is a list of the format ["~", sigil_char, separator, ... sigil_content ..., end_separator].
# A sigil containing interpolation is split into multiple :string_sigil tokens,
# of which only the first starts with "~"; the rest are plain sigil content
# and fall through to the catch-all clause.
sigil =
content
|> Enum.at(1)
Expand Down
23 changes: 23 additions & 0 deletions test/makeup/lexers/elixir_lexer/elixir_lexer_sigil_lexer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ defmodule ElixirLexerSigilLexerTest do
Application.put_env(:makeup_elixir, :sigil_lexers, %{})
end

test "does not treat continuation tokens of interpolated sigils as sigils" do
defmodule PassthroughLexer do
def lex(input, opts) do
[{:keyword, %{opts: opts}, input}]
end
end

Makeup.Lexers.ElixirLexer.register_sigil_lexer("H", PassthroughLexer)

# A sigil containing interpolation is split into multiple :string_sigil
# tokens. The `H` at index 1 of the continuation token `/HEAD"` must not
# be mistaken for a ~H sigil.
assert lex(~S[~w"refs/remotes/#{remote}/HEAD"]) == [
{:string_sigil, %{}, "~w\"refs/remotes/"},
{:string_interpol, %{group_id: "group-1"}, "\#{"},
{:name, %{}, "remote"},
{:string_interpol, %{group_id: "group-1"}, "}"},
{:string_sigil, %{}, "/HEAD\""}
]
after
Application.put_env(:makeup_elixir, :sigil_lexers, %{})
end

test "does not lex inside iex prompts" do
# does not exist, but should also not be invoked
Makeup.Lexers.ElixirLexer.register_sigil_lexer("PY", DoesNotExist, foo: :bar)
Expand Down
Loading