Shorten Reference: Disable ${...} shortening inside completion insert handler
This commit is contained in:
+5
-1
@@ -31,6 +31,10 @@ import org.jetbrains.kotlin.renderer.render
|
|||||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||||
|
|
||||||
abstract class KotlinCallableInsertHandler(val callType: CallType<*>) : BaseDeclarationInsertHandler() {
|
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) {
|
override fun handleInsert(context: InsertionContext, item: LookupElement) {
|
||||||
super.handleInsert(context, item)
|
super.handleInsert(context, item)
|
||||||
|
|
||||||
@@ -57,7 +61,7 @@ abstract class KotlinCallableInsertHandler(val callType: CallType<*>) : BaseDecl
|
|||||||
|
|
||||||
psiDocumentManager.commitAllDocuments()
|
psiDocumentManager.commitAllDocuments()
|
||||||
|
|
||||||
ShortenReferences.DEFAULT.process(file, context.startOffset, context.tailOffset - 1)
|
shortenReferences.process(file, context.startOffset, context.tailOffset - 1)
|
||||||
|
|
||||||
psiDocumentManager.doPostponedOperationsAndUnblockDocument(context.document)
|
psiDocumentManager.doPostponedOperationsAndUnblockDocument(context.document)
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
val removeThisLabels: Boolean = false,
|
val removeThisLabels: Boolean = false,
|
||||||
val removeThis: Boolean = false,
|
val removeThis: Boolean = false,
|
||||||
// TODO: remove this option and all related stuff (RETAIN_COMPANION etc.) after KT-13934 fixed
|
// 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 {
|
companion object {
|
||||||
val DEFAULT = Options()
|
val DEFAULT = Options()
|
||||||
@@ -176,7 +177,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
processors.forEach { it.analyzeCollectedElements(bindingContext) }
|
processors.forEach { it.analyzeCollectedElements(bindingContext) }
|
||||||
|
|
||||||
// step 3: shorten elements that can be shortened right now
|
// 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
|
// step 4: try to import descriptors needed to shorten other elements
|
||||||
val descriptorsToImport = processors.flatMap { it.getDescriptorsToImport() }.toSet()
|
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()
|
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) {
|
for (elementPointer in elementsToShorten) {
|
||||||
val element = elementPointer.element ?: continue
|
val element = elementPointer.element ?: continue
|
||||||
if (!element.isValid) continue
|
if (!element.isValid) continue
|
||||||
@@ -317,7 +318,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
var newElement: KtElement? = null
|
var newElement: KtElement? = null
|
||||||
// we never want any reformatting to happen because sometimes it causes strange effects (see KT-11633)
|
// we never want any reformatting to happen because sometimes it causes strange effects (see KT-11633)
|
||||||
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
|
PostprocessReformattingAspect.getInstance(element.project).disablePostprocessFormattingInside {
|
||||||
newElement = shortenElement(element)
|
newElement = shortenElement(element, options(element))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (element in elementSetToUpdate && newElement != 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()
|
element.deleteQualifier()
|
||||||
return element
|
return element
|
||||||
}
|
}
|
||||||
@@ -571,13 +572,13 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
rc1?.functionCall?.candidateDescriptor?.asString() == rc2?.functionCall?.candidateDescriptor?.asString()
|
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 parens = element.parent as? KtParenthesizedExpression
|
||||||
val requiredParens = parens != null && !KtPsiUtil.areParenthesesUseless(parens)
|
val requiredParens = parens != null && !KtPsiUtil.areParenthesesUseless(parens)
|
||||||
val shortenedElement = element.replace(element.selectorExpression!!) as KtElement
|
val shortenedElement = element.replace(element.selectorExpression!!) as KtElement
|
||||||
val newParent = shortenedElement.parent
|
val newParent = shortenedElement.parent
|
||||||
if (requiredParens) return newParent.replaced(shortenedElement)
|
if (requiredParens) return newParent.replaced(shortenedElement)
|
||||||
if (newParent is KtBlockStringTemplateEntry && newParent.canDropBraces()) {
|
if (options.dropBracesInStringTemplates && newParent is KtBlockStringTemplateEntry && newParent.canDropBraces()) {
|
||||||
newParent.dropBraces()
|
newParent.dropBraces()
|
||||||
}
|
}
|
||||||
return shortenedElement
|
return shortenedElement
|
||||||
@@ -608,7 +609,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
return if (targetBefore == targetAfter) AnalyzeQualifiedElementResult.ShortenNow else AnalyzeQualifiedElementResult.Skip
|
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
|
return element.replace(simpleThis) as KtElement
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -662,7 +663,7 @@ class ShortenReferences(val options: (KtElement) -> Options = { Options.DEFAULT
|
|||||||
return AnalyzeQualifiedElementResult.ShortenNow
|
return AnalyzeQualifiedElementResult.ShortenNow
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun shortenElement(element: KtDotQualifiedExpression): KtElement {
|
override fun shortenElement(element: KtDotQualifiedExpression, options: Options): KtElement {
|
||||||
val receiver = element.receiverExpression
|
val receiver = element.receiverExpression
|
||||||
val selector = element.selectorExpression ?: return element
|
val selector = element.selectorExpression ?: return element
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user