Change Signature: Replace isConstructor flag with enum. Fix signature preview

This commit is contained in:
Alexey Sedunov
2015-03-20 20:00:16 +03:00
parent d19e6337a3
commit e0a394ec62
7 changed files with 83 additions and 32 deletions
@@ -41,6 +41,7 @@ import kotlin.properties.Delegates
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList
import org.jetbrains.kotlin.idea.project.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetMethodDescriptor.Kind
public class JetChangeInfo(
val methodDescriptor: JetMethodDescriptor,
@@ -149,30 +150,34 @@ public class JetChangeInfo(
public fun getNewSignature(inheritedFunction: JetFunctionDefinitionUsage<PsiElement>): String {
val buffer = StringBuilder()
if (isConstructor) {
val defaultVisibility = if (kind.isConstructor) Visibilities.PUBLIC else Visibilities.INTERNAL
if (kind == Kind.PRIMARY_CONSTRUCTOR) {
buffer.append(name)
if (newVisibility != Visibilities.PUBLIC) {
if (newVisibility != defaultVisibility) {
buffer.append(' ').append(newVisibility).append(' ')
}
}
else {
if (newVisibility != Visibilities.INTERNAL) {
if (newVisibility != defaultVisibility) {
buffer.append(newVisibility).append(' ')
}
buffer.append(JetTokens.FUN_KEYWORD).append(' ')
}
buffer.append(if (kind == Kind.SECONDARY_CONSTRUCTOR) JetTokens.CONSTRUCTOR_KEYWORD else JetTokens.FUN_KEYWORD).append(' ')
receiverParameterInfo?.let {
buffer.append(it.currentTypeText).append('.')
}
if (kind == Kind.FUNCTION) {
receiverParameterInfo?.let {
buffer.append(it.currentTypeText).append('.')
}
}
buffer.append(name)
buffer.append(name)
}
buffer.append(getNewParametersSignature(inheritedFunction))
if (newReturnType != null && !KotlinBuiltIns.isUnit(newReturnType) && !isConstructor)
if (newReturnType != null && !KotlinBuiltIns.isUnit(newReturnType) && kind == Kind.FUNCTION)
buffer.append(": ").append(newReturnTypeText)
return buffer.toString()
@@ -272,7 +277,7 @@ public class JetChangeInfo(
public val JetChangeInfo.originalBaseFunctionDescriptor: FunctionDescriptor
get() = methodDescriptor.baseDescriptor
public val JetChangeInfo.isConstructor: Boolean get() = methodDescriptor.isConstructor
public val JetChangeInfo.kind: Kind get() = methodDescriptor.kind
public val JetChangeInfo.oldName: String?
get() = (methodDescriptor.getMethod() as? JetFunction)?.getName()
@@ -56,6 +56,7 @@ import org.jetbrains.kotlin.idea.JetFileType;
import org.jetbrains.kotlin.idea.refactoring.JetRefactoringBundle;
import org.jetbrains.kotlin.psi.JetTypeCodeFragment;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetMethodDescriptor.Kind;
import javax.swing.*;
import java.awt.*;
@@ -91,10 +92,15 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
@Override
protected JetCallableParameterTableModel createParametersInfoModel(JetMethodDescriptor descriptor) {
if (ChangeSignaturePackage.getIsConstructor(descriptor))
return new JetConstructorParameterTableModel(myDefaultValueContext);
else
return new JetFunctionParameterTableModel(descriptor, myDefaultValueContext);
switch (descriptor.getKind()) {
case FUNCTION:
return new JetFunctionParameterTableModel(descriptor, myDefaultValueContext);
case PRIMARY_CONSTRUCTOR:
return new JetPrimaryConstructorParameterTableModel(myDefaultValueContext);
case SECONDARY_CONSTRUCTOR:
return new JetSecondaryConstructorParameterTableModel(myDefaultValueContext);
}
throw new AssertionError("Invalid kind: " + descriptor.getKind());
}
@Override
@@ -118,7 +124,7 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
JPanel panel = new JPanel(new BorderLayout());
String valOrVar = "";
if (ChangeSignaturePackage.getIsConstructor(myMethod)) {
if (myMethod.getKind() == Kind.PRIMARY_CONSTRUCTOR) {
switch (item.parameter.getValOrVar()) {
case None:
valOrVar = " ";
@@ -263,7 +269,7 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
Document document = PsiDocumentManager.getInstance(getProject()).getDocument(item.defaultValueCodeFragment);
component = editor = new EditorTextField(document, getProject(), getFileType());
}
else if (JetConstructorParameterTableModel.isValVarColumn(columnInfo)) {
else if (JetPrimaryConstructorParameterTableModel.isValVarColumn(columnInfo)) {
JComboBox comboBox = new JComboBox(JetValVar.values());
comboBox.setSelectedItem(item.parameter.getValOrVar());
comboBox.addItemListener(new ItemListener() {
@@ -321,7 +327,7 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
public Object getValueAt(int column) {
ColumnInfo columnInfo = myParametersTableModel.getColumnInfos()[column];
if (JetConstructorParameterTableModel.isValVarColumn(columnInfo))
if (JetPrimaryConstructorParameterTableModel.isValVarColumn(columnInfo))
return ((JComboBox) components.get(column)).getSelectedItem();
else if (JetCallableParameterTableModel.isTypeColumn(columnInfo))
return item.typeCodeFragment;
@@ -347,7 +353,7 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
new int[] { 4, getParamNamesMaxLength(), getTypesMaxLength() };
int columnIndex = 0;
for (int i = ChangeSignaturePackage.getIsConstructor(myMethod) ? 0 : 1; i < columnLetters.length; i ++) {
for (int i = myMethod.getKind() == Kind.PRIMARY_CONSTRUCTOR ? 0 : 1; i < columnLetters.length; i ++) {
int width = getColumnWidth(columnLetters[i]);
if (x <= width)
@@ -365,7 +371,7 @@ public class JetChangeSignatureDialog extends ChangeSignatureDialogBase<
MouseEvent me = getMouseEvent();
int index = me != null
? getEditorIndex((int) me.getPoint().getX())
: ChangeSignaturePackage.getIsConstructor(myMethod) ? 1 : 0;
: myMethod.getKind() == Kind.PRIMARY_CONSTRUCTOR ? 1 : 0;
JComponent component = components.get(index);
return component instanceof EditorTextField ? ((EditorTextField) component).getFocusTarget() : component;
}
@@ -48,7 +48,7 @@ public class JetChangeSignatureProcessor extends ChangeSignatureProcessorBase {
@NotNull
@Override
protected UsageViewDescriptor createUsageViewDescriptor(UsageInfo[] usages) {
String subject = ChangeSignaturePackage.getIsConstructor(getChangeInfo()) ? "constructor" : "function";
String subject = ChangeSignaturePackage.getKind(getChangeInfo()).getIsConstructor() ? "constructor" : "function";
return new JetUsagesViewDescriptor(myChangeInfo.getMethod(), RefactoringBundle.message("0.to.change.signature", subject));
}
@@ -61,10 +61,7 @@ import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver;
import org.jetbrains.kotlin.types.JetType;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsageProcessor {
@Override
@@ -347,7 +344,8 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro
JetScope functionScope = RefactoringPackage.getContainingScope(oldDescriptor, bindingContext);
if (!ChangeSignaturePackage.getIsConstructor(changeInfo) && functionScope != null && !info.getNewName().isEmpty()) {
JetMethodDescriptor.Kind kind = ChangeSignaturePackage.getKind(changeInfo);
if (!kind.getIsConstructor() && functionScope != null && !info.getNewName().isEmpty()) {
for (FunctionDescriptor conflict : functionScope.getFunctions(Name.identifier(info.getNewName()))) {
if (conflict == oldDescriptor) continue;
@@ -369,7 +367,7 @@ public class JetChangeSignatureUsageProcessor implements ChangeSignatureUsagePro
result.putValue(element, "Duplicating parameter '" + parameterName + "'");
}
if (parametersScope != null) {
if (ChangeSignaturePackage.getIsConstructor(changeInfo) && valOrVar != JetValVar.None) {
if (kind == JetMethodDescriptor.Kind.PRIMARY_CONSTRUCTOR && valOrVar != JetValVar.None) {
for (VariableDescriptor property : parametersScope.getProperties(Name.identifier(parameterName))) {
PsiElement propertyDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(property);
@@ -26,6 +26,21 @@ import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
public trait JetMethodDescriptor : MethodDescriptor<JetParameterInfo, Visibility> {
enum class Kind(val isConstructor: Boolean) {
FUNCTION: Kind(false)
PRIMARY_CONSTRUCTOR: Kind(true)
SECONDARY_CONSTRUCTOR: Kind(true)
}
val kind: Kind get() {
val descriptor = baseDescriptor
return when {
descriptor !is ConstructorDescriptor -> Kind.FUNCTION
descriptor.isPrimary() -> Kind.PRIMARY_CONSTRUCTOR
else -> Kind.SECONDARY_CONSTRUCTOR
}
}
val baseDeclaration: PsiElement
val baseDescriptor: FunctionDescriptor
@@ -36,9 +51,6 @@ public trait JetMethodDescriptor : MethodDescriptor<JetParameterInfo, Visibility
val receiver: JetParameterInfo?
}
val JetMethodDescriptor.isConstructor: Boolean
get() = baseDescriptor is ConstructorDescriptor
fun JetMethodDescriptor.renderOriginalReturnType(): String =
baseDescriptor.getReturnType()?.let { IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(it) } ?: ""
@@ -28,8 +28,8 @@ import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
public class JetConstructorParameterTableModel extends JetCallableParameterTableModel {
public JetConstructorParameterTableModel(PsiElement context) {
public class JetPrimaryConstructorParameterTableModel extends JetCallableParameterTableModel {
public JetPrimaryConstructorParameterTableModel(PsiElement context) {
super(context,
new ValVarColumn(),
new NameColumn(context.getProject()),
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2015 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.kotlin.idea.refactoring.changeSignature;
import com.intellij.psi.PsiElement;
import com.intellij.refactoring.changeSignature.ParameterTableModelItemBase;
import org.jetbrains.kotlin.idea.JetFileType;
public class JetSecondaryConstructorParameterTableModel extends JetCallableParameterTableModel {
public JetSecondaryConstructorParameterTableModel(PsiElement context) {
super(context,
new NameColumn(context.getProject()),
new TypeColumn(context.getProject(), JetFileType.INSTANCE),
new DefaultValueColumn<JetParameterInfo, ParameterTableModelItemBase<JetParameterInfo>>(context.getProject(), JetFileType.INSTANCE));
}
}