Java: make "Create enum constant" quick-fix more available (IDEA-239983)

GitOrigin-RevId: b0d9510a97e4d5af5c6dea8e640938268b7874c4
This commit is contained in:
Bas Leijdekkers
2023-11-03 12:14:45 +01:00
committed by intellij-monorepo-bot
parent 60a6cb2a00
commit 5fb47bfc3e
6 changed files with 38 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.intention.impl
import com.intellij.codeInsight.daemon.QuickFixBundle
@@ -116,7 +116,7 @@ class JavaElementActionsFactory : JvmElementActionsFactory() {
val constantRequested = request.isConstant || javaClass.isInterface || javaClass.isRecord || request.modifiers.containsAll(
constantModifiers)
val result = ArrayList<IntentionAction>()
if (canCreateEnumConstant(javaClass, request)) {
if (canCreateEnumConstant(javaClass)) {
result += CreateEnumConstantAction(javaClass, request)
}
if (constantRequested || request.fieldName.uppercase(Locale.ENGLISH) == request.fieldName) {

View File

@@ -1,9 +1,8 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.lang.java.actions
import com.intellij.codeInsight.CodeInsightUtil.positionCursor
import com.intellij.codeInsight.CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement
import com.intellij.codeInsight.ExpectedTypeUtil
import com.intellij.codeInsight.daemon.impl.quickfix.CreateFromUsageBaseFix.startTemplate
import com.intellij.codeInsight.daemon.impl.quickfix.EmptyExpression
import com.intellij.codeInsight.intention.HighPriorityAction
@@ -13,7 +12,6 @@ import com.intellij.codeInspection.CommonQuickFixBundle
import com.intellij.ide.highlighter.JavaFileType
import com.intellij.lang.jvm.actions.CreateEnumConstantActionGroup
import com.intellij.lang.jvm.actions.CreateFieldRequest
import com.intellij.lang.jvm.actions.ExpectedTypes
import com.intellij.lang.jvm.actions.JvmActionGroup
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
@@ -34,8 +32,8 @@ internal class CreateEnumConstantAction(
override fun getText(): String = CommonQuickFixBundle.message("fix.create.title.x", JavaElementKind.ENUM_CONSTANT.`object`(), request.fieldName)
override fun generatePreview(project: Project, editor: Editor, file: PsiFile): IntentionPreviewInfo {
val constructor = target.constructors.firstOrNull() ?: return IntentionPreviewInfo.EMPTY
val hasParameters = constructor.parameters.isNotEmpty()
val constructor = target.constructors.firstOrNull()
val hasParameters = constructor?.parameters?.isNotEmpty() ?: false
val text = if (hasParameters) "${request.fieldName}(...)" else request.fieldName
return IntentionPreviewInfo.CustomDiff(JavaFileType.INSTANCE, "", text)
}
@@ -71,20 +69,9 @@ internal class CreateEnumConstantAction(
}
}
internal fun canCreateEnumConstant(targetClass: PsiClass, request: CreateFieldRequest): Boolean {
internal fun canCreateEnumConstant(targetClass: PsiClass): Boolean {
if (!targetClass.isEnum) return false
val lastConstant = targetClass.fields.filterIsInstance<PsiEnumConstant>().lastOrNull()
if (lastConstant != null && PsiTreeUtil.hasErrorElements(lastConstant)) return false
return checkExpectedTypes(request.fieldType, targetClass, targetClass.project)
}
private fun checkExpectedTypes(types: ExpectedTypes, targetClass: PsiClass, project: Project): Boolean {
val typeInfos = extractExpectedTypes(project, types)
if (typeInfos.isEmpty()) return true
val enumType = JavaPsiFacade.getElementFactory(project).createType(targetClass)
return typeInfos.any {
ExpectedTypeUtil.matches(enumType, it)
}
return lastConstant == null || !PsiTreeUtil.hasErrorElements(lastConstant)
}

View File

@@ -0,0 +1,8 @@
// "Create enum constant 'EEE'" "true"
enum E {
AAA, EEE;
void t() {
int t = EEE;
}
}

View File

@@ -0,0 +1,11 @@
// "Create enum constant 'SUPERUSER'" "true"
enum Role {
USER, ADMIN, SUPERUSER;
}
class User {
String x() {
Role.SUPERUSER.name();
}
}

View File

@@ -1,4 +1,4 @@
// "Create enum constant 'EEE'" "false"
// "Create enum constant 'EEE'" "true"
enum E {
AAA;

View File

@@ -0,0 +1,11 @@
// "Create enum constant 'SUPERUSER'" "true"
enum Role {
USER, ADMIN;
}
class User {
String x() {
Role.SUPERUSER<caret>.name();
}
}