From c8decba90a3fc0ff4839951bcb69994ddeca8c8f Mon Sep 17 00:00:00 2001 From: Alexandr Suhinin Date: Thu, 7 May 2020 11:36:00 +0300 Subject: [PATCH] extract method: fix input parameters in dialog GitOrigin-RevId: acbbcc10070531b1ca1427c6e44c23cc6f5a5adc --- .../extractMethod/newImpl/ExtractOptionsPipeline.kt | 7 +++---- .../extractMethod/newImpl/MapFromDialog.kt | 13 ++++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractOptionsPipeline.kt b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractOptionsPipeline.kt index 257a9a9af885..b9097cd18371 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractOptionsPipeline.kt +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/ExtractOptionsPipeline.kt @@ -36,6 +36,9 @@ object ExtractMethodPipeline { ): ExtractOptions { val analyzer = CodeFragmentAnalyzer(extractOptions.elements) var options = withMappedName(extractOptions, methodName) + if (isStatic && ! options.isStatic) { + options = withForcedStatic(analyzer, options) ?: options + } options = withMappedParametersInput(options, variableData.toList()) val targetClass = extractOptions.anchor.containingClass!! options = if (targetClass.isInterface) { @@ -44,10 +47,6 @@ object ExtractMethodPipeline { options.copy(visibility = visibility) } - if (isStatic && ! options.isStatic) { - options = withForcedStatic(analyzer, options) ?: options - } - if (isConstructor) { options = asConstructor(analyzer, options) ?: options } else { diff --git a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/MapFromDialog.kt b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/MapFromDialog.kt index d2352601e3e2..302e87df8562 100644 --- a/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/MapFromDialog.kt +++ b/java/java-impl/src/com/intellij/refactoring/extractMethod/newImpl/MapFromDialog.kt @@ -2,14 +2,12 @@ package com.intellij.refactoring.extractMethod.newImpl import com.intellij.codeInsight.CodeInsightUtil -import com.intellij.codeInsight.Nullability import com.intellij.codeInsight.generation.GenerateMembersUtil import com.intellij.psi.* import com.intellij.psi.codeStyle.JavaCodeStyleManager import com.intellij.psi.codeStyle.VariableKind import com.intellij.psi.impl.source.codeStyle.JavaCodeStyleManagerImpl import com.intellij.psi.search.LocalSearchScope -import com.intellij.refactoring.HelpID import com.intellij.refactoring.extractMethod.ExtractMethodDialog import com.intellij.refactoring.extractMethod.InputVariables import com.intellij.refactoring.extractMethod.newImpl.structures.DataOutput @@ -39,17 +37,22 @@ object MapFromDialog { val elements = extractOptions.elements.toTypedArray() val nullability = extractOptions.dataOutput.nullability val analyzer = CodeFragmentAnalyzer(extractOptions.elements) + val staticOptions = ExtractMethodPipeline.withForcedStatic(analyzer, extractOptions) val canBeStatic = ExtractMethodPipeline.withForcedStatic(analyzer, extractOptions) != null val canBeChainedConstructor = ExtractMethodPipeline.canBeConstructor(analyzer) + val factory = PsiElementFactory.getInstance(project) val variables = extractOptions.inputParameters - .map { it.references.first() as? PsiReferenceExpression } - .map { it?.resolve() as? PsiVariable } + .map { factory.createVariableDeclarationStatement(it.name, it.type, null, it.references.first().context) } + .map { declaration -> declaration.declaredElements[0] as PsiVariable } + val parameterNames = extractOptions.inputParameters.map { it.name }.toSet() + val fields = staticOptions?.inputParameters.orEmpty() + .filterNot { it.name in parameterNames }.map { factory.createField(it.name, it.type) }.toSet() val inputVariables = InputVariables( variables, extractOptions.project, LocalSearchScope(extractOptions.elements.toTypedArray()), false, - emptySet() + fields ) val typeParameterList = PsiElementFactory.getInstance(extractOptions.project).createTypeParameterList() typeParameters.forEach { typeParameterList.add(it) }