Move: Drop ${...} around shortened qualified expression

#KT-22692 Fixed
This commit is contained in:
Alexey Sedunov
2018-02-16 20:51:05 +03:00
parent e63e48933d
commit 91fdc0e967
12 changed files with 52 additions and 31 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.util.Key
import com.intellij.psi.PsiTreeChangeAdapter
import com.intellij.psi.PsiTreeChangeEvent
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.dropBraces
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.intentions.ConvertToBlockBodyIntention
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention
@@ -171,7 +172,7 @@ internal class ExpressionReplacementPerformer(
if (templateEntry != null) {
val intention = RemoveCurlyBracesFromTemplateIntention()
if (intention.isApplicableTo(templateEntry)) {
val newEntry = intention.applyTo(templateEntry)
val newEntry = templateEntry.dropBraces()
return newEntry.expression
}
}
@@ -17,29 +17,17 @@
package org.jetbrains.kotlin.idea.intentions
import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.core.canDropBraces
import org.jetbrains.kotlin.idea.core.dropBraces
import org.jetbrains.kotlin.idea.inspections.IntentionBasedInspection
import org.jetbrains.kotlin.psi.KtBlockStringTemplateEntry
import org.jetbrains.kotlin.psi.KtNameReferenceExpression
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.psi.KtSimpleNameStringTemplateEntry
import org.jetbrains.kotlin.psi.psiUtil.canPlaceAfterSimpleNameEntry
class RemoveCurlyBracesFromTemplateInspection : IntentionBasedInspection<KtBlockStringTemplateEntry>(RemoveCurlyBracesFromTemplateIntention::class)
class RemoveCurlyBracesFromTemplateIntention : SelfTargetingOffsetIndependentIntention<KtBlockStringTemplateEntry>(KtBlockStringTemplateEntry::class.java, "Remove curly braces") {
override fun isApplicableTo(element: KtBlockStringTemplateEntry): Boolean {
if (element.expression !is KtNameReferenceExpression) return false
return canPlaceAfterSimpleNameEntry(element.nextSibling)
}
override fun isApplicableTo(element: KtBlockStringTemplateEntry) = element.canDropBraces()
override fun applyTo(element: KtBlockStringTemplateEntry, editor: Editor?) {
applyTo(element)
}
fun applyTo(element: KtBlockStringTemplateEntry): KtSimpleNameStringTemplateEntry {
val name = (element.expression as KtNameReferenceExpression).getReferencedName()
val newEntry = KtPsiFactory(element).createSimpleNameStringTemplateEntry(name)
return element.replaced(newEntry)
element.dropBraces()
}
}
@@ -61,13 +61,10 @@ import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.idea.caches.resolve.*
import org.jetbrains.kotlin.idea.codeInsight.DescriptorToSourceUtilsIde
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.getPackage
import org.jetbrains.kotlin.idea.core.quoteSegmentsIfNeeded
import org.jetbrains.kotlin.idea.core.*
import org.jetbrains.kotlin.idea.core.util.showYesNoCancelDialog
import org.jetbrains.kotlin.idea.highlighter.markers.actualsForExpected
import org.jetbrains.kotlin.idea.highlighter.markers.liftToExpected
import org.jetbrains.kotlin.idea.intentions.RemoveCurlyBracesFromTemplateIntention
import org.jetbrains.kotlin.idea.j2k.IdeaJavaToKotlinServices
import org.jetbrains.kotlin.idea.project.languageVersionSettings
import org.jetbrains.kotlin.idea.refactoring.changeSignature.KotlinValVar
@@ -779,11 +776,8 @@ fun <T> Pass(body: (T) -> Unit) = object : Pass<T>() {
}
fun KtExpression.removeTemplateEntryBracesIfPossible(): KtExpression {
val parent = parent
if (parent !is KtBlockStringTemplateEntry) return this
val intention = RemoveCurlyBracesFromTemplateIntention()
val newEntry = if (intention.isApplicableTo(parent)) intention.applyTo(parent) else parent
val parent = parent as? KtBlockStringTemplateEntry ?: return this
val newEntry = if (parent.canDropBraces()) parent.dropBraces() else parent
return newEntry.expression!!
}