Search in sources :

Example 1 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 2 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 3 with GrMember

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

the class GroovyInlineLocalHandler method createSettings.

/**
   * Returns Settings object for referenced definition in case of local variable
   */
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
    final String localName = variable.getName();
    final Project project = variable.getProject();
    GrExpression initializer = null;
    Instruction writeInstr = null;
    Instruction[] flow = null;
    //search for initializer to inline
    if (invokedOnReference) {
        LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
        final PsiReference ref = TargetElementUtil.findReference(editor);
        LOG.assertTrue(ref != null);
        PsiElement cur = ref.getElement();
        if (cur instanceof GrReferenceExpression) {
            GrControlFlowOwner controlFlowOwner;
            do {
                controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
                if (controlFlowOwner == null)
                    break;
                flow = controlFlowOwner.getControlFlow();
                final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
                final PsiElement finalCur = cur;
                Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
                LOG.assertTrue(instruction != null);
                final BitSet prev = writes.get(instruction.num());
                if (prev.cardinality() == 1) {
                    writeInstr = flow[prev.nextSetBit(0)];
                    final PsiElement element = writeInstr.getElement();
                    if (element instanceof GrVariable) {
                        initializer = ((GrVariable) element).getInitializerGroovy();
                    } else if (element instanceof GrReferenceExpression) {
                        initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
                    }
                }
                PsiElement old_cur = cur;
                if (controlFlowOwner instanceof GrClosableBlock) {
                    cur = controlFlowOwner;
                } else {
                    PsiElement parent = controlFlowOwner.getParent();
                    if (parent instanceof GrMember)
                        cur = ((GrMember) parent).getContainingClass();
                }
                if (cur == old_cur)
                    break;
            } while (initializer == null);
        }
    } else {
        flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
        initializer = variable.getInitializerGroovy();
        writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
    }
    if (initializer == null || writeInstr == null) {
        String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
        CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
        return null;
    }
    int writeInstructionNumber = writeInstr.num();
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
    RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
    if (dialog.showAndGet()) {
        return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) GroovyRefactoringBundle(org.jetbrains.plugins.groovy.refactoring.GroovyRefactoringBundle) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GroovyLanguage(org.jetbrains.plugins.groovy.GroovyLanguage) RefactoringBundle(com.intellij.refactoring.RefactoringBundle) ContainerUtil(com.intellij.util.containers.ContainerUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) ControlFlowUtils(org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils) TypeInferenceHelper(org.jetbrains.plugins.groovy.lang.psi.dataFlow.types.TypeInferenceHelper) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Logger(com.intellij.openapi.diagnostic.Logger) HelpID(com.intellij.refactoring.HelpID) InlineActionHandler(com.intellij.lang.refactoring.InlineActionHandler) PsiReference(com.intellij.psi.PsiReference) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) Editor(com.intellij.openapi.editor.Editor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) CommonRefactoringUtil(com.intellij.refactoring.util.CommonRefactoringUtil) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) TargetElementUtil(com.intellij.codeInsight.TargetElementUtil) ApplicationManager(com.intellij.openapi.application.ApplicationManager) BitSet(java.util.BitSet) PsiUtil(org.jetbrains.plugins.groovy.lang.psi.util.PsiUtil) GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) BitSet(java.util.BitSet) PsiReference(com.intellij.psi.PsiReference) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) Project(com.intellij.openapi.project.Project) GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GrMember

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

the class GroovyInlineMethodUtil method collectReferenceInfoImpl.

private static void collectReferenceInfoImpl(Collection<ReferenceExpressionInfo> infos, PsiElement elem, GrMethod method) {
    if (elem instanceof GrReferenceExpression) {
        GrReferenceExpression expr = (GrReferenceExpression) elem;
        PsiReference ref = expr.getReference();
        if (ref != null) {
            PsiElement declaration = ref.resolve();
            if (declaration instanceof GrMember) {
                int offsetInMethod = expr.getTextRange().getStartOffset() - method.getTextRange().getStartOffset();
                GrMember member = (GrMember) declaration;
                infos.add(new ReferenceExpressionInfo(expr, offsetInMethod, member, member.getContainingClass()));
            }
        }
    }
    for (PsiElement element : elem.getChildren()) {
        collectReferenceInfoImpl(infos, element, method);
    }
}
Also used : GrMember(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMember)

Example 5 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)

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