Skip to content

Commit

Permalink
[Java] Fix default method modifier (#3439)
Browse files Browse the repository at this point in the history
* [Java] Fix default method modifier scope

Fixes #3438

This commit re-orders `declarations` and `statements` contexts to ensure to first consume `default Type method()` declarations, which is
required to properly scope `default` keyword in that context.

That change revealed `yield` being missing in `control_keywords` which
caused it to be scoped `storage.type` in some contexts by accident.
That's fixed to keep syntax tests passing.

* [Java] Adjust modifier scopes

This commit renames scope of `default` and `throws` keyword in context
of defining method attributes to `storage.modifier`.

Motivation

1. `default` is scoped `storage.modifier` in front of default method
    declarations and `keyword.control` in switch-case statements.
    A third scope for the same keyword is probably too much and may
    feel inconsistent.
2.  `extends` or `inherits` keywords, which are used in exactly the
     same position (related context with regards to class/interface
     declarations) are scoped `storage.modifier`.
3.   scope of `throws` keyword is adjusted, too, because of 2.) and
     to keep in consistent with scope of `default` in same position.
  • Loading branch information
deathaxe committed Jun 30, 2022
1 parent e5ab298 commit e754ec4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
12 changes: 6 additions & 6 deletions Java/Java.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ variables:
(?x: class | enum | @?interface | record | var | void | extends | implements | import | package )
control_keywords: |-
(?x: assert | break | case | catch | continue | do | else | finally | for
| if | return | switch | throw | throws | try | while )
| if | return | switch | throw | throws | try | while | yield )
operator_keywords: |-
(?x: new | instanceof )
illegal_keywords: |-
Expand Down Expand Up @@ -131,8 +131,8 @@ contexts:
- include: import
- include: module
- include: package
- include: statements
- include: declarations
- include: statements
- include: else-expressions

statements:
Expand Down Expand Up @@ -653,8 +653,8 @@ contexts:
# https://docs.oracle.com/javase/specs/jls/se13/html/jls-8.html#jls-8.1.6
- meta_scope: meta.class.java meta.block.java
- include: block-end
- include: member-statements
- include: member-declarations
- include: member-statements
- include: else-expressions

###[ ENUM DECLARATIONS ]#######################################################
Expand Down Expand Up @@ -1135,14 +1135,14 @@ contexts:
method-attributes:
# https://docs.oracle.com/javase/specs/jls/se13/html/jls-8.html#jls-8.4.6
- match: throws{{break}}
scope: keyword.declaration.throws.java
scope: storage.modifier.throws.java
set:
- method-throws-body
- maybe-illegal-array-modifiers
- expect-object-type
# https://docs.oracle.com/javase/specs/jls/se13/html/jls-9.html#jls-9.6.2
- match: default{{break}}
scope: keyword.declaration.default.java
scope: storage.modifier.default.java
set: method-default-body
- include: else-pop

Expand Down Expand Up @@ -1764,8 +1764,8 @@ contexts:
# https://docs.oracle.com/javase/specs/jls/se13/html/jls-14.html#jls-14.2
- meta_scope: meta.block.java
- include: block-end
- include: statements
- include: declarations
- include: statements
- include: else-expressions

###[ GROUPS ]##################################################################
Expand Down
58 changes: 45 additions & 13 deletions Java/tests/syntax_test_java.java
Original file line number Diff line number Diff line change
Expand Up @@ -2274,7 +2274,7 @@ interface // comment
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^^ constant.numeric

boolean booleanValue() default true;
Expand All @@ -2288,7 +2288,7 @@ interface // comment
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^^^^ constant.language

char charValue() default 'S';
Expand All @@ -2302,7 +2302,7 @@ interface // comment
// ^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^^^ string.quoted.single.java

String value() default "Default value";
Expand All @@ -2316,7 +2316,7 @@ interface // comment
// ^ punctuation.section.group.end.java
// ^^^^^^^^^^^^^^^^^^^^^^^ meta.function.default.java
// ^ - meta.function
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^^^^^^^^^^^^^^^ string.quoted.double.java

