Skip to content

Conversation

@katafrakt
Copy link
Contributor

Closes #269

The full-module notation (<MyMod.button>) was already supported in "go to definition", because when passed to ElixirSense, it correctly recognized the form and found the appropriate module and the function. However, with a shorthand notation it was not so simple, because .button is not valid Elixir and ElixirSense was not able to make anything of it.

This implements the support for shorthand notation by doing a preliminary step before sending the code to ElixirSense. It modifies the AST and the Document of ForgeAnalysis and replaces all the calls to <.button> with < button(assigns) so that ElixirSense can correctly interpret it as a local function call with arity 1. This only happens when phoenix_live_view is added as a dependency1.

This works for functions defined in the same module, but also for imported functions. Support for ending tag is also included.

While this might seem as a hacky solution, it makes handling shorthand notation as close as possible to handling the full-module notation, making sure these two stay conceptually close.

Footnotes

  1. I looked into making a more detailed check here, basically checking if Phoenix.Component is imported into current module, but there's just too many ways to do so. I don't think the code analysis can do that, we would need to actually compile the code (using ElixirSense?). But I settled for a much simpler condition in this case.

@katafrakt katafrakt changed the title Support jump to definition in HEEx templates feat(engine): support shorthand notation inside ~H sigil Dec 23, 2025
Copy link
Collaborator

@doorgan doorgan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

This is working, I only have some minor comments so far. One thing I could not get to work was Hover, I get logs saying the entity was resolved to the right thing, but no hover docs show.

Another note but I think this is more of an ElixirSense issue, I get the logs spammed with this sometimes when I trigger go to definition:

18:44:25.258 [warning] ** (RuntimeError) ~H requires a variable named "assigns" to exist and be set to a map
    (phoenix_live_view 0.20.7) lib/phoenix_component.ex:791: Phoenix.Component."MACRO-sigil_H"/3
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/normalized/macro/env.ex:54: anonymous fn/5 in XPElixirSense.Core.Normalized.Macro.Env.wrap_expansion/7
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:2144: XPElixirSense.Core.Compiler.expand_macro_callback/7
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:117: XPElixirSense.Core.Compiler.expand/3
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:2055: XPElixirSense.Core.Compiler.expand_macro/7
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:117: XPElixirSense.Core.Compiler.expand/3
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:2165: XPElixirSense.Core.Compiler.expand_macro_callback/7
    (xp_elixir_sense 2.0.0) lib/elixir_sense/core/compiler.ex:117: XPElixirSense.Core.Compiler.expand/3

end

defp phoenix_component_available? do
Code.ensure_loaded?(Phoenix.Component)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want Engine.Module.Loader.ensure_loaded? here. Code.ensure_loaded?/1 can be quite slow, so we have a cache for it

Suggested change
Code.ensure_loaded?(Phoenix.Component)
Engine.Module.Loader.ensure_loaded?(Phoenix.Component)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thanks

# (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
Copy link
Collaborator

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 analysis scopes if sigil_H from Phoenix.Component is imported

Copy link
Contributor Author

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 imports sigil_H, not to mention some potential more elaborate metaprogramming resulting in an import.

Comment on lines 49 to 53
|> Zipper.zip()
|> Zipper.find(&(&1 == sigil))
|> case do
nil -> analysis.ast
zipper -> zipper |> Zipper.replace(new_sigil) |> Zipper.root()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case we need to speed this up for large files, there's also Sourceror.FastZipper

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to FastZipper

The full-module notation (`<MyMod.button>`) was already supported in "go
to definition", because when passed to ElixirSense, it correctly
recognized the form and found the appropriate module and the function.
However, with a shorthand notation it was not so simple, because
`.button` is not valid Elixir and ElixirSense was not able to make
anything of it.

This implements the support for shorthand notation by doing a
preliminary step before sending the code to ElixirSense. It modifies the
AST and the Document of ForgeAnalysis and replaces all the calls to
`<.button>` with `< button(assigns)` so that ElixirSense can correctly
interpret it as a local function call with arity 1.

This works for functions defined in the same module, but also for
imported functions. Support for ending tag is also included.

While this might seem as a hacky solution, it makes handling shorthand
notation as close as possible to handling the full-module notation,
making sure these two stay conceptually close.
The testing happens in Engine and there the presence of LiveView is mocked.
@katafrakt katafrakt force-pushed the support-h-sigil-shorthand branch from 36d2efd to 2eea4c5 Compare January 5, 2026 10:50
@katafrakt
Copy link
Contributor Author

@doorgan thank you for the review!

Admittedly, I didn't really focus on Hover while working on this. I can have a look, if you think this is in the scope here.

As for error logs, I haven't really seen these errors yet, but I would like to look into this. Where do they appear for you? In the project.log?

@doorgan
Copy link
Collaborator

doorgan commented Jan 5, 2026

@katafrakt

Admittedly, I didn't really focus on Hover while working on this. I can have a look, if you think this is in the scope here.

I think it's in scope for this feature, but also feel free to do it in a follow up. If this gets merged and you decide to tackle that in another PR, let's open an issue to track it.

As for error logs, I haven't really seen these errors yet, but I would like to look into this. Where do they appear for you? In the project.log?

They appear in expert.log
But again this may be an ElixirSense quirk, like it happens sometimes for module attributes

I think this is good to go, but I'll defer to @mhanberg on merging. I want to make sure we're good with this approach in the short term vs revamping the plugin system first.

@katafrakt katafrakt force-pushed the support-h-sigil-shorthand branch from be21465 to 5158ee6 Compare January 5, 2026 11:51
@katafrakt
Copy link
Contributor Author

katafrakt commented Jan 5, 2026

I'm trying to understand if there's something special about this OTP 27.3.4.1 / Elixir 1.18.4 version combination, where tests for this fail on a seemingly unrelated test case, given that is passes with this elixir version and higher/lower OTP. I'm able to reproduce this locally, so it's not a CI issue.

UPDATE: I traced it to the ElixirSense.definition call in Engine.CodeIntelligence.Definition, so I think it's a problem there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Jump to definition in HEEx templates

2 participants