SearchScope (@NotNull PsiClass aClass) { if (aClass instanceof PsiAnonymousClass) { return new LocalSearchScope(aClass); } if (aClass instanceof StubBasedPsiElement) { StubElement<?> stubElement = ((StubBasedPsiElement<?>)aClass).getStub(); if (stubElement instanceof PsiClassStub) { PsiClassStub<?> stub = (PsiClassStub<?>)stubElement; if (stub instanceof PsiClassStubImpl && ((PsiClassStubImpl<?>)stub).isLocalClassInner()) { // at least it's smaller than the containing package return new LocalSearchScope(aClass.getContainingFile()); } } } else { PsiElement parent = aClass.getParent(); if (parent instanceof PsiDeclarationStatement) { PsiElement grandParent = parent.getParent(); if (grandParent instanceof PsiCodeBlock) { return new LocalSearchScope(grandParent); // Actually: The scope of a local class or interface declaration immediately enclosed by a block is the rest // of the immediately enclosing block, including the local class or interface declaration itself (jls-6.3). } } } GlobalSearchScope maximalUseScope = ResolveScopeManager.getElementUseScope(aClass); PsiFile file = aClass.getContainingFile(); if (PsiImplUtil.isInServerPage(file)) return maximalUseScope; SearchScope searchScope = PsiSearchScopeUtil.USE_SCOPE_KEY.get(file); if (searchScope != null) return searchScope; PsiClass containingClass = aClass.getContainingClass(); if (aClass.hasModifierProperty(PsiModifier.PUBLIC) || aClass.hasModifierProperty(PsiModifier.PROTECTED)) { return containingClass == null ? maximalUseScope : containingClass.getUseScope(); } else if (aClass.hasModifierProperty(PsiModifier.PRIVATE) || aClass instanceof PsiTypeParameter) { PsiClass topClass = PsiUtil.getTopLevelClass(aClass); return new LocalSearchScope(topClass == null ? aClass.getContainingFile() : topClass); } else { PsiPackage aPackage = null; if (file instanceof PsiJavaFile) { aPackage = JavaPsiFacade.getInstance(aClass.getProject()).findPackage(((PsiJavaFile)file).getPackageName()); } if (aPackage == null) { PsiDirectory dir = file.getContainingDirectory(); if (dir != null) { aPackage = JavaDirectoryService.getInstance().getPackage(dir); } } if (aPackage != null) { SearchScope scope = PackageScope.packageScope(aPackage, false); scope = scope.intersectWith(maximalUseScope); return scope; } return new LocalSearchScope(file); } }