Search in sources :

Example 1 with PsiEnumConstant

use of com.intellij.psi.PsiEnumConstant in project intellij-community by JetBrains.

the class GroovyEditorTextProvider method getEditorText.

@Override
public TextWithImports getEditorText(PsiElement elementAtCaret) {
    String result = "";
    PsiElement element = findExpressionInner(elementAtCaret, true);
    if (element != null) {
        if (element instanceof GrReferenceExpression) {
            final GrReferenceExpression reference = (GrReferenceExpression) element;
            if (reference.getQualifier() == null) {
                final PsiElement resolved = reference.resolve();
                if (resolved instanceof PsiEnumConstant) {
                    final PsiEnumConstant enumConstant = (PsiEnumConstant) resolved;
                    final PsiClass enumClass = enumConstant.getContainingClass();
                    if (enumClass != null) {
                        result = enumClass.getName() + "." + enumConstant.getName();
                    }
                }
            }
        }
        if (result.isEmpty()) {
            result = element.getText();
        }
    }
    return new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, result);
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClass(com.intellij.psi.PsiClass) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 2 with PsiEnumConstant

use of com.intellij.psi.PsiEnumConstant in project intellij-community by JetBrains.

the class FormEnumUsageTest method testEnumUsage.

public void testEnumUsage() throws IncorrectOperationException {
    LanguageLevelProjectExtension.getInstance(myJavaFacade.getProject()).setLanguageLevel(LanguageLevel.JDK_1_5);
    CommandProcessor.getInstance().executeCommand(myProject, () -> {
        try {
            createFile(myModule, myTestProjectRoot, "PropEnum.java", "public enum PropEnum { valueA, valueB }");
            createFile(myModule, myTestProjectRoot, "CustomComponent.java", "public class CustomComponent extends JLabel { private PropEnum e; public PropEnum getE() { return e; } public void setE(E newE) { e = newE; } }");
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }, "", null);
    PsiClass enumClass = myJavaFacade.findClass("PropEnum", ProjectScope.getAllScope(myProject));
    PsiField valueBField = enumClass.findFieldByName("valueB", false);
    assertNotNull(valueBField);
    assertTrue(valueBField instanceof PsiEnumConstant);
    final PsiClass componentClass = myJavaFacade.findClass("CustomComponent", ProjectScope.getAllScope(myProject));
    assertNotNull(componentClass);
    assertEquals(1, ReferencesSearch.search(componentClass).findAll().size());
    assertEquals(1, ReferencesSearch.search(valueBField).findAll().size());
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiField(com.intellij.psi.PsiField) PsiClass(com.intellij.psi.PsiClass) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with PsiEnumConstant

use of com.intellij.psi.PsiEnumConstant in project kotlin by JetBrains.

the class KtEnumEntry method isEquivalentTo.

@Override
public boolean isEquivalentTo(@Nullable PsiElement another) {
    if (another instanceof PsiEnumConstant) {
        PsiEnumConstant enumConstant = (PsiEnumConstant) another;
        PsiClass containingClass = enumConstant.getContainingClass();
        if (containingClass != null) {
            String containingClassQName = containingClass.getQualifiedName();
            if (containingClassQName != null && enumConstant.getName() != null) {
                String theirFQName = containingClassQName + "." + enumConstant.getName();
                if (theirFQName.equals(getQualifiedName())) {
                    return true;
                }
            }
        }
    }
    return super.isEquivalentTo(another);
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiClass(com.intellij.psi.PsiClass)

Example 4 with PsiEnumConstant

use of com.intellij.psi.PsiEnumConstant in project intellij-community by JetBrains.

the class ImplementMethodsFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") final Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    final PsiElement myPsiElement = startElement;
    if (editor == null || !FileModificationService.getInstance().prepareFileForWrite(myPsiElement.getContainingFile()))
        return;
    if (myPsiElement instanceof PsiEnumConstant) {
        final boolean hasClassInitializer = ((PsiEnumConstant) myPsiElement).getInitializingClass() != null;
        final MemberChooser<PsiMethodMember> chooser = chooseMethodsToImplement(editor, startElement, ((PsiEnumConstant) myPsiElement).getContainingClass(), hasClassInitializer);
        if (chooser == null)
            return;
        final List<PsiMethodMember> selectedElements = chooser.getSelectedElements();
        if (selectedElements == null || selectedElements.isEmpty())
            return;
        new WriteCommandAction(project, file) {

            @Override
            protected void run(@NotNull final Result result) throws Throwable {
                final PsiClass psiClass = ((PsiEnumConstant) myPsiElement).getOrCreateInitializingClass();
                OverrideImplementUtil.overrideOrImplementMethodsInRightPlace(editor, psiClass, selectedElements, chooser.isCopyJavadoc(), chooser.isInsertOverrideAnnotation());
            }
        }.execute();
    } else {
        OverrideImplementUtil.chooseAndImplementMethods(project, editor, (PsiClass) myPsiElement);
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) PsiEnumConstant(com.intellij.psi.PsiEnumConstant) PsiMethodMember(com.intellij.codeInsight.generation.PsiMethodMember) PsiClass(com.intellij.psi.PsiClass) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement) Result(com.intellij.openapi.application.Result)

Example 5 with PsiEnumConstant

use of com.intellij.psi.PsiEnumConstant in project intellij-community by JetBrains.

the class EnumFieldFixer method apply.

@Override
public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException {
    if (psiElement instanceof PsiEnumConstant) {
        int insertionOffset = psiElement.getTextRange().getEndOffset();
        Document doc = editor.getDocument();
        final CharSequence text = doc.getCharsSequence();
        final int probableCommaOffset = CharArrayUtil.shiftForward(text, insertionOffset, " \t");
        if (probableCommaOffset >= text.length() || text.charAt(probableCommaOffset) != ',') {
            doc.insertString(insertionOffset, ",");
        }
    }
}
Also used : PsiEnumConstant(com.intellij.psi.PsiEnumConstant) Document(com.intellij.openapi.editor.Document)

Aggregations

PsiEnumConstant (com.intellij.psi.PsiEnumConstant)5 PsiClass (com.intellij.psi.PsiClass)4 PsiElement (com.intellij.psi.PsiElement)2 PsiMethodMember (com.intellij.codeInsight.generation.PsiMethodMember)1 LocalQuickFixAndIntentionActionOnPsiElement (com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement)1 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Document (com.intellij.openapi.editor.Document)1 PsiField (com.intellij.psi.PsiField)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)1