Introduce Parameter: Support parameters without default values
This commit is contained in:
+13
-1
@@ -135,8 +135,9 @@ public class KotlinInplaceParameterIntroducer(
|
||||
val parameterName = currentName ?: parameter.getName()
|
||||
val parameterType = currentType ?: parameter.getTypeReference()!!.getText()
|
||||
val modifier = if (valVar != JetValVar.None) "${valVar.name} " else ""
|
||||
val defaultValue = if (withDefaultValue) " = ${originalExpression.getText()}" else ""
|
||||
|
||||
"$modifier$parameterName: $parameterType"
|
||||
"$modifier$parameterName: $parameterType$defaultValue"
|
||||
}
|
||||
else parameter.getText()
|
||||
|
||||
@@ -210,6 +211,17 @@ public class KotlinInplaceParameterIntroducer(
|
||||
previewerPanel
|
||||
}
|
||||
|
||||
addPanelControl {
|
||||
val defaultValueCheckBox = NonFocusableCheckBox("Introduce default value")
|
||||
defaultValueCheckBox.setSelected(descriptor.withDefaultValue)
|
||||
defaultValueCheckBox.setMnemonic('d')
|
||||
defaultValueCheckBox.addActionListener {
|
||||
descriptor = descriptor.copy(withDefaultValue = defaultValueCheckBox.isSelected())
|
||||
updatePreview(null, null)
|
||||
}
|
||||
defaultValueCheckBox
|
||||
}
|
||||
|
||||
val occurrenceCount = descriptor.occurrencesToReplace.size()
|
||||
if (occurrenceCount > 1) {
|
||||
addPanelControl {
|
||||
|
||||
+10
-7
@@ -65,6 +65,7 @@ public data class IntroduceParameterDescriptor(
|
||||
val callableDescriptor: FunctionDescriptor,
|
||||
val addedParameter: JetParameter,
|
||||
val parameterType: JetType,
|
||||
val withDefaultValue: Boolean,
|
||||
val parametersUsages: Map<JetParameter, List<PsiReference>>,
|
||||
val occurrencesToReplace: List<JetExpression>
|
||||
) {
|
||||
@@ -114,7 +115,8 @@ fun IntroduceParameterDescriptor.performRefactoring() {
|
||||
|
||||
val parameterInfo = JetParameterInfo(name = addedParameter.getName()!!,
|
||||
type = parameterType,
|
||||
defaultValueForParameter = JetPsiUtil.deparenthesize(originalExpression),
|
||||
defaultValueForCall = if (withDefaultValue) "" else originalExpression.getText(),
|
||||
defaultValueForParameter = if (withDefaultValue) originalExpression else null,
|
||||
valOrVar = valVar)
|
||||
parameterInfo.currentTypeText = addedParameter.getTypeReference()?.getText() ?: "Any"
|
||||
addParameter(parameterInfo)
|
||||
@@ -209,12 +211,13 @@ public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase()
|
||||
|
||||
val introduceParameterDescriptor =
|
||||
configure(IntroduceParameterDescriptor(JetPsiUtil.deparenthesize(expression)!!,
|
||||
targetParent,
|
||||
functionDescriptor,
|
||||
addedParameter,
|
||||
parameterType,
|
||||
parametersUsages,
|
||||
occurrencesToReplace))
|
||||
targetParent,
|
||||
functionDescriptor,
|
||||
addedParameter,
|
||||
parameterType,
|
||||
false,
|
||||
parametersUsages,
|
||||
occurrencesToReplace))
|
||||
if (editor.getSettings().isVariableInplaceRenameEnabled() && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
with(PsiDocumentManager.getInstance(project)) {
|
||||
commitDocument(editor.getDocument())
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_DEFAULT_VALUE: false
|
||||
fun foo(a: Int): Int {
|
||||
val b = (<selection>a + 1</selection>) * 2
|
||||
return a + b
|
||||
}
|
||||
|
||||
fun test() {
|
||||
foo(1)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_DEFAULT_VALUE: false
|
||||
fun foo(a: Int, i: Int): Int {
|
||||
val b = i * 2
|
||||
return a + b
|
||||
}
|
||||
|
||||
fun test() {
|
||||
foo(1, a + 1)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_DEFAULT_VALUE: false
|
||||
fun foo(a: Int): Int {
|
||||
val b = (<selection>a + 1</selection>) * 2
|
||||
return a + 1
|
||||
}
|
||||
|
||||
fun test() {
|
||||
foo(1)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// WITH_DEFAULT_VALUE: false
|
||||
fun foo(i: Int): Int {
|
||||
val b = i * 2
|
||||
return i
|
||||
}
|
||||
|
||||
fun test() {
|
||||
foo(a + 1)
|
||||
}
|
||||
+3
-1
@@ -70,8 +70,10 @@ public abstract class AbstractJetExtractionTest() : JetLightCodeInsightFixtureTe
|
||||
override fun configure(descriptor: IntroduceParameterDescriptor): IntroduceParameterDescriptor {
|
||||
val fileText = file.getText()
|
||||
val singleReplace = InTextDirectivesUtils.isDirectiveDefined(fileText, "// SINGLE_REPLACE")
|
||||
val withDefaultValue = InTextDirectivesUtils.getPrefixedBoolean(fileText, "// WITH_DEFAULT_VALUE:") ?: true
|
||||
return with (descriptor) {
|
||||
copy(occurrencesToReplace = if (singleReplace) Collections.singletonList(originalOccurrence) else occurrencesToReplace)
|
||||
copy(occurrencesToReplace = if (singleReplace) Collections.singletonList(originalOccurrence) else occurrencesToReplace,
|
||||
withDefaultValue = withDefaultValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -2322,5 +2322,17 @@ public class JetExtractionTestGenerated extends AbstractJetExtractionTest {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/secondaryConstructorWithDefaultValue.kt");
|
||||
doIntroduceParameterTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueAtCallSite.kt")
|
||||
public void testValueAtCallSite() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/valueAtCallSite.kt");
|
||||
doIntroduceParameterTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("valueAtCallSiteMultipleUsages.kt")
|
||||
public void testValueAtCallSiteMultipleUsages() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/valueAtCallSiteMultipleUsages.kt");
|
||||
doIntroduceParameterTest(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user