mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-47083 Quickfix to convert Java-style array creation to Groovy-style
This commit is contained in:
@@ -375,6 +375,11 @@
|
||||
-->
|
||||
|
||||
<!-- conversions -->
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
<className>org.jetbrains.plugins.groovy.intentions.conversions.ConvertJavaStyleArrayCreationIntention</className>
|
||||
</intentionAction>
|
||||
<intentionAction>
|
||||
<bundleName>org.jetbrains.plugins.groovy.intentions.GroovyIntentionsBundle</bundleName>
|
||||
<categoryKey>intention.category.groovy/intention.category.conversions</categoryKey>
|
||||
|
||||
+4
-1
@@ -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
|
||||
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
|
||||
+65
@@ -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;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+32
@@ -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);
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
def foo = new Ob<caret>ject[] {1, 2, 3}
|
||||
+1
@@ -0,0 +1 @@
|
||||
def foo = [1, 2, 3]
|
||||
Reference in New Issue
Block a user