-
-
Notifications
You must be signed in to change notification settings - Fork 72
feat(engine): support shorthand notation inside ~H sigil #278
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
Merged
doorgan
merged 11 commits into
elixir-lang:main
from
katafrakt:support-h-sigil-shorthand
Jan 8, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d716c19
feat(engine): support shorthand notation inside ~H sigil
katafrakt 8b8f0e0
Fix regex for opening and closing tags
katafrakt beb5615
Only trigger HEEx normalization when phoenix_live_view is present in …
katafrakt fe22b39
Rename call as maybe_normalize
katafrakt c599772
Delete fixture app (not used in tests)
katafrakt e166a3f
Revert unnecessary test change
katafrakt 2eea4c5
More test cases
katafrakt d89b1d8
Use Engine.Module.Loader for faster lookups
katafrakt 5158ee6
Use Sourceror.FastZipper
katafrakt 072753e
Simplify tests
katafrakt 83cf8ec
Provide custom module defining sigil_H
katafrakt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
apps/engine/lib/engine/code_intelligence/heex_normalizer.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| defmodule Engine.CodeIntelligence.HeexNormalizer do | ||
| @moduledoc false | ||
|
|
||
| alias Forge.Ast | ||
| alias Forge.Ast.Analysis | ||
| alias Forge.Document | ||
| alias Forge.Document.Position | ||
| alias Forge.Document.Range | ||
| alias Sourceror.FastZipper | ||
|
|
||
| # Matches both opening and closing shorthand components (used for cursor detection) | ||
| @component_regex ~r/<\/?\.([a-zA-Z0-9_!?.]+)/ | ||
| # Separate regexes for AST normalization to avoid overlap issues | ||
| @opening_component_regex ~r/<\.([a-zA-Z0-9_!?.]+)/ | ||
| @closing_component_regex ~r/<\/\.([a-zA-Z0-9_!?.]+)/ | ||
| @opening_replacement "< \\1(assigns)" | ||
| @closing_replacement "</ \\1(assigns)" | ||
|
|
||
| # Normalizes HEEx templates by converting anonymous component references | ||
| # (e.g., `<.component`) to explicit function calls (e.g., `<component(assigns)`). | ||
| # It's done in both the AST and document text. | ||
| # | ||
| # This allows ElixirSense to understand the shorthand HEEX notation as a local function | ||
| # (be it imported or not) and return correct location for go-to-definition and hover. | ||
| # | ||
| # This normalization is only performed when Phoenix.Component is available in the project | ||
| # (i.e., phoenix_live_view is in the dependencies). | ||
| @spec maybe_normalize(Analysis.t(), Position.t()) :: Analysis.t() | ||
| def maybe_normalize(analysis, position) do | ||
| if phoenix_component_available?() do | ||
| new_ast = normalize_ast(analysis, position) | ||
| new_document = normalize_document(analysis, position) | ||
| %{analysis | ast: new_ast, document: new_document} | ||
| else | ||
| analysis | ||
| end | ||
| end | ||
|
|
||
| defp phoenix_component_available? do | ||
| Engine.Module.Loader.ensure_loaded?(Phoenix.Component) | ||
| end | ||
|
|
||
| defp normalize_ast(analysis, position) do | ||
| with {:ok, path} <- Ast.path_at(analysis, position), | ||
| {:sigil_H, _, _} = sigil <- Enum.find(path, &match?({:sigil_H, _, _}, &1)) do | ||
| new_sigil = normalize_heex_node(sigil) | ||
|
|
||
| analysis.ast | ||
| |> FastZipper.zip() | ||
| |> FastZipper.find(&(&1 == sigil)) | ||
| |> case do | ||
| nil -> analysis.ast | ||
| zipper -> zipper |> FastZipper.replace(new_sigil) |> FastZipper.root() | ||
| end | ||
| else | ||
| _ -> analysis.ast | ||
| end | ||
| end | ||
|
|
||
| defp normalize_document(analysis, position) do | ||
| case extract_heex_range(analysis, position) do | ||
| {:ok, _sigil, start_pos, end_pos} -> | ||
| start_pos = Position.new(analysis.document, start_pos[:line], start_pos[:column]) | ||
| end_pos = Position.new(analysis.document, end_pos[:line], end_pos[:column]) | ||
| range = Range.new(start_pos, end_pos) | ||
|
|
||
| original_text = Document.fragment(analysis.document, start_pos, end_pos) | ||
| new_text = normalize_heex_text(analysis.document, original_text, position, start_pos) | ||
|
|
||
| change = %{range: range, text: new_text} | ||
|
|
||
| case Document.apply_content_changes(analysis.document, analysis.document.version + 1, [ | ||
| change | ||
| ]) do | ||
| {:ok, doc} -> doc | ||
| _ -> analysis.document | ||
| end | ||
|
|
||
| _ -> | ||
| analysis.document | ||
| end | ||
| end | ||
|
|
||
| defp extract_heex_range(analysis, position) do | ||
| with {:ok, path} <- Ast.path_at(analysis, position), | ||
| {:sigil_H, _, _} = sigil <- Enum.find(path, &match?({:sigil_H, _, _}, &1)), | ||
| %{start: start_pos, end: end_pos} <- Sourceror.get_range(sigil) do | ||
| {:ok, sigil, start_pos, end_pos} | ||
| else | ||
| _ -> :error | ||
| end | ||
| end | ||
|
|
||
| defp normalize_heex_text(document, original_text, cursor_position, start_pos) do | ||
| text_before = Document.fragment(document, start_pos, cursor_position) | ||
| cursor_offset = byte_size(text_before) | ||
|
|
||
| case find_component_match(original_text, cursor_offset) do | ||
| {match_start, match_length, component_name, is_closing} -> | ||
| build_replacement_text( | ||
| original_text, | ||
| match_start, | ||
| match_length, | ||
| component_name, | ||
| is_closing | ||
| ) | ||
|
|
||
| nil -> | ||
| original_text | ||
| end | ||
| end | ||
|
|
||
| defp find_component_match(text, cursor_offset) do | ||
| matches = Regex.scan(@component_regex, text, return: :index) | ||
|
|
||
| Enum.find_value(matches, fn | ||
| [{match_start, match_len}, {name_start, name_len}] -> | ||
| if cursor_offset >= match_start and cursor_offset <= match_start + match_len do | ||
| matched_text = binary_part(text, match_start, match_len) | ||
| component_name = binary_part(text, name_start, name_len) | ||
| is_closing = String.starts_with?(matched_text, "</") | ||
| {match_start, match_len, component_name, is_closing} | ||
| else | ||
| nil | ||
| end | ||
| end) | ||
| end | ||
|
|
||
| defp build_replacement_text( | ||
| original_text, | ||
| match_start, | ||
| match_length, | ||
| component_name, | ||
| is_closing | ||
| ) do | ||
| prefix = binary_part(original_text, 0, match_start) | ||
|
|
||
| suffix = | ||
| binary_part( | ||
| original_text, | ||
| match_start + match_length, | ||
| byte_size(original_text) - (match_start + match_length) | ||
| ) | ||
|
|
||
| replacement = | ||
| if is_closing do | ||
| "</ #{component_name}(assigns)" | ||
| else | ||
| "< #{component_name}(assigns)" | ||
| end | ||
|
|
||
| prefix <> replacement <> suffix | ||
| end | ||
|
|
||
| defp normalize_heex_node({:sigil_H, meta, [{:<<>>, string_meta, parts}, modifiers]}) | ||
| when is_list(parts) do | ||
| new_parts = | ||
| Enum.map(parts, fn | ||
| part when is_binary(part) -> | ||
| part | ||
| |> then(&Regex.replace(@closing_component_regex, &1, @closing_replacement)) | ||
| |> then(&Regex.replace(@opening_component_regex, &1, @opening_replacement)) | ||
|
|
||
| other -> | ||
| other | ||
| end) | ||
|
|
||
| {:sigil_H, meta, [{:<<>>, string_meta, new_parts}, modifiers]} | ||
| end | ||
|
|
||
| defp normalize_heex_node(node), do: node | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If possible, I think we also need to check in the
analysisscopes ifsigil_HfromPhoenix.Componentis importedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to do that, but I'm not sure if this can be done reliably. For example, I find it hard to detect things like
use MyAppWeb, :live_component, which importssigil_H, not to mention some potential more elaborate metaprogramming resulting in an import.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once we integrate spitfire, we should be able to detect any aliases and imports from use macros and metaprogramming. it can get you the current environment for a cursor location (imported functions, aliased modules, etc)