don't offer functions with block body and functions with declared return type as candidates for "show expression type"

This commit is contained in:
Dmitry Jemerov
2016-08-25 20:48:09 +02:00
parent 7b644e2c52
commit fbd2c48fbd
4 changed files with 18 additions and 4 deletions

View File

@@ -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 {

View File

@@ -0,0 +1,5 @@
fun foo() {
val <caret>x = 1
}
// TYPE: val x = 1 -> <html>kotlin.Int</html>

View File

@@ -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)
}

View File

@@ -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");