Search in sources :

Example 6 with GrMember

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

the class GrIntroduceFieldProcessor method initializeInMethod.

void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
    final PsiElement _scope = myContext.getScope();
    final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
    final PsiElement place = replaced.get(0);
    final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
    GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
    assert container != null;
    final PsiElement anchor;
    if (mySettings.removeLocalVar()) {
        GrVariable variable = myLocalVariable;
        anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
    } else {
        anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
        GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
    }
    initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Also used : GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrStatementOwner(org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 7 with GrMember

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

the class GroovyRefactoringSupportProvider method isMemberInplaceRenameAvailable.

@Override
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement element, @Nullable PsiElement context) {
    if (context == null || context.getContainingFile() instanceof GroovyFile)
        return false;
    PsiElement parent = context.getParent();
    //don't try to inplace rename aliased imported references
    if (parent instanceof GrReferenceElement) {
        GroovyResolveResult result = ((GrReferenceElement) parent).advancedResolve();
        PsiElement fileResolveContext = result.getCurrentFileResolveContext();
        if (fileResolveContext instanceof GrImportStatement && ((GrImportStatement) fileResolveContext).isAliasedImport()) {
            return false;
        }
    }
    return element instanceof GrMember;
}
Also used : GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Example 8 with GrMember

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember 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 9 with GrMember

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

the class FieldAnnotationChecker method checkScriptField.

private static void checkScriptField(AnnotationHolder holder, GrAnnotation annotation) {
    final PsiAnnotationOwner owner = annotation.getOwner();
    final GrMember container = PsiTreeUtil.getParentOfType(((PsiElement) owner), GrMember.class);
    if (container != null) {
        if (container.getContainingClass() instanceof GroovyScriptClass) {
            holder.createErrorAnnotation(annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script.body"));
        } else {
            holder.createErrorAnnotation(annotation, GroovyBundle.message("annotation.field.can.only.be.used.within.a.script"));
        }
    }
}
Also used : GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) PsiAnnotationOwner(com.intellij.psi.PsiAnnotationOwner) PsiElement(com.intellij.psi.PsiElement)

Example 10 with GrMember

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

the class GroovySuppressableInspectionTool method getElementToolSuppressedIn.

@Nullable
public static PsiElement getElementToolSuppressedIn(final PsiElement place, @NotNull String toolId) {
    if (place == null)
        return null;
    AccessToken accessToken = ApplicationManager.getApplication().acquireReadActionLock();
    try {
        final PsiElement statement = PsiUtil.findEnclosingStatement(place);
        if (statement != null) {
            PsiElement prev = statement.getPrevSibling();
            while (prev != null && StringUtil.isEmpty(prev.getText().trim())) {
                prev = prev.getPrevSibling();
            }
            if (prev instanceof PsiComment) {
                String text = prev.getText();
                Matcher matcher = SuppressionUtil.SUPPRESS_IN_LINE_COMMENT_PATTERN.matcher(text);
                if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) {
                    return prev;
                }
            }
        }
        GrMember member = null;
        GrDocComment docComment = PsiTreeUtil.getParentOfType(place, GrDocComment.class);
        if (docComment != null) {
            GrDocCommentOwner owner = docComment.getOwner();
            if (owner instanceof GrMember) {
                member = (GrMember) owner;
            }
        }
        if (member == null) {
            member = PsiTreeUtil.getNonStrictParentOfType(place, GrMember.class);
        }
        while (member != null) {
            GrModifierList modifierList = member.getModifierList();
            for (String ids : getInspectionIdsSuppressedInAnnotation(modifierList)) {
                if (SuppressionUtil.isInspectionToolIdMentioned(ids, toolId)) {
                    return modifierList;
                }
            }
            member = PsiTreeUtil.getParentOfType(member, GrMember.class);
        }
        return null;
    } finally {
        accessToken.finish();
    }
}
Also used : GrModifierList(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList) PsiComment(com.intellij.psi.PsiComment) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) Matcher(java.util.regex.Matcher) AccessToken(com.intellij.openapi.application.AccessToken) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) PsiElement(com.intellij.psi.PsiElement) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GrMember (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)23 PsiElement (com.intellij.psi.PsiElement)7 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)5 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)5 Nullable (org.jetbrains.annotations.Nullable)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 NotNull (org.jetbrains.annotations.NotNull)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)3 GroovyScriptClass (org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass)3 Editor (com.intellij.openapi.editor.Editor)2 GrDocCommentOwner (org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner)2 GrReferenceElement (org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)2 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)2 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 TargetElementUtil (com.intellij.codeInsight.TargetElementUtil)1 Language (com.intellij.lang.Language)1 InlineActionHandler (com.intellij.lang.refactoring.InlineActionHandler)1 AccessToken (com.intellij.openapi.application.AccessToken)1