|
|
|
|
@@ -1,5 +1,5 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright 2000-2013 JetBrains s.r.o.
|
|
|
|
|
* Copyright 2000-2016 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.
|
|
|
|
|
@@ -37,7 +37,6 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
|
|
|
|
import com.intellij.psi.codeStyle.SuggestedNameInfo;
|
|
|
|
|
import com.intellij.psi.codeStyle.VariableKind;
|
|
|
|
|
import com.intellij.psi.util.PsiTreeUtil;
|
|
|
|
|
import com.intellij.psi.util.PsiUtil;
|
|
|
|
|
import com.intellij.psi.util.TypeConversionUtil;
|
|
|
|
|
import com.intellij.refactoring.JavaRefactoringSettings;
|
|
|
|
|
import com.intellij.util.ArrayUtil;
|
|
|
|
|
@@ -45,9 +44,7 @@ import com.intellij.util.IncorrectOperationException;
|
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.LinkedHashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@@ -106,7 +103,10 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
|
|
|
|
PsiExpression initializer = ((PsiLocalVariable)element).getInitializer();
|
|
|
|
|
if (!(initializer instanceof PsiTypeCastExpression)) continue;
|
|
|
|
|
|
|
|
|
|
PsiTypeElement castTypeElement = ((PsiTypeCastExpression)initializer).getCastType();
|
|
|
|
|
final PsiTypeCastExpression typeCastExpression = (PsiTypeCastExpression)initializer;
|
|
|
|
|
final PsiExpression operand = typeCastExpression.getOperand();
|
|
|
|
|
if (operand != null && !PsiEquivalenceUtil.areElementsEquivalent(operand, instanceOfExpression.getOperand())) continue;
|
|
|
|
|
PsiTypeElement castTypeElement = typeCastExpression.getCastType();
|
|
|
|
|
if (castTypeElement == null) continue;
|
|
|
|
|
PsiType castType = castTypeElement.getType();
|
|
|
|
|
if (castType.equals(type)) return true;
|
|
|
|
|
@@ -213,7 +213,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
|
|
|
|
TemplateBuilderImpl builder = new TemplateBuilderImpl(localVariable);
|
|
|
|
|
builder.setEndVariableAfter(localVariable.getNameIdentifier());
|
|
|
|
|
|
|
|
|
|
Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType());
|
|
|
|
|
Template template = generateTemplate(project, localVariable.getInitializer(), localVariable.getType(), instanceOfExpression);
|
|
|
|
|
|
|
|
|
|
Editor newEditor = CreateFromUsageBaseFix.positionCursor(project, file, localVariable.getNameIdentifier());
|
|
|
|
|
if (newEditor == null) return;
|
|
|
|
|
@@ -343,11 +343,7 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
|
|
|
|
anchorAfter = newBranch.getCodeBlock().getLBrace();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
final PsiJavaToken lBrace = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
|
|
|
|
|
if (lBrace != null) {
|
|
|
|
|
final PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(lBrace, PsiWhiteSpace.class);
|
|
|
|
|
anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : lBrace;
|
|
|
|
|
}
|
|
|
|
|
anchorAfter = ((PsiBlockStatement)thenBranch).getCodeBlock().getLBrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@@ -377,6 +373,13 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
|
|
|
|
if (anchorAfter == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(anchorAfter, PsiWhiteSpace.class);
|
|
|
|
|
anchorAfter = nextSibling instanceof PsiComment ? PsiTreeUtil.skipSiblingsForward(nextSibling, PsiComment.class) : anchorAfter;
|
|
|
|
|
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
|
|
|
|
while (nextSibling instanceof PsiDeclarationStatement) {
|
|
|
|
|
anchorAfter = nextSibling;
|
|
|
|
|
nextSibling = PsiTreeUtil.getNextSiblingOfType(anchorAfter, PsiStatement.class);
|
|
|
|
|
}
|
|
|
|
|
return anchorAfter.getParent().addAfter(toInsert, anchorAfter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -395,31 +398,21 @@ public class CreateLocalVarFromInstanceofAction extends BaseIntentionAction {
|
|
|
|
|
return element instanceof PsiPrefixExpression && ((PsiPrefixExpression)element).getOperationTokenType() == JavaTokenType.EXCL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Template generateTemplate(Project project, PsiExpression initializer, PsiType type) {
|
|
|
|
|
private static Template generateTemplate(Project project, PsiExpression initializer, PsiType type, PsiElement place) {
|
|
|
|
|
final TemplateManager templateManager = TemplateManager.getInstance(project);
|
|
|
|
|
final Template template = templateManager.createTemplate("", "");
|
|
|
|
|
template.setToReformat(true);
|
|
|
|
|
|
|
|
|
|
SuggestedNameInfo suggestedNameInfo = JavaCodeStyleManager.getInstance(project).suggestVariableName(VariableKind.LOCAL_VARIABLE, null,
|
|
|
|
|
initializer, type);
|
|
|
|
|
List<String> uniqueNames = new ArrayList<String>();
|
|
|
|
|
for (String name : suggestedNameInfo.names) {
|
|
|
|
|
if (PsiUtil.isVariableNameUnique(name, initializer)) {
|
|
|
|
|
uniqueNames.add(name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (uniqueNames.isEmpty() && suggestedNameInfo.names.length != 0) {
|
|
|
|
|
String baseName = suggestedNameInfo.names[0];
|
|
|
|
|
String name = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(baseName, initializer, true);
|
|
|
|
|
uniqueNames.add(name);
|
|
|
|
|
}
|
|
|
|
|
initializer, type);
|
|
|
|
|
suggestedNameInfo = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName(suggestedNameInfo, place, true);
|
|
|
|
|
|
|
|
|
|
Set<LookupElement> itemSet = new LinkedHashSet<LookupElement>();
|
|
|
|
|
for (String name : uniqueNames) {
|
|
|
|
|
for (String name : suggestedNameInfo.names) {
|
|
|
|
|
itemSet.add(LookupElementBuilder.create(name));
|
|
|
|
|
}
|
|
|
|
|
final LookupElement[] lookupItems = itemSet.toArray(new LookupElement[itemSet.size()]);
|
|
|
|
|
final Result result = uniqueNames.isEmpty() ? null : new TextResult(uniqueNames.get(0));
|
|
|
|
|
final Result result = suggestedNameInfo.names.length == 0 ? null : new TextResult(suggestedNameInfo.names[0]);
|
|
|
|
|
|
|
|
|
|
Expression expr = new Expression() {
|
|
|
|
|
@Override
|
|
|
|
|
|