Fast get/set operators search

This commit is contained in:
Valentin Kipyatkov
2016-09-02 15:43:23 +03:00
parent eeea85c86a
commit 0f32574be0
8 changed files with 75 additions and 9 deletions
@@ -24,17 +24,17 @@ import com.intellij.util.Processor
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.psi.KtBinaryExpression
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
class BinaryOperatorReferenceSearcher(
targetDeclaration: KtDeclaration,
targetFunction: KtFunction,
private val operationTokens: List<KtSingleValueToken>,
searchScope: SearchScope,
consumer: Processor<PsiReference>,
optimizer: SearchRequestCollector
) : OperatorReferenceSearcher<KtBinaryExpression>(targetDeclaration, searchScope, consumer, optimizer, wordsToSearch = operationTokens.map { it.value }) {
) : OperatorReferenceSearcher<KtBinaryExpression>(targetFunction, searchScope, consumer, optimizer, wordsToSearch = operationTokens.map { it.value }) {
override fun processSuspiciousExpression(expression: KtExpression) {
val binaryExpression = expression.parent as? KtBinaryExpression ?: return
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.search.usagesSearch
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReference
import com.intellij.psi.search.SearchRequestCollector
import com.intellij.psi.search.SearchScope
import com.intellij.util.Processor
import org.jetbrains.kotlin.idea.references.KtArrayAccessReference
import org.jetbrains.kotlin.psi.KtArrayAccessExpression
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
//TODO: more effective search of 'set'
class IndexingOperatorReferenceSearcher(
targetFunction: KtFunction,
searchScope: SearchScope,
consumer: Processor<PsiReference>,
optimizer: SearchRequestCollector
) : OperatorReferenceSearcher<KtArrayAccessExpression>(targetFunction, searchScope, consumer, optimizer, wordsToSearch = listOf("[")) {
override fun processSuspiciousExpression(expression: KtExpression) {
val accessExpression = expression.parent as? KtArrayAccessExpression ?: return
if (expression != accessExpression.arrayExpression) return
processReferenceElement(accessExpression)
}
override fun isReferenceToCheck(ref: PsiReference) = ref is KtArrayAccessReference
override fun extractReference(element: PsiElement): PsiReference? {
val accessExpression = element as? KtArrayAccessExpression ?: return null
return accessExpression.references.firstIsInstance<KtArrayAccessReference>()
}
}
@@ -30,11 +30,11 @@ import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
class InvokeOperatorReferenceSearcher(
targetDeclaration: KtFunction,
targetFunction: KtFunction,
searchScope: SearchScope,
consumer: Processor<PsiReference>,
optimizer: SearchRequestCollector
) : OperatorReferenceSearcher<KtCallExpression>(targetDeclaration, searchScope, consumer, optimizer, wordsToSearch = emptyList()) {
) : OperatorReferenceSearcher<KtCallExpression>(targetFunction, searchScope, consumer, optimizer, wordsToSearch = emptyList()) {
companion object {
fun runForPsiMethod(
@@ -133,6 +133,10 @@ abstract class OperatorReferenceSearcher<TReferenceElement : KtElement>(
return UnaryOperatorReferenceSearcher(declaration, unaryOp, searchScope, consumer, optimizer)
}
if (name in INDEXING_OPERATION_NAMES) {
return IndexingOperatorReferenceSearcher(declaration, searchScope, consumer, optimizer)
}
return null
}
@@ -23,18 +23,18 @@ import com.intellij.psi.search.SearchScope
import com.intellij.util.Processor
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtFunction
import org.jetbrains.kotlin.psi.KtUnaryExpression
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
class UnaryOperatorReferenceSearcher(
targetDeclaration: KtDeclaration,
targetFunction: KtFunction,
private val operationToken: KtSingleValueToken,
searchScope: SearchScope,
consumer: Processor<PsiReference>,
optimizer: SearchRequestCollector
) : OperatorReferenceSearcher<KtUnaryExpression>(targetDeclaration, searchScope, consumer, optimizer, wordsToSearch = listOf(operationToken.value)) {
) : OperatorReferenceSearcher<KtUnaryExpression>(targetFunction, searchScope, consumer, optimizer, wordsToSearch = listOf(operationToken.value)) {
override fun processSuspiciousExpression(expression: KtExpression) {
val unaryExpression = expression.parent as? KtUnaryExpression ?: return
@@ -54,7 +54,6 @@ fun Name.getOperationSymbolsToSearch(): Pair<Set<KtToken>, Class<*>>? {
OperatorNameConventions.EQUALS -> return EQUALS_OPERATIONS to KtSimpleNameReference::class.java
OperatorNameConventions.CONTAINS -> return IN_OPERATIONS_TO_SEARCH to KtSimpleNameReference::class.java
OperatorNameConventions.ITERATOR -> return IN_OPERATIONS_TO_SEARCH to KtForLoopInReference::class.java
in INDEXING_OPERATION_NAMES -> return setOf(KtTokens.LBRACKET) to KtArrayAccessReference::class.java
in DELEGATE_ACCESSOR_NAMES -> return setOf(KtTokens.BY_KEYWORD) to KtPropertyDelegationMethodsReference::class.java
DelegatedPropertyResolver.PROPERTY_DELEGATED_FUNCTION_NAME -> return setOf(KtTokens.BY_KEYWORD) to KtPropertyDelegationMethodsReference::class.java
}
+5
View File
@@ -0,0 +1,5 @@
Resolved B(1)[2]
Searched references to B
Searched references to B.get(i: Int) in Kotlin files
Used plain search of B.get(i: Int) in LocalSearchScope:
CLASS:B
+8
View File
@@ -0,0 +1,8 @@
Checked type of a
Resolved a[2]
Resolved a[2]
Searched references to B
Searched references to a in Kotlin files
Searched references to parameter a of B.set(i: Int, a: B) in Kotlin files
Used plain search of B.set(i: Int, a: B) in LocalSearchScope:
CLASS:B