Find Usages: Restrict search scope for private class members

#KT-6124 Fixed
This commit is contained in:
Alexey Sedunov
2014-10-27 14:51:33 +03:00
parent a2930a581f
commit a13f334df2
8 changed files with 25 additions and 24 deletions
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.psi.search.LocalSearchScope;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.stubs.IStubElementType;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -89,9 +90,12 @@ abstract class JetNamedDeclarationStub<T extends PsiJetStubWithFqName> extends J
@NotNull
@Override
public SearchScope getUseScope() {
JetElement enclosingBlock = JetPsiUtil.getEnclosingElementForLocalDeclaration(this);
if (enclosingBlock != null) {
return new LocalSearchScope(enclosingBlock);
JetElement enclosingBlock = JetPsiUtil.getEnclosingElementForLocalDeclaration(this, false);
if (enclosingBlock != null) return new LocalSearchScope(enclosingBlock);
if (hasModifier(JetTokens.PRIVATE_KEYWORD)) {
JetElement containingClass = PsiTreeUtil.getParentOfType(this, JetClassOrObject.class);
if (containingClass != null) return new LocalSearchScope(containingClass);
}
return super.getUseScope();
@@ -819,12 +819,19 @@ public class JetPsiUtil {
@Nullable
public static JetElement getEnclosingElementForLocalDeclaration(@NotNull JetDeclaration declaration) {
if (declaration instanceof JetTypeParameter) {
return getEnclosingElementForLocalDeclaration(declaration, true);
}
@Nullable
public static JetElement getEnclosingElementForLocalDeclaration(@NotNull JetDeclaration declaration, boolean skipParameters) {
if (declaration instanceof JetTypeParameter && skipParameters) {
declaration = PsiTreeUtil.getParentOfType(declaration, JetNamedDeclaration.class);
}
else if (declaration instanceof JetParameter) {
if (((JetParameter) declaration).getValOrVarNode() != null) return null;
PsiElement parent = declaration.getParent();
if (parent != null && parent.getParent() instanceof JetNamedFunction) {
if (skipParameters && parent != null && parent.getParent() instanceof JetNamedFunction) {
declaration = (JetNamedFunction) parent.getParent();
}
}
@@ -77,10 +77,16 @@ public data class UsagesSearchLocation(
public data class UsagesSearchTarget<out T : PsiElement>(
val element: T,
val scope: SearchScope,
private val scope: SearchScope,
val location: UsagesSearchLocation = UsagesSearchLocation.DEFAULT,
val restrictByTargetScope: Boolean = true
)
) {
fun <U: PsiElement> retarget(element: U) =
UsagesSearchTarget(element, scope, location, restrictByTargetScope)
val effectiveScope: SearchScope
get() = if (restrictByTargetScope) scope and element.effectiveScope else scope
}
public trait UsagesSearchFilter {
class object {
@@ -150,7 +156,7 @@ public class KotlinPsiSearchHelper(private val project: Project): PsiSearchHelpe
public fun processFilesWithText(item: UsagesSearchRequestItem, consumer: Processor<PsiReference>): Boolean {
return item.words.all { word ->
val textProcessor = ResultTextProcessorImpl(item, consumer)
processElementsWithWord(textProcessor, item.target.scope, word, UsageSearchContext.IN_CODE, true)
processElementsWithWord(textProcessor, item.target.effectiveScope, word, UsageSearchContext.IN_CODE, true)
}
}
}
@@ -174,12 +180,6 @@ public object UsagesSearch: QueryFactory<PsiReference, UsagesSearchRequest>() {
fun UsagesSearchRequest.search(): Query<PsiReference> = UsagesSearch.search(this)
fun <A: PsiElement, B: PsiElement> UsagesSearchTarget<A>.retarget(element: B) =
UsagesSearchTarget(element, scope, location, restrictByTargetScope)
val <T: PsiElement> UsagesSearchTarget<T>.effectiveScope: SearchScope
get() = if (restrictByTargetScope) scope and element.effectiveScope else scope
val PsiElement.effectiveScope: SearchScope
get() = PsiSearchHelper.SERVICE.getInstance(getProject())!!.getUseScope(this)
@@ -1,3 +1 @@
New instance creation (10: 33) open fun processRequest() = Foo()
New instance creation (14: 44) override fun processRequest() = Server.Foo()
New instance creation (5: 24) println(Server.Foo())
@@ -1,3 +1 @@
Function call (12: 45) override fun processRequest() = "foo" + doProcessRequest()
Function call (5: 18) Server().doProcessRequest()
Function call (8: 33) open fun processRequest() = doProcessRequest()
@@ -1,3 +1 @@
Value read (10: 33) open fun processRequest() = Foo
Value read (14: 44) override fun processRequest() = Server.Foo
Value read (5: 26) println(Server().Foo)
@@ -1,3 +1 @@
Value read (12: 45) override fun processRequest() = "foo" + foo
Value read (5: 26) println(Server().foo)
Value read (8: 33) open fun processRequest() = foo
@@ -1,3 +1 @@
Value read (10: 45) override fun processRequest() = "foo" + foo
Value read (5: 26) println(Server().foo)
Value read (6: 33) open fun processRequest() = foo