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];
}
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;
}
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;
}
}
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());
}
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);
}
Aggregations