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