Join with assignment: remove comment from target range #KT-22110 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-02-01 12:24:15 +03:00
committed by Mikhail Glukhikh
parent 36128ba66f
commit f9555f3f64
4 changed files with 48 additions and 19 deletions
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.ScrollType import com.intellij.openapi.editor.ScrollType
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement import com.intellij.psi.PsiElement
import com.intellij.psi.PsiReferenceService import com.intellij.psi.PsiReferenceService
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
@@ -36,13 +37,13 @@ import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.types.TypeUtils
class JoinDeclarationAndAssignmentInspection : IntentionBasedInspection<KtProperty>( class JoinDeclarationAndAssignmentInspection : IntentionBasedInspection<KtProperty>(
JoinDeclarationAndAssignmentIntention::class, JoinDeclarationAndAssignmentIntention::class,
"Can be joined with assignment" "Can be joined with assignment"
) )
class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentIntention<KtProperty>( class JoinDeclarationAndAssignmentIntention : SelfTargetingRangeIntention<KtProperty>(
KtProperty::class.java, KtProperty::class.java,
"Join declaration and assignment" "Join declaration and assignment"
) { ) {
private fun equalNullableTypes(type1: KotlinType?, type2: KotlinType?): Boolean { private fun equalNullableTypes(type1: KotlinType?, type2: KotlinType?): Boolean {
@@ -51,24 +52,25 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte
return TypeUtils.equalTypes(type1, type2) return TypeUtils.equalTypes(type1, type2)
} }
override fun isApplicableTo(element: KtProperty): Boolean { override fun applicabilityRange(element: KtProperty): TextRange? {
if (element.hasDelegate() if (element.hasDelegate()
|| element.hasInitializer() || element.hasInitializer()
|| element.setter != null || element.setter != null
|| element.getter != null || element.getter != null
|| element.receiverTypeReference != null || element.receiverTypeReference != null
|| element.name == null) { || element.name == null) {
return false return null
} }
val assignment = findAssignment(element) ?: return false val assignment = findAssignment(element) ?: return null
return assignment.right?.let { if (assignment.right?.let {
hasNoLocalDependencies(it, element.parent) && hasNoLocalDependencies(it, element.parent) && assignment.analyze().let { context ->
assignment.analyze().let { context -> (element.isVar && !element.isLocal) ||
(element.isVar && !element.isLocal) || equalNullableTypes(it.getType(context), context[BindingContext.TYPE, element.typeReference])
equalNullableTypes(it.getType(context), context[BindingContext.TYPE, element.typeReference]) }
} } != true) return null
} ?: false
return TextRange((element.modifierList ?: element.valOrVarKeyword).startOffset, (element.typeReference ?: element).endOffset)
} }
override fun applyTo(element: KtProperty, editor: Editor?) { override fun applyTo(element: KtProperty, editor: Editor?) {
@@ -93,8 +95,7 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte
val colon = element.colon!! val colon = element.colon!!
selectionModel.setSelection(colon.startOffset, typeReference.endOffset) selectionModel.setSelection(colon.startOffset, typeReference.endOffset)
moveCaret(typeReference.endOffset, ScrollType.CENTER) moveCaret(typeReference.endOffset, ScrollType.CENTER)
} } else {
else {
moveCaret(newInitializer.startOffset, ScrollType.CENTER) moveCaret(newInitializer.startOffset, ScrollType.CENTER)
} }
} }
@@ -161,6 +162,6 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte
private fun PsiElement.resolveAllReferences(): Sequence<PsiElement?> { private fun PsiElement.resolveAllReferences(): Sequence<PsiElement?> {
return PsiReferenceService.getService().getReferences(this, PsiReferenceService.Hints.NO_HINTS) return PsiReferenceService.getService().getReferences(this, PsiReferenceService.Hints.NO_HINTS)
.asSequence() .asSequence()
.map { it.resolve() } .map { it.resolve() }
} }
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
class Test {
/* foo */ // bar<caret>
private val x: Int /* baz */ // qux
init {
x = 1
}
}
@@ -0,0 +1,8 @@
// IS_APPLICABLE: false
class Test {
/* foo */ // bar
private val x: Int <caret>/* baz */ // qux
init {
x = 1
}
}
@@ -9351,6 +9351,18 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("caretOnHeadComment.kt")
public void testCaretOnHeadComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/caretOnHeadComment.kt");
doTest(fileName);
}
@TestMetadata("caretOnTailComment.kt")
public void testCaretOnTailComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/caretOnTailComment.kt");
doTest(fileName);
}
@TestMetadata("comment.kt") @TestMetadata("comment.kt")
public void testComment() throws Exception { public void testComment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/comment.kt"); String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/comment.kt");