Search in sources :

Example 11 with StringSearcher

use of com.intellij.util.text.StringSearcher in project intellij-community by JetBrains.

the class DuplicateStringLiteralInspection method findDuplicateLiterals.

@NotNull
private List<PsiLiteralExpression> findDuplicateLiterals(String stringToFind, Project project) {
    Set<PsiFile> resultFiles = getCandidateFiles(stringToFind, project);
    if (resultFiles.isEmpty())
        return Collections.emptyList();
    List<PsiLiteralExpression> foundExpr = new ArrayList<>();
    for (final PsiFile file : resultFiles) {
        ProgressManager.checkCanceled();
        FileViewProvider viewProvider = file.getViewProvider();
        // important: skip non-java files with given word in literal (IDEA-126201)
        if (viewProvider.getPsi(JavaLanguage.INSTANCE) == null)
            continue;
        CharSequence text = viewProvider.getContents();
        StringSearcher searcher = new StringSearcher(stringToFind, true, true);
        LowLevelSearchUtil.processTextOccurrences(text, 0, text.length(), searcher, ProgressManager.getInstance().getProgressIndicator(), offset -> {
            PsiElement element = file.findElementAt(offset);
            if (element == null || !(element.getParent() instanceof PsiLiteralExpression))
                return true;
            PsiLiteralExpression expression = (PsiLiteralExpression) element.getParent();
            if (Comparing.equal(stringToFind, expression.getValue()) && shouldCheck(expression)) {
                foundExpr.add(expression);
            }
            return true;
        });
    }
    return foundExpr;
}
Also used : StringSearcher(com.intellij.util.text.StringSearcher) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with StringSearcher

use of com.intellij.util.text.StringSearcher in project intellij-community by JetBrains.

the class PatternEditorContextMembersProvider method calcDevPatternClassNames.

private static Set<String> calcDevPatternClassNames(@NotNull final Project project) {
    final List<String> roots = ContainerUtil.createLockFreeCopyOnWriteList();
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    PsiClass beanClass = psiFacade.findClass(PatternClassBean.class.getName(), GlobalSearchScope.allScope(project));
    if (beanClass != null) {
        GlobalSearchScope scope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.allScope(project), StdFileTypes.XML);
        final TextOccurenceProcessor occurenceProcessor = new TextOccurenceProcessor() {

            @Override
            public boolean execute(@NotNull PsiElement element, int offsetInElement) {
                XmlTag tag = PsiTreeUtil.getParentOfType(element, XmlTag.class);
                String className = tag == null ? null : tag.getAttributeValue("className");
                if (StringUtil.isNotEmpty(className) && tag.getLocalName().endsWith("patternClass")) {
                    roots.add(className);
                }
                return true;
            }
        };
        final StringSearcher searcher = new StringSearcher("patternClass", true, true);
        CacheManager.SERVICE.getInstance(beanClass.getProject()).processFilesWithWord(psiFile -> {
            LowLevelSearchUtil.processElementsContainingWordInElement(occurenceProcessor, psiFile, searcher, true, new EmptyProgressIndicator());
            return true;
        }, searcher.getPattern(), UsageSearchContext.IN_FOREIGN_LANGUAGES, scope, searcher.isCaseSensitive());
    }
    return ContainerUtil.newHashSet(roots);
}
Also used : PatternClassBean(com.intellij.patterns.compiler.PatternClassBean) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull) TextOccurenceProcessor(com.intellij.psi.search.TextOccurenceProcessor) XmlTag(com.intellij.psi.xml.XmlTag) StringSearcher(com.intellij.util.text.StringSearcher)

Example 13 with StringSearcher

use of com.intellij.util.text.StringSearcher in project intellij-community by JetBrains.

the class DuplicatePropertyInspection method prepareDuplicateValuesByFile.

