Search in sources :

Example 6 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.

the class GroovyShellCodeFragment method processTypeDefinitions.

private boolean processTypeDefinitions(@NotNull PsiScopeProcessor processor, @NotNull ResolveState state) {
    ElementClassHint classHint = processor.getHint(ElementClassHint.KEY);
    if (!ResolveUtil.shouldProcessClasses(classHint)) {
        return true;
    }
    NameHint nameHint = processor.getHint(NameHint.KEY);
    String name = nameHint != null ? nameHint.getName(state) : null;
    if (name != null) {
        final GrTypeDefinition definition = myTypeDefinitions.get(name);
        if (definition != null) {
            if (processor.execute(definition, state)) {
                return false;
            }
        }
    } else {
        for (GrTypeDefinition definition : myTypeDefinitions.values()) {
            if (!processor.execute(definition, state)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) ElementClassHint(com.intellij.psi.scope.ElementClassHint) NameHint(com.intellij.psi.scope.NameHint)

Example 7 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.

the class OverridingTester method doTest.

public void doTest() throws Throwable {
    final String testFile = getTestName(true) + ".test";
    final List<String> strings = TestUtils.readInput(getTestDataPath() + "/" + testFile);
    GroovyFileBase psiFile = (GroovyFileBase) myFixture.addFileToProject("foo.groovy", strings.get(0));
    StringBuilder buffer = new StringBuilder();
    GrTypeDefinition[] grTypeDefinitions = psiFile.getTypeDefinitions();
    GrTypeDefinition lastTypeDefinition = psiFile.getTypeDefinitions()[grTypeDefinitions.length - 1];
    PsiMethod[] psiMethods = lastTypeDefinition.getMethods();
    for (PsiMethod method : psiMethods) {
        PsiMethod[] superMethods = findMethod(method);
        String[] classes = sortUseContainingClass(superMethods);
        for (String classAsString : classes) {
            buffer.append(classAsString);
            //between different super methods
            buffer.append("\n");
        }
        //between different methods
        buffer.append("\n");
    }
    //between class definitions
    buffer.append("\n");
    assertEquals(strings.get(1), buffer.toString().trim());
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiMethod(com.intellij.psi.PsiMethod)

Example 8 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.

the class GrCreateSubclassAction method createSubclassGroovy.

@Nullable
public static PsiClass createSubclassGroovy(final GrTypeDefinition psiClass, final PsiDirectory targetDirectory, final String className) {
    final Project project = psiClass.getProject();
    final Ref<GrTypeDefinition> targetClass = new Ref<>();
    new WriteCommandAction(project, getTitle(psiClass), getTitle(psiClass)) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            IdeDocumentHistory.getInstance(project).includeCurrentPlaceAsChangePlace();
            final GrTypeParameterList oldTypeParameterList = psiClass.getTypeParameterList();
            try {
                targetClass.set(CreateClassActionBase.createClassByType(targetDirectory, className, PsiManager.getInstance(project), psiClass, GroovyTemplates.GROOVY_CLASS, true));
            } catch (final IncorrectOperationException e) {
                ApplicationManager.getApplication().invokeLater(() -> Messages.showErrorDialog(project, CodeInsightBundle.message("intention.error.cannot.create.class.message", className) + "\n" + e.getLocalizedMessage(), CodeInsightBundle.message("intention.error.cannot.create.class.title")));
                return;
            }
            startTemplate(oldTypeParameterList, project, psiClass, targetClass.get(), false);
        }
    }.execute();
    if (targetClass.get() == null)
        return null;
    if (!ApplicationManager.getApplication().isUnitTestMode() && !psiClass.hasTypeParameters()) {
        final Editor editor = CodeInsightUtil.positionCursorAtLBrace(project, targetClass.get().getContainingFile(), targetClass.get());
        if (editor == null)
            return targetClass.get();
        chooseAndImplement(psiClass, project, targetClass.get(), editor);
    }
    return targetClass.get();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) GrTypeParameterList(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeParameterList) Ref(com.intellij.openapi.util.Ref) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) Result(com.intellij.openapi.application.Result) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.

the class GrPullUpHandler method invoke.

@Override
public void invoke(@NotNull final Project project, @NotNull PsiElement[] elements, DataContext dataContext) {
    if (elements.length != 1)
        return;
    myProject = project;
    PsiElement element = elements[0];
    GrTypeDefinition aClass;
    PsiElement aMember = null;
    if (element instanceof GrTypeDefinition) {
        aClass = (GrTypeDefinition) element;
    } else if (element instanceof GrMethod) {
        aClass = DefaultGroovyMethods.asType(((GrMethod) element).getContainingClass(), GrTypeDefinition.class);
        aMember = element;
    } else if (element instanceof GrField) {
        aClass = DefaultGroovyMethods.asType(((GrField) element).getContainingClass(), GrTypeDefinition.class);
        aMember = element;
    } else {
        return;
    }
    invokeImpl(project, dataContext, aClass, aMember);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)

Example 10 with GrTypeDefinition

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition in project intellij-community by JetBrains.

the class GrPullUpHelper method doMoveClass.

private void doMoveClass(PsiSubstitutor substitutor, MemberInfo info) {
    if (Boolean.FALSE.equals(info.getOverrides())) {
        PsiClass aClass = (PsiClass) info.getMember();
        if (myTargetSuperClass instanceof GrTypeDefinition) {
            addClassToSupers(info, aClass, substitutor, (GrTypeDefinition) myTargetSuperClass);
        }
    } else {
        GrTypeDefinition aClass = (GrTypeDefinition) info.getMember();
        GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(myProject);
        replaceMovedMemberTypeParameters(aClass, PsiUtil.typeParametersIterable(mySourceClass), substitutor, elementFactory);
        fixReferencesToStatic(aClass);
        PsiMember movedElement = (PsiMember) myTargetSuperClass.addAfter(convertClassToLanguage(aClass, myTargetSuperClass.getLanguage()), null);
        myMembersAfterMove.add(movedElement);
        deleteMemberWithDocComment(aClass);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)

Aggregations

GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)78 PsiElement (com.intellij.psi.PsiElement)17 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)17 PsiClass (com.intellij.psi.PsiClass)16 NotNull (org.jetbrains.annotations.NotNull)15 Nullable (org.jetbrains.annotations.Nullable)14 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)9 PsiFile (com.intellij.psi.PsiFile)8 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)8 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)8 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 Project (com.intellij.openapi.project.Project)6 ArrayList (java.util.ArrayList)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 Editor (com.intellij.openapi.editor.Editor)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4