charlesfsl

charlesfsl

I’ve been having fun getting familiar with Igniter for code generation and modification. Thanks for building and releasing it @zachdaniel !

Is there a suggested approach for modifying Heex code?

I have a scenario where I’d like to modify some of the Heex code that mix phx.new generates into core_components.ex. I’m working with Phoenix v1.8-rc.0, which makes notable changes to the core components compared with previous versions. See Chris McCord’s post for more details.

This default core_components.ex includes component markup inline with sigil_H heredocs, like this:

  def input(%{type: "select"} = assigns) do
    ~H"""
    <fieldset class="fieldset mb-2">
      <label>
        <span :if={@label} class="fieldset-label mb-1">{@label}</span>
        <select
          id={@id}
          name={@name}
          class={["w-full select", @errors != [] && "select-error"]}
          multiple={@multiple}
          {@rest}
        >
          <option :if={@prompt} value="">{@prompt}</option>
          {Phoenix.HTML.Form.options_for_select(@options, @value)}
        </select>
      </label>
      <.error :for={msg <- @errors}>{msg}</.error>
    </fieldset>
    """
  end

The AST represents the code inside the heredoc as a bitstring. I’ve tried several approaches, including simply replacing everything in the Heex block with what I want to be there, but Sourceror.parse_string/1 chokes on the first < in the string I pass to it. Does the existence of characters like < necessitate using a template to overwrite the file?

Thanks!

Showing Posts 1 to 9

LostKobrakai

LostKobrakai

Sourceror can parse elixir code. Text within sigils is not elixir code. It could be arbitrary text. Only additional tools we have allow us making sense of the text within sigils like for heex the (eex) engines and the tag parsing it can do as well as the related formatter plugin. That’s all provided by the phoenix_live_view package and therefore would need integration with that to be used. There’s nothing to be known about sigil contents from an elixir AST level.

olivermt

olivermt

@shahryarjb is doing this in mischka chelekom library I believe

zachdaniel

zachdaniel

Creator of Ash

Yeah, we’d need to have a separate parser that creates an AST for the contents of ~H sigils, and then you’d have to update that, and then you can replace the arguments to the ~H sigil with the new resulting string. I haven’t looked into it myself, but I’m positive something like this exists as Phoenix must itself be using it.

charlesfsl

charlesfsl OP

Thanks all. That makes sense to me. I looked a little at what Phoenix is doing for non-umbrella apps, and have so far seen that it’s copying template files instead of modifying code. I’ll look further, and check out what’s going on in mischka chelekom.

shahryarjb

shahryarjb

Hi! At Mishka Chelekom, we don’t modify Phoenix components using AST directly. Instead, we have around 90 Phoenix components that are generated by Igniter and placed into your project.

https://github.com/mishka-group/mishka_chelekom

For example you want to have Combobox, you just need to run mix mishka.ui.gen.component combobox

Source: mishka_chelekom/priv/components/combobox.eex at master · mishka-group/mishka_chelekom · GitHub

Igniter CLI: mishka_chelekom/lib/mix/tasks/mishka.ui.gen.component.ex at master · mishka-group/mishka_chelekom · GitHub

Igniter also includes parsers for modifying JavaScript and CSS, but when it comes to changing Phoenix-related HTML code (Heex), it’s best to ask @zachdaniel, who has already responded about this.

charlesfsl

charlesfsl OP

Thanks @shahryarjb. I was just checking out the code in Mishka Chelekom.

Looking forward to trying it out with Phoenix 1.8 :slight_smile:

zachdaniel

zachdaniel

Creator of Ash

You could potentially take a look at Phoenix.LiveView.Tokenizer, using it to turn the text into a data structure you can manipulate, and then see if there is some way to turn it back into a string? That is the general pattern for all of igniter, i.e source -> AST -> manipulate -> source.

shahryarjb

shahryarjb

Thank you; after releasing the Phoenix 1.8, we will test and update the lib for this version :folded_hands:t2:

charlesfsl

charlesfsl OP

Oh of course! I apparently had blinders on and was only thinking about Phoenix generators and not LiveView’s more core functionality of diffing markup changes. Thanks @zachdaniel

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews