Search in sources :

Example 6 with StringSearcher

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

the class LowLevelSearchUtilTest method doTest.

private static int doTest(String pattern, String text) {
    StringSearcher searcher = new StringSearcher(pattern, true, true, true);
    final int[] index = { -1 };
    LowLevelSearchUtil.processTextOccurrences(text, 0, text.length(), searcher, null, value -> {
        index[0] = value;
        return false;
    });
    return index[0];
}
Also used : StringSearcher(com.intellij.util.text.StringSearcher)

Example 7 with StringSearcher

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

the class InjectorUtils method calcInjections.

@NotNull
protected static TreeMap<TextRange, CommentInjectionData> calcInjections(PsiFile file) {
    final TreeMap<TextRange, CommentInjectionData> injectionMap = new TreeMap<>(RANGE_COMPARATOR);
    TIntArrayList ints = new TIntArrayList();
    StringSearcher searcher = new StringSearcher("language=", true, true, false);
    CharSequence contents = file.getViewProvider().getContents();
    final char[] contentsArray = CharArrayUtil.fromSequenceWithoutCopying(contents);
    int s0 = 0, s1 = contents.length();
    for (int idx = searcher.scan(contents, contentsArray, s0, s1); idx != -1; idx = searcher.scan(contents, contentsArray, idx + 1, s1)) {
        ints.add(idx);
        PsiComment element = PsiTreeUtil.findElementOfClassAtOffset(file, idx, PsiComment.class, false);
        if (element != null) {
            String str = ElementManipulators.getValueText(element).trim();
            CommentInjectionData injection = str.startsWith("language=") ? new CommentInjectionData(decodeMap(str), str) : null;
            if (injection != null) {
                injectionMap.put(element.getTextRange(), injection);
            }
        }
    }
    return injectionMap;
}
Also used : TIntArrayList(gnu.trove.TIntArrayList) StringSearcher(com.intellij.util.text.StringSearcher) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with StringSearcher

use of com.intellij.util.text.StringSearcher in project smali by JesusFreke.

the class SmaliClassReferenceSearcher method processQuery.

@Override
public void processQuery(final SearchParameters queryParameters, final Processor<PsiReference> consumer) {
    final PsiElement element = queryParameters.getElementToSearch();
    if (!(element instanceof PsiClass)) {
        return;
    }
    String smaliType = ApplicationManager.getApplication().runReadAction(new Computable<String>() {

        @Override
        public String compute() {
            String qualifiedName = ((PsiClass) element).getQualifiedName();
            if (qualifiedName != null) {
                return NameUtils.javaToSmaliType((PsiClass) element);
            }
            return null;
        }
    });
    if (smaliType == null) {
        return;
    }
    final StringSearcher stringSearcher = new StringSearcher(smaliType, true, true, false, false);
    final SingleTargetRequestResultProcessor processor = new SingleTargetRequestResultProcessor(element);
    SearchScope querySearchScope = ApplicationManager.getApplication().runReadAction(new Computable<SearchScope>() {

        @Override
        public SearchScope compute() {
            return queryParameters.getEffectiveSearchScope();
        }
    });
    if (querySearchScope instanceof LocalSearchScope) {
        for (final PsiElement scopeElement : ((LocalSearchScope) querySearchScope).getScope()) {
            ApplicationManager.getApplication().runReadAction(new Runnable() {

                @Override
                public void run() {
                    LowLevelSearchUtil.processElementsContainingWordInElement(new TextOccurenceProcessor() {

                        @Override
                        public boolean execute(@NotNull PsiElement element, int offsetInElement) {
                            return processor.processTextOccurrence(element, offsetInElement, consumer);
                        }
                    }, scopeElement, stringSearcher, true, new EmptyProgressIndicator());
                }
            });
        }
    } else if (querySearchScope instanceof GlobalSearchScope) {
        PsiSearchHelper helper = PsiSearchHelper.SERVICE.getInstance(element.getProject());
        // TODO: limit search scope to only smali files. See, e.g. AnnotatedPackagesSearcher.PackageInfoFilesOnly
        helper.processAllFilesWithWord(smaliType, (GlobalSearchScope) querySearchScope, new Processor<PsiFile>() {

            @Override
            public boolean process(PsiFile file) {
                LowLevelSearchUtil.processElementsContainingWordInElement(new TextOccurenceProcessor() {

                    @Override
                    public boolean execute(@NotNull PsiElement element, int offsetInElement) {
                        return processor.processTextOccurrence(element, offsetInElement, consumer);
                    }
                }, file, stringSearcher, true, new EmptyProgressIndicator());
                return true;
            }
        }, true);
    } else {
        assert false;
        return;
    }
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Processor(com.intellij.util.Processor) PsiClass(com.intellij.psi.PsiClass) NotNull(org.jetbrains.annotations.NotNull) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) StringSearcher(com.intellij.util.text.StringSearcher)

Example 9 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)

Example 10 with StringSearcher

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

the class PsiSearchHelperImpl method bulkProcessElementsWithWord.

private boolean bulkProcessElementsWithWord(@NotNull SearchScope searchScope, @NotNull final String text, final short searchContext, @NotNull EnumSet<Options> options, @Nullable String containerName, @NotNull final BulkOccurrenceProcessor processor) {
    if (text.isEmpty()) {
        throw new IllegalArgumentException("Cannot search for elements with empty text");
    }
    final ProgressIndicator progress = getOrCreateIndicator();
    if (searchScope instanceof GlobalSearchScope) {
        StringSearcher searcher = new StringSearcher(text, options.contains(Options.CASE_SENSITIVE_SEARCH), true, searchContext == UsageSearchContext.IN_STRINGS, options.contains(Options.PROCESS_ONLY_JAVA_IDENTIFIERS_IF_POSSIBLE));
        return processElementsWithTextInGlobalScope(processor, (GlobalSearchScope) searchScope, searcher, searchContext, options.contains(Options.CASE_SENSITIVE_SEARCH), containerName, progress);
    }
    LocalSearchScope scope = (LocalSearchScope) searchScope;
    PsiElement[] scopeElements = scope.getScope();
    final StringSearcher searcher = new StringSearcher(text, options.contains(Options.CASE_SENSITIVE_SEARCH), true, searchContext == UsageSearchContext.IN_STRINGS, options.contains(Options.PROCESS_ONLY_JAVA_IDENTIFIERS_IF_POSSIBLE));
    ReadActionProcessor<PsiElement> localProcessor = new ReadActionProcessor<PsiElement>() {

        @Override
        public boolean processInReadAction(PsiElement scopeElement) {
            if (!scopeElement.isValid())
                return true;
            if (!scopeElement.isPhysical() || scopeElement instanceof PsiCompiledElement) {
                scopeElement = scopeElement.getNavigationElement();
            }
            if (scopeElement instanceof PsiCompiledElement) {
                // can't scan text of the element
                return true;
            }
            if (scopeElement.getTextRange() == null) {
                // clients can put whatever they want to the LocalSearchScope. Skip what we can't process.
                LOG.debug("Element " + scopeElement + " of class " + scopeElement.getClass() + " has null range");
                return true;
            }
            return processor.execute(scopeElement, LowLevelSearchUtil.getTextOccurrencesInScope(scopeElement, searcher, progress), searcher);
        }

        @Override
        public String toString() {
            return processor.toString();
        }
    };
    return JobLauncher.getInstance().invokeConcurrentlyUnderProgress(Arrays.asList(scopeElements), progress, true, true, localProcessor);
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) StringSearcher(com.intellij.util.text.StringSearcher) ReadActionProcessor(com.intellij.openapi.application.ReadActionProcessor)

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