Search in sources :

Example 86 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class GrIntroduceHandlerBase method collectVariableUsages.

public static PsiElement[] collectVariableUsages(GrVariable variable, PsiElement scope) {
    final List<PsiElement> list = Collections.synchronizedList(new ArrayList<PsiElement>());
    if (scope instanceof GroovyScriptClass) {
        scope = scope.getContainingFile();
    }
    ReferencesSearch.search(variable, new LocalSearchScope(scope)).forEach(psiReference -> {
        final PsiElement element = psiReference.getElement();
        if (element != null) {
            list.add(element);
        }
        return true;
    });
    return list.toArray(new PsiElement[list.size()]);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GroovyScriptClass(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GroovyScriptClass) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 87 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class ListenerNavigateButton method buildNavigateActionGroup.

private static DefaultActionGroup buildNavigateActionGroup(RadComponent component, final PsiField boundField) {
    final DefaultActionGroup actionGroup = new DefaultActionGroup();
    final EventSetDescriptor[] eventSetDescriptors;
    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(component.getComponentClass());
        eventSetDescriptors = beanInfo.getEventSetDescriptors();
    } catch (IntrospectionException e) {
        LOG.error(e);
        return null;
    }
    PsiFile boundClassFile = boundField.getContainingFile();
    if (boundClassFile == null) {
        return null;
    }
    final LocalSearchScope scope = new LocalSearchScope(boundClassFile);
    ReferencesSearch.search(boundField, scope).forEach(ref -> {
        final PsiElement element = ref.getElement();
        if (element.getParent() instanceof PsiReferenceExpression) {
            PsiReferenceExpression refExpr = (PsiReferenceExpression) element.getParent();
            if (refExpr.getParent() instanceof PsiMethodCallExpression) {
                PsiMethodCallExpression methodCall = (PsiMethodCallExpression) refExpr.getParent();
                final PsiElement psiElement = refExpr.resolve();
                if (psiElement instanceof PsiMethod) {
                    PsiMethod method = (PsiMethod) psiElement;
                    for (EventSetDescriptor eventSetDescriptor : eventSetDescriptors) {
                        if (Comparing.equal(eventSetDescriptor.getAddListenerMethod().getName(), method.getName())) {
                            final String eventName = eventSetDescriptor.getName();
                            final PsiExpression[] args = methodCall.getArgumentList().getExpressions();
                            if (args.length > 0) {
                                addListenerRef(actionGroup, eventName, args[0]);
                            }
                        }
                    }
                }
            }
        }
        return true;
    });
    return actionGroup;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) EventSetDescriptor(java.beans.EventSetDescriptor)

Example 88 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class GrParameterImpl method getUseScope.

@Override
@NotNull
public SearchScope getUseScope() {
    if (!isPhysical()) {
        final PsiFile file = getContainingFile();
        final PsiElement context = file.getContext();
        if (context != null)
            return new LocalSearchScope(context);
        return super.getUseScope();
    }
    final PsiElement scope = getDeclarationScope();
    if (scope instanceof GrDocCommentOwner) {
        GrDocCommentOwner owner = (GrDocCommentOwner) scope;
        final GrDocComment comment = owner.getDocComment();
        if (comment != null) {
            return new LocalSearchScope(new PsiElement[] { scope, comment });
        }
    }
    return new LocalSearchScope(scope);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrDocCommentOwner(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocCommentOwner) GrDocComment(org.jetbrains.plugins.groovy.lang.groovydoc.psi.api.GrDocComment) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class OldReferencesResolver method inlineParam.

private PsiElement inlineParam(PsiElement newExpr, GrExpression actualArg, PsiParameter parameter) {
    if (myParamsToNotInline.contains(parameter))
        return newExpr;
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
    if (myExpr instanceof GrClosableBlock) {
        int count = 0;
        for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(myParameterInitializer))) {
            count++;
            if (count > 1)
                break;
        }
        if (count > 1) {
            myParamsToNotInline.add(parameter);
            final PsiType type;
            if (parameter instanceof GrParameter) {
                type = ((GrParameter) parameter).getDeclaredType();
            } else {
                type = parameter.getType();
            }
            final GrVariableDeclaration declaration = factory.createVariableDeclaration(ArrayUtil.EMPTY_STRING_ARRAY, actualArg, type, parameter.getName());
            final GrStatement[] statements = ((GrClosableBlock) myExpr).getStatements();
            GrStatement anchor = statements.length > 0 ? statements[0] : null;
            return ((GrClosableBlock) myExpr).addStatementBefore(declaration, anchor);
        }
    }
    int copyingSafetyLevel = GroovyRefactoringUtil.verifySafeCopyExpression(actualArg);
    if (copyingSafetyLevel == RefactoringUtil.EXPR_COPY_PROHIBITED) {
        actualArg = factory.createExpressionFromText(getTempVar(actualArg));
    }
    newExpr = newExpr.replace(actualArg);
    return newExpr;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 90 with LocalSearchScope

