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

Add "Pattern match on argument" code action #4153

Merged
merged 21 commits into from
Jan 9, 2025

Conversation

giacomocavalieri
Copy link
Member

This PR closes #4139.

The tests cover loads of different cases but here's a small example of what it looks like in practice:

Comment on lines -1894 to +1900
kind: TodoKind::EmptyFunction,
location: SrcSpan { start, end },
kind: TodoKind::EmptyFunction {
function_location: SrcSpan { start, end }
},
location: SrcSpan {
start: left_brace_start + 1,
end: right_brace_end
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously when the compiler inserted an implicit todo in an empty function body it marked its position as starting at the start of the function declaration. This would mess up code actions that rely on a function's body's statement to always come inside the curly braces.

I found this out implementing the "Destructure argument" code action and changed it here so that an implicit todo now has the following span:

   pub fn main() { }
//                ^^ This span inside the function's body
// ^^^^^^^^^^^^^ Instead of this span outside the function's body

It still carries around the entire function span to produce nice error messages for incomplete code in the TodoKind::EmptyFunction variant

@giacomocavalieri
Copy link
Member Author

I've updated this to:

  • use the term "pattern match" instead of "destructure"
  • work on types with more than a single constructor as well, an example below:

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

This looks fantastic!

Does it work with anonymous functions? I could see it being useful there too.

Heck, are there other places it would be useful? Like let x =?

@giacomocavalieri giacomocavalieri changed the title Add "Destructure argument" code action Add "Pattern match on argument" code action Jan 8, 2025
Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you!!

@lpil lpil merged commit 42c85df into gleam-lang:main Jan 9, 2025
12 checks passed
@giacomocavalieri giacomocavalieri deleted the fix-4139 branch January 9, 2025 14:12
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.

Code action to destructure a function argument
2 participants