KotlinSuppressIntentionAction: cleanup code

This commit is contained in:
Dmitry Gridin
2019-10-10 21:36:47 +07:00
parent 3832924bd7
commit e3d9f05d28
@@ -30,17 +30,21 @@ import org.jetbrains.kotlin.psi.psiUtil.replaceFileAnnotationList
import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.BindingContext
class KotlinSuppressIntentionAction private constructor( class KotlinSuppressIntentionAction private constructor(
private val suppressAt: PsiElement, private val suppressAt: PsiElement,
private val suppressKey: String, private val suppressKey: String,
private val kind: AnnotationHostKind private val kind: AnnotationHostKind
) : SuppressIntentionAction() { ) : SuppressIntentionAction() {
constructor(suppressAt: KtExpression, constructor(
suppressKey: String, suppressAt: KtExpression,
kind: AnnotationHostKind) : this(suppressAt as PsiElement, suppressKey, kind) suppressKey: String,
kind: AnnotationHostKind
) : this(suppressAt as PsiElement, suppressKey, kind)
constructor(suppressAt: KtFile, constructor(
suppressKey: String, suppressAt: KtFile,
kind: AnnotationHostKind) : this(suppressAt as PsiElement, suppressKey, kind) suppressKey: String,
kind: AnnotationHostKind
) : this(suppressAt as PsiElement, suppressKey, kind)
override fun getFamilyName() = KotlinBundle.message("suppress.warnings.family") override fun getFamilyName() = KotlinBundle.message("suppress.warnings.family")
override fun getText() = KotlinBundle.message("suppress.warning.for", suppressKey, kind.kind, kind.name) override fun getText() = KotlinBundle.message("suppress.warning.for", suppressKey, kind.kind, kind.name)
@@ -53,14 +57,16 @@ class KotlinSuppressIntentionAction private constructor(
val id = "\"$suppressKey\"" val id = "\"$suppressKey\""
when (suppressAt) { when (suppressAt) {
is KtModifierListOwner -> is KtModifierListOwner -> suppressAt.addAnnotation(
suppressAt.addAnnotation(KotlinBuiltIns.FQ_NAMES.suppress, KotlinBuiltIns.FQ_NAMES.suppress,
id, id,
whiteSpaceText = if (kind.newLineNeeded) "\n" else " ", whiteSpaceText = if (kind.newLineNeeded) "\n" else " ",
addToExistingAnnotation = { entry -> addToExistingAnnotation = { entry ->
addArgumentToSuppressAnnotation(entry, id) addArgumentToSuppressAnnotation(
true entry,
}) id
); true
})
is KtAnnotatedExpression -> is KtAnnotatedExpression ->
suppressAtAnnotatedExpression(CaretBox(suppressAt, editor), id) suppressAtAnnotatedExpression(CaretBox(suppressAt, editor), id)
@@ -102,8 +108,7 @@ class KotlinSuppressIntentionAction private constructor(
if (entry != null) { if (entry != null) {
// already annotated with @suppress // already annotated with @suppress
addArgumentToSuppressAnnotation(entry, id) addArgumentToSuppressAnnotation(entry, id)
} } else {
else {
suppressAtExpression(suppressAt, id) suppressAtExpression(suppressAt, id)
} }
} }
@@ -119,7 +124,7 @@ class KotlinSuppressIntentionAction private constructor(
val afterReplace = suppressAt.replace(annotatedExpression) as KtAnnotatedExpression val afterReplace = suppressAt.replace(annotatedExpression) as KtAnnotatedExpression
val toReplace = afterReplace.findElementAt(afterReplace.textLength - 2)!! val toReplace = afterReplace.findElementAt(afterReplace.textLength - 2)!!
assert (toReplace.text == placeholderText) assert(toReplace.text == placeholderText)
val result = toReplace.replace(copy)!! val result = toReplace.replace(copy)!!
caretBox.positionCaretInCopy(result) caretBox.positionCaretInCopy(result)
@@ -139,7 +144,8 @@ class KotlinSuppressIntentionAction private constructor(
} }
} }
private fun suppressAnnotationText(id: String, withAt: Boolean = true) = "${if (withAt) "@" else ""}${KotlinBuiltIns.FQ_NAMES.suppress.shortName()}($id)" private fun suppressAnnotationText(id: String, withAt: Boolean = true) =
"${if (withAt) "@" else ""}${KotlinBuiltIns.FQ_NAMES.suppress.shortName()}($id)"
private fun findSuppressAnnotation(annotated: KtAnnotated): KtAnnotationEntry? { private fun findSuppressAnnotation(annotated: KtAnnotated): KtAnnotationEntry? {
val context = annotated.analyze() val context = annotated.analyze()
@@ -164,9 +170,9 @@ private fun KtPsiFactory.createWhiteSpace(kind: AnnotationHostKind): PsiElement
return if (kind.newLineNeeded) createNewLine() else createWhiteSpace() return if (kind.newLineNeeded) createNewLine() else createWhiteSpace()
} }
private class CaretBox<out E: KtExpression>( private class CaretBox<out E : KtExpression>(
val expression: E, val expression: E,
private val editor: Editor? private val editor: Editor?
) { ) {
private val offsetInExpression: Int = (editor?.caretModel?.offset ?: 0) - expression.textRange!!.startOffset private val offsetInExpression: Int = (editor?.caretModel?.offset ?: 0) - expression.textRange!!.startOffset