mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-04-03 00:21:31 +00:00
don't offer functions with block body and functions with declared return type as candidates for "show expression type"
This commit is contained in:
@@ -29,10 +29,13 @@ import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
class KotlinExpressionTypeProvider : ExpressionTypeProvider<KtExpression>() {
|
||||
override fun getExpressionsAt(elementAt: PsiElement): List<KtExpression> =
|
||||
elementAt.parentsWithSelf.filterIsInstance<KtExpression>().filterNot { it.shouldSkip() }.toList()
|
||||
elementAt.parentsWithSelf.filterIsInstance<KtExpression>().filter { it.shouldShowType() }.toList()
|
||||
|
||||
private fun KtExpression.shouldSkip(): Boolean {
|
||||
return this is KtStatementExpression && this !is KtFunction && this !is KtProperty
|
||||
private fun KtExpression.shouldShowType() = when(this) {
|
||||
is KtFunction -> !hasBlockBody() && !hasDeclaredReturnType()
|
||||
is KtProperty -> true
|
||||
is KtStatementExpression -> false
|
||||
else -> true
|
||||
}
|
||||
|
||||
override fun getInformationHint(element: KtExpression): String {
|
||||
|
||||
5
idea/testData/codeInsight/expressionType/BlockBodyFunction.kt
vendored
Normal file
5
idea/testData/codeInsight/expressionType/BlockBodyFunction.kt
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
fun foo() {
|
||||
val <caret>x = 1
|
||||
}
|
||||
|
||||
// TYPE: val x = 1 -> <html>kotlin.Int</html>
|
||||
@@ -28,7 +28,7 @@ abstract class AbstractExpressionTypeTest : KotlinLightCodeInsightFixtureTestCas
|
||||
myFixture.configureByFile(path)
|
||||
val expressionTypeProvider = KotlinExpressionTypeProvider()
|
||||
val expressions = expressionTypeProvider.getExpressionsAt(myFixture.elementAtCaret)
|
||||
val types = expressions.map { "${it.text} -> ${expressionTypeProvider.getInformationHint(it)}" }
|
||||
val types = expressions.map { "${it.text.replace('\n', ' ')} -> ${expressionTypeProvider.getInformationHint(it)}" }
|
||||
val expectedTypes = InTextDirectivesUtils.findListWithPrefixes(myFixture.file.text, "// TYPE: ")
|
||||
UsefulTestCase.assertSameElements(types, expectedTypes)
|
||||
}
|
||||
|
||||
@@ -35,6 +35,12 @@ public class ExpressionTypeTestGenerated extends AbstractExpressionTypeTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/expressionType"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("BlockBodyFunction.kt")
|
||||
public void testBlockBodyFunction() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/BlockBodyFunction.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("VariableDeclaration.kt")
|
||||
public void testVariableDeclaration() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/expressionType/VariableDeclaration.kt");
|
||||
|
||||
Reference in New Issue
Block a user