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