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: support non-indented cases in Scala3 match expression #436

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ dist/
*.tar.gz
*.tgz
*.zip

# scala-cli
.scala-build
.bsp
7 changes: 6 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ module.exports = grammar({
[$._if_condition, $._simple_expression],
// _postfix_expression_choice ':' '(' wildcard • ':' …
[$.binding, $._simple_type],
// expression 'match' 'case' _case_pattern ';' _automatic_semicolon expression • 'match' …
[$.match_expression, $._block],
],

word: $ => $._alpha_identifier,
Expand Down Expand Up @@ -806,6 +808,9 @@ module.exports = grammar({
indented_cases: $ =>
prec.left(seq($._indent, repeat1($.case_clause), $._outdent)),

non_indented_cases: $ =>
prec.right(sep1($._automatic_semicolon, $.case_clause)),

_indented_type_cases: $ =>
prec.left(seq($._indent, repeat1($.type_case_clause), $._outdent)),

Expand Down Expand Up @@ -1157,7 +1162,7 @@ module.exports = grammar({
optional($.inline_modifier),
field("value", $.expression),
"match",
field("body", choice($.case_block, $.indented_cases)),
field("body", choice($.case_block, $.indented_cases, $.non_indented_cases)),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should non_indented_cases be aliased to indented_cases here to preserve compatibility for any queries that may not expect nodes other than case_block/indented_cases, wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to this.

),

try_expression: $ =>
Expand Down
36 changes: 26 additions & 10 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -689,12 +689,12 @@ def matchTest(x: Int): String = x match {
case 3 => {
"3"
}
case 4 =>
case 4 =>
;
case A if a == 1 =>
case A if a == 2 => 2
case ((i, _)) => i
case s"$l1 -- $l2" =>
case s"$l1 -- $l2" =>
l1 + l2
case _ =>
val x = "many"
Expand Down Expand Up @@ -791,6 +791,10 @@ def matchTest(x: Int): String =
case _ =>
val x = "many"
"more"
y match
case 0 => "zero"
case 1 => "one"
case _ => "idk"

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -837,6 +841,18 @@ def matchTest(x: Int): String =
(val_definition
(identifier)
(string))
(string))))
(match_expression
(identifier)
(non_indented_cases
(case_clause
(integer_literal)
(string))
(case_clause
(integer_literal)
(string))
(case_clause
(wildcard)
(string)))))))

================================================================================
Expand All @@ -850,8 +866,8 @@ class C {
a
.b
// comment1
/*
comment2
/*
Copy link
Collaborator Author

@susliko susliko Nov 25, 2024

Choose a reason for hiding this comment

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

Changes here and below are caused by changes in the behavior of tree-sitter test -u, which now removes trailing spaces

comment2
*/
.c
}
Expand Down Expand Up @@ -1105,11 +1121,11 @@ object O {
val l = a => a + 1
val b = (x: Int, y: Int) => { x * y }
val f = _ => 2
foo { i =>
val x = 2 + i
foo { i =>
val x = 2 + i
x
}
{ x =>
{ x =>
val y = 2 * x
y * y
}
Expand Down Expand Up @@ -1223,7 +1239,7 @@ Unit expressions
================================================================================

val x = ()
def f(): Unit = { (
def f(): Unit = { (
); }

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -1649,9 +1665,9 @@ class A:
Inline matches (Scala 3)
================================================================================

def hello =
def hello =
inline c match {
case 1 =>
case 1 =>
}

--------------------------------------------------------------------------------
Expand Down
Loading