Search in sources :

Example 21 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class ConvertToJavaProcessor method performRefactoring.

//private static String
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final GeneratorClassNameProvider classNameProvider = new GeneratorClassNameProvider();
    ExpressionContext context = new ExpressionContext(myProject, myFiles);
    final ClassGenerator classGenerator = new ClassGenerator(classNameProvider, new ClassItemGeneratorImpl(context));
    for (GroovyFile file : myFiles) {
        final PsiClass[] classes = file.getClasses();
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (PsiClass aClass : classes) {
            classGenerator.writeTypeDefinition(builder, aClass, true, first);
            first = false;
            builder.append('\n');
        }
        final Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
        LOG.assertTrue(document != null);
        document.setText(builder.toString());
        PsiDocumentManager.getInstance(myProject).commitDocument(document);
        String fileName = getNewFileName(file);
        PsiElement newFile;
        try {
            newFile = file.setName(fileName);
        } catch (final IncorrectOperationException e) {
            ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(myProject, e.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
            return;
        }
        doPostProcessing(newFile);
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 22 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class ClosureGenerator method getOwner.

@NonNls
@NotNull
private CharSequence getOwner(@NotNull GrClosableBlock closure) {
    final GroovyPsiElement context = PsiTreeUtil.getParentOfType(closure, GrMember.class, GroovyFile.class);
    LOG.assertTrue(context != null);
    final PsiClass contextClass;
    if (context instanceof GroovyFile) {
        contextClass = ((GroovyFile) context).getScriptClass();
    } else if (context instanceof PsiClass) {
        contextClass = (PsiClass) context;
    } else if (context instanceof GrMember) {
        if (((GrMember) context).hasModifierProperty(PsiModifier.STATIC)) {
            //no context class
            contextClass = null;
        } else {
            contextClass = ((GrMember) context).getContainingClass();
        }
    } else {
        contextClass = null;
    }
    if (contextClass == null)
        return "null";
    final PsiElement implicitClass = GenerationUtil.getWrappingImplicitClass(closure);
    if (implicitClass == null) {
        return "this";
    } else {
        final StringBuilder buffer = new StringBuilder();
        GenerationUtil.writeThisReference(contextClass, buffer, this.context);
        return buffer;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NonNls(org.jetbrains.annotations.NonNls) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class GroovyCompletionData method hasReturnValue.

private static boolean hasReturnValue(PsiElement context) {
    GrControlFlowOwner flowOwner = ControlFlowUtils.findControlFlowOwner(context);
    if (flowOwner instanceof GrClosableBlock)
        return true;
    if (flowOwner instanceof GroovyFile)
        return true;
    if (flowOwner == null)
        return true;
    PsiElement parent = flowOwner.getParent();
    if (parent instanceof GrMethod) {
        return !PsiType.VOID.equals(((GrMethod) parent).getReturnType());
    } else if (parent instanceof GrClassInitializer) {
        return false;
    }
    return true;
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 24 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class GroovyCompletionData method isControlStructure.

private static boolean isControlStructure(PsiElement context) {
    final int offset = context.getTextRange().getStartOffset();
    PsiElement prevSibling = context.getPrevSibling();
    if (context.getParent() instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
        ASTNode node = prevSibling.getNode();
        return !TokenSets.DOTS.contains(node.getElementType());
    }
    if (GroovyCompletionUtil.isNewStatement(context, true)) {
        final PsiElement leaf = GroovyCompletionUtil.getLeafByOffset(offset - 1, context);
        if (leaf != null && (leaf.getParent() instanceof GrStatementOwner || leaf.getParent() instanceof GrLabeledStatement)) {
            return true;
        }
    }
    if (context.getParent() != null) {
        PsiElement parent = context.getParent();
        if (parent instanceof GrExpression && parent.getParent() instanceof GroovyFile) {
            return true;
        }
        if (parent instanceof GrReferenceExpression) {
            PsiElement superParent = parent.getParent();
            if (superParent instanceof GrStatementOwner || superParent instanceof GrLabeledStatement || superParent instanceof GrControlStatement || superParent instanceof GrMethodCall) {
                return true;
            }
        }
        return false;
    }
    return false;
}
Also used : ASTNode(com.intellij.lang.ASTNode) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 25 with GroovyFile

use of org.jetbrains.plugins.groovy.lang.psi.GroovyFile in project intellij-community by JetBrains.

the class GroovyCompletionData method suggestPrimitiveTypes.

private static boolean suggestPrimitiveTypes(PsiElement context) {
    if (isInfixOperatorPosition(context))
        return false;
    if (isAfterForParameter(context))
        return false;
    final PsiElement parent = context.getParent();
    if (parent == null)
        return false;
    PsiElement previous = PsiImplUtil.realPrevious(parent.getPrevSibling());
    if (parent instanceof GrReferenceElement && parent.getParent() instanceof GrArgumentList) {
        PsiElement prevSibling = context.getPrevSibling();
        if (prevSibling != null && prevSibling.getNode() != null) {
            if (!TokenSets.DOTS.contains(prevSibling.getNode().getElementType())) {
                return true;
            }
        } else if (!(previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType()))) {
            return true;
        }
    }
    if (GroovyCompletionUtil.isTupleVarNameWithoutTypeDeclared(context))
        return true;
    if (previous != null && GroovyTokenTypes.mAT.equals(previous.getNode().getElementType())) {
        return false;
    }
    if (GroovyCompletionUtil.asSimpleVariable(context) || GroovyCompletionUtil.asTypedMethod(context) || GroovyCompletionUtil.asVariableInBlock(context) || asVariableAfterModifiers(context)) {
        return true;
    }
    if ((parent instanceof GrParameter && ((GrParameter) parent).getTypeElementGroovy() == null) || parent instanceof GrReferenceElement && !(parent.getParent() instanceof GrImportStatement) && !(parent.getParent() instanceof GrPackageDefinition) && !(parent.getParent() instanceof GrArgumentList)) {
        PsiElement prevSibling = context.getPrevSibling();
        if (parent instanceof GrReferenceElement && prevSibling != null && prevSibling.getNode() != null) {
            ASTNode node = prevSibling.getNode();
            return !TokenSets.DOTS.contains(node.getElementType());
        } else {
            return true;
        }
    }
    if (PsiImplUtil.realPrevious(parent.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    if (PsiImplUtil.realPrevious(context.getPrevSibling()) instanceof GrModifierList) {
        return true;
    }
    return parent instanceof GrExpression && parent.getParent() instanceof GroovyFile && GroovyCompletionUtil.isNewStatement(context, false);
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) ASTNode(com.intellij.lang.ASTNode) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Aggregations

GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)91 PsiFile (com.intellij.psi.PsiFile)26 PsiElement (com.intellij.psi.PsiElement)21 NotNull (org.jetbrains.annotations.NotNull)17 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 Project (com.intellij.openapi.project.Project)10 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)10 PsiClass (com.intellij.psi.PsiClass)9 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)8 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)8 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)7 Module (com.intellij.openapi.module.Module)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)6 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)6 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)6 ASTNode (com.intellij.lang.ASTNode)5