-
-
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
base: main
Are you sure you want to change the base?
Conversation
doorgan
left a comment
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.
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) |
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.
We probably want Engine.Module.Loader.ensure_loaded? here. Code.ensure_loaded?/1 can be quite slow, so we have a cache for it
| Code.ensure_loaded?(Phoenix.Component) | |
| Engine.Module.Loader.ensure_loaded?(Phoenix.Component) |
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.
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 |
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 analysis scopes if sigil_H from Phoenix.Component is imported
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.
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.
| |> Zipper.zip() | ||
| |> Zipper.find(&(&1 == sigil)) | ||
| |> case do | ||
| nil -> analysis.ast | ||
| zipper -> zipper |> Zipper.replace(new_sigil) |> Zipper.root() |
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.
In case we need to speed this up for large files, there's also Sourceror.FastZipper
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.
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.
36d2efd to
2eea4c5
Compare
|
@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 |
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.
They appear in expert.log 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. |
be21465 to
5158ee6
Compare
|
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 |
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.buttonis 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 whenphoenix_live_viewis 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
I looked into making a more detailed check here, basically checking if
Phoenix.Componentis 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. ↩