Search in sources :

Example 96 with SearchScope

use of com.intellij.psi.search.SearchScope in project yii2support by nvlad.

the class ViewUtil method getPhpViewVariables.

@NotNull
public static Collection<String> getPhpViewVariables(PsiFile psiFile) {
    final ArrayList<String> result = new ArrayList<>();
    final HashSet<String> allVariables = new HashSet<>();
    final HashSet<String> declaredVariables = new HashSet<>();
    final Collection<Variable> viewVariables = PsiTreeUtil.findChildrenOfType(psiFile, Variable.class);
    for (FunctionReference reference : PsiTreeUtil.findChildrenOfType(psiFile, FunctionReference.class)) {
        if (reference.getNode().getElementType() == PhpElementTypes.FUNCTION_CALL && psiFile.getUseScope().equals(reference.getUseScope())) {
            if (reference.getName() != null && reference.getName().equals("compact")) {
                for (PsiElement element : reference.getParameters()) {
                    if (element instanceof StringLiteralExpression) {
                        allVariables.add(((StringLiteralExpression) element).getContents());
                    }
                }
            }
        }
    }
    final SearchScope fileScope = psiFile.getUseScope();
    final HashSet<String> usedBeforeDeclaration = new HashSet<>();
    for (Variable variable : viewVariables) {
        String variableName = variable.getName();
        if (variable.isDeclaration() && fileScope.equals(variable.getUseScope()) && !(variable.getParent() instanceof PhpUseList) && !(variable.getParent() instanceof UnaryExpression) && !(variable.getParent() instanceof SelfAssignmentExpression) && !usedBeforeDeclaration.contains(variableName)) {
            declaredVariables.add(variableName);
        } else {
            if (!ignoredVariables.contains(variableName)) {
                if (fileScope.equals(variable.getUseScope()) || variable.getParent() instanceof PhpUseList) {
                    if (variable.getName().equals("") && variable.getParent() instanceof StringLiteralExpression) {
                        Variable inlineVariable = PsiTreeUtil.findChildOfType(variable, Variable.class);
                        if (inlineVariable != null) {
                            allVariables.add(inlineVariable.getName());
                            usedBeforeDeclaration.add(variableName);
                        }
                    } else {
                        allVariables.add(variableName);
                        usedBeforeDeclaration.add(variableName);
                    }
                }
            }
        }
    }
    for (String variable : allVariables) {
        if (!declaredVariables.contains(variable)) {
            result.add(variable);
        }
    }
    return result;
}
Also used : SearchScope(com.intellij.psi.search.SearchScope) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 97 with SearchScope

use of com.intellij.psi.search.SearchScope in project intellij by bazelbuild.

the class BuildReferenceSearcher method processQuery.

@Override
public void processQuery(SearchParameters params, Processor<PsiReference> consumer) {
    PsiElement element = params.getElementToSearch();
    if (element instanceof NamedBuildElement) {
        String fnName = ((NamedBuildElement) element).getName();
        if (fnName != null) {
            searchForString(params, element, fnName);
        }
        return;
    }
    PsiFile file = ResolveUtil.asFileSearch(element);
    if (file != null) {
        processFileReferences(params, file);
        return;
    }
    if (!(element instanceof FuncallExpression)) {
        return;
    }
    FuncallExpression funcall = (FuncallExpression) element;
    PsiFile localFile = element.getContainingFile();
    if (localFile == null) {
        return;
    }
    Label label = funcall.resolveBuildLabel();
    if (label == null) {
        searchForExternalWorkspace(params, localFile, funcall);
        return;
    }
    List<String> stringsToSearch = LabelUtils.getAllValidLabelStrings(label, true);
    for (String string : stringsToSearch) {
        if (LabelUtils.isAbsolute(string)) {
            searchForString(params, element, string);
        } else {
            // only a valid reference from local package -- restrict the search scope accordingly
            SearchScope scope = limitScopeToFile(params.getScopeDeterminedByUser(), localFile);
            if (scope != null) {
                searchForString(params, scope, element, string);
            }
        }
    }
}
Also used : NamedBuildElement(com.google.idea.blaze.base.lang.buildfile.psi.NamedBuildElement) Label(com.google.idea.blaze.base.model.primitives.Label) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiFile(com.intellij.psi.PsiFile) FuncallExpression(com.google.idea.blaze.base.lang.buildfile.psi.FuncallExpression) PsiElement(com.intellij.psi.PsiElement)

Example 98 with SearchScope

use of com.intellij.psi.search.SearchScope in project intellij by bazelbuild.

the class BuildReferenceSearcher method processFileReferences.

/**
 * Find all references to the given file within BUILD files.
 */
private void processFileReferences(SearchParameters params, PsiFile file) {
    if (file instanceof BuildFile) {
        BuildFile buildFile = (BuildFile) file;
        processBuildFileReferences(params, buildFile);
        if (buildFile.getBlazeFileType() == BlazeFileType.BuildPackage) {
            return;
        }
    // for skylark extensions, we also check for package-local references, below
    }
    BlazePackage blazePackage = BlazePackage.getContainingPackage(file);
    PsiDirectory directory = blazePackage != null ? blazePackage.getContainingDirectory() : null;
    if (directory == null) {
        return;
    }
    Label label = LabelUtils.createLabelForFile(blazePackage, PsiUtils.getFilePath(file));
    if (label == null) {
        return;
    }
    if (!(file instanceof BuildFile)) {
        // search globally, for an absolute label reference
        String absoluteLabel = String.format("//%s:%s", label.blazePackage(), label.targetName());
        searchForString(params, file, absoluteLabel);
    }
    // search for local references in the containing blaze package
    List<String> stringsToSearch = LabelUtils.getAllValidLabelStrings(label, true);
    SearchScope scope = params.getScopeDeterminedByUser().intersectWith(blazePackage.getSearchScope(true));
    for (String string : stringsToSearch) {
        searchForString(params, scope, file, string);
    }
}
Also used : BuildFile(com.google.idea.blaze.base.lang.buildfile.psi.BuildFile) PsiDirectory(com.intellij.psi.PsiDirectory) Label(com.google.idea.blaze.base.model.primitives.Label) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope)

Aggregations

SearchScope (com.intellij.psi.search.SearchScope)98 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)42 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)38 NotNull (org.jetbrains.annotations.NotNull)23 PsiElement (com.intellij.psi.PsiElement)22 Project (com.intellij.openapi.project.Project)21 VirtualFile (com.intellij.openapi.vfs.VirtualFile)18 PsiClass (com.intellij.psi.PsiClass)9 Processor (com.intellij.util.Processor)8 com.intellij.psi (com.intellij.psi)7 PsiMethod (com.intellij.psi.PsiMethod)7 PsiReference (com.intellij.psi.PsiReference)6 HashMap (com.intellij.util.containers.HashMap)6 Nullable (org.jetbrains.annotations.Nullable)6 ClassInheritorsSearch (com.intellij.psi.search.searches.ClassInheritorsSearch)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