Search in sources :

Example 11 with SearchScope

use of com.intellij.psi.search.SearchScope in project kotlin by JetBrains.

the class MoveDeclarationsOutHelper method move.

public static PsiElement[] move(@NotNull PsiElement container, @NotNull PsiElement[] statements, boolean generateDefaultInitializers) {
    if (statements.length == 0) {
        return statements;
    }
    Project project = container.getProject();
    List<PsiElement> resultStatements = new ArrayList<PsiElement>();
    List<KtProperty> propertiesDeclarations = new ArrayList<KtProperty>();
    // Dummy element to add new declarations at the beginning
    KtPsiFactory psiFactory = KtPsiFactoryKt.KtPsiFactory(project);
    PsiElement dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0]);
    try {
        SearchScope scope = new LocalSearchScope(container);
        int lastStatementOffset = statements[statements.length - 1].getTextRange().getEndOffset();
        for (PsiElement statement : statements) {
            if (needToDeclareOut(statement, lastStatementOffset, scope)) {
                if (statement instanceof KtProperty && ((KtProperty) statement).getInitializer() != null) {
                    KtProperty property = (KtProperty) statement;
                    KtProperty declaration = createVariableDeclaration(property, generateDefaultInitializers);
                    declaration = (KtProperty) container.addBefore(declaration, dummyFirstStatement);
                    propertiesDeclarations.add(declaration);
                    container.addAfter(psiFactory.createNewLine(), declaration);
                    KtBinaryExpression assignment = createVariableAssignment(property);
                    resultStatements.add(property.replace(assignment));
                } else {
                    PsiElement newStatement = container.addBefore(statement, dummyFirstStatement);
                    container.addAfter(psiFactory.createNewLine(), newStatement);
                    container.deleteChildRange(statement, statement);
                }
            } else {
                resultStatements.add(statement);
            }
        }
    } finally {
        dummyFirstStatement.delete();
    }
    ShortenReferences.DEFAULT.process(propertiesDeclarations);
    return PsiUtilCore.toPsiElementArray(resultStatements);
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Project(com.intellij.openapi.project.Project) ArrayList(java.util.ArrayList) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement)

Example 12 with SearchScope

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

the class InplaceRefactoring method performInplaceRefactoring.

public boolean performInplaceRefactoring(final LinkedHashSet<String> nameSuggestions) {
    myNameSuggestions = nameSuggestions;
    if (InjectedLanguageUtil.isInInjectedLanguagePrefixSuffix(myElementToRename)) {
        return false;
    }
    final FileViewProvider fileViewProvider = myElementToRename.getContainingFile().getViewProvider();
    VirtualFile file = getTopLevelVirtualFile(fileViewProvider);
    SearchScope referencesSearchScope = getReferencesSearchScope(file);
    final Collection<PsiReference> refs = collectRefs(referencesSearchScope);
    addReferenceAtCaret(refs);
    for (PsiReference ref : refs) {
        final PsiFile containingFile = ref.getElement().getContainingFile();
        if (notSameFile(file, containingFile)) {
            return false;
        }
    }
    final PsiElement scope = checkLocalScope();
    if (scope == null) {
        // Should have valid local search scope for inplace rename
        return false;
    }
    final PsiFile containingFile = scope.getContainingFile();
    if (containingFile == null) {
        // Should have valid local search scope for inplace rename
        return false;
    }
    //no need to process further when file is read-only
    if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, containingFile))
        return true;
    myEditor.putUserData(INPLACE_RENAMER, this);
    ourRenamersStack.push(this);
    final List<Pair<PsiElement, TextRange>> stringUsages = new ArrayList<>();
    collectAdditionalElementsToRename(stringUsages);
    return buildTemplateAndStart(refs, stringUsages, scope, containingFile);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 13 with SearchScope

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

the class FindDialog method applyTo.

