From 75a6b0ca5a0e3c8f489ff6e89980e767c5f4696c Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 24 Sep 2016 21:41:50 +0300 Subject: [PATCH] Quick search of iterator operator usages --- .../IteratorOperatorReferenceSearcher.kt | 54 +++++++++++++++++++ .../usagesSearch/OperatorReferenceSearcher.kt | 3 ++ .../idea/search/usagesSearch/conventions.kt | 5 -- .../kotlin/conventions/forIteration.log | 7 +++ 4 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/IteratorOperatorReferenceSearcher.kt create mode 100644 idea/testData/findUsages/kotlin/conventions/forIteration.log diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/IteratorOperatorReferenceSearcher.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/IteratorOperatorReferenceSearcher.kt new file mode 100644 index 00000000000..6e7c9b00e4b --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/IteratorOperatorReferenceSearcher.kt @@ -0,0 +1,54 @@ +/* + * 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.KtNodeTypes +import org.jetbrains.kotlin.idea.references.KtForLoopInReference +import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions +import org.jetbrains.kotlin.psi.KtElement +import org.jetbrains.kotlin.psi.KtExpression +import org.jetbrains.kotlin.psi.KtForExpression +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance + +class IteratorOperatorReferenceSearcher( + targetFunction: PsiElement, + searchScope: SearchScope, + consumer: Processor, + optimizer: SearchRequestCollector, + options: KotlinReferencesSearchOptions +) : OperatorReferenceSearcher(targetFunction, searchScope, consumer, optimizer, options, wordsToSearch = listOf("in")) { + + override fun processPossibleReceiverExpression(expression: KtExpression) { + val parent = expression.parent + if (parent.node.elementType == KtNodeTypes.LOOP_RANGE) { + processReferenceElement(parent.parent as KtForExpression) + } + } + + override fun isReferenceToCheck(ref: PsiReference): Boolean { + return ref is KtForLoopInReference + } + + override fun extractReference(element: KtElement): PsiReference? { + return (element as? KtForExpression)?.references?.firstIsInstance() + } +} \ No newline at end of file diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/OperatorReferenceSearcher.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/OperatorReferenceSearcher.kt index cad69d63cd4..005d2cf761e 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/OperatorReferenceSearcher.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/OperatorReferenceSearcher.kt @@ -180,6 +180,9 @@ abstract class OperatorReferenceSearcher( name == OperatorNameConventions.COMPARE_TO -> return BinaryOperatorReferenceSearcher(declaration, listOf(KtTokens.LT, KtTokens.GT, KtTokens.LTEQ, KtTokens.GTEQ), searchScope, consumer, optimizer, options) + name == OperatorNameConventions.ITERATOR -> + return IteratorOperatorReferenceSearcher(declaration, searchScope, consumer, optimizer, options) + else -> return null } diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/conventions.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/conventions.kt index 639e4f41daa..6a0add4476b 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/conventions.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/search/usagesSearch/conventions.kt @@ -17,14 +17,12 @@ package org.jetbrains.kotlin.idea.search.usagesSearch import com.google.common.collect.ImmutableSet -import org.jetbrains.kotlin.idea.references.KtForLoopInReference import org.jetbrains.kotlin.idea.references.KtPropertyDelegationMethodsReference import org.jetbrains.kotlin.lexer.KtToken import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.DelegatedPropertyResolver import org.jetbrains.kotlin.types.expressions.OperatorConventions.* -import org.jetbrains.kotlin.util.OperatorNameConventions val ALL_SEARCHABLE_OPERATIONS: ImmutableSet = ImmutableSet .builder() @@ -40,11 +38,8 @@ val ALL_SEARCHABLE_OPERATIONS: ImmutableSet = ImmutableSet val DELEGATE_ACCESSOR_NAMES = setOf(Name.identifier("getValue"), Name.identifier("setValue")) -val IN_OPERATIONS_TO_SEARCH = setOf(KtTokens.IN_KEYWORD) - fun Name.getOperationSymbolsToSearch(): Pair, Class<*>>? { when (this) { - OperatorNameConventions.ITERATOR -> return IN_OPERATIONS_TO_SEARCH to KtForLoopInReference::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 } diff --git a/idea/testData/findUsages/kotlin/conventions/forIteration.log b/idea/testData/findUsages/kotlin/conventions/forIteration.log new file mode 100644 index 00000000000..d334804dece --- /dev/null +++ b/idea/testData/findUsages/kotlin/conventions/forIteration.log @@ -0,0 +1,7 @@ +Checked type of parameter a of FOR +Checked type of parameter a of FOR +Resolved for (a in A()) {} +Resolved for (a in A()) {} +Searched references to A +Used plain search of A.iterator() in LocalSearchScope: + CLASS:A