Search in sources :

Example 6 with GrTopStatement

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

the class GrPackageInspection method getElementToHighlight.

@Nullable
private static PsiElement getElementToHighlight(GroovyFile file) {
    GrPackageDefinition packageDefinition = file.getPackageDefinition();
    if (packageDefinition != null)
        return packageDefinition;
    PsiClass[] classes = file.getClasses();
    for (PsiClass aClass : classes) {
        if (!(aClass instanceof SyntheticElement) && aClass instanceof GrTypeDefinition) {
            return ((GrTypeDefinition) aClass).getNameIdentifierGroovy();
        }
    }
    GrTopStatement[] statements = file.getTopStatements();
    if (statements.length > 0) {
        GrTopStatement first = statements[0];
        if (first instanceof GrNamedElement)
            return ((GrNamedElement) first).getNameIdentifierGroovy();
        return first;
    }
    return null;
}
Also used : GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrNamedElement(org.jetbrains.plugins.groovy.lang.psi.GrNamedElement) PsiClass(com.intellij.psi.PsiClass) GrPackageDefinition(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition) SyntheticElement(com.intellij.psi.SyntheticElement) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GrTopStatement

use of org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement 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 8 with GrTopStatement

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

the class GroovyPsiElementFactoryImpl method createModifierFromText.

@NotNull
@Override
public PsiElement createModifierFromText(@NotNull String name) {
    final GroovyFileBase file = createGroovyFileChecked(name + " foo() {}");
    final GrTopStatement[] definitions = file.getTopStatements();
    if (definitions.length != 1)
        throw new IncorrectOperationException(name);
    return definitions[0].getFirstChild().getFirstChild();
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GrTopStatement

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

the class GroovyPsiElementFactoryImpl method createReferenceExpressionFromText.

@NotNull
@Override
public GrReferenceExpression createReferenceExpressionFromText(@NotNull String idText, PsiElement context) {
    GroovyFile file = createGroovyFileChecked(idText, false, context);
    GrTopStatement[] statements = file.getTopStatements();
    if (statements.length != 1)
        throw new IncorrectOperationException("refText: " + idText);
    if (!(statements[0] instanceof GrReferenceExpression))
        throw new IncorrectOperationException("refText: " + idText);
    return (GrReferenceExpression) statements[0];
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GrTopStatement

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

the class GroovyPsiElementFactoryImpl method createTypeElement.

@Override
@NotNull
public GrTypeElement createTypeElement(@NotNull String typeText, @Nullable final PsiElement context) throws IncorrectOperationException {
    final GroovyFile file = createGroovyFileChecked("def " + typeText + " someVar", false, context);
    GrTopStatement[] topStatements = file.getTopStatements();
    if (topStatements == null || topStatements.length == 0)
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    GrTopStatement statement = topStatements[0];
    if (!(statement instanceof GrVariableDeclaration))
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    GrVariableDeclaration decl = (GrVariableDeclaration) statement;
    final GrTypeElement element = decl.getTypeElementGroovy();
    if (element == null)
        throw new IncorrectOperationException("can't create type element from:" + typeText);
    return element;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrTopStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GrTopStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.GrTopStatement)13 IncorrectOperationException (com.intellij.util.IncorrectOperationException)7 NotNull (org.jetbrains.annotations.NotNull)7 GrImportStatement (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement)4 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)3 GrPackageDefinition (org.jetbrains.plugins.groovy.lang.psi.api.toplevel.packaging.GrPackageDefinition)3 PsiElement (com.intellij.psi.PsiElement)2 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)2 FileASTNode (com.intellij.lang.FileASTNode)1 Project (com.intellij.openapi.project.Project)1 PsiClass (com.intellij.psi.PsiClass)1 PsiType (com.intellij.psi.PsiType)1 StubBasedPsiElement (com.intellij.psi.StubBasedPsiElement)1 SyntheticElement (com.intellij.psi.SyntheticElement)1 PsiDocComment (com.intellij.psi.javadoc.PsiDocComment)1 Nullable (org.jetbrains.annotations.Nullable)1 GrDocComment (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment)1 GrDocCommentOwner (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner)1 GrNamedElement (org.jetbrains.plugins.groovy.lang.psi.GrNamedElement)1 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)1