Class<?> classValue() default String.class;
Expand All @@ -2330,7 +2330,7 @@ interface // comment
// ^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^^^^^^ storage.type.class.java
// ^ punctuation.accessor.dot.java
// ^^^^^ variable.language.class.java - storage.type.java
Expand All @@ -2348,7 +2348,7 @@ interface // comment
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^ storage.modifier.array.java
// ^^^^^^^ keyword.declaration.default.java
// ^^^^^^^ storage.modifier.default.java
// ^ punctuation.section.braces.begin.java
// ^^^^^ string.quoted.double.java
// ^ punctuation.separator.comma.java
Expand Down Expand Up @@ -3413,6 +3413,38 @@ void method()
// ^^^ variable.parameter.java
// ^ punctuation.terminator.java

default void method() {}
//^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.java meta.block.java meta.function - meta.function meta.function
//^^^^^^^^ meta.function.modifier.java
// ^^^^^ meta.function.return-type.java
// ^^^^^^ meta.function.identifier.java
// ^^ meta.function.parameters.java meta.group.java
// ^ meta.function.java - meta.group - meta.block meta.block
// ^^ meta.function.java meta.block.java
//^^^^^^^ storage.modifier.java
// ^^^^ storage.type.void.java
// ^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^ punctuation.section.block.begin.java
// ^ punctuation.section.block.end.java

default MyType method() {}
//^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.java meta.block.java meta.function - meta.function meta.function
//^^^^^^^^ meta.function.modifier.java
// ^^^^^^^ meta.function.return-type.java
// ^^^^^^ meta.function.identifier.java
// ^^ meta.function.parameters.java meta.group.java
// ^ meta.function.java - meta.group - meta.block meta.block
// ^^ meta.function.java meta.block.java
//^^^^^^^ storage.modifier.java
// ^^^^^^ storage.type.class.java
// ^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^ punctuation.section.block.begin.java
// ^ punctuation.section.block.end.java

static void method()
//^^^^^^^^^^^^^^^^^^^^^ meta.class.java meta.block.java meta.function - meta.function meta.function
//^^^^^^^ meta.function.modifier.java
Expand Down Expand Up @@ -3627,7 +3659,7 @@ private static int methodThrows() throws MyException<Abc> {
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
// ^^^^^^^^^^^ storage.type.class.java
// ^ punctuation.definition.generic.begin.java
// ^^^ storage.type.class.java
Expand Down Expand Up @@ -3673,7 +3705,7 @@ private static int methodthrows() throws int {}
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
// ^^^ invalid.illegal.unexpected-keyword.java
// ^ punctuation.section.block.begin.java
// ^ punctuation.section.block.end.java
Expand All @@ -3694,7 +3726,7 @@ private static int methodthrows() throws array[] {}
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
// ^^^^^ storage.type.class.java
// ^^ invalid.illegal.unexpected-modifier.java
// ^ punctuation.section.block.begin.java
Expand All @@ -3717,7 +3749,7 @@ private static int methodthrows() throws myexception<abc> {}
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
// ^^^^^^^^^^^ storage.type.class.java
// ^ punctuation.definition.generic.begin.java
// ^^^ storage.type.class.java
Expand Down Expand Up @@ -3746,7 +3778,7 @@ private static int methodthrows() throws java.myexception<java.abc> {}
// ^^^^^^^^^^^^ entity.name.function.java
// ^ punctuation.section.group.begin.java
// ^ punctuation.section.group.end.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
// ^^^^ variable.namespace.java
// ^ punctuation.accessor.dot.java
// ^^^^^^^^^^^ storage.type.class.java
Expand Down Expand Up @@ -4099,7 +4131,7 @@ public static <T> T genericTypeMethod(Collection<? extends T>, Sink<T>) {}
//^^^^^^^^^ meta.class.java meta.block.java meta.function - meta.function meta.function
//^^ meta.function.java
// ^^^^^^^ meta.function.throws.java
// ^^^^^^ keyword.declaration.throws.java
// ^^^^^^ storage.modifier.throws.java
Exception
//^^^^^^^^^^^^^^^^ meta.function.throws.java - meta.function meta.function
// ^^^^^^^^^ storage.type.class.java
Expand Down Expand Up @@ -4145,7 +4177,7 @@ public void someReallyReallyLongMethodNameThatMakesTheBraceOverflowToTheNextLine
// ^ meta.function.parameters.java variable.parameter.java
// ^ punctuation.section.group.end
throws Exception {
// ^ meta.function.throws.java keyword.declaration.throws.java
// ^ meta.function.throws.java storage.modifier.throws.java
// ^ meta.function.throws.java storage.type.class.java
return someMethod (new Function<V, V>() {
// ^ - meta.function-call
Expand Down

0 comments on commit e754ec4

Please sign in to comment.