Minor: Generify KotlinInplaceVariableIntroducer

This commit is contained in:
Alexey Sedunov
2015-04-02 17:50:04 +03:00
parent ee8df94f19
commit 95a8db458b
3 changed files with 35 additions and 28 deletions
@@ -42,7 +42,7 @@ public class KotlinInplacePropertyIntroducer(
exprType: JetType?,
extractionResult: ExtractionResult,
private val availableTargets: List<ExtractionTarget>
): KotlinInplaceVariableIntroducer(
): KotlinInplaceVariableIntroducer<JetProperty>(
property, editor, project, title, JetExpression.EMPTY_ARRAY, null, false, property, false, doNotChangeVar, exprType, false
) {
init {
@@ -58,9 +58,9 @@ public class KotlinInplacePropertyIntroducer(
$currentTarget = value
runWriteActionAndRestartRefactoring {
with (extractionResult.config) {
extractionResult = copy(generatorOptions = generatorOptions.copy(target = currentTarget)).generateDeclaration(myProperty)
myProperty = extractionResult.declaration as JetProperty
myElementToRename = myProperty
extractionResult = copy(generatorOptions = generatorOptions.copy(target = currentTarget)).generateDeclaration(property)
property = extractionResult.declaration as JetProperty
myElementToRename = property
}
}
updatePanelControls()
@@ -68,6 +68,12 @@ public class KotlinInplacePropertyIntroducer(
private var replaceAll: Boolean = true
protected var property: JetProperty
get() = myDeclaration
set(value: JetProperty) {
myDeclaration = value
}
private fun isInitializer(): Boolean = currentTarget == ExtractionTarget.PROPERTY_WITH_INITIALIZER
override fun initPanelControls() {
@@ -109,7 +115,7 @@ public class KotlinInplacePropertyIntroducer(
getCreateVarCheckBox()?.let {
val initializer = object: Pass<JComponent>() {
override fun pass(t: JComponent) {
(t as JCheckBox).setSelected(myProperty.isVar())
(t as JCheckBox).setSelected(property.isVar())
}
}
addPanelControl(ControlWrapper(it, condition, initializer))
@@ -117,7 +123,7 @@ public class KotlinInplacePropertyIntroducer(
getCreateExplicitTypeCheckBox()?.let {
val initializer = object: Pass<JComponent>() {
override fun pass(t: JComponent) {
(t as JCheckBox).setSelected(myProperty.getTypeReference() != null)
(t as JCheckBox).setSelected(property.getTypeReference() != null)
}
}
addPanelControl(ControlWrapper(it, condition, initializer))
@@ -43,10 +43,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.idea.intentions.SpecifyTypeExplicitlyAction;
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetProperty;
import org.jetbrains.kotlin.psi.JetPsiFactory;
import org.jetbrains.kotlin.psi.JetTypeReference;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.types.JetType;
import javax.swing.*;
@@ -58,7 +55,7 @@ import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<JetExpression> {
public class KotlinInplaceVariableIntroducer<D extends JetCallableDeclaration> extends InplaceVariableIntroducer<JetExpression> {
private static final Function0<Boolean> TRUE = new Function0<Boolean>() {
@Override
public Boolean invoke() {
@@ -113,7 +110,7 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
}
private final boolean myReplaceOccurrence;
protected JetProperty myProperty;
protected D myDeclaration;
private final boolean isVar;
private final boolean myDoNotChangeVar;
@Nullable private final JetType myExprType;
@@ -125,12 +122,12 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
PsiNamedElement elementToRename, Editor editor, Project project,
String title, JetExpression[] occurrences,
@Nullable JetExpression expr, boolean replaceOccurrence,
JetProperty property, boolean isVar, boolean doNotChangeVar,
D declaration, boolean isVar, boolean doNotChangeVar,
@Nullable JetType exprType, boolean noTypeInference
) {
super(elementToRename, editor, project, title, occurrences, expr);
this.myReplaceOccurrence = replaceOccurrence;
myProperty = property;
myDeclaration = declaration;
this.isVar = isVar;
myDoNotChangeVar = doNotChangeVar;
myExprType = exprType;
@@ -212,10 +209,10 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
if (exprTypeCheckbox.isSelected()) {
String renderedType =
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(myExprType);
myProperty.setTypeReference(new JetPsiFactory(myProject).createType(renderedType));
myDeclaration.setTypeReference(new JetPsiFactory(myProject).createType(renderedType));
}
else {
myProperty.setTypeReference(null);
myDeclaration.setTypeReference(null);
}
}
}
@@ -248,7 +245,11 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
JetPsiFactory psiFactory = new JetPsiFactory(myProject);
ASTNode node = varCheckbox.isSelected() ? psiFactory.createVarNode() : psiFactory.createValNode();
myProperty.getValOrVarNode().getPsi().replace(node.getPsi());
ASTNode valOrVarNode = myDeclaration instanceof JetProperty
? ((JetProperty) myDeclaration).getValOrVarNode()
: ((JetParameter) myDeclaration).getValOrVarNode();
valOrVarNode.getPsi().replace(node.getPsi());
}
}.execute();
}
@@ -266,7 +267,7 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
protected void run(@NotNull Result result) throws Throwable {
PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
ASTNode identifier = myProperty.getNode().findChildByType(JetTokens.IDENTIFIER);
ASTNode identifier = myDeclaration.getNode().findChildByType(JetTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
@@ -293,7 +294,7 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
ApplicationManager.getApplication().runReadAction(new Runnable() {
@Override
public void run() {
ASTNode identifier = myProperty.getNode().findChildByType(JetTokens.IDENTIFIER);
ASTNode identifier = myDeclaration.getNode().findChildByType(JetTokens.IDENTIFIER);
if (identifier != null) {
TextRange range = identifier.getTextRange();
RangeHighlighter[] highlighters = myEditor.getMarkupModel().getAllHighlighters();
@@ -309,14 +310,14 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
});
if (myEditor.getUserData(INTRODUCE_RESTART) == Boolean.TRUE) {
myInitialName = myProperty.getName();
myInitialName = myDeclaration.getName();
performInplaceRefactoring(getSuggestionsForNextRun());
}
}
private LinkedHashSet<String> getSuggestionsForNextRun() {
LinkedHashSet<String> nameSuggestions;
String currentName = myProperty.getName();
String currentName = myDeclaration.getName();
if (myNameSuggestions.contains(currentName)) {
nameSuggestions = myNameSuggestions;
}
@@ -329,7 +330,7 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
}
protected void addTypeReferenceVariable(TemplateBuilderImpl builder) {
JetTypeReference typeReference = myProperty.getTypeReference();
JetTypeReference typeReference = myDeclaration.getTypeReference();
if (typeReference != null) {
builder.replaceElement(typeReference, SpecifyTypeExplicitlyAction.createTypeExpressionForTemplate(myExprType));
}
@@ -354,8 +355,8 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
TemplateState templateState =
TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(myEditor));
if (templateState != null && myProperty.getTypeReference() != null) {
templateState.addTemplateStateListener(SpecifyTypeExplicitlyAction.createTypeReferencePostprocessor(myProperty));
if (templateState != null && myDeclaration.getTypeReference() != null) {
templateState.addTemplateStateListener(SpecifyTypeExplicitlyAction.createTypeReferencePostprocessor(myDeclaration));
}
return result;
@@ -377,11 +378,11 @@ public class KotlinInplaceVariableIntroducer extends InplaceVariableIntroducer<J
@Override
protected void moveOffsetAfter(boolean success) {
if (!myReplaceOccurrence || myExprMarker == null) {
myEditor.getCaretModel().moveToOffset(myProperty.getTextRange().getEndOffset());
myEditor.getCaretModel().moveToOffset(myDeclaration.getTextRange().getEndOffset());
}
else {
int startOffset = myExprMarker.getStartOffset();
PsiFile file = myProperty.getContainingFile();
PsiFile file = myDeclaration.getContainingFile();
PsiElement elementAt = file.findElementAt(startOffset);
if (elementAt != null) {
myEditor.getCaretModel().moveToOffset(elementAt.getTextRange().getEndOffset());
@@ -218,8 +218,8 @@ public class KotlinIntroduceVariableHandler extends KotlinIntroduceHandlerBase {
PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument());
PsiDocumentManager.getInstance(project).
doPostponedOperationsAndUnblockDocument(editor.getDocument());
KotlinInplaceVariableIntroducer variableIntroducer =
new KotlinInplaceVariableIntroducer(property, editor, project, INTRODUCE_VARIABLE,
KotlinInplaceVariableIntroducer<JetProperty> variableIntroducer =
new KotlinInplaceVariableIntroducer<JetProperty>(property, editor, project, INTRODUCE_VARIABLE,
references.toArray(new JetExpression[references.size()]),
reference.get(), finalReplaceOccurrence,
property, /*todo*/false, /*todo*/false,