From 2118e9b2df91f7c5f28cfda48519f6457b8c6172 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Mon, 1 Jan 2024 20:24:06 +0100 Subject: [PATCH 1/2] heex parsing parse html-eex with phoenix eex engine --- .../lib/language_server/parser.ex | 82 ++++++++++++++++--- 1 file changed, 72 insertions(+), 10 deletions(-) diff --git a/apps/language_server/lib/language_server/parser.ex b/apps/language_server/lib/language_server/parser.ex index 481e50414..094cb100d 100644 --- a/apps/language_server/lib/language_server/parser.ex +++ b/apps/language_server/lib/language_server/parser.ex @@ -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 @@ -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 -> @@ -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} From efeb9f27c6542028ff909af03087c5f2ddd701a0 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 2 Jan 2024 19:09:07 +0100 Subject: [PATCH 2/2] update comment --- apps/language_server/lib/language_server/parser.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/language_server/lib/language_server/parser.ex b/apps/language_server/lib/language_server/parser.ex index 094cb100d..773f2ccbd 100644 --- a/apps/language_server/lib/language_server/parser.ex +++ b/apps/language_server/lib/language_server/parser.ex @@ -562,7 +562,7 @@ defmodule ElixirLS.LanguageServer.Parser do ) Code.ensure_loaded?(Phoenix.LiveView.HTMLEngine) -> - # LV <= 0.17 + # LV <= 0.18.17 EEx.compile_string(text, file: file, parser_options: parser_options,