FIR IDE: Add simple shortening completion handler for properties

This commit is contained in:
Roman Golyshev
2021-03-01 17:55:20 +03:00
parent 8392b36964
commit d955f8e0d3
3 changed files with 20 additions and 1 deletions
@@ -1,3 +1,4 @@
// FIR_COMPARISON
package some
fun other() {
@@ -1,3 +1,4 @@
// FIR_COMPARISON
package some
import other.someProp
@@ -115,7 +115,9 @@ private class VariableLookupElementFactory {
if (setterName != null) "$getterName()/$setterName()" else "$getterName()"
private fun createInsertHandler(symbol: KtVariableLikeSymbol): InsertHandler<LookupElement> {
return QuotedNamesAwareInsertionHandler(symbol.name)
val callableId = symbol.callableIdIfExists ?: return QuotedNamesAwareInsertionHandler(symbol.name)
return ShorteningVariableInsertionHandler(callableId)
}
private val KtVariableLikeSymbol.callableIdIfExists: FqName?
@@ -320,6 +322,21 @@ private class ShorteningFunctionInsertionHandler(
}
}
private class ShorteningVariableInsertionHandler(private val name: FqName) : InsertHandler<LookupElement> {
override fun handleInsert(context: InsertionContext, item: LookupElement) {
val targetFile = context.file as? KtFile ?: return
context.document.replaceString(
context.startOffset,
context.tailOffset,
name.withRootPrefixIfNeeded().render()
)
context.commitDocument()
shortenReferences(targetFile, TextRange(context.startOffset, context.tailOffset))
}
}
private open class QuotedNamesAwareInsertionHandler(private val name: Name) : InsertHandler<LookupElement> {
override fun handleInsert(context: InsertionContext, item: LookupElement) {
val startOffset = context.startOffset