Search in sources :

Example 96 with LocalSearchScope

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

the class MatcherImpl method testFindMatches.

/**
   * Finds the matches of given pattern starting from given tree element.
   * @param source string for search
   * @param pattern to be searched
   * @return list of matches found
   * @throws MalformedPatternException
   * @throws UnsupportedPatternException
   */
protected List<MatchResult> testFindMatches(String source, String pattern, MatchOptions options, boolean filePattern, FileType sourceFileType, String sourceExtension, boolean physicalSourceFile) throws MalformedPatternException, UnsupportedPatternException {
    CollectingMatchResultSink sink = new CollectingMatchResultSink();
    try {
        PsiElement[] elements = MatcherImplUtil.createSourceTreeFromText(source, filePattern ? PatternTreeContext.File : PatternTreeContext.Block, sourceFileType, sourceExtension, project, physicalSourceFile);
        options.setSearchPattern(pattern);
        options.setScope(new LocalSearchScope(elements));
        testFindMatches(sink, options);
    } catch (IncorrectOperationException e) {
        MalformedPatternException exception = new MalformedPatternException();
        exception.initCause(e);
        throw exception;
    } finally {
        options.setScope(null);
    }
    return sink.getMatches();
}
Also used : CollectingMatchResultSink(com.intellij.structuralsearch.plugin.util.CollectingMatchResultSink) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 97 with LocalSearchScope

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

the class ExtractMethodProcessor method applyChosenClassAndExtract.

