Skip to content

Commit 2339b52

Browse files
authored
Heex parse (#1047)
* heex parsing parse html-eex with phoenix eex engine * update comment
1 parent 62415f9 commit 2339b52

File tree

1 file changed

+72
-10
lines changed
  • apps/language_server/lib/language_server

1 file changed

+72
-10
lines changed

apps/language_server/lib/language_server/parser.ex

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ defmodule ElixirLS.LanguageServer.Parser do
319319
end
320320

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

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

509+
defp html_eex?(file, language_id) do
510+
(is_binary(file) and
511+
(String.ends_with?(file, ".html.eex") or String.ends_with?(file, ".htm.eex"))) or
512+
language_id in ["html-eex"]
513+
end
514+
515+
defp heex?(file, language_id) do
516+
(is_binary(file) and String.ends_with?(file, ".heex")) or language_id in ["phoenix-heex"]
517+
end
518+
509519
defp parse_file(text, file, language_id) do
510520
{result, raw_diagnostics} =
511521
Build.with_diagnostics([log: false], fn ->
@@ -517,18 +527,70 @@ defmodule ElixirLS.LanguageServer.Parser do
517527
]
518528

519529
ast =
520-
if eex?(file, language_id) do
521-
EEx.compile_string(text,
522-
file: file,
523-
parser_options: parser_options
524-
)
525-
else
526-
Code.string_to_quoted!(text, parser_options)
530+
cond do
531+
eex?(file, language_id) ->
532+
EEx.compile_string(text,
533+
file: file,
534+
parser_options: parser_options
535+
)
536+
537+
html_eex?(file, language_id) ->
538+
if Code.ensure_loaded?(Phoenix.HTML.Engine) do
539+
EEx.compile_string(text,
540+
file: file,
541+
parser_options: parser_options,
542+
engine: Phoenix.HTML.Engine
543+
)
544+
else
545+
EEx.compile_string(text,
546+
file: file,
547+
parser_options: parser_options
548+
)
549+
end
550+
551+
heex?(file, language_id) ->
552+
cond do
553+
Code.ensure_loaded?(Phoenix.LiveView.TagEngine) ->
554+
# LV 0.18+
555+
EEx.compile_string(text,
556+
file: file,
557+
parser_options: parser_options,
558+
source: text,
559+
caller: __ENV__,
560+
engine: Phoenix.LiveView.TagEngine,
561+
tag_handler: Phoenix.LiveView.HTMLEngine
562+
)
563+
564+
Code.ensure_loaded?(Phoenix.LiveView.HTMLEngine) ->
565+
# LV <= 0.18.17
566+
EEx.compile_string(text,
567+
file: file,
568+
parser_options: parser_options,
569+
source: text,
570+
caller: __ENV__,
571+
engine: Phoenix.LiveView.HTMLEngine
572+
)
573+
574+
true ->
575+
EEx.compile_string(text,
576+
file: file,
577+
parser_options: parser_options
578+
)
579+
end
580+
581+
true ->
582+
Code.string_to_quoted!(text, parser_options)
527583
end
528584

529585
{:ok, ast}
530586
rescue
531-
e in [EEx.SyntaxError, SyntaxError, TokenMissingError, MismatchedDelimiterError] ->
587+
e in [
588+
EEx.SyntaxError,
589+
SyntaxError,
590+
TokenMissingError,
591+
MismatchedDelimiterError,
592+
Phoenix.LiveView.Tokenizer.ParseError
593+
] ->
532594
diagnostic = Diagnostics.from_error(:error, e, __STACKTRACE__, file, :no_stacktrace)
533595

534596
{:error, diagnostic}

0 commit comments

Comments
 (0)