From 3d42a6ea9aec4b33e1cc05bf2acf89840f7c4311 Mon Sep 17 00:00:00 2001 From: SirYwell Date: Mon, 12 Dec 2022 19:59:25 +0100 Subject: [PATCH] Groovy: Fix parenthesizing of ranges in ForToEachIntention closes https://github.com/JetBrains/intellij-community/pull/2271 GitOrigin-RevId: a8ad04ce835e30ba06d8bc4a45c7f3d9d032087b --- .../lang/psi/impl/utils/ParenthesesUtils.java | 2 ++ .../forToEach/ForToEachIntentionTest.groovy | 26 +++++++++++++++++++ .../ForToEachOnRangeWithParentheses.groovy | 3 +++ ...rToEachOnRangeWithParentheses_after.groovy | 3 +++ .../ForToEachOnRangeWithoutParentheses.groovy | 3 +++ ...EachOnRangeWithoutParentheses_after.groovy | 3 +++ 6 files changed, 40 insertions(+) create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/closure/forToEach/ForToEachIntentionTest.groovy create mode 100644 plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses.groovy create mode 100644 plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses_after.groovy create mode 100644 plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses.groovy create mode 100644 plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses_after.groovy diff --git a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/utils/ParenthesesUtils.java b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/utils/ParenthesesUtils.java index f8707658303b..4a921d6fc18c 100644 --- a/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/utils/ParenthesesUtils.java +++ b/plugins/groovy/groovy-psi/src/org/jetbrains/plugins/groovy/lang/psi/impl/utils/ParenthesesUtils.java @@ -8,6 +8,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.parser.GroovyElementTypes; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; +import org.jetbrains.plugins.groovy.lang.psi.api.GrRangeExpression; import org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.*; @@ -129,6 +130,7 @@ public final class ParenthesesUtils { public static int getPrecedence(GrExpression expr) { if (expr instanceof GrUnaryExpression) return ((GrUnaryExpression)expr).isPostfix() ? POSTFIX_PRECEDENCE : PREFIX_PRECEDENCE; if (expr instanceof GrTypeCastExpression) return TYPE_CAST_PRECEDENCE; + if (expr instanceof GrRangeExpression) return RANGE_PRECEDENCE; if (expr instanceof GrConditionalExpression) return CONDITIONAL_PRECEDENCE; if (expr instanceof GrSafeCastExpression) return SAFE_CAST_PRECEDENCE; if (expr instanceof GrAssignmentExpression) return ASSIGNMENT_PRECEDENCE; diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/closure/forToEach/ForToEachIntentionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/closure/forToEach/ForToEachIntentionTest.groovy new file mode 100644 index 000000000000..3fda6eba022d --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/closure/forToEach/ForToEachIntentionTest.groovy @@ -0,0 +1,26 @@ +// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package org.jetbrains.plugins.groovy.intentions.closure.forToEach; + +import org.jetbrains.plugins.groovy.intentions.GrIntentionTestCase; +import org.jetbrains.plugins.groovy.intentions.closure.ForToEachIntention; +import org.jetbrains.plugins.groovy.util.TestUtils; + +class ForToEachIntentionTest extends GrIntentionTestCase { + + ForToEachIntentionTest() { + super(ForToEachIntention.class) + } + + @Override + protected String getBasePath() { + return TestUtils.testDataPath + "intentions/ForToEach/" + } + + void testForToEachOnRangeWithoutParentheses() { + doTest(true) + } + + void testForToEachOnRangeWithParentheses() { + doTest(true) + } +} diff --git a/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses.groovy b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses.groovy new file mode 100644 index 000000000000..a2d1d8692780 --- /dev/null +++ b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses.groovy @@ -0,0 +1,3 @@ +for (i in (0..<10)) { + println i +} diff --git a/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses_after.groovy b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses_after.groovy new file mode 100644 index 000000000000..dbce960416d0 --- /dev/null +++ b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithParentheses_after.groovy @@ -0,0 +1,3 @@ +(0..<10).each { i -> + println i +} diff --git a/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses.groovy b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses.groovy new file mode 100644 index 000000000000..c217d95a4c0c --- /dev/null +++ b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses.groovy @@ -0,0 +1,3 @@ +for (i in 0..<10) { + println i +} diff --git a/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses_after.groovy b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses_after.groovy new file mode 100644 index 000000000000..dbce960416d0 --- /dev/null +++ b/plugins/groovy/testdata/intentions/ForToEach/ForToEachOnRangeWithoutParentheses_after.groovy @@ -0,0 +1,3 @@ +(0..<10).each { i -> + println i +}