IDEA-45767 ("unwrap" for groovy)

Add unwrapper for 'else' branch.
This commit is contained in:
Sergey Evdokimov
2011-09-19 15:58:53 +04:00
parent 5dbc04fd67
commit b2ef1abf03
8 changed files with 225 additions and 24 deletions
@@ -84,7 +84,7 @@ public abstract class GroovyPsiElementFactory implements JVMElementFactory {
public abstract GrNamedArgument createNamedArgument(String name, GrExpression expression);
public abstract GrStatement createStatementFromText(String text);
public abstract GrStatement createStatementFromText(String text, PsiElement context);
public abstract GrStatement createStatementFromText(String text, @Nullable PsiElement context);
public abstract GrBlockStatement createBlockStatement(GrStatement... statements);
@@ -519,7 +519,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory {
}
@Override
public GrStatement createStatementFromText(String text, PsiElement context) {
public GrStatement createStatementFromText(String text, @Nullable PsiElement context) {
PsiFile file = createGroovyFile(text, false, context);
assert ((GroovyFileBase) file).getTopStatements()[0] instanceof GrStatement;
return (GrStatement) ((GroovyFileBase) file).getTopStatements()[0];
@@ -77,6 +77,20 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState
return null;
}
@Override
public void deleteChildInternal(@NotNull ASTNode child) {
GrStatement elseBranch = getElseBranch();
if (elseBranch != null && child == elseBranch.getNode()) {
PsiElement elseKeywordElement = findChildByType(GroovyTokenTypes.kELSE);
if (elseKeywordElement != null) {
super.deleteChildInternal(elseKeywordElement.getNode());
}
}
super.deleteChildInternal(child);
}
public <T extends GrStatement> T replaceThenBranch(T newBranch) throws IncorrectOperationException {
return PsiImplUtil.replaceBody(newBranch, getThenBranch(), getNode(), getProject());
}
@@ -0,0 +1,58 @@
/*
* 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.unwrap;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import java.util.List;
public class GroovyElseRemover extends GroovyElseUnwrapperBase {
public GroovyElseRemover() {
super(CodeInsightBundle.message("remove.else"));
}
@Override
public PsiElement collectAffectedElements(PsiElement e, List<PsiElement> toExtract) {
super.collectAffectedElements(e, toExtract);
return ((GrIfStatement)e.getParent()).getElseBranch();
}
@Override
protected void unwrapElseBranch(GrStatement branch, PsiElement parent, Context context) throws IncorrectOperationException {
if (branch instanceof GrIfStatement) {
deleteSelectedElseIf((GrIfStatement)branch, context);
}
else {
context.delete(branch);
}
}
private static void deleteSelectedElseIf(GrIfStatement selectedBranch, Context context) throws IncorrectOperationException {
GrIfStatement parentIf = (GrIfStatement)selectedBranch.getParent();
GrStatement childElse = selectedBranch.getElseBranch();
if (childElse == null) {
context.delete(selectedBranch);
return;
}
context.setElseBranch(parentIf, childElse);
}
}
@@ -0,0 +1,56 @@
/*
* 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.unwrap;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import java.util.List;
public class GroovyElseUnwrapper extends GroovyElseUnwrapperBase {
public GroovyElseUnwrapper() {
super(CodeInsightBundle.message("unwrap.else"));
}
@Override
public PsiElement collectAffectedElements(PsiElement e, List<PsiElement> toExtract) {
super.collectAffectedElements(e, toExtract);
return findTopmostIfStatement(e);
}
@Override
protected void unwrapElseBranch(GrStatement branch, PsiElement parent, Context context) throws IncorrectOperationException {
// if we have 'else if' then we have to extract statements from the 'if' branch
if (branch instanceof GrIfStatement) {
branch = ((GrIfStatement)branch).getThenBranch();
}
parent = findTopmostIfStatement(parent);
context.extractFromBlockOrSingleStatement(branch, parent);
context.delete(parent);
}
private static PsiElement findTopmostIfStatement(PsiElement parent) {
while (parent.getParent() instanceof GrIfStatement) {
parent = parent.getParent();
}
return parent;
}
}
@@ -0,0 +1,72 @@
/*
* 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.unwrap;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiIfStatement;
import com.intellij.psi.PsiStatement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import java.util.Set;
public abstract class GroovyElseUnwrapperBase extends GroovyUnwrapper {
public GroovyElseUnwrapperBase(String description) {
super(description);
}
public boolean isApplicableTo(PsiElement e) {
return (isElseBlock(e) || isElseKeyword(e)) && isValidConstruct(e);
}
private static boolean isElseKeyword(PsiElement e) {
PsiElement p = e.getParent();
return p instanceof GrIfStatement && PsiUtil.isLeafElementOfType(e, GroovyTokenTypes.kELSE);
}
private static boolean isValidConstruct(PsiElement e) {
return ((GrIfStatement)e.getParent()).getElseBranch() != null;
}
@Override
public void collectElementsToIgnore(PsiElement element, Set<PsiElement> result) {
PsiElement parent = element.getParent();
while (parent instanceof GrIfStatement) {
result.add(parent);
parent = parent.getParent();
}
}
@Override
protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException {
GrStatement elseBranch;
if (isElseKeyword(element)) {
elseBranch = ((GrIfStatement)element.getParent()).getElseBranch();
}
else {
elseBranch = (GrStatement)element;
}
unwrapElseBranch(elseBranch, element.getParent(), context);
}
protected abstract void unwrapElseBranch(GrStatement branch, PsiElement parent, Context context) throws IncorrectOperationException;
}
@@ -15,19 +15,8 @@
*/
package org.jetbrains.plugins.groovy.unwrap;
import com.intellij.codeInsight.unwrap.*;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.intellij.codeInsight.unwrap.JavaUnwrapDescriptor;
import com.intellij.codeInsight.unwrap.Unwrapper;
public class GroovyUnwrapDescriptor extends JavaUnwrapDescriptor {
@@ -42,6 +31,8 @@ public class GroovyUnwrapDescriptor extends JavaUnwrapDescriptor {
new GroovyForUnwrapper(),
new GroovyCatchRemover(),
new GroovyBracesUnwrapper(),
new GroovyElseRemover(),
new GroovyElseUnwrapper(),
};
}
}
@@ -16,24 +16,17 @@
package org.jetbrains.plugins.groovy.unwrap;
import com.intellij.codeInsight.unwrap.AbstractUnwrapper;
import com.intellij.codeInsight.unwrap.Unwrapper;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.*;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
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.GroovyPsiElementFactory;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrBlockStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock;
import org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public abstract class GroovyUnwrapper extends AbstractUnwrapper<GroovyUnwrapper.Context> {
public GroovyUnwrapper(String description) {
super(description);
@@ -99,5 +92,22 @@ public abstract class GroovyUnwrapper extends AbstractUnwrapper<GroovyUnwrapper.
protected boolean isWhiteSpace(PsiElement element) {
return PsiUtil.isLeafElementOfType(element, TokenSets.WHITE_SPACES_SET);
}
public void setElseBranch(GrIfStatement ifStatement, GrStatement elseBranch) throws IncorrectOperationException {
GrStatement toExtract = elseBranch;
if (myIsEffective) {
ifStatement.replaceElseBranch(copyElement(elseBranch));
toExtract = ifStatement.getElseBranch();
}
addElementToExtract(toExtract);
}
private static GrStatement copyElement(GrStatement e) throws IncorrectOperationException {
// We cannot call el.copy() for 'else' since it sets context to parent 'if'.
// This causes copy to be invalidated after parent 'if' is removed by setElseBranch method.
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(e.getProject());
return factory.createStatementFromText(e.getText(), null);
}
}
}