From 13b5f3d866b405222e49fc9c0022b15aebab58ea Mon Sep 17 00:00:00 2001 From: Andrey Cherkasov Date: Mon, 9 Jan 2023 16:31:56 +0400 Subject: [PATCH] [java-highlighting] Record Patterns (Second Preview): remove support for named record patterns IDEA-310052 GitOrigin-RevId: 0128d70c6343a65e44bf14bef7d89aeb94599c71 --- .../daemon/impl/analysis/HighlightVisitorImpl.java | 9 +++++++++ .../src/messages/JavaErrorBundle.properties | 1 + .../RecordPatternsInForEachJava20.java | 13 +++++++++++++ 3 files changed, 23 insertions(+) diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java index fe7ebbb6ce3c..501875a33916 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/daemon/impl/analysis/HighlightVisitorImpl.java @@ -1963,6 +1963,15 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh } } + @Override + public void visitPatternVariable(@NotNull PsiPatternVariable variable) { + super.visitPatternVariable(variable); + if (myLanguageLevel.isAtLeast(LanguageLevel.JDK_20_PREVIEW) && variable.getPattern() instanceof PsiDeconstructionPattern) { + String message = JavaErrorBundle.message("identifier.is.not.allowed.here"); + add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(variable).descriptionAndTooltip(message)); + } + } + @Nullable private static HighlightInfo.Builder checkGuardingExpressionHasBooleanType(@Nullable PsiExpression guardingExpression) { if (guardingExpression != null && !TypeConversionUtil.isBooleanType(guardingExpression.getType())) { diff --git a/java/java-psi-impl/src/messages/JavaErrorBundle.properties b/java/java-psi-impl/src/messages/JavaErrorBundle.properties index f37e22f610e8..6fc3e2945d0b 100644 --- a/java/java-psi-impl/src/messages/JavaErrorBundle.properties +++ b/java/java-psi-impl/src/messages/JavaErrorBundle.properties @@ -275,6 +275,7 @@ switch.expr.rule.should.produce.result=Switch expression rule should produce res guarded.patterns.unavailable=Guarded patterns from JEP 406 are not available since Java 19 preview incorrect.number.of.nested.patterns=Incorrect number of nested patterns: expected {0} but found {1} pattern.is.not.exhaustive=Pattern ''{0}'' is not exhaustive on ''{1}'' +identifier.is.not.allowed.here=Identifier is not allowed here illegal.forward.reference=Illegal forward reference illegal.self.reference=Illegal self reference diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatterns/RecordPatternsInForEachJava20.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatterns/RecordPatternsInForEachJava20.java index 02ad2abc2153..38cc2dbc1c9d 100644 --- a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatterns/RecordPatternsInForEachJava20.java +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/advHighlightingPatterns/RecordPatternsInForEachJava20.java @@ -70,4 +70,17 @@ class Main { System.out.println(s); } } + + void testNamedRecordPattern(Object obj, List rectangles) { + if (obj instanceof Point(int x, int y) point) { + } + switch (obj) { + case Point(int x, int y) point -> System.out.println("point"); + case Rect(Point point1, Point(int x2, int y2) point2) rect -> System.out.println("rectangle"); + } + + for (Rect(Point(int x1, int y1) point1, Point(int x2, int y2) point2) : rectangles) { + System.out.println("blah blah blah"); + } + } }