-
Notifications
You must be signed in to change notification settings - Fork 55
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
Fix parenthesized expressions in braceless if
conditions
#350
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,8 @@ module.exports = grammar({ | |
// 'for' operator_identifier ':' _annotated_type • ':' … | ||
[$._type, $.compound_type], | ||
[$.lambda_expression, $.modifiers], | ||
// 'if' parenthesized_expression • '{' … | ||
[$._if_condition, $._simple_expression], | ||
// _postfix_expression_choice ':' '(' wildcard • ':' … | ||
[$.binding, $._simple_type], | ||
], | ||
|
@@ -1106,22 +1108,25 @@ module.exports = grammar({ | |
), | ||
|
||
if_expression: $ => | ||
prec.right( | ||
PREC.control, | ||
seq( | ||
optional($.inline_modifier), | ||
"if", | ||
field( | ||
"condition", | ||
choice( | ||
$.parenthesized_expression, | ||
seq($._indentable_expression, "then"), | ||
), | ||
), | ||
field("consequence", $._indentable_expression), | ||
optional(seq("else", field("alternative", $._indentable_expression))), | ||
), | ||
), | ||
prec.right(PREC.control, seq( | ||
optional($.inline_modifier), | ||
"if", | ||
field( | ||
"condition", | ||
$._if_condition, | ||
), | ||
field("consequence", $._indentable_expression), | ||
optional(seq("else", field("alternative", $._indentable_expression))), | ||
)), | ||
|
||
// NOTE(susliko): _if_condition and its magic dynamic precedence were introduced as a fix to | ||
// https://github.com/tree-sitter/tree-sitter-scala/issues/263 and | ||
// https://github.com/tree-sitter/tree-sitter-scala/issues/342 | ||
// Neither do I understand why this works, nor have I found a better solution | ||
_if_condition: $ => prec.dynamic(4, choice( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm ashamed of this |
||
$.parenthesized_expression, | ||
seq($._indentable_expression, "then"), | ||
)), | ||
|
||
/* | ||
* MatchClause ::= 'match' <<< CaseClauses >>> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,19 +26,19 @@ object Hello { | |
trait Test { | ||
// ^ keyword | ||
// ^ type | ||
def meth(i: Int)(implicit x: Boolean) = ??? | ||
def meth(i: Int)(implicit x: Boolean) = ??? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've made definitions in this trait to be of the same indentation because trait Foo {
def meth = ???
def anonFun = ???
} is parsed with an ERROR and this causes flacking of the highlighting test We might want to open an issue on this, but I'm reluctant to it as
|
||
// ^keyword.function | ||
// ^keyword | ||
// ^type | ||
// ^method | ||
// ^parameter | ||
// ^parameter | ||
|
||
val anonFun: Int => Int = (a: Int) => a | ||
// ^variable | ||
// ^type | ||
// ^operator | ||
// ^type | ||
// ^parameter | ||
val anonFun: Int => Int = (a: Int) => a | ||
// ^variable | ||
// ^type | ||
// ^operator | ||
// ^type | ||
// ^parameter | ||
} | ||
|
||
protected abstract class Bla(test: String) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!