Revert "[extract method] IDEA-269072 IDEA-188743: add option to declare extracted variable as 'var'"

This reverts commit 27b857ab3be3a76bc80417338ac34e691c64ebca.

GitOrigin-RevId: ba9ba9b3846f9dc56c3184f8e5a1ff7a59e1b072
This commit is contained in:
Alexandr Suhinin
2023-04-21 10:58:30 +00:00
committed by intellij-monorepo-bot
parent 650d8bc75f
commit 1d2c163976
13 changed files with 9 additions and 122 deletions
@@ -5,16 +5,14 @@ import com.intellij.psi.*
import com.intellij.psi.codeStyle.CodeStyleManager
import com.intellij.psi.impl.source.resolve.JavaResolveUtil
import com.intellij.psi.util.PsiUtil
import com.intellij.refactoring.IntroduceVariableUtil
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.canDeclareVarType
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.createDeclaration
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.shouldDeclareVarType
import com.intellij.refactoring.extractMethod.newImpl.structures.DataOutput
import com.intellij.refactoring.extractMethod.newImpl.structures.DataOutput.*
import com.intellij.refactoring.extractMethod.newImpl.structures.ExtractOptions
import com.intellij.refactoring.extractMethod.newImpl.structures.FlowOutput
import com.intellij.refactoring.extractMethod.newImpl.structures.FlowOutput.*
import com.intellij.refactoring.util.RefactoringChangeUtil
class CallBuilder(private val context: PsiElement) {
private val factory: PsiElementFactory = PsiElementFactory.getInstance(context.project)
@@ -39,14 +37,9 @@ class CallBuilder(private val context: PsiElement) {
}
val declarationStatement = declaration as? PsiDeclarationStatement
val declaredVariable = declarationStatement?.declaredElements?.firstOrNull() as? PsiVariable
if (declaredVariable != null) {
if (dataOutput is VariableOutput) {
val needsFinal = dataOutput.variable.hasModifierProperty(PsiModifier.FINAL)
PsiUtil.setModifierProperty(declaredVariable, PsiModifier.FINAL, needsFinal)
}
if (canDeclareVarType(dataOutput, context) && shouldDeclareVarType(dataOutput)){
IntroduceVariableUtil.expandDiamondsAndReplaceExplicitTypeWithVar(declaredVariable.typeElement, declaredVariable)
}
if (dataOutput is VariableOutput && declaredVariable != null) {
val needsFinal = dataOutput.variable.hasModifierProperty(PsiModifier.FINAL)
PsiUtil.setModifierProperty(declaredVariable, PsiModifier.FINAL, needsFinal)
}
return listOfNotNull(declaration)
}
@@ -115,7 +108,7 @@ class CallBuilder(private val context: PsiElement) {
factory.createReferenceExpression(methodClass)
}
else {
RefactoringChangeUtil.createThisExpression(method.manager, methodClass)
RefactoringChangeUtil.createThisExpression(PsiManager.getInstance(method.project), methodClass)
}
callElement.methodExpression.qualifierExpression = ref
}
@@ -18,7 +18,6 @@ import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtil
import com.intellij.refactoring.IntroduceVariableUtil
import com.intellij.refactoring.JavaRefactoringSettings
import com.intellij.refactoring.extractMethod.newImpl.structures.DataOutput
import com.intellij.refactoring.extractMethod.newImpl.structures.DataOutput.*
import com.intellij.refactoring.extractMethod.newImpl.structures.ExtractOptions
@@ -28,23 +27,6 @@ import com.intellij.util.CommonJavaRefactoringUtil
object ExtractMethodHelper {
fun canDeclareVarType(dataOutput: DataOutput, context: PsiElement): Boolean {
if (!PsiUtil.isLanguageLevel10OrHigher(context)) {
return false
}
return when (dataOutput) {
ArtificialBooleanOutput -> true
is VariableOutput -> true
is EmptyOutput -> false
is ExpressionOutput -> false
}
}
fun shouldDeclareVarType(dataOutput: DataOutput): Boolean {
val isInferredType = (dataOutput as? VariableOutput)?.variable?.typeElement?.isInferredType == true
return isInferredType || JavaRefactoringSettings.getInstance().DECLARE_VAR_TYPE
}
fun hasReferencesToScope(scope: List<PsiElement>, elements: List<PsiElement>): Boolean {
val localVariables = scope.asSequence().flatMap { element -> PsiTreeUtil.findChildrenOfType(element, PsiVariable::class.java) }.toSet()
return elements.asSequence()
@@ -25,10 +25,8 @@ import com.intellij.refactoring.JavaRefactoringSettings
import com.intellij.refactoring.RefactoringBundle
import com.intellij.refactoring.extractMethod.ExtractMethodDialog
import com.intellij.refactoring.extractMethod.ExtractMethodHandler
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.canDeclareVarType
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.guessMethodName
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.replaceWithMethod
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodHelper.shouldDeclareVarType
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodPipeline.findAllOptionsToExtract
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodPipeline.selectOptionWithTargetClass
import com.intellij.refactoring.extractMethod.newImpl.ExtractMethodPipeline.withFilteredAnnotations
@@ -138,14 +136,12 @@ class MethodExtractor {
val defaultStatic = with (JavaRefactoringSettings.getInstance()) {
if (makeStaticAndPassFields) EXTRACT_STATIC_METHOD_AND_PASS_FIELDS else EXTRACT_STATIC_METHOD
}
val dataOutput = options.dataOutput
val hasAnnotation = dataOutput.nullability != Nullability.UNKNOWN && dataOutput.type !is PsiPrimitiveType
val hasAnnotation = options.dataOutput.nullability != Nullability.UNKNOWN && options.dataOutput.type !is PsiPrimitiveType
val annotationAvailable = ExtractMethodHelper.isNullabilityAvailable(options)
return ExtractMethodPopupProvider(
annotateDefault = if (hasAnnotation && annotationAvailable) needsNullabilityAnnotations(options.project) else null,
makeStaticDefault = if (showStatic) defaultStatic else null,
staticPassFields = makeStaticAndPassFields,
declareVarTypeDefault = shouldDeclareVarType(dataOutput).takeIf { canDeclareVarType(dataOutput, options.targetClass) }
staticPassFields = makeStaticAndPassFields
)
}
@@ -14,7 +14,6 @@ import javax.swing.LayoutFocusTraversalPolicy
class ExtractMethodPopupProvider(val annotateDefault: Boolean? = null,
val makeStaticDefault: Boolean? = null,
val declareVarTypeDefault: Boolean? = null,
val staticPassFields: Boolean = false) {
var annotate = annotateDefault
@@ -23,11 +22,8 @@ class ExtractMethodPopupProvider(val annotateDefault: Boolean? = null,
var makeStatic = makeStaticDefault
private set
var declareVarType = declareVarTypeDefault
private set
val isChanged: Boolean
get() = annotate != annotateDefault || makeStatic != makeStaticDefault || declareVarType != declareVarTypeDefault
get() = annotate != annotateDefault || makeStatic != makeStaticDefault
private var changeListener: () -> Unit = {}
@@ -72,16 +68,6 @@ class ExtractMethodPopupProvider(val annotateDefault: Boolean? = null,
}
}
}
if (declareVarType != null) {
row {
checkBox(JavaRefactoringBundle.message("declare.var.type"))
.selected(declareVarType ?: false)
.onChanged { component ->
declareVarType = component.isSelected
changeListener.invoke()
}
}
}
row {
link(JavaRefactoringBundle.message("extract.method.link.label.more.options")) { showDialogAction(null) }
.gap(RightGap.SMALL)
@@ -28,6 +28,5 @@ class InplaceExtractMethodCollector : CounterUsagesCollector() {
enum class ExtractMethodSettingChange(val fusName: String) {
AnnotateOn("AnnotateOn"), AnnotateOff("AnnotateOff"), MakeStaticOn("MakeStaticOn"),
MakeStaticOff("MakeStaticOff"), MakeStaticWithFieldsOn("MakeStaticWithFieldsOn"),
MakeStaticWithFieldsOff("MakeStaticWithFieldsOff"), DeclareVarTypeOn("DeclareVarTypeOn"),
DeclareVarTypeOff("DeclareVarTypeOff")
MakeStaticWithFieldsOff("MakeStaticWithFieldsOff")
}
@@ -224,15 +224,6 @@ object InplaceExtractUtils {
val change = if (popupProvider.annotate == true) ExtractMethodSettingChange.AnnotateOn else ExtractMethodSettingChange.AnnotateOff
logSettingsChange(project, change)
}
if (popupProvider.declareVarType != popupProvider.declareVarTypeDefault){
val change = if (popupProvider.declareVarType == true) {
ExtractMethodSettingChange.DeclareVarTypeOn
}
else {
ExtractMethodSettingChange.DeclareVarTypeOff
}
logSettingsChange(project, change)
}
if (popupProvider.makeStatic != popupProvider.makeStaticDefault) {
val change = when {
popupProvider.makeStatic == true && popupProvider.staticPassFields -> ExtractMethodSettingChange.MakeStaticWithFieldsOn
@@ -157,10 +157,6 @@ class InplaceMethodExtractor(private val editor: Editor,
if (!popupProvider.staticPassFields && makeStatic != null) {
JavaRefactoringSettings.getInstance().EXTRACT_STATIC_METHOD = makeStatic
}
val declareVarType = popupProvider.declareVarType
if (declareVarType != null) {
JavaRefactoringSettings.getInstance().DECLARE_VAR_TYPE = declareVarType
}
restartInplace()
}
popupProvider.setShowDialogAction { actionEvent -> restartInDialog(actionEvent == null) }
@@ -76,7 +76,6 @@ public class JavaRefactoringSettings implements PersistentStateComponent<JavaRef
public boolean EXTRACT_STATIC_METHOD = true;
public boolean EXTRACT_STATIC_METHOD_AND_PASS_FIELDS = false;
public boolean DECLARE_VAR_TYPE = false;
public static JavaRefactoringSettings getInstance() {
return ApplicationManager.getApplication().getService(JavaRefactoringSettings.class);
@@ -1,7 +0,0 @@
class SomeClass {
public void test(String filePath) {
<selection>String s = "42";</selection>
System.out.println(s);
}
}
@@ -1,15 +0,0 @@
import org.jetbrains.annotations.NotNull;
class SomeClass {
public void test(String filePath) {
var s = getString();
System.out.println(s);
}
@NotNull
private static String getString() {
String s = "42";
return s;
}
}
@@ -1,7 +0,0 @@
class SomeClass {
public void test(String filePath) {
<selection>var s = "42";</selection>
System.out.println(s);
}
}
@@ -1,15 +0,0 @@
import org.jetbrains.annotations.NotNull;
class SomeClass {
public void test(String filePath) {
var s = getString();
System.out.println(s);
}
@NotNull
private static String getString() {
var s = "42";
return s;
}
}
@@ -520,15 +520,6 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
doTest()
}
fun testKeepVarKeyword(){
doTest()
}
fun testDeclareVarType(){
JavaRefactoringSettings.getInstance().DECLARE_VAR_TYPE = true
doTest()
}
fun testRefactoringListener(){
templateTest {
configureByFile("$BASE_PATH/${getTestName(false)}.java")
@@ -575,13 +566,11 @@ class ExtractMethodAndDuplicatesInplaceTest: LightJavaCodeInsightTestCase() {
val settings = JavaRefactoringSettings.getInstance()
val defaultStatic = settings.EXTRACT_STATIC_METHOD
val defaultPassFields = settings.EXTRACT_STATIC_METHOD_AND_PASS_FIELDS
val defaultDeclareVar = settings.DECLARE_VAR_TYPE
val defaultChangeSignature = DuplicatesMethodExtractor.changeSignatureDefault
val defaultReplaceDuplicates = DuplicatesMethodExtractor.replaceDuplicatesDefault
Disposer.register(testRootDisposable) {
settings.EXTRACT_STATIC_METHOD = defaultStatic
settings.EXTRACT_STATIC_METHOD_AND_PASS_FIELDS = defaultPassFields
settings.DECLARE_VAR_TYPE = defaultDeclareVar
DuplicatesMethodExtractor.changeSignatureDefault = defaultChangeSignature
DuplicatesMethodExtractor.replaceDuplicatesDefault = defaultReplaceDuplicates
}