private static void prepareDuplicateValuesByFile(final Map<String, Set<PsiFile>> valueToFiles, final InspectionManager manager, final List<ProblemDescriptor> problemDescriptors, final PsiFile psiFile, final ProgressIndicator progress) {
    for (final String value : valueToFiles.keySet()) {
        if (progress != null) {
            progress.setText2(InspectionsBundle.message("duplicate.property.value.progress.indicator.text", value));
            progress.checkCanceled();
        }
        if (value.length() == 0)
            continue;
        StringSearcher searcher = new StringSearcher(value, true, true);
        final StringBuffer message = new StringBuffer();
        final int[] duplicatesCount = { 0 };
        Set<PsiFile> psiFilesWithDuplicates = valueToFiles.get(value);
        for (final PsiFile file : psiFilesWithDuplicates) {
            CharSequence text = file.getViewProvider().getContents();
            LowLevelSearchUtil.processTextOccurrences(text, 0, text.length(), searcher, progress, offset -> {
                PsiElement element = file.findElementAt(offset);
                if (element != null && element.getParent() instanceof Property) {
                    final Property property = (Property) element.getParent();
                    if (Comparing.equal(property.getValue(), value) && element.getStartOffsetInParent() != 0) {
                        if (duplicatesCount[0] == 0) {
                            message.append(InspectionsBundle.message("duplicate.property.value.problem.descriptor", property.getValue()));
                        }
                        surroundWithHref(message, element, true);
                        duplicatesCount[0]++;
                    }
                }
                return true;
            });
        }
        if (duplicatesCount[0] > 1) {
            problemDescriptors.add(manager.createProblemDescriptor(psiFile, message.toString(), false, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
        }
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) PsiElement(com.intellij.psi.PsiElement) StringSearcher(com.intellij.util.text.StringSearcher)

Example 14 with StringSearcher

use of com.intellij.util.text.StringSearcher in project intellij-community by JetBrains.

the class FindManagerImpl method doFindString.

@NotNull
private FindResult doFindString(@NotNull CharSequence text, @Nullable char[] textArray, int offset, @NotNull FindModel findmodel, @Nullable VirtualFile file) {
    FindModel model = normalizeIfMultilined(findmodel);
    String toFind = model.getStringToFind();
    if (toFind.isEmpty()) {
        return NOT_FOUND_RESULT;
    }
    if (model.isInCommentsOnly() || model.isInStringLiteralsOnly()) {
        if (file == null)
            return NOT_FOUND_RESULT;
        return findInCommentsAndLiterals(text, textArray, offset, model, file);
    }
    if (model.isRegularExpressions()) {
        return findStringByRegularExpression(text, offset, model);
    }
    final StringSearcher searcher = createStringSearcher(model);
    int index;
    if (model.isForward()) {
        final int res = searcher.scan(text, textArray, offset, text.length());
        index = res < 0 ? -1 : res;
    } else {
        index = offset == 0 ? -1 : searcher.scan(text, textArray, 0, offset - 1);
    }
    if (index < 0) {
        return NOT_FOUND_RESULT;
    }
    return new FindResultImpl(index, index + toFind.length());
}
Also used : LightweightHint(com.intellij.ui.LightweightHint) StringSearcher(com.intellij.util.text.StringSearcher) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StringSearcher (com.intellij.util.text.StringSearcher)14 NotNull (org.jetbrains.annotations.NotNull)7 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)4 LightweightHint (com.intellij.ui.LightweightHint)3 TIntArrayList (gnu.trove.TIntArrayList)3 ReadActionProcessor (com.intellij.openapi.application.ReadActionProcessor)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 PsiElement (com.intellij.psi.PsiElement)2 PsiFile (com.intellij.psi.PsiFile)2 Processor (com.intellij.util.Processor)2 THashSet (gnu.trove.THashSet)2 Matcher (java.util.regex.Matcher)2 Language (com.intellij.lang.Language)1 IProperty (com.intellij.lang.properties.IProperty)1 Property (com.intellij.lang.properties.psi.Property)1 Lexer (com.intellij.lexer.Lexer)1 Document (com.intellij.openapi.editor.Document)1 TextAttributesKey (com.intellij.openapi.editor.colors.TextAttributesKey)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 AbstractFileType (com.intellij.openapi.fileTypes.impl.AbstractFileType)1