Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heex parse #1047

Merged
merged 2 commits into from
Jan 5, 2024
Merged
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
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.18.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
Loading