groovy surround with for

surrounders in the same order as in Java (IDEA-48059)
This commit is contained in:
peter
2011-03-29 20:06:40 +02:00
parent 1227dd4181
commit bd146fa96c
13 changed files with 165 additions and 94 deletions
+1 -1
View File
@@ -174,7 +174,7 @@
<lang.refactoringSupport language="Groovy"
implementationClass="org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringSupportProvider"/>
<lang.surroundDescriptor language="Groovy"
implementationClass="org.jetbrains.plugins.groovy.lang.surroundWith.descriptors.GroovyStmtsSurroundDescriptor"/>
implementationClass="org.jetbrains.plugins.groovy.lang.surroundWith.descriptors.GroovySurroundDescriptor"/>
<lang.findUsagesProvider language="Groovy" implementationClass="org.jetbrains.plugins.groovy.findUsages.GroovyFindUsagesProvider"/>
<readWriteAccessDetector implementation="org.jetbrains.plugins.groovy.findUsages.GroovyReadWriteAccessDetector" order="before java"/>
<findUsagesHandlerFactory implementation="org.jetbrains.plugins.groovy.findUsages.GroovyFindUsagesHandlerFactory"/>
@@ -1,82 +0,0 @@
/*
* Copyright 2000-2009 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.lang.surroundWith.descriptors;
import com.intellij.lang.surroundWith.Surrounder;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovySurrounderByClosure;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.blocks.open.*;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithParenthesisExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithTypeCastSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithWithExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfElseExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithWhileExprSurrounder;
import java.util.ArrayList;
import java.util.List;
/**
* User: Dmitry.Krasilschikov
* Date: 22.05.2007
*/
public class GroovyStmtsSurroundDescriptor extends GroovySurroundDescriptor {
private static final Surrounder[] stmtsSurrounders = new Surrounder[]{
new GroovyWithWithStatementsSurrounder(),
new GroovyWithIfSurrounder(),
new GroovyWithIfElseSurrounder(),
new GroovyWithTryCatchSurrounder(),
new GroovyWithTryFinallySurrounder(),
new GroovyWithTryCatchFinallySurrounder(),
new GroovyWithWhileSurrounder(),
new GroovySurrounderByClosure(),
new GroovyWithShouldFailWithTypeStatementsSurrounder(),
};
/********** ***********/
private static final Surrounder[] exprSurrounders = new Surrounder[]{
new GroovyWithParenthesisExprSurrounder(),
new GroovyWithTypeCastSurrounder(),
new GroovyWithWithExprSurrounder(),
new GroovyWithIfExprSurrounder(),
new GroovyWithIfElseExprSurrounder(),
new GroovyWithWhileExprSurrounder()
};
public static Surrounder[] getStmtsSurrounders() {
return stmtsSurrounders;
}
public static Surrounder[] getExprSurrounders() {
return exprSurrounders;
}
@NotNull
public Surrounder[] getSurrounders() {
List<Surrounder> surroundersList = new ArrayList<Surrounder>();
ContainerUtil.addAll(surroundersList, exprSurrounders);
ContainerUtil.addAll(surroundersList, stmtsSurrounders);
return surroundersList.toArray(new Surrounder[0]);
}
}
@@ -17,6 +17,7 @@ package org.jetbrains.plugins.groovy.lang.surroundWith.descriptors;
import com.intellij.lang.ASTNode;
import com.intellij.lang.surroundWith.SurroundDescriptor;
import com.intellij.lang.surroundWith.Surrounder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
@@ -26,6 +27,14 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.lexer.TokenSets;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovySurrounderByClosure;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.blocks.open.*;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithParenthesisExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithTypeCastSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.GroovyWithWithExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfElseExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithIfExprSurrounder;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersImpl.expressions.conditions.GroovyWithWhileExprSurrounder;
import java.util.ArrayList;
import java.util.List;
@@ -34,7 +43,40 @@ import java.util.List;
* User: Dmitry.Krasilschikov
* Date: 22.05.2007
*/
public abstract class GroovySurroundDescriptor implements SurroundDescriptor {
public class GroovySurroundDescriptor implements SurroundDescriptor {
private static final Surrounder[] ourSurrounders = new Surrounder[]{
//statements: like in java
new GroovyWithIfSurrounder(),
new GroovyWithIfElseSurrounder(),
new GroovyWithWhileSurrounder(),
//there's no do-while in Groovy
new GroovySurrounderByClosure(),
//like in Java
new GroovyWithForSurrounder(),
new GroovyWithTryCatchSurrounder(),
new GroovyWithTryFinallySurrounder(),
new GroovyWithTryCatchFinallySurrounder(),
//groovy-specific statements
new GroovyWithShouldFailWithTypeStatementsSurrounder(),
//expressions: like in java
new GroovyWithParenthesisExprSurrounder(),
new GroovyWithTypeCastSurrounder(),
//groovy-specific
new GroovyWithWithExprSurrounder(),
new GroovyWithWithStatementsSurrounder(),
new GroovyWithIfExprSurrounder(),
new GroovyWithIfElseExprSurrounder(),
new GroovyWithWhileExprSurrounder()
};
@NotNull
public Surrounder[] getSurrounders() {
return ourSurrounders;
}
@NotNull
public PsiElement[] getElementsToSurround(PsiFile file, int startOffset, int endOffset) {
GrStatement[] statements = findStatementsInRange(file, startOffset, endOffset);
@@ -44,7 +86,7 @@ public abstract class GroovySurroundDescriptor implements SurroundDescriptor {
}
@Nullable
private GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
private static GrStatement[] findStatementsInRange(PsiFile file, int startOffset, int endOffset) {
GrStatement statement;
int endOffsetLocal = endOffset;
@@ -0,0 +1,55 @@
/*
* Copyright 2000-2011 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.lang.surroundWith.surrounders.surroundersImpl.blocks.open;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovyManyStatementsSurrounder;
/**
* User: Dmitry.Krasilschikov
* Date: 25.05.2007
*/
public class GroovyWithForSurrounder extends GroovyManyStatementsSurrounder {
protected GroovyPsiElement doSurroundElements(PsiElement[] elements) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
GrForStatement whileStatement = (GrForStatement) factory.createTopElementFromText("for(a in b){\n}");
addStatements(((GrBlockStatement) whileStatement.getBody()).getBlock(), elements);
return whileStatement;
}
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
assert element instanceof GrForStatement;
GrForClause clause = ((GrForStatement) element).getClause();
int endOffset = element.getTextRange().getEndOffset();
if (clause != null) {
endOffset = clause.getTextRange().getStartOffset();
clause.getParent().getNode().removeChild(clause.getNode());
}
return new TextRange(endOffset, endOffset);
}
public String getTemplateDescription() {
return "for";
}
}
@@ -37,6 +37,6 @@ public class GroovyWithIfElseSurrounder extends GroovyWithIfSurrounder {
@Override
public String getTemplateDescription() {
return super.getTemplateDescription() + " / else";
return "if / else";
}
}
@@ -56,7 +56,7 @@ public class GroovyWithIfSurrounder extends GroovyManyStatementsSurrounder {
}
public String getTemplateDescription() {
return "if () {...}";
return "if";
}
public boolean isApplicable(@NotNull PsiElement[] elements) {
@@ -17,14 +17,15 @@ package org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.surroundersIm
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiPrimitiveType;
import com.intellij.psi.PsiType;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement;
import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.*;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression;
import org.jetbrains.plugins.groovy.lang.surroundWith.surrounders.GroovyManyStatementsSurrounder;
@@ -57,13 +58,13 @@ public class GroovyWithWhileSurrounder extends GroovyManyStatementsSurrounder {
if (elements.length == 1 && elements[0] instanceof GrStatement) {
if (elements[0] instanceof GrExpression) {
PsiType type = ((GrExpression) elements[0]).getType();
return type == null || !((PsiPrimitiveType) PsiType.BOOLEAN).getBoxedTypeName().equals(type.getCanonicalText());
return type == null || !PsiType.BOOLEAN.getBoxedTypeName().equals(type.getCanonicalText());
}
}
return true;
}
public String getTemplateDescription() {
return "while () {...}";
return "while";
}
}
@@ -34,6 +34,6 @@ public class GroovyWithParenthesisExprSurrounder extends GroovyExpressionSurroun
}
public String getTemplateDescription() {
return "(...)";
return "(expr)";
}
}
@@ -42,6 +42,6 @@ public class GroovyWithTypeCastSurrounder extends GroovyExpressionSurrounder {
}
public String getTemplateDescription() {
return "((Type) ...)";
return "((Type) expr)";
}
}
@@ -42,6 +42,6 @@ public class GroovyWithWithExprSurrounder extends GroovyExpressionSurrounder {
}
public String getTemplateDescription() {
return "with (...)";
return "with (expr)";
}
}
@@ -28,5 +28,6 @@ public class SurroundStatementsTest extends SurroundTestCase {
public void testTry_finally1() throws Exception { doTest(new GroovyWithTryFinallySurrounder()); }
public void testWhile1() throws Exception { doTest(new GroovyWithWhileSurrounder()); }
public void testWith2() throws Exception { doTest(new GroovyWithWithStatementsSurrounder()); }
public void testFor1() throws Exception { doTest(new GroovyWithForSurrounder()); }
}
@@ -0,0 +1,49 @@
/*
* Copyright 2000-2011 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.lang.surroundWith
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler
import com.intellij.openapi.actionSystem.Separator
/**
* @author peter
*/
class SurrounderOrderTest extends LightCodeInsightFixtureTestCase {
public void testStatementSurrounders() {
myFixture.configureByText("a.groovy", "<selection>println a</selection>")
def actions = SurroundWithHandler.buildSurroundActions(project, myFixture.editor, myFixture.file, null)
def names = []
for (action in actions) {
if (action instanceof Separator) {
break
}
def text = action.templatePresentation.text
names << text.substring(text.indexOf('. ') + 2)
}
assertOrderedEquals names,
"if", "if / else", "while",
"{ -> ... }.call()",
"for", "try / catch", "try / finally", "try / catch / finally",
"shouldFail () {...}",
"(expr)", "((Type) expr)", "with (expr)",
"with () {...}"
}
}
@@ -0,0 +1,5 @@
<selection>println "foo"</selection>
-----
for (<caret>) {
println "foo"
}