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

PHP: Improve enum cases #6825

Merged
merged 4 commits into from
Dec 11, 2023
Merged

Conversation

junichi11
Copy link
Member

@junichi11 junichi11 commented Dec 10, 2023

Add the enum case icon

CC Light

Before:

nb-php-enum-case-icon-light-cc-before

After:

nb-php-enum-case-icon-light-cc-after

Navigator Light

Before:

nb-php-enum-case-icon-light-navi-before

After:

nb-php-enum-case-icon-light-navi-after

CC Dark

Before:

nb-php-enum-case-icon-dark-cc-before

After:

nb-php-enum-case-icon-dark-cc-after

Navigator Dark

After:

nb-php-enum-case-icon-dark-navi-before

Before:

nb-php-enum-case-icon-dark-navi-after

Improve the code completion documentation for enum cases

  • Add the isBacked() method to the CaseElement and the EnumCaseElement interfaces
  • Don't add values of non-backed enums to the code completion documentation
  • Index whether it is an enum case of a backed enum as a boolean value
  • Fix/Add unit tests
  • Increment spec version

Before:

nb-php-improve-enum-case-cc-doc-before

After:

nb-php-improve-enum-case-cc-doc-after

Check an initializer of an enum case

  • Fix the IncorrectEnumHintErorr
    • Cases of non-backed enum must not have a value (e.g. the following
      is a fatal error: enum NonBacked {case EXAMPLE = 1;})
    • Cases of backed enum must have a value (e.g. the following is a
      fatal error: enum Backed: int {case EXAMPLE;})
  • Add/Fix unit tests

Before:

nb-php-check-enum-case-initializer-before

After:

nb-php-check-enum-case-initializer-after

Improve the code completion for enum cases

There is an error in the following example because case B = self:: is not a complete statement.
Thus, the result of parsing doesn't recognize the enum declaration.
This means that some features don't work correctly.
(e.g. The enum declaration doesn't exist in the navigator. The code completion doesn't work.)

enum Example: int {
    case A = 1;
    case B = self::
}

To improve this, add the ASTErrorExpression as a result of an enum case initializer part.

  • Fix the parser
  • Add unit tests

Before:

nb-php-improve-enum-case-navi-with-error-before

nb-php-improve-enum-case-cc-with-error-before

After:

nb-php-improve-enum-case-navi-with-error-after

nb-php-improve-enum-case-cc-with-error-after

- Add the `isBacked()` method to the `CaseElement` and the `EnumCaseElement` interfaces
- Don't add values of non-backed enums to the code completion documentation
- Index whether it is an enum case of a backed enum as a boolean value
- Fix/Add unit tests
- Increment spec version
@junichi11 junichi11 added the PHP [ci] enable extra PHP tests (php/php.editor) label Dec 10, 2023
@junichi11 junichi11 added this to the NB21 milestone Dec 10, 2023
@junichi11 junichi11 requested a review from tmysik December 10, 2023 02:40
@@ -289,15 +341,41 @@ private void checkStatements(List<Statement> statements, EnumDeclaration node) {
incorrectEnumProperties.add((FieldsDeclaration) statement);
} else if (statement instanceof UseTraitStatement) {
useTraits.put(node, (UseTraitStatement) statement);
} else if (statement instanceof CaseDeclaration) {
checkCaseDeclaration((CaseDeclaration) statement, isBackedEnum);

Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: an extra empty line perhaps?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

incorrectNonBackedEnumCases.add(caseDeclaration);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: an extra empty line?

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

}
}

}
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick:no empty line after this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Member

@tmysik tmysik left a comment

Choose a reason for hiding this comment

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

Really nice and useful improvement! 👍

@junichi11
Copy link
Member Author

I'll fix them later. Thank you, Tomas!

- Fix the `IncorrectEnumHintErorr`
  - Cases of non-backed enum must not have a value (e.g. the following
    is a fatal error: `enum NonBacked {case EXAMPLE = 1;}`)
  - Cases of backed enum must have a value (e.g. the following is a
    fatal error: `enum Backed: int {case EXAMPLE;}`)
- Add/Fix unit tests
There is an error in the following example because `case B = self::` is not a complete statement.
Thus, the result of parsing doesn't recognize the enum declaration.
This means that some features don't work correctly.
(e.g. The enum declaration doesn't exist in the navigator. The code completion doesn't work.)

```php
enum Example: int {
    case A = 1;
    case B = self::
}
```

To improve this, add the `ASTErrorExpression` as a result of an enum case initializer part.

- Fix the parser
- Add unit tests
@junichi11 junichi11 force-pushed the php-improve-enum-cases branch from 00870b5 to 52127d6 Compare December 11, 2023 03:43
@junichi11 junichi11 merged commit 8d3e459 into apache:master Dec 11, 2023
@junichi11 junichi11 deleted the php-improve-enum-cases branch December 11, 2023 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PHP [ci] enable extra PHP tests (php/php.editor)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants