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 parameterName = currentName ?: parameter.getName()
|
||||||
val parameterType = currentType ?: parameter.getTypeReference()!!.getText()
|
val parameterType = currentType ?: parameter.getTypeReference()!!.getText()
|
||||||
val modifier = if (valVar != JetValVar.None) "${valVar.name} " else ""
|
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()
|
else parameter.getText()
|
||||||
|
|
||||||
@@ -210,6 +211,17 @@ public class KotlinInplaceParameterIntroducer(
|
|||||||
previewerPanel
|
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()
|
val occurrenceCount = descriptor.occurrencesToReplace.size()
|
||||||
if (occurrenceCount > 1) {
|
if (occurrenceCount > 1) {
|
||||||
addPanelControl {
|
addPanelControl {
|
||||||
|
|||||||
+10
-7
@@ -65,6 +65,7 @@ public data class IntroduceParameterDescriptor(
|
|||||||
val callableDescriptor: FunctionDescriptor,
|
val callableDescriptor: FunctionDescriptor,
|
||||||
val addedParameter: JetParameter,
|
val addedParameter: JetParameter,
|
||||||
val parameterType: JetType,
|
val parameterType: JetType,
|
||||||
|
val withDefaultValue: Boolean,
|
||||||
val parametersUsages: Map<JetParameter, List<PsiReference>>,
|
val parametersUsages: Map<JetParameter, List<PsiReference>>,
|
||||||
val occurrencesToReplace: List<JetExpression>
|
val occurrencesToReplace: List<JetExpression>
|
||||||
) {
|
) {
|
||||||
@@ -114,7 +115,8 @@ fun IntroduceParameterDescriptor.performRefactoring() {
|
|||||||
|
|
||||||
val parameterInfo = JetParameterInfo(name = addedParameter.getName()!!,
|
val parameterInfo = JetParameterInfo(name = addedParameter.getName()!!,
|
||||||
type = parameterType,
|
type = parameterType,
|
||||||
defaultValueForParameter = JetPsiUtil.deparenthesize(originalExpression),
|
defaultValueForCall = if (withDefaultValue) "" else originalExpression.getText(),
|
||||||
|
defaultValueForParameter = if (withDefaultValue) originalExpression else null,
|
||||||
valOrVar = valVar)
|
valOrVar = valVar)
|
||||||
parameterInfo.currentTypeText = addedParameter.getTypeReference()?.getText() ?: "Any"
|
parameterInfo.currentTypeText = addedParameter.getTypeReference()?.getText() ?: "Any"
|
||||||
addParameter(parameterInfo)
|
addParameter(parameterInfo)
|
||||||
@@ -209,12 +211,13 @@ public open class KotlinIntroduceParameterHandler: KotlinIntroduceHandlerBase()
|
|||||||
|
|
||||||
val introduceParameterDescriptor =
|
val introduceParameterDescriptor =
|
||||||
configure(IntroduceParameterDescriptor(JetPsiUtil.deparenthesize(expression)!!,
|
configure(IntroduceParameterDescriptor(JetPsiUtil.deparenthesize(expression)!!,
|
||||||
targetParent,
|
targetParent,
|
||||||
functionDescriptor,
|
functionDescriptor,
|
||||||
addedParameter,
|
addedParameter,
|
||||||
parameterType,
|
parameterType,
|
||||||
parametersUsages,
|
false,
|
||||||
occurrencesToReplace))
|
parametersUsages,
|
||||||
|
occurrencesToReplace))
|
||||||
if (editor.getSettings().isVariableInplaceRenameEnabled() && !ApplicationManager.getApplication().isUnitTestMode()) {
|
if (editor.getSettings().isVariableInplaceRenameEnabled() && !ApplicationManager.getApplication().isUnitTestMode()) {
|
||||||
with(PsiDocumentManager.getInstance(project)) {
|
with(PsiDocumentManager.getInstance(project)) {
|
||||||
commitDocument(editor.getDocument())
|
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 {
|
override fun configure(descriptor: IntroduceParameterDescriptor): IntroduceParameterDescriptor {
|
||||||
val fileText = file.getText()
|
val fileText = file.getText()
|
||||||
val singleReplace = InTextDirectivesUtils.isDirectiveDefined(fileText, "// SINGLE_REPLACE")
|
val singleReplace = InTextDirectivesUtils.isDirectiveDefined(fileText, "// SINGLE_REPLACE")
|
||||||
|
val withDefaultValue = InTextDirectivesUtils.getPrefixedBoolean(fileText, "// WITH_DEFAULT_VALUE:") ?: true
|
||||||
return with (descriptor) {
|
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");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/refactoring/introduceParameter/secondaryConstructorWithDefaultValue.kt");
|
||||||
doIntroduceParameterTest(fileName);
|
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