Java Code Examples for com.intellij.psi.search.GlobalSearchScope.getScopeRestrictedByFileTypes()

The following are Jave code examples for showing how to use getScopeRestrictedByFileTypes() of the com.intellij.psi.search.GlobalSearchScope class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: jgiven-intellij-plugin   File: ScenarioStateReferenceProvider.java   Source Code and License Vote up 4 votes
@NotNull
private GlobalSearchScope javaFilesScope(Project project) {
    return GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(project), StdFileTypes.JAVA);
}
 
Example 2
Project: yii2inspections   File: UnusedTranslationsInspector.java   Source Code and License Vote up 4 votes
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PhpElementVisitor() {
        @Override
        public void visitPhpArrayCreationExpression(ArrayCreationExpression expression) {
            final PsiFile file = expression.getContainingFile();
            if (!TranslationProviderUtil.isProvider(file)) {
                return;
            }

            /* prepare scope of index search */
            GlobalSearchScope theScope = GlobalSearchScope.allScope(expression.getProject());
            theScope = GlobalSearchScope.getScopeRestrictedByFileTypes(theScope, PhpFileType.INSTANCE, HtmlFileType.INSTANCE, TwigFileType.INSTANCE);

            /* iterate defined translations and report unused */
            final String searchPrefix = file.getName().replaceAll("\\.php$", "|");
            final Project project     = expression.getProject();
            for (ArrayHashElement pair : expression.getHashElements()) {
                final PhpPsiElement key = pair.getKey();
                if (!(key instanceof StringLiteralExpression)) {
                    continue;
                }

                final StringLiteralExpression literal = (StringLiteralExpression) key;
                final String messageToFind            = PhpStringUtil.unescapeText(literal.getContents(), literal.isSingleQuote());
                final String regularEntry             = searchPrefix + messageToFind;

                final Set<String> usages
                    = new HashSet<>(FileBasedIndex.getInstance().getAllKeys(TranslationCallsIndexer.identity, project));
                boolean found = usages.contains(regularEntry);
                if (!found && usages.size() > 0) {
                    final String slashedCategoryEntry = "/" + regularEntry;
                    for (String usage : usages) {
                        if (usage.endsWith(slashedCategoryEntry)) {
                            found = true;
                            break;
                        }
                    }
                }
                usages.clear();

                if (!found) {
                    holder.registerProblem(pair, messagePattern, ProblemHighlightType.LIKE_UNUSED_SYMBOL, new TheLocalFix());
                }
            }
        }
    };
}
 
Example 3
Project: yii2inspections   File: TranslationsCorrectnessInspector.java   Source Code and License Vote up 4 votes
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new PhpElementVisitor() {
        @Override
        public void visitPhpMethodReference(MethodReference reference) {
            /* ensure that it's a target call; category is not empty and has no injections; we have messages */
            TranslationCallsProcessUtil.ProcessingResult extracted = TranslationCallsProcessUtil.process(reference, true);
            if (null == extracted) {
                return;
            }

            /* handle `category` and `category/subcategory` cases */
            String categoryForFileName = extracted.getCategory().getContents();
            if (-1 != categoryForFileName.indexOf('/')) {
                categoryForFileName
                    = categoryForFileName.substring(1 + categoryForFileName.lastIndexOf('/'), categoryForFileName.length());
            }
            final String expectedFileName = categoryForFileName + ".php";

            /* iterate found translations and validate correctness */
            final Map<StringLiteralExpression, PsiElement> messages = extracted.getMessages();
            for (StringLiteralExpression literal : messages.keySet()) {
                /* only quotes, no content presented */
                if (literal.getTextLength() <= 2) {
                    continue;
                }

                final String message             = literal.getContents();
                final PsiElement reportingTarget = messages.get(literal);

                /* warn injections are presented and skip further processing */
                if (REPORT_INJECTIONS && null != literal.getFirstPsiChild()) {
                    holder.registerProblem(reportingTarget, messageInjection, ProblemHighlightType.WEAK_WARNING);
                    continue;
                }
                /* warn if non-ascii characters has been used */
                if (REPORT_NONASCII_CHARACTERS && nonAsciiCharsRegex.matcher(message).matches()) {
                    holder.registerProblem(reportingTarget, messageNonAscii, ProblemHighlightType.WEAK_WARNING);
                }

                /* warn if the message is have no translations in the group */
                final Set<String> searchEntry
                    = new HashSet<>(Collections.singletonList(PhpStringUtil.unescapeText(message, literal.isSingleQuote())));
                GlobalSearchScope theScope = GlobalSearchScope.allScope(reference.getProject());
                theScope                   = GlobalSearchScope.getScopeRestrictedByFileTypes(theScope, PhpFileType.INSTANCE);
                final Set<VirtualFile> providers = new HashSet<>();
                FileBasedIndex.getInstance()
                        .getFilesWithKey(TranslationKeysIndexer.identity, searchEntry, virtualFile -> {
                            if (virtualFile.getName().equals(expectedFileName)) {
                                providers.add(virtualFile);
                            }

                            return true;
                        }, theScope);

                /* report found cases */
                if (REPORT_UNKNOWN_TRANSLATIONS && 0 == providers.size()) {
                    holder.registerProblem(reportingTarget, messageNoTranslations, ProblemHighlightType.WEAK_WARNING);
                }
                providers.clear();
            }

            extracted.dispose();
        }
    };
}