Search in sources :

Example 71 with GrVariable

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

the class GrHighlightUtil method collectReassignedNames.

private static Set<String> collectReassignedNames(PsiElement scope) {
    final Set<String> result = ContainerUtil.newHashSet();
    PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element) {
            if (!(element instanceof GrReferenceExpression) || !((GrReferenceExpression) element).isQualified()) {
                return true;
            }
            GrReferenceExpression ref = (GrReferenceExpression) element;
            if (isWriteAccess(ref)) {
                String varName = ref.getReferenceName();
                if (!result.contains(varName)) {
                    PsiElement target = ref.resolve();
                    if (target instanceof GrVariable && ((GrVariable) target).getInitializerGroovy() != null || target instanceof GrParameter) {
                        result.add(varName);
                    }
                }
            }
            return true;
        }
    });
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 72 with GrVariable

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

the class GrInplaceVariableIntroducer method getComponent.

@Override
protected JComponent getComponent() {
    myCanBeFinalCb = new NonFocusableCheckBox("Declare final");
    myCanBeFinalCb.setSelected(false);
    myCanBeFinalCb.setMnemonic('f');
    final GrFinalListener finalListener = new GrFinalListener(myEditor);
    myCanBeFinalCb.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            new WriteCommandAction(myProject, getCommandName(), getCommandName()) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    PsiDocumentManager.getInstance(myProject).commitDocument(myEditor.getDocument());
                    final GrVariable variable = getVariable();
                    if (variable != null) {
                        finalListener.perform(myCanBeFinalCb.isSelected(), variable);
                    }
                }
            }.execute();
        }
    });
    final JPanel panel = new JPanel(new GridBagLayout());
    panel.setBorder(null);
    if (myCanBeFinalCb != null) {
        panel.add(myCanBeFinalCb, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
    }
    panel.add(Box.createVerticalBox(), new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
    return panel;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) GrFinalListener(org.jetbrains.plugins.groovy.refactoring.introduce.GrFinalListener) ActionEvent(java.awt.event.ActionEvent) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ActionListener(java.awt.event.ActionListener) NonFocusableCheckBox(com.intellij.ui.NonFocusableCheckBox)

Example 73 with GrVariable

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

the class GrInplaceVariableIntroducer method getInitialSettingsForInplace.

@Nullable
@Override
protected GroovyIntroduceVariableSettings getInitialSettingsForInplace(@NotNull final GrIntroduceContext context, @NotNull final OccurrencesChooser.ReplaceChoice choice, final String[] names) {
    return new GroovyIntroduceVariableSettings() {

        private final CanonicalTypes.Type myType;

        {
            GrExpression expression = context.getExpression();
            StringPartInfo stringPart = context.getStringPart();
            GrVariable var = context.getVar();
            PsiType type = expression != null ? expression.getType() : var != null ? var.getType() : stringPart != null ? stringPart.getLiteral().getType() : null;
            myType = type != null && !PsiType.NULL.equals(type) ? CanonicalTypes.createTypeWrapper(type) : null;
        }

        @Override
        public boolean isDeclareFinal() {
            return myCanBeFinalCb != null ? myCanBeFinalCb.isSelected() : false;
        }

        @Nullable
        @Override
        public String getName() {
            return names[0];
        }

        @Override
        public boolean replaceAllOccurrences() {
            return choice == OccurrencesChooser.ReplaceChoice.ALL;
        }

        @Nullable
        @Override
        public PsiType getSelectedType() {
            return myType != null ? myType.getType(context.getPlace()) : null;
        }
    };
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) PsiType(com.intellij.psi.PsiType) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) PsiType(com.intellij.psi.PsiType) Nullable(org.jetbrains.annotations.Nullable)

Example 74 with GrVariable

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

the class UnusedDefInspection method check.