use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.

the class IncrementDecrementUsedAsExpressionInspection method extractPrefixPostfixExpressionToSeparateStatement.

public static void extractPrefixPostfixExpressionToSeparateStatement(PsiElement element) {
    final PsiExpression operand;
    if (element instanceof PsiPostfixExpression) {
        final PsiPostfixExpression postfixExpression = (PsiPostfixExpression) element;
        operand = postfixExpression.getOperand();
    } else if (element instanceof PsiPrefixExpression) {
        final PsiPrefixExpression prefixExpression = (PsiPrefixExpression) element;
        operand = prefixExpression.getOperand();
    } else {
        assert false;
        return;
    }
    if (operand == null) {
        return;
    }
    final PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
    if (statement == null) {
        return;
    }
    final PsiElement parent = statement.getParent();
    if (parent == null) {
        return;
    }
    final Project project = element.getProject();
    final PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    final String newStatementText = element.getText() + ';';
    final String operandText = operand.getText();
    if (parent instanceof PsiIfStatement || parent instanceof PsiLoopStatement) {
        // need to add braces because
        // in/decrement is inside braceless control statement body
        final StringBuilder text = new StringBuilder();
        text.append('{');
        final String elementText = PsiReplacementUtil.getElementText(statement, element, operandText);
        if (element instanceof PsiPostfixExpression) {
            text.append(elementText);
            text.append(newStatementText);
        } else {
            text.append(newStatementText);
            text.append(elementText);
        }
        text.append('}');
        final PsiCodeBlock codeBlock = factory.createCodeBlockFromText(text.toString(), parent);
        statement.replace(codeBlock);
        return;
    }
    final PsiStatement newStatement = factory.createStatementFromText(newStatementText, element);
    if (statement instanceof PsiReturnStatement) {
        if (element instanceof PsiPostfixExpression) {
            // special handling of postfix expression in return statement
            final PsiReturnStatement returnStatement = (PsiReturnStatement) statement;
            final PsiExpression returnValue = returnStatement.getReturnValue();
            if (returnValue == null) {
                return;
            }
            final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
            final String variableName = javaCodeStyleManager.suggestUniqueVariableName("result", returnValue, true);
            final PsiType type = returnValue.getType();
            if (type == null) {
                return;
            }
            final String newReturnValueText = PsiReplacementUtil.getElementText(returnValue, element, operandText);
            final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
            final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
            parent.addBefore(declarationStatement, statement);
            parent.addBefore(newStatement, statement);
            final PsiStatement newReturnStatement = factory.createStatementFromText("return " + variableName + ';', returnStatement);
            returnStatement.replace(newReturnStatement);
            return;
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (statement instanceof PsiThrowStatement) {
        if (element instanceof PsiPostfixExpression) {
            // special handling of postfix expression in throw statement
            final PsiThrowStatement returnStatement = (PsiThrowStatement) statement;
            final PsiExpression exception = returnStatement.getException();
            if (exception == null) {
                return;
            }
            final JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);
            final String variableName = javaCodeStyleManager.suggestUniqueVariableName("e", exception, true);
            final PsiType type = exception.getType();
            if (type == null) {
                return;
            }
            final String newReturnValueText = PsiReplacementUtil.getElementText(exception, element, operandText);
            final String declarationStatementText = type.getCanonicalText() + ' ' + variableName + '=' + newReturnValueText + ';';
            final PsiStatement declarationStatement = factory.createStatementFromText(declarationStatementText, returnStatement);
            parent.addBefore(declarationStatement, statement);
            parent.addBefore(newStatement, statement);
            final PsiStatement newReturnStatement = factory.createStatementFromText("throw " + variableName + ';', returnStatement);
            returnStatement.replace(newReturnStatement);
            return;
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (!(statement instanceof PsiForStatement)) {
        if (element instanceof PsiPostfixExpression) {
            parent.addAfter(newStatement, statement);
        } else {
            parent.addBefore(newStatement, statement);
        }
    } else if (operand instanceof PsiReferenceExpression) {
        final PsiReferenceExpression referenceExpression = (PsiReferenceExpression) operand;
        final PsiElement target = referenceExpression.resolve();
        if (target != null) {
            final SearchScope useScope = target.getUseScope();
            if (!new LocalSearchScope(statement).equals(useScope)) {
                if (element instanceof PsiPostfixExpression) {
                    parent.addAfter(newStatement, statement);
                } else {
                    parent.addBefore(newStatement, statement);
                }
            }
        }
    }
    if (statement instanceof PsiLoopStatement) {
        // in/decrement inside loop statement condition
        final PsiLoopStatement loopStatement = (PsiLoopStatement) statement;
        final PsiStatement body = loopStatement.getBody();
        if (body instanceof PsiBlockStatement) {
            final PsiBlockStatement blockStatement = (PsiBlockStatement) body;
            final PsiCodeBlock codeBlock = blockStatement.getCodeBlock();
            if (element instanceof PsiPostfixExpression) {
                final PsiElement firstElement = codeBlock.getFirstBodyElement();
                codeBlock.addBefore(newStatement, firstElement);
            } else {
                codeBlock.add(newStatement);
            }
        } else {
            final StringBuilder blockText = new StringBuilder();
            blockText.append('{');
            if (element instanceof PsiPostfixExpression) {
                blockText.append(newStatementText);
                if (body != null) {
                    blockText.append(body.getText());
                }
            } else {
                if (body != null) {
                    blockText.append(body.getText());
                }
                blockText.append(newStatementText);
            }
            blockText.append('}');
            final PsiStatement blockStatement = factory.createStatementFromText(blockText.toString(), statement);
            if (body == null) {
                loopStatement.add(blockStatement);
            } else {
                body.replace(blockStatement);
            }
        }
    }
    PsiReplacementUtil.replaceExpression((PsiExpression) element, operandText);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

LocalSearchScope (com.intellij.psi.search.LocalSearchScope)113 SearchScope (com.intellij.psi.search.SearchScope)31 NotNull (org.jetbrains.annotations.NotNull)22 PsiElement (com.intellij.psi.PsiElement)19 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)19 Project (com.intellij.openapi.project.Project)18 Nullable (org.jetbrains.annotations.Nullable)13 ArrayList (java.util.ArrayList)12 VirtualFile (com.intellij.openapi.vfs.VirtualFile)11 UsageInfo (com.intellij.usageView.UsageInfo)11 TextRange (com.intellij.openapi.util.TextRange)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 com.intellij.psi (com.intellij.psi)8 PsiReference (com.intellij.psi.PsiReference)8 PsiFile (com.intellij.psi.PsiFile)7 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)7 AnalysisScope (com.intellij.analysis.AnalysisScope)6 Processor (com.intellij.util.Processor)6 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)5 Ref (com.intellij.openapi.util.Ref)4