Surround with null check : apply for ITERATOR_ON_NULLABLE

This commit is contained in:
Mikhail Glukhikh
2016-06-13 14:52:14 +03:00
parent 7f771075fb
commit 954952ae71
6 changed files with 35 additions and 0 deletions
@@ -193,6 +193,7 @@ class QuickFixRegistrar : QuickFixContributor {
UNSAFE_CALL.registerFactory(SurroundWithNullCheckFix)
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(SurroundWithNullCheckFix)
UNSAFE_INFIX_CALL.registerFactory(SurroundWithNullCheckFix)
ITERATOR_ON_NULLABLE.registerFactory(SurroundWithNullCheckFix.IteratorOnNullableFactory)
UNSAFE_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
UNSAFE_IMPLICIT_INVOKE_CALL.registerFactory(WrapWithSafeLetCallFix.UnsafeFactory)
@@ -69,6 +69,19 @@ class SurroundWithNullCheckFix(
return SurroundWithNullCheckFix(expression, nullableExpression)
}
}
object IteratorOnNullableFactory : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
val nullableExpression = diagnostic.psiElement as? KtReferenceExpression ?: return null
val forExpression = nullableExpression.parent?.parent as? KtForExpression ?: return null
if (forExpression.parent !is KtBlockExpression) return null
if (!nullableExpression.isPredictable()) return null
return SurroundWithNullCheckFix(forExpression, nullableExpression)
}
}
}
private fun KtExpression.isPredictable(): Boolean {