Add exit point highlighting for accessors (KT-21318)
#KT-21318 Fixed
This commit is contained in:
+12
-9
@@ -29,13 +29,10 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.references.mainReference
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parents
|
||||
import org.jetbrains.kotlin.resolve.inline.InlineUtil
|
||||
|
||||
class KotlinHighlightExitPointsHandlerFactory: HighlightUsagesHandlerFactoryBase() {
|
||||
class KotlinHighlightExitPointsHandlerFactory : HighlightUsagesHandlerFactoryBase() {
|
||||
companion object {
|
||||
private val RETURN_AND_THROW = TokenSet.create(KtTokens.RETURN_KEYWORD, KtTokens.THROW_KEYWORD)
|
||||
}
|
||||
@@ -61,14 +58,14 @@ class KotlinHighlightExitPointsHandlerFactory: HighlightUsagesHandlerFactoryBase
|
||||
}
|
||||
|
||||
override fun computeUsages(targets: MutableList<PsiElement>?) {
|
||||
val relevantFunction = target.getRelevantFunction()
|
||||
val relevantFunction = target.getRelevantDeclaration()
|
||||
relevantFunction?.accept(object : KtVisitorVoid() {
|
||||
override fun visitKtElement(element: KtElement) {
|
||||
element.acceptChildren(this)
|
||||
}
|
||||
|
||||
private fun visitReturnOrThrow(expression: KtExpression) {
|
||||
if (expression.getRelevantFunction() == relevantFunction) {
|
||||
if (expression.getRelevantDeclaration() == relevantFunction) {
|
||||
addOccurrence(expression)
|
||||
}
|
||||
}
|
||||
@@ -85,14 +82,20 @@ class KotlinHighlightExitPointsHandlerFactory: HighlightUsagesHandlerFactoryBase
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtExpression.getRelevantFunction(): KtFunction? {
|
||||
private fun KtExpression.getRelevantDeclaration(): KtDeclarationWithBody? {
|
||||
if (this is KtReturnExpression) {
|
||||
(this.getTargetLabel()?.mainReference?.resolve() as? KtFunction)?.let { return it }
|
||||
}
|
||||
|
||||
for (parent in parents) {
|
||||
if (InlineUtil.canBeInlineArgument(parent) && !InlineUtil.isInlinedArgument(parent as KtFunction, parent.analyze(), false)) {
|
||||
return parent
|
||||
if (parent is KtDeclarationWithBody) {
|
||||
when {
|
||||
parent is KtPropertyAccessor -> return parent
|
||||
InlineUtil.canBeInlineArgument(parent) && !InlineUtil.isInlinedArgument(parent as KtFunction, parent.analyze(), false) ->
|
||||
return parent
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
val Any?.foo: Int
|
||||
get() {
|
||||
this?.let {
|
||||
<caret>return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
|
||||
public inline fun <T> T.let(block: (T) -> Unit) {}
|
||||
|
||||
//HIGHLIGHTED: return 1
|
||||
//HIGHLIGHTED: return 2
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
fun Any?.foo(): Int {
|
||||
this?.let {
|
||||
<caret>return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
|
||||
public inline fun <T> T.let(block: (T) -> Unit) {}
|
||||
|
||||
//HIGHLIGHTED: return 1
|
||||
//HIGHLIGHTED: return 2
|
||||
+12
@@ -36,6 +36,12 @@ public class HighlightExitPointsTestGenerated extends AbstractHighlightExitPoint
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/exitPoints"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("getter.kt")
|
||||
public void testGetter() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/exitPoints/getter.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inline1.kt")
|
||||
public void testInline1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/exitPoints/inline1.kt");
|
||||
@@ -48,6 +54,12 @@ public class HighlightExitPointsTestGenerated extends AbstractHighlightExitPoint
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inline3.kt")
|
||||
public void testInline3() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/exitPoints/inline3.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("inlineLocalReturn1.kt")
|
||||
public void testInlineLocalReturn1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/exitPoints/inlineLocalReturn1.kt");
|
||||
|
||||
Reference in New Issue
Block a user