Skip to content
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

feat: Expose typedef edit actions in OpenAPI #949

Merged
merged 16 commits into from
May 31, 2023
Merged

Conversation

georgefst
Copy link
Contributor

@georgefst georgefst commented Apr 18, 2023

This allows us to finally take advantage of #367 (with the caveats detailed in #399, #401 and #402).

This will ultimately enable us to render typedefs alongside terms in our frontend, and modify them using the same actions panel with which we manipulate terms.

See #1040 and #1041 for potential follow-ups.

@georgefst georgefst force-pushed the georgefst/typedef-actions branch 7 times, most recently from 0e11bc5 to 17c9725 Compare April 27, 2023 11:14
@georgefst georgefst marked this pull request as ready for review April 27, 2023 11:20
@georgefst georgefst force-pushed the georgefst/typedef-actions branch 3 times, most recently from 29155fc to 0695d8c Compare April 27, 2023 14:14
@georgefst georgefst changed the title Expose typedef edit actions in OpenAPI feat: Expose typedef edit actions in OpenAPI May 9, 2023
@georgefst georgefst force-pushed the georgefst/typedef-actions branch 5 times, most recently from 73787bf to 0ec0742 Compare May 18, 2023 16:36
@georgefst georgefst force-pushed the georgefst/typedef-actions branch 2 times, most recently from f5cadd5 to 17dbeb3 Compare May 24, 2023 14:23
primer/src/Primer/Action.hs Show resolved Hide resolved
Just wrappedTy ->
f wrappedTy >>= \case
InType zt -> pure zt
z -> maybe unwrapError pure (focusType (unfocusLoc z))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This whole function is a bit of a copy-paste from applyActionsToType, and I'm not certain that all details are relevant, or whether there's anything missing. Also, I don't know if there's anything from the recently-fixed SetConFieldType (#932) that we're missing. However, #1040 doesn't seem to be revealing any issues from forTypeDefConsFieldNode, so maybe we're in the clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

r.e. #932, I think this is fine: you are calling checkEverything instead of manually fixing all usages of this constructor.

There is some DRYing that could happen to withWrappedType in particular, but this could be done later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

r.e. #932, I think this is fine: you are calling checkEverything instead of manually fixing all usages of this constructor.

Ah, of course.

And indeed, given how general one of these actions can be, there didn't seem much alternative. But I suppose, now that you allude to it, we could in theory look for uses of the given constructor, and try to fix things more locally. I'm certainly not suggesting that we change that in this PR, however.

primer/src/Primer/Action/ProgAction.hs Show resolved Hide resolved
primer/src/Primer/Action.hs Outdated Show resolved Hide resolved
findAndAdjustA' :: Applicative m => (a -> Bool) -> (a -> m (a, z)) -> [a] -> m (Maybe ([a], z))
findAndAdjustA' p f = \case
[] -> pure Nothing
x : xs -> if p x then (\(x', z) -> Just . (,z) . (: xs) $ x') <$> f x else first (x :) <<$>> findAndAdjustA' p f xs
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If these functions are staying (see above), I should at least add haddocks, and maybe pick a better name.

. Just
. TypeDefConsNodeSelection
. TypeDefConsSelection con
$ Nothing
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might not be the selection we want. This means that when we add a field, the selection stays in the current constructor, rather than descending to the newly-created hole. The current approach has the advantage that it's easy to quickly add multiple fields, but it might feel inconsistent with what we do elsewhere.

If we do want to change this, I think we'll need something similar to the findAndAdjustA' machinery in applyActionsToField, for extracting the zipper and thus the ID of the hole.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not too bothered either way. I assume it would be easy to change if desired?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes

@georgefst georgefst requested a review from a team May 25, 2023 11:29
primer/src/Primer/App.hs Outdated Show resolved Hide resolved
Copy link
Contributor

@brprice brprice left a comment

Choose a reason for hiding this comment

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

Other than the issue with genNames, this looks good to me

This will be necessary in order to make use of them in actions-related modules.

Signed-off-by: George Thomas <[email protected]>
We parameterise this way, rather than parameterising `ExprMeta` and `TypeMeta` separately, since we otherwise wouldn't be able to reconstruct even a basic `Selection' ID` from the OpenAPI API, without clients knowing whether a particular ID corresponds to a type or term. We want clients to be able to be dumber than that.

Note that, for `Selection`, we use a synonym for the non-parameterised version, but not for `NodeSelection`, and we will not in future for `DefSelection` and `TypeDefSelection`. That's because this synonym is actually widely useful, and we use it in several modules to make things more readable. With `NodeSelection` etc. we never really require such a synonym, and we avoid it because of all the ceremony it would add, particularly around imports.

Signed-off-by: George Thomas <[email protected]>
This will significantly decrease the amount of breakage when we unify our selection types.

Signed-off-by: George Thomas <[email protected]>
This removes some boilerplate where we converted between types which are essentially the same. That boilerplate would have become much bigger once we extend selections to cover type definitions.

Despite the previous commit, there is one small breaking change, seen in `openapi.json` (`meta` instead of `id`).

Signed-off-by: George Thomas <[email protected]>
This will make it easier to output typedefs in the API, due to increased uniformity with AST typedefs.

Note that these names aren't even currently used anywhere since we don't actually have any higher-kinded primitives, only ints and chars. These would be used if we added, for example, `IO` or `Array` primitives, in which case the names would be likely useful at least for _displaying_ primitive typedefs, even though they don't actually scope over anything like they do for ASTs.

Signed-off-by: George Thomas <[email protected]>
Note that no new unit tests are added. This is because the underlying `ProgAction`s are already well-tested, e.g. in `unit_RenameType`. In due course, the property test `tasty_available_actions_accepted` will be generalised to cover typedef actions, which will therefore check that all of these new actions can be applied without error whenever they are available.

We don't yet expose actions for constructor fields (i.e. `Available.forTypeDefConsFieldNode` always returns `[]`), since making this work will, unlike the other positions, require changes to the core of the library.

We _could_ actually make all the `for*`s part of one definition, now that `Selection` is in `App.Base`. But we do actually use the individual functions in some tests, and with them separate, we have slightly more control, in that we don't need to provide as much context.

Note that most of the changes in this commit are actually knock-on effects of generalising `Selection` to cover type defs.

Signed-off-by: George Thomas <[email protected]>
This was motivated by a surprising deep equality failure on selections in a TypeScript frontend, due to a null field only present in one of the compared values.

Signed-off-by: George Thomas <[email protected]>
It is of little use now that we have the more general `ConFieldAction`.

Signed-off-by: George Thomas <[email protected]>
We can define these in terms of their un-primed equivalents, by choosing the correct applicative, so we effectively do so and then inline.

Signed-off-by: George Thomas <[email protected]>
@georgefst georgefst added this pull request to the merge queue May 31, 2023
Merged via the queue into main with commit f353800 May 31, 2023
@georgefst georgefst deleted the georgefst/typedef-actions branch May 31, 2023 10:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants