Fix Android SuppressLint and Api quickfixes: proper annotation placing
#KT-17783 Fixed #KT-17787 Fixed
This commit is contained in:
+23
-16
@@ -22,6 +22,7 @@ import com.intellij.codeInsight.FileModificationService
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.android.util.AndroidBundle
|
||||
import org.jetbrains.kotlin.android.hasBackingField
|
||||
import org.jetbrains.kotlin.idea.util.addAnnotation
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -60,27 +61,33 @@ class AddTargetApiQuickFix(
|
||||
annotationContainer.addAnnotation(
|
||||
if (useRequiresApi) FQNAME_REQUIRES_API else FQNAME_TARGET_API,
|
||||
getAnnotationValue(true),
|
||||
whiteSpaceText = "\n")
|
||||
whiteSpaceText = if (annotationContainer.isNewLineNeededForAnnotation()) "\n" else " ")
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtElement.isNewLineNeededForAnnotation() = !(this is KtParameter || this is KtTypeParameter || this is KtPropertyAccessor)
|
||||
|
||||
private fun getAnnotationValue(fullyQualified: Boolean) = getVersionField(api, fullyQualified)
|
||||
|
||||
private fun getAnnotationContainer(element: PsiElement, useRequiresApi: Boolean) =
|
||||
PsiTreeUtil.findFirstParent(element) {
|
||||
if (useRequiresApi)
|
||||
it.isRequiresApiAnnotationValidTarget()
|
||||
else
|
||||
it.isTargetApiAnnotationValidTarget()
|
||||
}
|
||||
private fun getAnnotationContainer(element: PsiElement, useRequiresApi: Boolean): PsiElement? {
|
||||
return PsiTreeUtil.findFirstParent(element) {
|
||||
if (useRequiresApi)
|
||||
it.isRequiresApiAnnotationValidTarget()
|
||||
else
|
||||
it.isTargetApiAnnotationValidTarget()
|
||||
}
|
||||
}
|
||||
|
||||
private fun PsiElement.isRequiresApiAnnotationValidTarget(): Boolean {
|
||||
return this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral) ||
|
||||
(this is KtProperty && !isLocal && hasBackingField()) ||
|
||||
this is KtPropertyAccessor
|
||||
}
|
||||
|
||||
// TODO: KtFunctionLiteral is not supported now because addAnnotation fails to shorten references, investigate
|
||||
private fun PsiElement.isRequiresApiAnnotationValidTarget() = this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral) ||
|
||||
(this is KtProperty && !this.isLocal)
|
||||
|
||||
// TODO: KtFunctionLiteral is not supported now because addAnnotation fails to shorten references, investigate
|
||||
private fun PsiElement.isTargetApiAnnotationValidTarget() = this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral)
|
||||
private fun PsiElement.isTargetApiAnnotationValidTarget(): Boolean {
|
||||
return this is KtClassOrObject ||
|
||||
(this is KtFunction && this !is KtFunctionLiteral) ||
|
||||
this is KtPropertyAccessor
|
||||
}
|
||||
}
|
||||
+12
-4
@@ -27,6 +27,7 @@ import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.android.util.AndroidBundle
|
||||
import org.jetbrains.kotlin.android.hasBackingField
|
||||
import org.jetbrains.kotlin.idea.util.addAnnotation
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
@@ -97,9 +98,16 @@ class SuppressLintIntentionAction(val id: String, val element: PsiElement) : Int
|
||||
private fun getLintId(intentionId: String) =
|
||||
if (intentionId.startsWith(INTENTION_NAME_PREFIX)) intentionId.substring(INTENTION_NAME_PREFIX.length) else intentionId
|
||||
|
||||
private fun KtElement.isNewLineNeededForAnnotation() = !(this is KtParameter || this is KtTypeParameter)
|
||||
private fun KtElement.isNewLineNeededForAnnotation(): Boolean {
|
||||
return !(this is KtParameter ||
|
||||
this is KtTypeParameter ||
|
||||
this is KtPropertyAccessor)
|
||||
}
|
||||
|
||||
private fun PsiElement.isSuppressLintTarget() = this is KtDeclaration &&
|
||||
this !is KtDestructuringDeclaration &&
|
||||
this !is KtFunctionLiteral
|
||||
private fun PsiElement.isSuppressLintTarget(): Boolean {
|
||||
return this is KtDeclaration &&
|
||||
(this as? KtProperty)?.hasBackingField() ?: true &&
|
||||
this !is KtFunctionLiteral &&
|
||||
this !is KtDestructuringDeclaration
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user