use of com.intellij.psi.search.LocalSearchScope in project intellij-plugins by JetBrains.
the class DartControlFlow method analyze.
public static DartControlFlow analyze(PsiElement[] elements) {
final PsiElement scope = PsiTreeUtil.getTopmostParentOfType(elements[0], DartExecutionScope.class);
final PsiElement lastElement = elements[elements.length - 1];
final int lastElementEndOffset = lastElement.getTextRange().getEndOffset();
final int firstElementStartOffset = elements[0].getTextRange().getStartOffset();
// find out params
assert scope != null;
final LocalSearchScope localSearchScope = new LocalSearchScope(scope);
final List<DartComponentName> outDeclarations = ContainerUtil.filter(DartControlFlowUtil.getSimpleDeclarations(elements, null, false), componentName -> {
for (PsiReference usage : ReferencesSearch.search(componentName, localSearchScope, false).findAll()) {
if (usage.getElement().getTextRange().getStartOffset() > lastElementEndOffset) {
return true;
}
}
return false;
});
// find params
final DartReferenceVisitor dartReferenceVisitor = new DartReferenceVisitor();
for (PsiElement element : elements) {
element.accept(dartReferenceVisitor);
}
final List<DartComponentName> inComponentNames = ContainerUtil.filter(dartReferenceVisitor.getComponentNames(), componentName -> {
final int offset = componentName.getTextRange().getStartOffset();
final boolean declarationInElements = firstElementStartOffset <= offset && offset < lastElementEndOffset;
return !declarationInElements;
});
return new DartControlFlow(inComponentNames, outDeclarations);
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-plugins by JetBrains.
the class DartServerFindUsagesTest method testBoolUsagesWithScope.
public void testBoolUsagesWithScope() throws Exception {
final PsiFile psiFile1 = myFixture.configureByText("file.dart", "/// [bool]\n" + "<caret>bool foo() {\n" + " var bool = #bool;\n" + "}");
final PsiFile psiFile2 = myFixture.addFileToProject("file1.dart", "bool x;");
// warm up
myFixture.doHighlighting();
DartTestUtils.letAnalyzerSmellCoreFile(myFixture, "iterable.dart");
myFixture.openFileInEditor(psiFile1.getVirtualFile());
final String[] allProjectUsages = { "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15", "DartReferenceExpressionImpl in file1.dart@0:4" };
checkUsages(new LocalSearchScope(psiFile1), "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15");
checkUsages(new LocalSearchScope(psiFile2), "DartReferenceExpressionImpl in file1.dart@0:4");
checkUsages(new LocalSearchScope(new PsiFile[] { psiFile1, psiFile2 }), allProjectUsages);
checkUsages(GlobalSearchScope.fileScope(getProject(), psiFile1.getVirtualFile()), "PsiCommentImpl in " + psiFile1.getName() + "@5:9 (non-code usage)", "DartReferenceExpressionImpl in " + psiFile1.getName() + "@11:15");
checkUsages(GlobalSearchScope.fileScope(getProject(), psiFile2.getVirtualFile()), "DartReferenceExpressionImpl in file1.dart@0:4");
checkUsages(GlobalSearchScope.filesScope(getProject(), Arrays.asList(psiFile1.getVirtualFile(), psiFile2.getVirtualFile())), allProjectUsages);
checkUsages(GlobalSearchScope.projectScope(getProject()), allProjectUsages);
final Collection<UsageInfo> usages = findUsages(GlobalSearchScope.allScope(getProject()));
assertTrue(String.valueOf(usages.size()), usages.size() > 15);
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class AnnotatedMembersSearcher method execute.
@Override
public boolean execute(@NotNull final AnnotatedElementsSearch.Parameters p, @NotNull final Processor<PsiModifierListOwner> consumer) {
final PsiClass annClass = p.getAnnotationClass();
assert annClass.isAnnotationType() : "Annotation type should be passed to annotated members search";
final String annotationFQN = ApplicationManager.getApplication().runReadAction(new Computable<String>() {
@Override
public String compute() {
return annClass.getQualifiedName();
}
});
assert annotationFQN != null;
final SearchScope scope = p.getScope();
final List<PsiModifierListOwner> candidates;
if (scope instanceof GlobalSearchScope) {
candidates = getAnnotatedMemberCandidates(annClass, ((GlobalSearchScope) scope));
} else {
candidates = new ArrayList<>();
for (final PsiElement element : ((LocalSearchScope) scope).getScope()) {
ApplicationManager.getApplication().runReadAction(() -> {
if (element instanceof GroovyPsiElement) {
((GroovyPsiElement) element).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitMethod(@NotNull GrMethod method) {
candidates.add(method);
}
@Override
public void visitField(@NotNull GrField field) {
candidates.add(field);
}
});
}
});
}
}
for (final PsiModifierListOwner candidate : candidates) {
boolean accepted = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
if (AnnotatedElementsSearcher.isInstanceof(candidate, p.getTypes())) {
PsiModifierList list = candidate.getModifierList();
if (list != null) {
for (PsiAnnotation annotation : list.getAnnotations()) {
if ((p.isApproximate() || annotationFQN.equals(annotation.getQualifiedName())) && !consumer.process(candidate)) {
return false;
}
}
}
}
return true;
}
});
if (!accepted)
return false;
}
return true;
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class IdentifierHighlighterPass method getUsages.
@NotNull
private static Couple<Collection<TextRange>> getUsages(@NotNull PsiElement target, PsiElement psiElement, boolean withDeclarations, boolean detectAccess) {
List<TextRange> readRanges = new ArrayList<>();
List<TextRange> writeRanges = new ArrayList<>();
final ReadWriteAccessDetector detector = detectAccess ? ReadWriteAccessDetector.findDetector(target) : null;
final FindUsagesManager findUsagesManager = ((FindManagerImpl) FindManager.getInstance(target.getProject())).getFindUsagesManager();
final FindUsagesHandler findUsagesHandler = findUsagesManager.getFindUsagesHandler(target, true);
final LocalSearchScope scope = new LocalSearchScope(psiElement);
Collection<PsiReference> refs = findUsagesHandler != null ? findUsagesHandler.findReferencesToHighlight(target, scope) : ReferencesSearch.search(target, scope).findAll();
for (PsiReference psiReference : refs) {
if (psiReference == null) {
LOG.error("Null reference returned, findUsagesHandler=" + findUsagesHandler + "; target=" + target + " of " + target.getClass());
continue;
}
List<TextRange> destination;
if (detector == null || detector.getReferenceAccess(target, psiReference) == ReadWriteAccessDetector.Access.Read) {
destination = readRanges;
} else {
destination = writeRanges;
}
HighlightUsagesHandler.collectRangesToHighlight(psiReference, destination);
}
if (withDeclarations) {
final TextRange declRange = HighlightUsagesHandler.getNameIdentifierRange(psiElement.getContainingFile(), target);
if (declRange != null) {
if (detector != null && detector.isDeclarationWriteAccess(target)) {
writeRanges.add(declRange);
} else {
readRanges.add(declRange);
}
}
}
return Couple.<Collection<TextRange>>of(readRanges, writeRanges);
}
use of com.intellij.psi.search.LocalSearchScope in project intellij-community by JetBrains.
the class MemberInplaceRenamer method appendAdditionalElement.
@Override
protected boolean appendAdditionalElement(Collection<PsiReference> refs, Collection<Pair<PsiElement, TextRange>> stringUsages) {
boolean showChooser = super.appendAdditionalElement(refs, stringUsages);
PsiNamedElement variable = getVariable();
if (variable != null) {
final PsiElement substituted = getSubstituted();
if (substituted != null) {
appendAdditionalElement(stringUsages, variable, substituted);
RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(substituted);
final HashMap<PsiElement, String> allRenames = new HashMap<>();
PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
processor.prepareRenaming(substituted, "", allRenames, new LocalSearchScope(currentFile));
for (PsiElement element : allRenames.keySet()) {
appendAdditionalElement(stringUsages, variable, element);
}
}
}
return showChooser;
}
Aggregations