mdgriffith / elm-codegen

https://package.elm-lang.org/packages/mdgriffith/elm-codegen/latest/
BSD 3-Clause "New" or "Revised" License
138 stars 16 forks source link

Apply module import aliases in Elm.ToString #40

Closed tesk9 closed 2 years ago

tesk9 commented 2 years ago

I would like to be able to present a code snippet that has module import aliases already applied, without needing to create an entire File's contents and then trim it down.

For example, if I want the following code snippet to show:

myIcon : Svg.Svg
myIcon =
    UiIcon.preview

assuming the generated code looks like this:

-- Copied from codegen/Gen/Nri/Ui/UiIcon/V1.elm
-- after running elm-codegen NoRedInk/noredink-ui
preview : Elm.Expression
preview =
    Elm.value
        { importFrom = [ "Nri", "Ui", "UiIcon", "V1" ]
        , name = "preview"
        , annotation =
            Just (Type.namedWith [ "Nri", "Ui", "Svg", "V1" ] "Svg" [])
        }

I believe I would need to do some mucking about with the result of the following in order to focus in on the part of the generated code that I care about.

    Elm.fileWith [ "My", "Module" ]
        { docs = \_ -> []
        , aliases =
            [ ( [ "Nri", "Ui", "UiIcon", "V1" ], "UiIcon" )
            , ( [ "Nri", "Ui", "Svg", "V1" ], "Svg" )
            ]
        }
        [ Elm.declaration "myIcon" preview
        ]
        |> .contents
        |> ... string trickery

It would be nice to be able to do:

Elm.ToString.expressionWith 
            { aliases =
            [ ( [ "Nri", "Ui", "UiIcon", "V1" ], "UiIcon" )
            , ( [ "Nri", "Ui", "Svg", "V1" ], "Svg" )
            ]}

and so forth instead.

mdgriffith commented 2 years ago

This is now supported in the new version! What a fantastic suggestion, thank you 🙏