Fast unary operators search

This commit is contained in:
Valentin Kipyatkov
2016-09-02 15:31:35 +03:00
parent 322df5b348
commit cdf328475e
6 changed files with 79 additions and 4 deletions
@@ -44,7 +44,10 @@ class BinaryOperatorReferenceSearcher(
}
override fun isReferenceToCheck(ref: PsiReference): Boolean {
return ref is KtSimpleNameReference && ref.element.getReferencedNameElementType() in operationTokens
if (ref !is KtSimpleNameReference) return false
val element = ref.element
if (element.parent !is KtBinaryExpression) return false
return element.getReferencedNameElementType() in operationTokens
}
override fun extractReference(element: PsiElement): PsiReference? {
@@ -123,6 +123,11 @@ abstract class OperatorReferenceSearcher<TReferenceElement : KtElement>(
return BinaryOperatorReferenceSearcher(declaration, operationTokens, searchScope, consumer, optimizer)
}
val unaryOp = OperatorConventions.UNARY_OPERATION_NAMES.inverse()[name]
if (unaryOp != null) {
return UnaryOperatorReferenceSearcher(declaration, unaryOp, searchScope, consumer, optimizer)
}
return null
}
@@ -0,0 +1,57 @@
/*
* 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.KtSimpleNameReference
import org.jetbrains.kotlin.lexer.KtSingleValueToken
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtUnaryExpression
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
class UnaryOperatorReferenceSearcher(
targetDeclaration: KtDeclaration,
private val operationToken: KtSingleValueToken,
searchScope: SearchScope,
consumer: Processor<PsiReference>,
optimizer: SearchRequestCollector
) : OperatorReferenceSearcher<KtUnaryExpression>(targetDeclaration, searchScope, consumer, optimizer, wordsToSearch = listOf(operationToken.value)) {
override fun processSuspiciousExpression(expression: KtExpression) {
val unaryExpression = expression.parent as? KtUnaryExpression ?: return
if (unaryExpression.operationToken != operationToken) return
processReferenceElement(unaryExpression)
}
override fun isReferenceToCheck(ref: PsiReference): Boolean {
if (ref !is KtSimpleNameReference) return false
val element = ref.element
if (element.parent !is KtUnaryExpression) return false
return element.getReferencedNameElementType() == operationToken
}
override fun extractReference(element: PsiElement): PsiReference? {
val unaryExpression = element as? KtUnaryExpression ?: return null
if (unaryExpression.operationToken != operationToken) return null
return unaryExpression.operationReference.references.firstIsInstance<KtSimpleNameReference>()
}
}
@@ -59,9 +59,6 @@ fun Name.getOperationSymbolsToSearch(): Pair<Set<KtToken>, Class<*>>? {
DelegatedPropertyResolver.PROPERTY_DELEGATED_FUNCTION_NAME -> return setOf(KtTokens.BY_KEYWORD) to KtPropertyDelegationMethodsReference::class.java
}
val unaryOp = UNARY_OPERATION_NAMES.inverse()[this]
if (unaryOp != null) return setOf(unaryOp) to KtSimpleNameReference::class.java
val assignmentOp = ASSIGNMENT_OPERATIONS.inverse()[this]
if (assignmentOp != null) return setOf(assignmentOp) to KtSimpleNameReference::class.java
+8
View File
@@ -0,0 +1,8 @@
Checked type of a
Resolved ++a
Resolved a++
Searched references to A
Searched references to A.inc() in Kotlin files
Searched references to a in Kotlin files
Used plain search of A.inc() in LocalSearchScope:
CLASS:A
@@ -0,0 +1,5 @@
Resolved -A(1)
Searched references to A
Searched references to A.unaryMinus() in Kotlin files
Used plain search of A.unaryMinus() in LocalSearchScope:
CLASS:A