Minor. Introduce 'isVar' parameter for KotlinIntroduceVariableHandler.doRefactoring
This commit is contained in:
committed by
Nikolay Krasko
parent
4d28199cc2
commit
7ba914f92a
@@ -56,6 +56,8 @@ class IntroduceVariableIntention : SelfTargetingRangeIntention<PsiElement>(
|
||||
|
||||
override fun applyTo(element: PsiElement, editor: Editor?) {
|
||||
val expression = getExpressionToProcess(element) ?: return
|
||||
KotlinIntroduceVariableHandler.doRefactoring(element.project, editor, expression, null, null)
|
||||
KotlinIntroduceVariableHandler.doRefactoring(
|
||||
element.project, editor, expression, isVar = false, occurrencesToReplace = null, onNonInteractiveFinish = null
|
||||
)
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -99,8 +99,8 @@ fun KtIfExpression.introduceValueForCondition(occurrenceInThenClause: KtExpressi
|
||||
KotlinIntroduceVariableHandler.doRefactoring(project,
|
||||
editor,
|
||||
occurrenceInConditional,
|
||||
listOf(occurrenceInConditional, occurrenceInThenClause),
|
||||
null)
|
||||
false,
|
||||
listOf(occurrenceInConditional, occurrenceInThenClause), null)
|
||||
}
|
||||
|
||||
fun KtNameReferenceExpression.inlineIfDeclaredLocallyAndOnlyUsedOnceWithPrompt(editor: Editor?) {
|
||||
|
||||
+6
-2
@@ -203,9 +203,13 @@ class KotlinFunctionCallUsage(
|
||||
KtSuperTypeListEntry::class.java,
|
||||
KtParameter::class.java) == null) {
|
||||
|
||||
KotlinIntroduceVariableHandler.doRefactoring(project, null, argumentExpression, listOf(argumentExpression)) {
|
||||
KotlinIntroduceVariableHandler.doRefactoring(
|
||||
project, null, argumentExpression,
|
||||
isVar = false,
|
||||
occurrencesToReplace = listOf(argumentExpression),
|
||||
onNonInteractiveFinish = {
|
||||
argumentExpression = psiFactory.createExpression(it.name!!)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var expressionToReplace: KtExpression = nameCounterpartMap[ref.element] ?: continue
|
||||
|
||||
+18
-10
@@ -130,7 +130,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
return result
|
||||
}
|
||||
|
||||
private fun runRefactoring (
|
||||
private fun runRefactoring(
|
||||
isVar: Boolean,
|
||||
expression: KtExpression,
|
||||
commonContainer: PsiElement,
|
||||
commonParent: PsiElement,
|
||||
@@ -139,16 +140,18 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
val initializer = (expression as? KtParenthesizedExpression)?.expression ?: expression
|
||||
val initializerText = if (initializer.mustBeParenthesizedInInitializerPosition()) "(${initializer.text})" else initializer.text
|
||||
|
||||
val varOvVal = if (isVar) "var" else "val"
|
||||
|
||||
var property: KtDeclaration = if (componentFunctions.isNotEmpty()) {
|
||||
buildString {
|
||||
componentFunctions.indices.joinTo(this, prefix = "val (", postfix = ")") { nameSuggestions[it].first() }
|
||||
componentFunctions.indices.joinTo(this, prefix = "$varOvVal (", postfix = ")") { nameSuggestions[it].first() }
|
||||
append(" = ")
|
||||
append(initializerText)
|
||||
}.let { psiFactory.createDestructuringDeclaration(it) }
|
||||
}
|
||||
else {
|
||||
buildString {
|
||||
append("val ")
|
||||
append("$varOvVal ")
|
||||
append(nameSuggestions.single().first())
|
||||
if (noTypeInference) {
|
||||
val typeToRender = expressionType ?: resolutionFacade.moduleDescriptor.builtIns.anyType
|
||||
@@ -281,8 +284,8 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
}
|
||||
}
|
||||
|
||||
fun runRefactoring() {
|
||||
if (commonContainer !is KtDeclarationWithBody) return runRefactoring(expression, commonContainer, commonParent, allReplaces)
|
||||
fun runRefactoring(isVar: Boolean) {
|
||||
if (commonContainer !is KtDeclarationWithBody) return runRefactoring(isVar, expression, commonContainer, commonParent, allReplaces)
|
||||
|
||||
commonContainer.bodyExpression.sure { "Original body is not found: " + commonContainer }
|
||||
|
||||
@@ -306,7 +309,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
} ?: newReplace
|
||||
}
|
||||
|
||||
runRefactoring(newExpression, newCommonContainer, newCommonParent, newAllReplaces)
|
||||
runRefactoring(isVar, newExpression, newCommonContainer, newCommonParent, newAllReplaces)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -446,6 +449,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
occurrenceContainer: KtElement,
|
||||
resolutionFacade: ResolutionFacade,
|
||||
bindingContext: BindingContext,
|
||||
isVar: Boolean,
|
||||
occurrencesToReplace: List<KtExpression>?,
|
||||
onNonInteractiveFinish: ((KtDeclaration) -> Unit)?
|
||||
) {
|
||||
@@ -555,7 +559,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
)
|
||||
|
||||
project.executeCommand(INTRODUCE_VARIABLE, null) {
|
||||
runWriteAction { introduceVariableContext.runRefactoring() }
|
||||
runWriteAction { introduceVariableContext.runRefactoring(isVar) }
|
||||
|
||||
val property = introduceVariableContext.propertyRef ?: return@executeCommand
|
||||
|
||||
@@ -582,7 +586,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
introduceVariableContext.reference,
|
||||
introduceVariableContext.references.toTypedArray(),
|
||||
suggestedNames.single(),
|
||||
/*todo*/ false,
|
||||
isVar,
|
||||
/*todo*/ false,
|
||||
expressionType,
|
||||
noTypeInference,
|
||||
@@ -683,6 +687,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
project: Project,
|
||||
editor: Editor?,
|
||||
expressionToExtract: KtExpression?,
|
||||
isVar: Boolean,
|
||||
occurrencesToReplace: List<KtExpression>?,
|
||||
onNonInteractiveFinish: ((KtDeclaration) -> Unit)?
|
||||
) {
|
||||
@@ -695,7 +700,10 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
val bindingContext = resolutionFacade.analyze(physicalExpression, BodyResolveMode.FULL)
|
||||
|
||||
fun runWithChosenContainers(container: KtElement, occurrenceContainer: KtElement) {
|
||||
doRefactoring(project, editor, expression, container, occurrenceContainer, resolutionFacade, bindingContext, occurrencesToReplace, onNonInteractiveFinish)
|
||||
doRefactoring(
|
||||
project, editor, expression, container, occurrenceContainer, resolutionFacade, bindingContext,
|
||||
isVar, occurrencesToReplace, onNonInteractiveFinish
|
||||
)
|
||||
}
|
||||
|
||||
val candidateContainers = expression.getCandidateContainers(resolutionFacade, bindingContext).ifEmpty {
|
||||
@@ -720,7 +728,7 @@ object KotlinIntroduceVariableHandler : RefactoringActionHandler {
|
||||
|
||||
try {
|
||||
selectElement(editor, file, listOf(CodeInsightUtils.ElementKind.EXPRESSION)) {
|
||||
doRefactoring(project, editor, it as KtExpression?, null, null)
|
||||
doRefactoring(project, editor, it as KtExpression?, false, null, null)
|
||||
}
|
||||
}
|
||||
catch (e: IntroduceRefactoringException) {
|
||||
|
||||
Reference in New Issue
Block a user