Shorten Reference: Disable ${...} shortening inside completion insert handler

This commit is contained in:
Alexey Sedunov
2018-03-01 17:32:05 +03:00
parent 68757fff2e
commit 30314d43d9
2 changed files with 16 additions and 11 deletions
@@ -31,6 +31,10 @@ import org.jetbrains.kotlin.renderer.render
import org.jetbrains.kotlin.resolve.DescriptorUtils
abstract class KotlinCallableInsertHandler(val callType: CallType<*>) : BaseDeclarationInsertHandler() {
companion object {
private val shortenReferences = ShortenReferences({ ShortenReferences.Options.DEFAULT.copy(dropBracesInStringTemplates = false)})
}
override fun handleInsert(context: InsertionContext, item: LookupElement) {
super.handleInsert(context, item)
@@ -57,7 +61,7 @@ abstract class KotlinCallableInsertHandler(val callType: CallType<*>) : BaseDecl
psiDocumentManager.commitAllDocuments()
ShortenReferences.DEFAULT.process(file, context.startOffset, context.tailOffset - 1)
shortenReferences.process(file, context.startOffset, context.tailOffset - 1)
psiDocumentManager.doPostponedOperationsAndUnblockDocument(context.document)
@@ -44,7 +44,8 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
val removeThisLabels: Boolean = false,
val removeThis: Boolean = false,
// TODO: remove this option and all related stuff (RETAIN_COMPANION etc.) after KT-13934 fixed
val removeExplicitCompanion: Boolean = true
val removeExplicitCompanion: Boolean = true,
val dropBracesInStringTemplates: Boolean = true
) {
companion object {
val DEFAULT = Options()
@@ -176,7 +177,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
processors.forEach { it.analyzeCollectedElements(bindingContext) }
// step 3: shorten elements that can be shortened right now
processors.forEach { it.shortenElements(elementSetToUpdate = elementsToUse) }
processors.forEach { it.shortenElements(elementSetToUpdate = elementsToUse, options = options) }
// step 4: try to import descriptors needed to shorten other elements
val descriptorsToImport = processors.flatMap { it.getDescriptorsToImport() }.toSet()
@@ -307,9 +308,9 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
class ImportDescriptors(val descriptors: Collection<DeclarationDescriptor>) : AnalyzeQualifiedElementResult()
}
protected abstract fun shortenElement(element: TElement): KtElement
protected abstract fun shortenElement(element: TElement, options: Options): KtElement
fun shortenElements(elementSetToUpdate: MutableSet<KtElement>) {
fun shortenElements(elementSetToUpdate: MutableSet<KtElement>, options: (KtElement) -> Options) {
for (elementPointer in elementsToShorten) {
val element = elementPointer.element ?: continue
if (!element.isValid) continue
@@ -317,7 +318,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
var newElement: KtElement? = null
// we never want any reformatting to happen because sometimes it causes strange effects (see KT-11633)
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
newElement = shortenElement(element)
newElement = shortenElement(element, options(element))
}
if (element in elementSetToUpdate && newElement != element) {
@@ -385,7 +386,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
)
}
override fun shortenElement(element: KtUserType): KtElement {
override fun shortenElement(element: KtUserType, options: Options): KtElement {
element.deleteQualifier()
return element
}
@@ -571,13 +572,13 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
rc1?.functionCall?.candidateDescriptor?.asString() == rc2?.functionCall?.candidateDescriptor?.asString()
}
override fun shortenElement(element: KtDotQualifiedExpression): KtElement {
override fun shortenElement(element: KtDotQualifiedExpression, options: Options): KtElement {
val parens = element.parent as? KtParenthesizedExpression
val requiredParens = parens != null && !KtPsiUtil.areParenthesesUseless(parens)
val shortenedElement = element.replace(element.selectorExpression!!) as KtElement
val newParent = shortenedElement.parent
if (requiredParens) return newParent.replaced(shortenedElement)
if (newParent is KtBlockStringTemplateEntry && newParent.canDropBraces()) {
if (options.dropBracesInStringTemplates && newParent is KtBlockStringTemplateEntry && newParent.canDropBraces()) {
newParent.dropBraces()
}
return shortenedElement
@@ -608,7 +609,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
return if (targetBefore == targetAfter) AnalyzeQualifiedElementResult.ShortenNow else AnalyzeQualifiedElementResult.Skip
}
override fun shortenElement(element: KtThisExpression): KtElement {
override fun shortenElement(element: KtThisExpression, options: Options): KtElement {
return element.replace(simpleThis) as KtElement
}
}
@@ -662,7 +663,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
return AnalyzeQualifiedElementResult.ShortenNow
}
override fun shortenElement(element: KtDotQualifiedExpression): KtElement {
override fun shortenElement(element: KtDotQualifiedExpression, options: Options): KtElement {
val receiver = element.receiverExpression
val selector = element.selectorExpression ?: return element