@Override
protected void check(@NotNull final GrControlFlowOwner owner, @NotNull final ProblemsHolder problemsHolder) {
    final Instruction[] flow = owner.getControlFlow();
    final ReachingDefinitionsDfaInstance dfaInstance = new ReachingDefinitionsDfaInstance(flow);
    final ReachingDefinitionsSemilattice lattice = new ReachingDefinitionsSemilattice();
    final DFAEngine<DefinitionMap> engine = new DFAEngine<>(flow, dfaInstance, lattice);
    final List<DefinitionMap> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null) {
        return;
    }
    final TIntHashSet unusedDefs = new TIntHashSet();
    for (Instruction instruction : flow) {
        if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
            unusedDefs.add(instruction.num());
        }
    }
    for (int i = 0; i < dfaResult.size(); i++) {
        final Instruction instruction = flow[i];
        if (instruction instanceof ReadWriteVariableInstruction) {
            final ReadWriteVariableInstruction varInst = (ReadWriteVariableInstruction) instruction;
            if (!varInst.isWrite()) {
                final String varName = varInst.getVariableName();
                DefinitionMap e = dfaResult.get(i);
                e.forEachValue(new TObjectProcedure<TIntHashSet>() {

                    @Override
                    public boolean execute(TIntHashSet reaching) {
                        reaching.forEach(new TIntProcedure() {

                            @Override
                            public boolean execute(int defNum) {
                                final String defName = ((ReadWriteVariableInstruction) flow[defNum]).getVariableName();
                                if (varName.equals(defName)) {
                                    unusedDefs.remove(defNum);
                                }
                                return true;
                            }
                        });
                        return true;
                    }
                });
            }
        }
    }
    final Set<PsiElement> checked = ContainerUtil.newHashSet();
    unusedDefs.forEach(new TIntProcedure() {

        @Override
        public boolean execute(int num) {
            final ReadWriteVariableInstruction instruction = (ReadWriteVariableInstruction) flow[num];
            final PsiElement element = instruction.getElement();
            process(element, checked, problemsHolder, GroovyInspectionBundle.message("unused.assignment.tooltip"));
            return true;
        }
    });
    owner.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitVariable(@NotNull GrVariable variable) {
            if (checked.contains(variable) || variable.getInitializerGroovy() != null)
                return;
            if (ReferencesSearch.search(variable, variable.getUseScope()).findFirst() == null) {
                process(variable, checked, problemsHolder, GroovyInspectionBundle.message("unused.variable"));
            }
        }
    });
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) TIntProcedure(gnu.trove.TIntProcedure) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) ReachingDefinitionsDfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsDfaInstance) TIntHashSet(gnu.trove.TIntHashSet) DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReachingDefinitionsSemilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsSemilattice) DefinitionMap(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.DefinitionMap) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 75 with GrVariable

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

the class PsiImplUtil method removeVariable.

public static void removeVariable(GrVariable variable) {
    final GrVariableDeclaration varDecl = (GrVariableDeclaration) variable.getParent();
    final List<GrVariable> variables = Arrays.asList(varDecl.getVariables());
    if (!variables.contains(variable)) {
        throw new IllegalArgumentException();
    }
    final PsiElement parent = varDecl.getParent();
    final ASTNode owner = parent.getNode();
    if (variables.size() == 1 && owner != null) {
        PsiElement next = varDecl.getNextSibling();
        //noinspection ConstantConditions
        while (next != null && next.getNode() != null && next.getNode().getElementType() == GroovyTokenTypes.mSEMI) {
            PsiElement tmpNext = next.getNextSibling();
            //noinspection ConstantConditions
            next.delete();
            next = tmpNext;
        }
        removeNewLineAfter(varDecl);
        varDecl.delete();
        return;
    }
    variable.delete();
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) ASTNode(com.intellij.lang.ASTNode)

Aggregations

GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)88 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)34 PsiElement (com.intellij.psi.PsiElement)24 Nullable (org.jetbrains.annotations.Nullable)20 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)19 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)16 NotNull (org.jetbrains.annotations.NotNull)14 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)14 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)12 StringPartInfo (org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo)11 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)10 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)10 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)8 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)8 PsiType (com.intellij.psi.PsiType)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 GrAssignmentExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression)7 TextRange (com.intellij.openapi.util.TextRange)6 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)6 ASTNode (com.intellij.lang.ASTNode)5