Skip to content

add split alternative pattern code action#5240

Open
vshakitskiy wants to merge 2 commits intogleam-lang:mainfrom
vshakitskiy:split-alternative-pattern-code-action
Open

add split alternative pattern code action#5240
vshakitskiy wants to merge 2 commits intogleam-lang:mainfrom
vshakitskiy:split-alternative-pattern-code-action

Conversation

@vshakitskiy
Copy link
Contributor

Closes #4856.

@vshakitskiy vshakitskiy force-pushed the split-alternative-pattern-code-action branch from 4949210 to 456c5c8 Compare December 31, 2025 13:01
Comment on lines +7 to +24
pub fn go(n: Int) {
case n {
1 | 2 | 3 | 4 -> todo
▔▔▔▔↑
_ -> todo
}
}


----- AFTER ACTION

pub fn go(n: Int) {
case n {
1 | 4 -> todo
2 -> todo
3 -> todo
_ -> todo
}
Copy link
Member

Choose a reason for hiding this comment

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

I think when selecting patterns that are in the middle, the alternative should be split by keeping the same order of patterns. So in this test it should become:

case n {
  1 -> todo
  2 | 3 -> todo
  4 -> todo
  _ -> todo
}

What do you think @lpil?

Copy link
Member

@GearsDatapacks GearsDatapacks Jan 2, 2026

Choose a reason for hiding this comment

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

I would expect to get 2 | 3 and 1 | 4 as patterns here I think

Since neither 1 or 4 is selected here, I definitely wouldn't expect those to be the ones that are split

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought the initial idea is that we split the ones that are selected? Quoting the example from the issue page:

case a {
  AAA | BBB | CCC | DDD -> todo
// ^^^^^^
}

// Becomes:
case a {
  AAA -> todo
  BBB -> todo
  CCC | DDD -> todo
}

So I guess it makes sense that if we split the ones on the middle, only them should be affected?

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.

"Split alternative pattern" code action

3 participants