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
14 changes: 14 additions & 0 deletions lib/makeup/lexers/elixir_lexer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,20 @@ defmodule Makeup.Lexers.ElixirLexer do
do: [{:comment, attrs, text} | postprocess_helper(tokens)]

# Custom sigil lexers
# special case: sigil inside iex prompt
defp postprocess_helper([
{:generic_prompt, %{language: :elixir}, ["iex" | _]} = first,
# in the iex case, we don't get the whole sigil content in one token
{:string_sigil, _, ["~", _sigil_char, _separator]} = second | rest
]) do
# we don't handle this case, because it would require some special inside
# and outside lexing, similar to what makeup_eex is doing
{extra_tokens, [end_sigil | rest]} =
Enum.split_while(rest, fn token -> not match?({:string_sigil, _, _}, token) end)

[first, second | extra_tokens ++ [end_sigil | 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]
sigil =
Expand Down
5 changes: 4 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ defmodule MakeupElixir.Mixfile do
deps: deps(),
package: package(),
description: description(),
aliases: aliases()
aliases: aliases(),
test_ignore_filters: [
&String.starts_with?(&1, "test/generators/")
]
]
end

Expand Down
24 changes: 24 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 @@ -20,5 +20,29 @@ defmodule ElixirLexerSigilLexerTest do
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)

# if it tried to use the sigil lexer, it would crash
assert lex("""
iex> import Pythonx
iex> x = 1
iex> ~PY\"""
...> y = 10
...> x + y
...> \"""
#Pythonx.Object<
11
>
iex> x
1
iex> y
#Pythonx.Object<
10
>
""")
end
end
end