JavaElementKind; usage example

GitOrigin-RevId: 1a511ad4611acebbe4b54edbf568560f72ead6df
This commit is contained in:
Tagir Valeev
2020-01-22 05:13:16 +00:00
committed by intellij-monorepo-bot
parent da0fccd5b2
commit 8f8464d394
12 changed files with 156 additions and 23 deletions
@@ -17,4 +17,8 @@ package com.intellij.codeInsight.daemon.impl.quickfix;
public interface ClassKind {
String getDescription();
default String getDescriptionAccusative() {
return getDescription();
}
}
@@ -15,11 +15,11 @@
*/
package com.intellij.codeInsight.daemon.impl.quickfix;
import com.intellij.codeInsight.daemon.QuickFixBundle;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.util.JavaElementKind;
import com.intellij.util.PlatformIcons;
import org.jetbrains.annotations.NotNull;
@@ -29,21 +29,26 @@ import javax.swing.*;
* @author ven
*/
public enum CreateClassKind implements ClassKind {
CLASS (QuickFixBundle.message("create.class")),
INTERFACE (QuickFixBundle.message("create.interface")),
ENUM (QuickFixBundle.message("create.enum")),
ANNOTATION(QuickFixBundle.message("create.annotation")),
RECORD (QuickFixBundle.message("create.record"));
CLASS(JavaElementKind.CLASS),
INTERFACE(JavaElementKind.INTERFACE),
ENUM(JavaElementKind.ENUM),
ANNOTATION(JavaElementKind.ANNOTATION),
RECORD(JavaElementKind.RECORD);
private final JavaElementKind myKind;
private final String myDescription;
CreateClassKind(final String description) {
myDescription = description;
CreateClassKind(JavaElementKind kind) {
myKind = kind;
}
@Override
public String getDescription() {
return myDescription;
return myKind.getNominativeName();
}
@Override
public String getDescriptionAccusative() {
return myKind.getAccusativeName();
}
@NotNull
@@ -82,11 +82,6 @@ create.field.from.usage.text=Create field ''{0}''
create.field.from.usage.full.text=Create field ''{0}'' in ''{1}''
target.class.chooser.title=Choose Target Class
create.class.title=Create {0}
create.annotation=annotation
create.enum=enum
create.interface=interface
create.class=class
create.record=record
new.method.body.template.error.text=Please Correct "New Method Body" Template
new.method.body.template.error.title=File Template Error
cannot.create.java.file.error.text=Cannot create {0}.java in {1}: {2}
@@ -245,7 +245,7 @@ public class CreateClassFromNewFix extends CreateFromUsageBaseFix {
}
protected String getText(final String varName) {
return QuickFixBundle.message("create.class.from.usage.text", getKind().getDescription(), varName);
return QuickFixBundle.message("create.class.from.usage.text", getKind().getDescriptionAccusative(), varName);
}
protected static PsiJavaCodeReferenceElement getReferenceElement(PsiNewExpression expression) {
@@ -41,7 +41,7 @@ public class CreateClassFromUsageFix extends CreateClassFromUsageBaseFix {
@Override
public String getText(String varName) {
return QuickFixBundle.message("create.class.from.usage.text", myKind.getDescription(), varName);
return QuickFixBundle.message("create.class.from.usage.text", myKind.getDescriptionAccusative(), varName);
}
@@ -363,7 +363,7 @@ public class CreateFromUsageUtils {
final PsiDirectory targetDirectory;
if (!ApplicationManager.getApplication().isUnitTestMode()) {
Project project = manager.getProject();
String title = QuickFixBundle.message("create.class.title", StringUtil.capitalize(classKind.getDescription()));
String title = QuickFixBundle.message("create.class.title", StringUtil.capitalize(classKind.getDescriptionAccusative()));
CreateClassDialog dialog = new CreateClassDialog(project, title, name, qualifierName, classKind, false, module){
@Override
@@ -38,7 +38,7 @@ public class CreateInnerClassFromNewFix extends CreateClassFromNewFix {
@Override
public String getText(String varName) {
return QuickFixBundle.message("create.inner.class.from.usage.text", getKind().getDescription(), varName);
return QuickFixBundle.message("create.inner.class.from.usage.text", getKind().getDescriptionAccusative(), varName);
}
@Override
@@ -46,7 +46,7 @@ public class CreateInnerClassFromUsageFix extends CreateClassFromUsageBaseFix {
@Override
public String getText(String varName) {
return QuickFixBundle.message("create.inner.class.from.usage.text", myKind.getDescription(), varName);
return QuickFixBundle.message("create.inner.class.from.usage.text", myKind.getDescriptionAccusative(), varName);
}
@Override
@@ -89,10 +89,10 @@ public class CreateClassDialog extends DialogWrapper {
init();
if (!myClassNameEditable) {
setTitle(CodeInsightBundle.message("dialog.create.class.name", StringUtil.capitalize(kind.getDescription()), targetClassName));
setTitle(CodeInsightBundle.message("dialog.create.class.name", StringUtil.capitalize(kind.getDescriptionAccusative()), targetClassName));
}
else {
myInformationLabel.setText(CodeInsightBundle.message("dialog.create.class.label", kind.getDescription()));
myInformationLabel.setText(CodeInsightBundle.message("dialog.create.class.label", kind.getDescriptionAccusative()));
setTitle(title);
}
@@ -30,4 +30,8 @@ public class JavaCoreBundle extends DynamicBundle {
public static String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) {
return INSTANCE.getMessage(key, params);
}
public static boolean contains(String key) {
return INSTANCE.containsKey(key);
}
}
@@ -0,0 +1,105 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.psi.util;
import com.intellij.core.JavaCoreBundle;
import com.intellij.psi.*;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import java.util.Locale;
/**
* Represents a kind of element that appears in Java source code.
* The main purpose of this enum is to be able to display localized element name in UI
*/
public enum JavaElementKind {
ANNOTATION,
CLASS,
CONSTANT,
CONSTRUCTOR,
ENUM,
ENUM_CONSTANT,
FIELD,
INITIALIZER,
INTERFACE,
LOCAL_VARIABLE,
METHOD,
MODULE,
PACKAGE,
PARAMETER,
PATTERN_VARIABLE,
RECORD,
RECORD_COMPONENT,
UNKNOWN,
VARIABLE;
@Nls
public String getNominativeName() {
return JavaCoreBundle.message("element." + name().toLowerCase(Locale.ROOT), 0);
}
@Nls
public String getAccusativeName() {
return JavaCoreBundle.message("element." + name().toLowerCase(Locale.ROOT), 1);
}
public static JavaElementKind fromElement(@NotNull PsiElement element) {
if (element instanceof PsiClass) {
PsiClass psiClass = (PsiClass)element;
if (psiClass.isEnum()) {
return ENUM;
}
if (psiClass.isRecord()) {
return RECORD;
}
if (psiClass.isAnnotationType()) {
return ANNOTATION;
}
if (psiClass.isInterface()) {
return INTERFACE;
}
return CLASS;
}
if (element instanceof PsiMethod) {
if (((PsiMethod)element).isConstructor()) {
return CONSTRUCTOR;
}
return METHOD;
}
if (element instanceof PsiField) {
PsiField field = (PsiField)element;
if (field instanceof PsiEnumConstant) {
return ENUM_CONSTANT;
}
if (field.hasModifierProperty(PsiModifier.STATIC) && field.hasModifierProperty(PsiModifier.FINAL)) {
return CONSTANT;
}
return FIELD;
}
if (element instanceof PsiRecordComponent) {
return RECORD_COMPONENT;
}
if (element instanceof PsiLocalVariable) {
return LOCAL_VARIABLE;
}
if (element instanceof PsiPatternVariable) {
return PATTERN_VARIABLE;
}
if (element instanceof PsiParameter) {
return PARAMETER;
}
if (element instanceof PsiVariable) {
return VARIABLE;
}
if (element instanceof PsiPackage) {
return PACKAGE;
}
if (element instanceof PsiJavaModule) {
return MODULE;
}
if (element instanceof PsiClassInitializer) {
return INITIALIZER;
}
return UNKNOWN;
}
}
@@ -16,3 +16,23 @@ jdk.13.preview.language.level.description=13 (Preview) - Switch expressions, tex
jdk.14.language.level.description=14 - Switch expressions
jdk.14.preview.language.level.description=14 (Preview) - Records, patterns, text blocks
jdk.X.language.level.description=X - Experimental features
element.class=class
element.interface=interface
element.record=record
element.initializer=initializer
element.field=field
element.constant=constant field
element.enum.constant=enum constant
element.record.component=record component
element.parameter=parameter
element.pattern.variable=pattern variable
element.local.variable=local variable
element.variable=variable
element.method=method
element.constructor=constructor
element.package=package
element.module=module
element.enum=enum
element.annotation=annotation
element.unknown=element