[kssr] KTIJ-20735 Change Val/Var only filter to Also match Val/Var modifier

GitOrigin-RevId: a41f66bf93fc884d7ca414feaeede00731869918
This commit is contained in:
Bart van Helvert
2022-01-26 20:16:37 +00:00
committed by intellij-monorepo-bot
parent d55b781327
commit f1907e692b
17 changed files with 112 additions and 128 deletions
@@ -1958,15 +1958,15 @@ error.expected.catch.or.finally=Expecting 'catch' or 'finally'
error.param.can.t.be.null.at.index.0.in.1=Param can''t be null at index {0} in {1}.
error.type.filter.node=Kotlin matching element should either be an expression or a statement.
filter.match.only.vals=Match only vals
filter.match.only.vars=Match only vars
modifier.match.val=Match val
modifier.match.var=Match var
label.match.only.vals=Only matches vals
label.match.only.vars=Only matches vars
modifier.also.match.val=Also match val
modifier.also.match.var=Also match var
predefined.configuration.all.methods.of.the.class=All methods of a class
predefined.configuration.all.vars.of.the.class=All vars/vals of a class
predefined.configuration.all.vars.of.the.object=All vars/vals of an object or companion object
predefined.configuration.all.vars.of.the.class=All vars of a class
predefined.configuration.all.vars.of.the.object=All vars of an object or companion object
predefined.configuration.annotations=Annotations
predefined.configuration.anonymous.class=Anonymous class
predefined.configuration.array.access=Array access
@@ -1991,9 +1991,8 @@ predefined.configuration.string.literals=String literals
predefined.configuration.strings=Strings
predefined.configuration.strings.with.long.template=Strings containing a long template
predefined.configuration.trys=Try's
predefined.configuration.vals.only=Vals only
predefined.configuration.vars.of.given.type=Vars and vals of given type
predefined.configuration.vars.only=Vars only
predefined.configuration.vars.of.given.type=Vars of given type
predefined.configuration.also.match.vals=Match both vars and vals
predefined.configuration.when=When expressions
predefined.configuration.while=While loops
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch
@@ -9,9 +9,8 @@ import org.jetbrains.annotations.Nls
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.structuralsearch.filters.AlsoMatchValModifier
import org.jetbrains.kotlin.idea.structuralsearch.filters.OneStateFilter
import org.jetbrains.kotlin.idea.structuralsearch.filters.ValOnlyFilter
import org.jetbrains.kotlin.idea.structuralsearch.filters.VarOnlyFilter
object KotlinPredefinedConfigurations {
private val CLASS_TYPE get() = KotlinBundle.message("category.class")
@@ -33,7 +32,7 @@ object KotlinPredefinedConfigurations {
// Classes
searchTemplate(
KotlinBundle.message("predefined.configuration.all.vars.of.the.class"),
"all vars/vals of a class",
"all vars of a class",
"""
class '_Class {
var 'Field+ = '_Init?
@@ -53,7 +52,7 @@ object KotlinPredefinedConfigurations {
),
searchTemplate(
KotlinBundle.message("predefined.configuration.all.vars.of.the.object"),
"all vars/vals of an object or companion object",
"all vars of an object or companion object",
"""
object '_Object {
var 'Field+ = '_Init?
@@ -139,20 +138,14 @@ object KotlinPredefinedConfigurations {
EXPRESSION_TYPE
),
searchTemplate(
KotlinBundle.message("predefined.configuration.vars.only"),
"vars only",
"""var '_Variable:[_${VarOnlyFilter.CONSTRAINT_NAME}(${OneStateFilter.ENABLED})]""",
EXPRESSION_TYPE
),
searchTemplate(
KotlinBundle.message("predefined.configuration.vals.only"),
"vals only",
"""val '_Value:[_${ValOnlyFilter.CONSTRAINT_NAME}(${OneStateFilter.ENABLED})]""",
KotlinBundle.message("predefined.configuration.also.match.vals"),
"var also match vals",
"""var '_Variable:[_${AlsoMatchValModifier.CONSTRAINT_NAME}(${OneStateFilter.ENABLED})]""",
EXPRESSION_TYPE
),
searchTemplate(
KotlinBundle.message("predefined.configuration.vars.of.given.type"),
"vars and vals of given type",
"vars of a given type",
"""var '_Variable:[exprtype(Int)] = '_Init""",
EXPRESSION_TYPE
),
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch
@@ -28,11 +28,11 @@ import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.liveTemplates.KotlinTemplateContextType
import org.jetbrains.kotlin.idea.structuralsearch.filters.AlsoMatchValModifier
import org.jetbrains.kotlin.idea.structuralsearch.filters.AlsoMatchVarModifier
import org.jetbrains.kotlin.idea.structuralsearch.filters.OneStateFilter
import org.jetbrains.kotlin.idea.structuralsearch.filters.ValOnlyFilter
import org.jetbrains.kotlin.idea.structuralsearch.filters.VarOnlyFilter
import org.jetbrains.kotlin.idea.structuralsearch.predicates.KotlinAlsoMatchValVarPredicate
import org.jetbrains.kotlin.idea.structuralsearch.predicates.KotlinExprTypePredicate
import org.jetbrains.kotlin.idea.structuralsearch.predicates.KotlinVarValOnlyPredicate
import org.jetbrains.kotlin.idea.structuralsearch.visitor.KotlinCompilingVisitor
import org.jetbrains.kotlin.idea.structuralsearch.visitor.KotlinMatchingVisitor
import org.jetbrains.kotlin.idea.structuralsearch.visitor.KotlinRecursiveElementWalkingVisitor
@@ -205,8 +205,8 @@ class KotlinStructuralSearchProfile : StructuralSearchProfile() {
UIUtil.MAXIMUM_UNLIMITED -> isApplicableMaxCount(variableNode) || isApplicableMinMaxCount(variableNode)
UIUtil.TEXT_HIERARCHY -> isApplicableTextHierarchy(variableNode)
UIUtil.REFERENCE -> isApplicableReference(variableNode)
ValOnlyFilter.CONSTRAINT_NAME -> variableNode.parent is KtProperty && !(variableNode.parent as KtProperty).isVar
VarOnlyFilter.CONSTRAINT_NAME -> variableNode.parent is KtProperty && (variableNode.parent as KtProperty).isVar
AlsoMatchVarModifier.CONSTRAINT_NAME -> variableNode.parent is KtProperty && !(variableNode.parent as KtProperty).isVar
AlsoMatchValModifier.CONSTRAINT_NAME -> variableNode.parent is KtProperty && (variableNode.parent as KtProperty).isVar
else -> super.isApplicableConstraint(constraintName, variableNode, completePattern, target)
}
@@ -331,10 +331,9 @@ class KotlinStructuralSearchProfile : StructuralSearchProfile() {
)
result.add(if (isInvertExprType) NotPredicate(predicate) else predicate)
}
if (getAdditionalConstraint(VarOnlyFilter.CONSTRAINT_NAME) == OneStateFilter.ENABLED)
result.add(KotlinVarValOnlyPredicate(true))
else if (getAdditionalConstraint(ValOnlyFilter.CONSTRAINT_NAME) == OneStateFilter.ENABLED)
result.add(KotlinVarValOnlyPredicate(false))
if (getAdditionalConstraint(AlsoMatchValModifier.CONSTRAINT_NAME) == OneStateFilter.ENABLED ||
getAdditionalConstraint(AlsoMatchVarModifier.CONSTRAINT_NAME) == OneStateFilter.ENABLED
) result.add(KotlinAlsoMatchValVarPredicate())
}
return result
}
@@ -0,0 +1,15 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.filters
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.idea.KotlinBundle
class AlsoMatchValModifier : OneStateFilter(
KotlinBundle.lazyMessage("modifier.match.val"),
KotlinBundle.message("modifier.also.match.val"),
CONSTRAINT_NAME
) {
companion object {
const val CONSTRAINT_NAME: @NonNls String = "kotlinAlsoMatchVal"
}
}
@@ -0,0 +1,15 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.filters
import org.jetbrains.annotations.NonNls
import org.jetbrains.kotlin.idea.KotlinBundle
class AlsoMatchVarModifier : OneStateFilter(
KotlinBundle.lazyMessage("modifier.match.var"),
KotlinBundle.message("modifier.also.match.var"),
CONSTRAINT_NAME
) {
companion object {
const val CONSTRAINT_NAME: @NonNls String = "kotlinAlsoMatchVar"
}
}
@@ -0,0 +1,9 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.filters
import com.intellij.structuralsearch.plugin.ui.filters.FilterAction
import com.intellij.structuralsearch.plugin.ui.filters.FilterProvider
class KotlinFilterProvider : FilterProvider {
override fun getFilters(): List<FilterAction> = listOf(AlsoMatchValModifier(), AlsoMatchVarModifier())
}
@@ -1,35 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.structuralsearch.filters
import com.intellij.structuralsearch.plugin.ui.filters.FilterAction
import com.intellij.structuralsearch.plugin.ui.filters.FilterProvider
import org.jetbrains.kotlin.idea.KotlinBundle
class KotlinFilterProvider : FilterProvider {
override fun getFilters(): List<FilterAction> = listOf(VarOnlyFilter(), ValOnlyFilter())
}
class VarOnlyFilter : OneStateFilter(
KotlinBundle.lazyMessage("filter.match.only.vars"),
KotlinBundle.message("label.match.only.vars"),
CONSTRAINT_NAME
) {
companion object {
const val CONSTRAINT_NAME: String = "kotlinVarOnly"
}
}
class ValOnlyFilter : OneStateFilter(
KotlinBundle.lazyMessage("filter.match.only.vals"),
KotlinBundle.message("label.match.only.vals"),
CONSTRAINT_NAME
) {
companion object {
const val CONSTRAINT_NAME: String = "kotlinValOnly"
}
}
@@ -0,0 +1,12 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.predicates
import com.intellij.psi.PsiElement
import com.intellij.structuralsearch.impl.matcher.MatchContext
import com.intellij.structuralsearch.impl.matcher.predicates.MatchPredicate
/** When applying also match val/var modifier, always match property */
class KotlinAlsoMatchValVarPredicate : MatchPredicate() {
override fun match(matchedNode: PsiElement, start: Int, end: Int, context: MatchContext): Boolean = true
}
@@ -1,16 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.structuralsearch.predicates
import com.intellij.psi.PsiElement
import com.intellij.structuralsearch.impl.matcher.MatchContext
import com.intellij.structuralsearch.impl.matcher.predicates.MatchPredicate
import org.jetbrains.kotlin.psi.KtProperty
class KotlinVarValOnlyPredicate(private val isVar: Boolean) : MatchPredicate() {
override fun match(matchedNode: PsiElement, start: Int, end: Int, context: MatchContext): Boolean {
val parent = matchedNode.parent
if (parent !is KtProperty) return false
return parent.isVar == isVar
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.visitor
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.idea.search.declarationsSearch.HierarchySearchReques
import org.jetbrains.kotlin.idea.search.declarationsSearch.searchInheritors
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.idea.structuralsearch.*
import org.jetbrains.kotlin.idea.structuralsearch.predicates.KotlinAlsoMatchValVarPredicate
import org.jetbrains.kotlin.idea.util.safeAnalyzeNonSourceRootCode
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
import org.jetbrains.kotlin.kdoc.psi.api.KDoc
@@ -1080,19 +1081,17 @@ class KotlinMatchingVisitor(private val myMatchingVisitor: GlobalMatchingVisitor
override fun visitProperty(property: KtProperty) {
val other = getTreeElementDepar<KtProperty>() ?: return
myMatchingVisitor.result = matchTypeReferenceWithDeclaration(property.typeReference, other)
val handler = getHandler(property.nameIdentifier!!)
myMatchingVisitor.result = (
property.isVar == other.isVar || (handler is SubstitutionHandler && handler.predicate is KotlinAlsoMatchValVarPredicate)
) && matchTypeReferenceWithDeclaration(property.typeReference, other)
&& myMatchingVisitor.match(property.modifierList, other.modifierList)
&& matchTextOrVariable(property.nameIdentifier, other.nameIdentifier)
&& myMatchingVisitor.match(property.docComment, other.docComment)
&& myMatchingVisitor.matchOptionally(
property.delegateExpressionOrInitializer,
other.delegateExpressionOrInitializer
)
&& myMatchingVisitor.matchOptionally(property.delegateExpressionOrInitializer, other.delegateExpressionOrInitializer)
&& myMatchingVisitor.match(property.getter, other.getter)
&& myMatchingVisitor.match(property.setter, other.setter)
&& myMatchingVisitor.match(property.receiverTypeReference, other.receiverTypeReference)
val handler = getHandler(property.nameIdentifier!!)
if (myMatchingVisitor.result && handler is SubstitutionHandler) {
handler.handle(other.nameIdentifier, myMatchingVisitor.matchContext)
@@ -1,16 +0,0 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.kotlin.idea.structuralsearch.filters
import org.jetbrains.kotlin.idea.structuralsearch.KotlinSSResourceInspectionTest
class KotlinSSCustomFilterTests: KotlinSSResourceInspectionTest() {
override fun getBasePath(): String = "customFilter"
private val enabled = OneStateFilter.ENABLED
fun testVarOnlyFilter() { doTest("var '_:[_${VarOnlyFilter.CONSTRAINT_NAME}($enabled)]") }
fun testValOnlyFilter() { doTest("val '_:[_${ValOnlyFilter.CONSTRAINT_NAME}($enabled)]") }
}
@@ -0,0 +1,26 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.structuralsearch.search.filters
import org.jetbrains.kotlin.idea.structuralsearch.KotlinSSResourceInspectionTest
import org.jetbrains.kotlin.idea.structuralsearch.filters.AlsoMatchValModifier
import org.jetbrains.kotlin.idea.structuralsearch.filters.AlsoMatchVarModifier
import org.jetbrains.kotlin.idea.structuralsearch.filters.OneStateFilter
class KotlinSSValVarModifierTests: KotlinSSResourceInspectionTest() {
fun testAlsoMatchValModifier() { doTest("var '_:[_${AlsoMatchValModifier.CONSTRAINT_NAME}(${OneStateFilter.ENABLED})]", """
fun main() {
<warning descr="SSR">var x = 1</warning>
<warning descr="SSR">val y = 1</warning>
print(x + y)
}
""".trimIndent()) }
fun testAlsoMatchVarModifier() { doTest("val '_:[_${AlsoMatchVarModifier.CONSTRAINT_NAME}(${OneStateFilter.ENABLED})]", """
fun main() {
<warning descr="SSR">var x = 1</warning>
<warning descr="SSR">val y = 1</warning>
print(x + y)
}
""".trimIndent()) }
}
@@ -1,8 +0,0 @@
fun main() {
var x = 1
<warning descr="SSR">val y = 1</warning>
print(x + y)
}
@@ -1,8 +0,0 @@
fun main() {
<warning descr="SSR">var x = 1</warning>
val y = 1
print(x + y)
}
@@ -1,5 +1,5 @@
fun main() {
<warning descr="SSR">val foo = 1</warning>
<warning descr="SSR">var bar: Int = 1</warning>
var bar: Int = 1
print(foo + bar)
}
@@ -1,7 +1,7 @@
fun main() {
<warning descr="SSR">val foo = 1</warning>
val foo2: Int
<warning descr="SSR">var bar: Int = 1</warning>
var bar: Int = 1
foo2 = 1
<warning descr="SSR">val bar2: Int = 1</warning>
<warning descr="SSR">val bar3: Int = (1)</warning>
@@ -1,5 +1,5 @@
fun main() {
<warning descr="SSR">val foo = 1</warning>
val foo = 1
<warning descr="SSR">var bar = 1</warning>
print(foo + bar)
}