I did some more work on the Type-Directed Autocomplete project and this is kind of a status update for what I showed in an earlier post. As explained there, the goal is to generate completions for Elm code which satisfy given type constraints, so that your editor only suggests code to you, which would actually lead to a compiling Elm file.
So, I already had some functions for generating the suggestions, but I also wanted a way to actually test these out in actual projects within the editor, to be able to see if the suggestions make sense and to further improve them. To do this, you need a tool that can infer the type of any expression within an Elm file and also collect all available values at this position. For example, given
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
NoOp ->
( model
, Cmd.none
)
NameChanged name ->
( whatGoesHere
, Cmd.none
)
that tool should tell me that whatGoesHere
is of the type Model
, and that at this position one can use the values name : String
, msg : Msg
, model : Model
and all the top-level values which are defined elsewhere in the module or imported from other modules. Given that information, the completion generator can then suggest { model | name = name }
, for example.
That sounded like a good learning opportunity, so I started writing such a tool. And as it seems to be in a state where one can at least try it out on smaller files/projects, I wanted to share it here.
So, my plan for this right now is to improve the actual completion generator. It seems that there is a lot of stuff which can be explored! For example: better ranking (based on how “far away” the available
values are), generating encoders and decoders, incorporating placeholders (which a lot of snippet plugins support), etc. I’m happy for feedback and ideas for this!
On the other hand I’m wondering, what would be a good way to proceed with this local-type-inference tool. Although it is fun to write your own inference algorithm, I feel like it may be beneficial to actually
start from the Elm compiler itself and see how one could modify it to get this type information. I just saw that the recommendation for the elm-find project is to
actually start with the Elm compiler. So my question here is, if that also makes sense for this tool?