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
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3053832
make sure todo expressions have the correct starting point
giacomocavalieri Jan 7, 2025
a226a84
Make sure module cannot import itself in code action
giacomocavalieri Jan 7, 2025
27fb807
Fix off by 1 error in implicitly inserted todo
giacomocavalieri Jan 7, 2025
f24ab51
Add destructure argument code action
giacomocavalieri Jan 7, 2025
421398f
Properly nest code following an inserted argument destructuring
giacomocavalieri Jan 7, 2025
d73529a
add tests for the "Destructure argument" code action
giacomocavalieri Jan 7, 2025
cf78b12
CHANGELOG
giacomocavalieri Jan 7, 2025
5f34c38
stop triggering action to destructure empty tuples
giacomocavalieri Jan 8, 2025
d71007d
use "pattern match on argument" instead of "destructure argument"
giacomocavalieri Jan 8, 2025
5116c2b
enable "pattern match on argument" code action with multiple construc…
giacomocavalieri Jan 8, 2025
3196ea3
use a better name for patterns with a single unlabelled field
giacomocavalieri Jan 8, 2025
bf3d528
add missing import
giacomocavalieri Jan 8, 2025
ee41ed8
cargo format
giacomocavalieri Jan 8, 2025
4041905
refactor destructure argument code to allow for extensions
giacomocavalieri Jan 8, 2025
f0ea7c4
allow "pattern match" action on internal type inside its own module
giacomocavalieri Jan 9, 2025
8a31e38
add "pattern match on variable" code action
giacomocavalieri Jan 9, 2025
453a834
enable "pattern match on argument" for anonymous functions
giacomocavalieri Jan 9, 2025
71b021c
use Vec1 instead of Vec when visiting a fn expr's body
giacomocavalieri Jan 9, 2025
e5d9056
CHANGELOG
giacomocavalieri Jan 9, 2025
4b19978
update documentation
giacomocavalieri Jan 9, 2025
6b67c5b
update documentation, for real
giacomocavalieri Jan 9, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,63 @@

([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- The language server now suggests a code action to pattern match on a
function's argument. For example:

```gleam
pub type Pokemon {
Pokemon(pokedex_number: Int, name: String)
}

pub fn to_string(pokemon: Pokemon) {
// ^ If you put your cursor over the argument
todo
}
```

Triggering the code action on the `pokemon` argument will generate the
following code for you:

```gleam
pub type Pokemon {
Pokemon(pokedex_number: Int, name: String)
}

pub fn to_string(pokemon: Pokemon) {
let Pokemon(pokedex_number:, name:) = pokemon
todo
}
```

([Giacomo Cavalieri](https://github.com/giacomocavalieri))

- The language server now suggests a code action to pattern match on a variable.
For example:

```gleam
pub fn main() {
let result = list.first(a_list)
// ^ If you put your cursor over the variable
todo
}
```

Triggering the code action on the `result` variable will generate the
following code for you:

```gleam
pub fn main() {
let result = list.first(a_list)
case result {
Ok(value) -> todo
Error(value) -> todo
}
todo
}
```

([Giacomo Cavalieri](https://github.com/giacomocavalieri))

### Formatter

### Bug fixes
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2180,7 +2180,7 @@ impl<A> BitArrayOption<A> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum TodoKind {
Keyword,
EmptyFunction,
EmptyFunction { function_location: SrcSpan },
IncompleteUse,
EmptyBlock,
}
Expand Down
4 changes: 3 additions & 1 deletion compiler-core/src/ast/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ impl TypedExpr {
// We don't want to match on todos that were implicitly inserted
// by the compiler as it would result in confusing suggestions
// from the LSP.
TodoKind::EmptyFunction | TodoKind::EmptyBlock | TodoKind::IncompleteUse => None,
TodoKind::EmptyFunction { .. } | TodoKind::EmptyBlock | TodoKind::IncompleteUse => {
None
}
},

Self::Pipeline {
Expand Down
5 changes: 3 additions & 2 deletions compiler-core/src/ast/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ use crate::{
use std::sync::Arc;

use ecow::EcoString;
use vec1::Vec1;

use crate::type_::Type;

Expand Down Expand Up @@ -143,7 +144,7 @@ pub trait Visit<'ast> {
type_: &'ast Arc<Type>,
kind: &'ast FunctionLiteralKind,
args: &'ast [TypedArg],
body: &'ast [TypedStatement],
body: &'ast Vec1<TypedStatement>,
return_annotation: &'ast Option<TypeAst>,
) {
visit_typed_expr_fn(self, location, type_, kind, args, body, return_annotation);
Expand Down Expand Up @@ -824,7 +825,7 @@ pub fn visit_typed_expr_fn<'a, V>(
_typ: &'a Arc<Type>,
_kind: &'a FunctionLiteralKind,
_args: &'a [TypedArg],
body: &'a [TypedStatement],
body: &'a Vec1<TypedStatement>,
_return_annotation: &'a Option<TypeAst>,
) where
V: Visit<'a> + ?Sized,
Expand Down
Loading
Loading