From 04095647a5ab2d7c857893be1a11e354dadd080f Mon Sep 17 00:00:00 2001 From: Maxim Medvedev Date: Tue, 20 Jul 2010 16:04:36 +0400 Subject: [PATCH] IDEA-47083 Quickfix to convert Java-style array creation to Groovy-style --- plugins/groovy/src/META-INF/plugin.xml | 5 ++ .../GroovyIntentionsBundle.properties | 5 +- ...onvertJavaStyleArrayCreationIntention.java | 65 +++++++++++++++++++ .../ConvertJavaStyleArrayCreationTest.java | 32 +++++++++ .../Conversion.groovy | 1 + .../Conversion_after.groovy | 1 + 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertJavaStyleArrayCreationIntention.java create mode 100644 plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ConvertJavaStyleArrayCreationTest.java create mode 100644 plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion.groovy create mode 100644 plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion_after.groovy diff --git a/plugins/groovy/src/META-INF/plugin.xml b/plugins/groovy/src/META-INF/plugin.xml index 0418c136e6bf..1eedae80b8bd 100644 --- a/plugins/groovy/src/META-INF/plugin.xml +++ b/plugins/groovy/src/META-INF/plugin.xml @@ -375,6 +375,11 @@ --> + + org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle + intention.category.groovy/intention.category.conversions + org.jetbrains.plugins.groovy.intentions.conversions.ConvertJavaStyleArrayCreationIntention + org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle intention.category.groovy/intention.category.conversions diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties index 113f31b0d971..beb7dffac824 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/GroovyIntentionsBundle.properties @@ -82,4 +82,7 @@ do.not.create.first.new=Use &existing first parameter as map for named arguments convert.cannot.itself=Refactoring is not available for map parameter itself perform.refactoring=Performing refactoring map.param.name=Map parameter &name: -convert.param.to.map.entry=Convert parameter to map entry \ No newline at end of file +convert.param.to.map.entry=Convert parameter to map entry + +convert.java.style.array.creation.intention.name=Convert java-style array creation +convert.java.style.array.creation.intention.family.name=Convert Java-Style Array Creation to Groovy Syntax \ No newline at end of file diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertJavaStyleArrayCreationIntention.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertJavaStyleArrayCreationIntention.java new file mode 100644 index 000000000000..a2c6052d879e --- /dev/null +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertJavaStyleArrayCreationIntention.java @@ -0,0 +1,65 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.intentions.conversions; + +import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.plugins.groovy.intentions.base.Intention; +import org.jetbrains.plugins.groovy.intentions.base.PsiElementPredicate; +import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression; +import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression; + +/** + * @author Maxim.Medvedev + */ +public class ConvertJavaStyleArrayCreationIntention extends Intention { + @Override + protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { + final GrClosableBlock block = ((GrMethodCallExpression)element).getClosureArguments()[0]; + final String text = block.getText(); + int start = block.getLBrace().getStartOffsetInParent() + 1; + int finish = block.getRBrace().getStartOffsetInParent(); + String newText = "[" + text.substring(start, finish) + "]"; + final GrExpression newExpr = GroovyPsiElementFactory.getInstance(element.getProject()).createExpressionFromText(newText); + ((GrMethodCallExpression)element).replaceWithStatement(newExpr); + } + + @NotNull + @Override + protected PsiElementPredicate getElementPredicate() { + return new PsiElementPredicate() { + @Override + public boolean satisfiedBy(PsiElement element) { + if (!(element instanceof GrMethodCallExpression)) return false; + final GrExpression expression = ((GrMethodCallExpression)element).getInvokedExpression(); + if (!(expression instanceof GrNewExpression)) return false; + if (((GrNewExpression)expression).getArrayCount() == 0) return false; + + if (((GrMethodCallExpression)element).getArgumentList().getText().trim().length() > 0) return false; + + final GrClosableBlock[] closureArguments = ((GrMethodCallExpression)element).getClosureArguments(); + if (closureArguments.length != 1) return false; + final GrClosableBlock block = closureArguments[0]; + if (block.getLBrace() == null || block.getRBrace() == null) return false; + return true; + } + }; + } +} diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ConvertJavaStyleArrayCreationTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ConvertJavaStyleArrayCreationTest.java new file mode 100644 index 000000000000..0353936fa62a --- /dev/null +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ConvertJavaStyleArrayCreationTest.java @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2010 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.plugins.groovy.intentions; + +import org.jetbrains.plugins.groovy.util.TestUtils; + +/** + * @author Maxim.Medvedev + */ +public class ConvertJavaStyleArrayCreationTest extends GrIntentionTestCase{ + @Override + protected String getBasePath() { + return TestUtils.getTestDataPath() + "intentions/convertJavaStyleArrayCreation/"; + } + + public void testConversion() { + doTest(GroovyIntentionsBundle.message("convert.java.style.array.creation.intention.name"), true); + } +} diff --git a/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion.groovy b/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion.groovy new file mode 100644 index 000000000000..11781d79e883 --- /dev/null +++ b/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion.groovy @@ -0,0 +1 @@ +def foo = new Object[] {1, 2, 3} \ No newline at end of file diff --git a/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion_after.groovy b/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion_after.groovy new file mode 100644 index 000000000000..2be10eba8fd1 --- /dev/null +++ b/plugins/groovy/testdata/intentions/convertJavaStyleArrayCreation/Conversion_after.groovy @@ -0,0 +1 @@ +def foo = [1, 2, 3] \ No newline at end of file