Skip to content

Case split plugin - #5014

Open
Aster89 wants to merge 1 commit into
haskell:masterfrom
Aster89:master
Open

Case split plugin#5014
Aster89 wants to merge 1 commit into
haskell:masterfrom
Aster89:master

Conversation

@Aster89

@Aster89 Aster89 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This PR is for (re-¹)introducing the so called case-split plugin, as requested in #5013, and it is part of my project for GSoC 2026.

To see the full summary of the project, click here. The summary also contains a list of stretch goals, some of which have been addressed already.

The work I've done and that I plan to submit via the present pull request, has consisted mainly of:

  • implementing the case-split plugin by incrementally focusing on several use-cases from less to more complex,
  • making use of ghc-exactprint for correctly layout out the patterns to be inserted,
  • contextually writing tests for guarding such use-cases as I enabled them,
  • finally, striving to address some stretch goals.

The result of such work is a plugin providing a code action for adding missing patterns to a case/\case expression that has a non-exhaustive list of patterns.

For instance, the plugin turns this

data X = A
       | B
       | C Int
       | D Int Int
       | E Int Int Int Int
       | F

foo :: X -> Int
foo x = case x of
          A -> 3;
                B -> 4;
              C _ -> 5

into this

data X = -- same as above

foo :: X -> Int
foo x = case x of
          A -> 3;
                B -> 4;
              C _ -> 5
          D _ _ -> _
          E {} -> _
          F -> _

¹ Earlier attempts

Indeed, there were some earlier attempts. See the summary for some pointers.

@dschrempf

Copy link
Copy Markdown
Collaborator

Everybody is eagerly anticipating this change, thank you!

Given the many WIP commits, I just wanted to ask if you could sqaush or reword them. I guess you had this in mind anyways.

@Aster89

Aster89 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Everybody is eagerly anticipating this change, thank you!

Glad to hear that!

Given the many WIP commits, I just wanted to ask if you could sqaush or reword them. I guess you had this in mind anyways.

Yep, see 3rd paragraph at the top :P

@dschrempf

Copy link
Copy Markdown
Collaborator

Ah, the second sub-clause :-P my attention had already shifted before that one, I apologize :-)! Thanks!

@fendor fendor changed the title Case split plugin - Fixes #5013 Case split plugin Jul 14, 2026
@fendor
fendor marked this pull request as draft July 20, 2026 09:21
Comment thread ghcide/src/Development/IDE/Core/Compile.hs

@MangoIV MangoIV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

First pass. I think after you clean up the main logic a bit more and add some documentation there, I can take another look. :)

Very good work, looking forward to having this in HLS!

Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated

@MangoIV MangoIV left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

another small round :)

Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
@Aster89
Aster89 force-pushed the master branch 7 times, most recently from 82aad14 to c7f9b61 Compare August 7, 2026 09:36
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
@Aster89
Aster89 requested a review from MangoIV August 7, 2026 09:42
@Aster89
Aster89 marked this pull request as ready for review August 7, 2026 10:52

@fendor fendor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Very nice, this looks like it is making great progress!

I reviewed the plugin, I have mostly comments regarding readability and suggestions for improvements that hopefully improve the maintainability in the long run.

Some things are nitpicks, and you should feel free to ignore them.

I think we need one Note that outlines what the implementation strategy of the case split plugin is (e.g., reads the structured error message, etc...) so that it is easier to tell what the individual steps for case splitting are.

Comment on lines +154 to +158
getActionByTitle :: T.Text -> [CodeAction] -> Session CodeAction
getActionByTitle title actions =
case find (\a -> a ^. L.title == title) actions of
Just a -> pure a
Nothing -> liftIO $ assertFailure $ "Action " <> show title <> " not found in " <> show [a ^. L.title | a <- actions]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

There is inspectCodeAction which might be what you are looking for here, should also be helpful to get rid of the _CACodeAction

Comment thread plugins/hls-case-split-plugin/test/Main.hs
Comment on lines +240 to +241
caseSplitPluginCodeActionTitle :: Text
caseSplitPluginCodeActionTitle = "Add placeholders for the first `-fmax-uncovered-patterns` missing patterns"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not a huge fan of the title, as it exposes implementation details... But I don't have a better idea for now

@Aster89 Aster89 Aug 11, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do I understand that you're alluding to the fact I mention -fmax-uncovered-patterns? I'd argue that this is rightfully documenting a current limitation of the plugin, rather than exposing an implementation detail.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If you case split on the non exhausted patterns, how soon would you like to know its current limitations?
Like this, it is the second thing you learn, and you learn that there is a flag called -fmax-uncovered-patterns. As a user, what are you supposed to do about this?

I just don't think we need to say in the code action title, why we are only completing -fmax-uncovered-patterns cases, as it doesn't mean anything to beginners, and is maybe not even observable in many applications.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If you case split on the non exhausted patterns, how soon would you like to know its current limitations?

I would say "soon", personally.

As a user, what are you supposed to do about this?

"Supposed to", nothing, but at least I'm given pointers to look for if I care about it.

I just don't think we need to say in the code action title, why we are

Well, technically I'm not telling why. I'm just saying what's the number of completions they get, to avoid surprising them when they don't see them all.

But I guess this is already in the land of preference/opinions? 😄

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, my opinion is, mentioning the option (not even the number of patterns you are going to match) doesn't add anything worthwhile to the average users. But I am not going to fight over it.

@MangoIV what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think it's crucial information in general here but I see an issue in users reporting bugs upstream because they think this is not a known caveat. I don't know where else to put the information though except perhaps in the documentation / on the website of HLS itself?

@fendor fendor Aug 11, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The documentation on the website feels like the appropriate place. (Should be added regardless)

I expect users are going to report it either way, as it doesn't really mean anything to them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The documentation on the website feels like the appropriate place.

Which website?

Are you by any chance referring to the bullet list here?

(Should be added regardless)

Before GSoC submission deadline, or can I do it afterwards?


Since

But I am not going to fight over it.

and

I see an issue in users reporting bugs upstream because they think this is not a known caveat

I'll keep the message as is. And take note (of changing this String) in the stretch goals.

Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
--
--
-- Refer to test cases to see practical examples.
appendMissingPats :: Maybe Int -> MatchGroup GhcPs (LHsExpr GhcPs) -> NonEmpty (LMatch GhcPs (LHsExpr GhcPs)) -> Maybe (MatchGroup GhcPs (LHsExpr GhcPs))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If I understand correctly, this is doing the heavy lifting of actually pushing the new matches to the AST.

Afaict, there are four ways we support grafting:

  1. LambdaCase with braces
  2. LambdaCase without braces
  3. case with braces
  4. case without braces

I am not sure merging these cases altogether is the best strategy.
What would the code look like if we differentiated between these cases explicitly? More duplication, but maybe also simpler?

Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
Comment thread plugins/hls-case-split-plugin/src/Ide/Plugin/CaseSplit.hs Outdated
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.

4 participants