Skip to content

Commit

Permalink
heex parsing
Browse files Browse the repository at this point in the history
parse html-eex with phoenix eex engine
  • Loading branch information
lukaszsamson committed Jan 1, 2024
1 parent 46c8766 commit 2118e9b
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions apps/language_server/lib/language_server/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ defmodule ElixirLS.LanguageServer.Parser do
end

defp should_parse?(uri, source_file) do
String.ends_with?(uri, [".ex", ".exs", ".eex"]) or
source_file.language_id in ["elixir", "eex", "html-eex"]
String.ends_with?(uri, [".ex", ".exs", ".eex", ".heex"]) or
source_file.language_id in ["elixir", "eex", "html-eex", "phoenix-heex"]
end

defp maybe_fix_missing_env(%Context{} = file, nil), do: file
Expand Down Expand Up @@ -506,6 +506,16 @@ defmodule ElixirLS.LanguageServer.Parser do
(is_binary(file) and String.ends_with?(file, ".eex")) or language_id in ["eex", "html-eex"]
end

defp html_eex?(file, language_id) do
(is_binary(file) and
(String.ends_with?(file, ".html.eex") or String.ends_with?(file, ".htm.eex"))) or
language_id in ["html-eex"]
end

defp heex?(file, language_id) do
(is_binary(file) and String.ends_with?(file, ".heex")) or language_id in ["phoenix-heex"]
end

defp parse_file(text, file, language_id) do
{result, raw_diagnostics} =
Build.with_diagnostics([log: false], fn ->
Expand All @@ -517,18 +527,70 @@ defmodule ElixirLS.LanguageServer.Parser do
]

ast =
if eex?(file, language_id) do
EEx.compile_string(text,
file: file,
parser_options: parser_options
)
else
Code.string_to_quoted!(text, parser_options)
cond do
eex?(file, language_id) ->
EEx.compile_string(text,
file: file,
parser_options: parser_options
)

html_eex?(file, language_id) ->
if Code.ensure_loaded?(Phoenix.HTML.Engine) do
EEx.compile_string(text,
file: file,
parser_options: parser_options,
engine: Phoenix.HTML.Engine
)
else
EEx.compile_string(text,
file: file,
parser_options: parser_options
)
end

heex?(file, language_id) ->
cond do
Code.ensure_loaded?(Phoenix.LiveView.TagEngine) ->
# LV 0.18+
EEx.compile_string(text,
file: file,
parser_options: parser_options,
source: text,
caller: __ENV__,
engine: Phoenix.LiveView.TagEngine,
tag_handler: Phoenix.LiveView.HTMLEngine
)

Code.ensure_loaded?(Phoenix.LiveView.HTMLEngine) ->
# LV <= 0.17
EEx.compile_string(text,
file: file,
parser_options: parser_options,
source: text,
caller: __ENV__,
engine: Phoenix.LiveView.HTMLEngine
)

true ->
EEx.compile_string(text,
file: file,
parser_options: parser_options
)
end

true ->
Code.string_to_quoted!(text, parser_options)
end

{:ok, ast}
rescue
e in [EEx.SyntaxError, SyntaxError, TokenMissingError, MismatchedDelimiterError] ->
e in [
EEx.SyntaxError,
SyntaxError,
TokenMissingError,
MismatchedDelimiterError,
Phoenix.LiveView.Tokenizer.ParseError
] ->
diagnostic = Diagnostics.from_error(:error, e, __STACKTRACE__, file, :no_stacktrace)

{:error, diagnostic}
Expand Down

0 comments on commit 2118e9b

Please sign in to comment.