Commit 23585729 authored by Vladimir Dolzhenko's avatar Vladimir Dolzhenko Committed by intellij-monorepo-bot
Browse files

[kotlin] improve isPotentiallyOperator with naming checks

Relates to #KTIJ-18868

(cherry picked from commit f2edf297bec850601929a218bd8a7556a288b95d)

IJ-CR-11481

GitOrigin-RevId: 7b38b36dd7b5acaf7e81bebef500fb2c9dca3417
parent c3d6667c
Branches unavailable Tags unavailable
Showing with 9 additions and 10 deletions
+9 -10
......@@ -50,7 +50,6 @@ import org.jetbrains.kotlin.idea.search.isPotentiallyOperator
import org.jetbrains.kotlin.idea.util.application.runReadAction
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.parameterIndex
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import javax.swing.event.HyperlinkEvent
import javax.swing.event.HyperlinkListener
......@@ -262,10 +261,10 @@ abstract class KotlinFindMemberUsagesHandler<T : KtNamedDeclaration> protected c
if (options.isUsages) {
val baseKotlinSearchOptions = createKotlinReferencesSearchOptions(options, forHighlight)
val kotlinSearchOptions = if (element.safeAs<KtElement>()?.isPotentiallyOperator() == false) {
baseKotlinSearchOptions.copy(searchForOperatorConventions = false)
} else {
val kotlinSearchOptions = if (element.isPotentiallyOperator()) {
baseKotlinSearchOptions
} else {
baseKotlinSearchOptions.copy(searchForOperatorConventions = false)
}
val searchParameters = KotlinReferencesSearchParameters(element, options.searchScope, kotlinOptions = kotlinSearchOptions)
......
......@@ -145,11 +145,13 @@ fun PsiReference.isImportUsage(): Boolean =
)
fun PsiElement.getKotlinFqName(): FqName? = getKotlinFqNameOriginal()
fun KtElement.isPotentiallyOperator(): Boolean {
fun PsiElement?.isPotentiallyOperator(): Boolean {
val namedFunction = safeAs<KtNamedFunction>() ?: return false
if (namedFunction.hasModifier(KtTokens.OPERATOR_KEYWORD)) return true
// operator modifier could be omitted for overriding function
if (!namedFunction.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return false
val name = namedFunction.name ?: return false
if (!OperatorConventions.isConventionName(Name.identifier(name))) return false
// TODO: it's fast PSI-based check, a proper check requires call to resolveDeclarationWithParents() that is not frontend-independent
return true
......
......@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.idea.search.and
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinAwareReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchOptions
import org.jetbrains.kotlin.idea.search.ideaExtensions.KotlinReferencesSearchParameters
import org.jetbrains.kotlin.idea.search.isPotentiallyOperator
import org.jetbrains.kotlin.idea.search.restrictToKotlinSources
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.getAllAccessibleFunctions
......@@ -390,11 +391,8 @@ internal fun checkNewNameUsagesRetargeting(
}
}
internal fun KtElement.isOperator(): Boolean {
val namedFunction = safeAs<KtNamedFunction>() ?: return false
if (namedFunction.hasModifier(KtTokens.OPERATOR_KEYWORD)) return true
// operator modifier could be omitted for overriding function
if (!namedFunction.hasModifier(KtTokens.OVERRIDE_KEYWORD)) return false
internal fun PsiElement?.isOperator(): Boolean {
if (!isPotentiallyOperator()) return false
val resolveWithParents = resolveDeclarationWithParents(this as KtNamedFunction)
return resolveWithParents.overriddenDescriptors.any {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment