Search in sources :

Example 66 with GrTypeDefinition

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

the class ClassNameDiffersFromFileNamePredicate method satisfiedBy.

@Override
public boolean satisfiedBy(PsiElement element) {
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrTypeDefinition))
        return false;
    if (((GrTypeDefinition) parent).getNameIdentifierGroovy() != element)
        return false;
    final String name = ((GrTypeDefinition) parent).getName();
    if (name == null || name.isEmpty())
        return false;
    if (myClassConsumer != null)
        myClassConsumer.consume(((GrTypeDefinition) parent));
    final PsiFile file = element.getContainingFile();
    if (!(file instanceof GroovyFile))
        return false;
    if (!file.isPhysical())
        return false;
    if (name.equals(FileUtil.getNameWithoutExtension(file.getName())))
        return false;
    if (mySearchForClassInMultiClassFile) {
        return ((GroovyFile) file).getClasses().length > 1;
    } else {
        return !((GroovyFile) file).isScript();
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 67 with GrTypeDefinition

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

the class GroovyShellLanguageConsoleView method processCode.

protected void processCode() {
    GroovyShellCodeFragment groovyFile = getGroovyFile();
    for (GrTopStatement statement : groovyFile.getTopStatements()) {
        if (statement instanceof GrImportStatement) {
            groovyFile.addImportsFromString(importToString((GrImportStatement) statement));
        } else if (statement instanceof GrMethod) {
            groovyFile.addVariable(((GrMethod) statement).getName(), generateClosure((GrMethod) statement));
        } else if (statement instanceof GrAssignmentExpression) {
            GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
            GrExpression left = assignment.getLValue();
            if (left instanceof GrReferenceExpression && !((GrReferenceExpression) left).isQualified()) {
                groovyFile.addVariable(((GrReferenceExpression) left).getReferenceName(), assignment.getRValue());
            }
        } else if (statement instanceof GrTypeDefinition) {
            groovyFile.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition) statement));
        }
    }
    PsiType scriptType = groovyFile.getInferredScriptReturnType();
    if (scriptType != null) {
        groovyFile.addVariable("_", scriptType);
    }
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrAssignmentExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) PsiType(com.intellij.psi.PsiType)

Example 68 with GrTypeDefinition

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

the class GrPullUpHandler method invokeImpl.

private void invokeImpl(Project project, DataContext dataContext, GrTypeDefinition aClass, PsiElement aMember) {
    final Editor editor = dataContext != null ? CommonDataKeys.EDITOR.getData(dataContext) : null;
    if (aClass == null) {
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("is.not.supported.in.the.current.context", REFACTORING_NAME));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MEMBERS_PULL_UP);
        return;
    }
    ArrayList<PsiClass> bases = RefactoringHierarchyUtil.createBasesList(aClass, false, true);
    if (bases.isEmpty()) {
        final GrTypeDefinition containingClass = DefaultGroovyMethods.asType(aClass.getContainingClass(), GrTypeDefinition.class);
        if (containingClass != null) {
            invokeImpl(project, dataContext, containingClass, aClass);
            return;
        }
        String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("class.does.not.have.base.classes.interfaces.in.current.project", aClass.getQualifiedName()));
        CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, HelpID.MEMBERS_PULL_UP);
        return;
    }
    mySubclass = aClass;
    GrMemberInfoStorage memberInfoStorage = new GrMemberInfoStorage((GrTypeDefinition) mySubclass, new MemberInfoBase.Filter<GrMember>() {

        @Override
        public boolean includeMember(GrMember element) {
            return true;
        }
    });
    List<GrMemberInfo> members = memberInfoStorage.getClassMemberInfos(mySubclass);
    PsiManager manager = mySubclass.getManager();
    for (GrMemberInfo member : members) {
        if (manager.areElementsEquivalent(member.getMember(), aMember)) {
            member.setChecked(true);
            break;
        }
    }
    final GrPullUpDialog dialog = new GrPullUpDialog(project, aClass, bases, memberInfoStorage, this);
    dialog.show();
}
Also used : GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMemberInfo(org.jetbrains.plugins.groovy.refactoring.classMembers.GrMemberInfo) MemberInfoBase(com.intellij.refactoring.classMembers.MemberInfoBase) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrMemberInfoStorage(org.jetbrains.plugins.groovy.refactoring.classMembers.GrMemberInfoStorage) Editor(com.intellij.openapi.editor.Editor)

Example 69 with GrTypeDefinition

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

the class GroovyMoveClassToInnerHandler method moveClass.

@Override
public PsiClass moveClass(@NotNull PsiClass aClass, @NotNull PsiClass targetClass) {
    if (!(aClass instanceof GrTypeDefinition))
        return null;
    GroovyChangeContextUtil.encodeContextInfo(aClass);
    PsiDocComment doc = aClass.getDocComment();
    PsiElement brace = targetClass.getRBrace();
    PsiClass newClass = (PsiClass) targetClass.addBefore(aClass, brace);
    PsiElement sibling = newClass.getPrevSibling();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(targetClass.getProject());
    if (!org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil.isNewLine(sibling)) {
        targetClass.addBefore(factory.createLineTerminator("\n "), newClass);
    } else if (doc != null) {
        LOG.assertTrue(sibling != null);
        sibling.replace(factory.createLineTerminator(sibling.getText() + " "));
    }
    if (doc != null) {
        targetClass.addBefore(doc, newClass);
        targetClass.addBefore(factory.createLineTerminator("\n"), newClass);
    }
    if (targetClass.isInterface()) {
        PsiUtil.setModifierProperty(newClass, PsiModifier.PUBLIC, true);
    } else {
        PsiUtil.setModifierProperty(newClass, PsiModifier.STATIC, true);
    }
    GroovyChangeContextUtil.decodeContextInfo(newClass, null, null);
    return newClass;
}
Also used : PsiDocComment(com.intellij.psi.javadoc.PsiDocComment) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)

Example 70 with GrTypeDefinition

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

the class GroovyPsiManager method getArrayClass.

@Nullable
public GrTypeDefinition getArrayClass(@NotNull PsiType type) {
    final String typeText = type.getCanonicalText();
    GrTypeDefinition definition = myArrayClass.get(typeText);
    if (definition == null) {
        try {
            definition = GroovyPsiElementFactory.getInstance(myProject).createTypeDefinition("class __ARRAY__ { public int length; public " + typeText + "[] clone(){} }");
            myArrayClass.put(typeText, definition);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
            return null;
        }
    }
    return definition;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

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