private boolean applyChosenClassAndExtract(List<PsiVariable> inputVariables, @Nullable Pass<ExtractMethodProcessor> extractPass) throws PrepareFailedException {
    myStatic = shouldBeStatic();
    final Set<PsiField> fields = new LinkedHashSet<>();
    if (!PsiUtil.isLocalOrAnonymousClass(myTargetClass) && (myTargetClass.getContainingClass() == null || myTargetClass.hasModifierProperty(PsiModifier.STATIC))) {
        boolean canBeStatic = true;
        if (myTargetClass.isInterface()) {
            final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(myCodeFragmentMember, PsiMethod.class, false);
            canBeStatic = containingMethod == null || containingMethod.hasModifierProperty(PsiModifier.STATIC);
        }
        if (canBeStatic) {
            ElementNeedsThis needsThis = new ElementNeedsThis(myTargetClass) {

                @Override
                protected void visitClassMemberReferenceElement(PsiMember classMember, PsiJavaCodeReferenceElement classMemberReference) {
                    if (classMember instanceof PsiField && !classMember.hasModifierProperty(PsiModifier.STATIC)) {
                        final PsiExpression expression = PsiTreeUtil.getParentOfType(classMemberReference, PsiExpression.class, false);
                        if (expression == null || !PsiUtil.isAccessedForWriting(expression)) {
                            fields.add((PsiField) classMember);
                            return;
                        }
                    }
                    super.visitClassMemberReferenceElement(classMember, classMemberReference);
                }
            };
            for (int i = 0; i < myElements.length && !needsThis.usesMembers(); i++) {
                PsiElement element = myElements[i];
                element.accept(needsThis);
            }
            myCanBeStatic = !needsThis.usesMembers();
        } else {
            myCanBeStatic = false;
        }
    } else {
        myCanBeStatic = false;
    }
    myInputVariables = new InputVariables(inputVariables, myProject, new LocalSearchScope(myElements), isFoldingApplicable());
    myInputVariables.setUsedInstanceFields(fields);
    if (!checkExitPoints()) {
        return false;
    }
    checkCanBeChainedConstructor();
    if (extractPass != null) {
        extractPass.pass(this);
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ElementNeedsThis(com.intellij.refactoring.util.classMembers.ElementNeedsThis)

Example 98 with LocalSearchScope

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

the class AnonymousToInnerHandler method renameReferences.

private void renameReferences(PsiElement scope) throws IncorrectOperationException {
    PsiElementFactory factory = JavaPsiFacade.getInstance(myManager.getProject()).getElementFactory();
    for (VariableInfo info : myVariableInfos) {
        for (PsiReference reference : ReferencesSearch.search(info.variable, new LocalSearchScope(scope))) {
            PsiElement ref = reference.getElement();
            PsiIdentifier identifier = (PsiIdentifier) ((PsiJavaCodeReferenceElement) ref).getReferenceNameElement();
            assert identifier != null;
            boolean renameToFieldName = !isUsedInInitializer(ref);
            PsiIdentifier newNameIdentifier = factory.createIdentifier(renameToFieldName ? info.fieldName : info.parameterName);
            if (renameToFieldName) {
                identifier.replace(newNameIdentifier);
            } else {
                if (info.passAsParameter) {
                    identifier.replace(newNameIdentifier);
                }
            }
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 99 with LocalSearchScope

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

the class ExtractMethodProcessor method doRefactoring.

/**
   * Invoked in command and in atomic action
   */
public void doRefactoring() throws IncorrectOperationException {
    initDuplicates();
    chooseAnchor();
    LogicalPosition pos1;
    if (myEditor != null) {
        int col = myEditor.getCaretModel().getLogicalPosition().column;
        int line = myEditor.getCaretModel().getLogicalPosition().line;
        pos1 = new LogicalPosition(line, col);
        LogicalPosition pos = new LogicalPosition(0, 0);
        myEditor.getCaretModel().moveToLogicalPosition(pos);
    } else {
        pos1 = null;
    }
    final SearchScope processConflictsScope = myMethodVisibility.equals(PsiModifier.PRIVATE) ? new LocalSearchScope(myTargetClass) : GlobalSearchScope.projectScope(myProject);
    final Map<PsiMethodCallExpression, PsiMethod> overloadsResolveMap = new HashMap<>();
    final Runnable collectOverloads = () -> ApplicationManager.getApplication().runReadAction(() -> {
        Map<PsiMethodCallExpression, PsiMethod> overloads = ExtractMethodUtil.encodeOverloadTargets(myTargetClass, processConflictsScope, myMethodName, myCodeFragmentMember);
        overloadsResolveMap.putAll(overloads);
    });
    final Runnable extract = () -> {
        doExtract();
        ExtractMethodUtil.decodeOverloadTargets(overloadsResolveMap, myExtractedMethod, myCodeFragmentMember);
    };
    if (ApplicationManager.getApplication().isWriteAccessAllowed()) {
        collectOverloads.run();
        extract.run();
    } else {
        if (!ProgressManager.getInstance().runProcessWithProgressSynchronously(collectOverloads, "Collect overloads...", true, myProject))
            return;
        ApplicationManager.getApplication().runWriteAction(extract);
    }
    if (myEditor != null) {
        myEditor.getCaretModel().moveToLogicalPosition(pos1);
        int offset = myMethodCall.getMethodExpression().getTextRange().getStartOffset();
        myEditor.getCaretModel().moveToOffset(offset);
        myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
        myEditor.getSelectionModel().removeSelection();
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) LogicalPosition(com.intellij.openapi.editor.LogicalPosition) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 100 with LocalSearchScope

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

the class ExtractMethodSignatureSuggester method removeUnusedStongParams.

private void removeUnusedStongParams(int strongParamsCound) {
    final PsiExpression[] expressions = myMethodCall.getArgumentList().getExpressions();
    final PsiParameter[] parameters = myExtractedMethod.getParameterList().getParameters();
    final PsiCodeBlock body = myExtractedMethod.getBody();
    if (body != null) {
        final LocalSearchScope scope = new LocalSearchScope(body);
        for (int i = strongParamsCound - 1; i >= 0; i--) {
            final PsiParameter parameter = parameters[i];
            if (ReferencesSearch.search(parameter, scope).findFirst() == null) {
                parameter.delete();
                expressions[i].delete();
            }
        }
    }
}
Also used : 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