private void applyTo(@NotNull FindModel model, boolean findAll) {
    model.setCaseSensitive(myCbCaseSensitive.isSelected());
    if (model.isReplaceState()) {
        model.setPreserveCase(myCbPreserveCase.isSelected());
    }
    model.setWholeWordsOnly(myCbWholeWordsOnly.isSelected());
    String selectedSearchContextInUi = (String) mySearchContext.getSelectedItem();
    FindModel.SearchContext searchContext = parseSearchContext(selectedSearchContextInUi);
    model.setSearchContext(searchContext);
    model.setRegularExpressions(myCbRegularExpressions.isSelected());
    String stringToFind = getStringToFind();
    model.setStringToFind(stringToFind);
    if (model.isReplaceState()) {
        model.setPromptOnReplace(true);
        model.setReplaceAll(false);
        String stringToReplace = getStringToReplace();
        model.setStringToReplace(StringUtil.convertLineSeparators(stringToReplace));
    }
    if (!model.isMultipleFiles()) {
        model.setForward(myRbForward.isSelected());
        model.setFromCursor(myRbFromCursor.isSelected());
        model.setGlobal(myRbGlobal.isSelected());
    } else {
        if (myCbToOpenInNewTab != null) {
            model.setOpenInNewTab(myCbToOpenInNewTab.isSelected());
        }
        model.setProjectScope(myRbProject.isSelected());
        model.setDirectoryName(null);
        model.setModuleName(null);
        model.setCustomScopeName(null);
        model.setCustomScope(null);
        model.setCustomScope(false);
        if (myRbDirectory.isSelected()) {
            String directory = getDirectory();
            model.setDirectoryName(directory == null ? "" : directory);
            model.setWithSubdirectories(myCbWithSubdirectories.isSelected());
        } else if (myRbModule.isSelected()) {
            model.setModuleName((String) myModuleComboBox.getSelectedItem());
        } else if (myRbCustomScope.isSelected()) {
            SearchScope selectedScope = myScopeCombo.getSelectedScope();
            String customScopeName = selectedScope == null ? null : selectedScope.getDisplayName();
            model.setCustomScopeName(customScopeName);
            model.setCustomScope(selectedScope == null ? null : selectedScope);
            model.setCustomScope(true);
        }
    }
    model.setFindAll(findAll);
    String mask = getFileTypeMask();
    model.setFileFilter(mask);
}
Also used : SearchScope(com.intellij.psi.search.SearchScope)

Example 14 with SearchScope

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

the class NonPhysicalReferenceSearcher method processQuery.

public void processQuery(@NotNull ReferencesSearch.SearchParameters queryParameters, @NotNull Processor<PsiReference> consumer) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
    }
    final SearchScope scope = queryParameters.getScopeDeterminedByUser();
    final PsiElement element = queryParameters.getElementToSearch();
    final PsiFile containingFile = element.getContainingFile();
    if (!(scope instanceof GlobalSearchScope) && !isApplicableTo(containingFile)) {
        return;
    }
    final LocalSearchScope currentScope;
    if (scope instanceof LocalSearchScope) {
        if (queryParameters.isIgnoreAccessScope()) {
            return;
        }
        currentScope = (LocalSearchScope) scope;
    } else {
        currentScope = null;
    }
    Project project = element.getProject();
    if (!project.isInitialized()) {
        // skip default and other projects that look weird
        return;
    }
    final PsiManager psiManager = PsiManager.getInstance(project);
    for (VirtualFile virtualFile : FileEditorManager.getInstance(project).getOpenFiles()) {
        if (virtualFile.getFileType().isBinary()) {
            continue;
        }
        PsiFile file = psiManager.findFile(virtualFile);
        if (isApplicableTo(file)) {
            final LocalSearchScope fileScope = new LocalSearchScope(file);
            final LocalSearchScope searchScope = currentScope == null ? fileScope : fileScope.intersectWith(currentScope);
            ReferencesSearch.searchOptimized(element, searchScope, true, queryParameters.getOptimizer(), consumer);
        }
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Example 15 with SearchScope

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

the class JavaClassInheritorsSearcher method processInheritors.

private static boolean processInheritors(@NotNull final ClassInheritorsSearch.SearchParameters parameters, @NotNull final Processor<PsiClass> consumer) {
    @NotNull final PsiClass baseClass = parameters.getClassToProcess();
    if (baseClass instanceof PsiAnonymousClass || isFinal(baseClass))
        return true;
    final SearchScope searchScope = parameters.getScope();
    Project project = PsiUtilCore.getProjectInReadAction(baseClass);
    if (isJavaLangObject(baseClass)) {
        return AllClassesSearch.search(searchScope, project, parameters.getNameCondition()).forEach(aClass -> {
            ProgressManager.checkCanceled();
            return isJavaLangObject(aClass) || consumer.process(aClass);
        });
    }
    if (searchScope instanceof LocalSearchScope) {
        return processLocalScope(project, parameters, (LocalSearchScope) searchScope, baseClass, consumer);
    }
    Iterable<PsiClass> cached = getOrComputeSubClasses(project, baseClass, searchScope);
    for (final PsiClass subClass : cached) {
        ProgressManager.checkCanceled();
        if (subClass instanceof PsiAnonymousClass && !parameters.isIncludeAnonymous()) {
            continue;
        }
        if (ReadAction.compute(() -> checkCandidate(subClass, parameters) && !consumer.process(subClass))) {
            return false;
        }
    }
    return true;
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)90 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)39 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)36 Project (com.intellij.openapi.project.Project)21 NotNull (org.jetbrains.annotations.NotNull)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiElement (com.intellij.psi.PsiElement)16 PsiClass (com.intellij.psi.PsiClass)8 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)6 PsiMethod (com.intellij.psi.PsiMethod)6 HashMap (com.intellij.util.containers.HashMap)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)5 Nullable (org.jetbrains.annotations.Nullable)5 AnalysisScope (com.intellij.analysis.AnalysisScope)4 ProgressManager (com.intellij.openapi.progress.ProgressManager)4 TextRange (com.intellij.openapi.util.TextRange)4 PsiFile (com.intellij.psi.PsiFile)4 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)4 QueryExecutor (com.intellij.util.QueryExecutor)4