Refactoring
This commit is contained in:
+51
-65
@@ -39,6 +39,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
|||||||
import org.jetbrains.kotlin.types.JetType
|
import org.jetbrains.kotlin.types.JetType
|
||||||
import org.jetbrains.kotlin.types.TypeUtils
|
import org.jetbrains.kotlin.types.TypeUtils
|
||||||
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
|
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
|
||||||
|
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||||
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptySet
|
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptySet
|
||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
import java.util.HashSet
|
import java.util.HashSet
|
||||||
@@ -67,84 +68,47 @@ class SmartCompletion(
|
|||||||
|
|
||||||
public val smartCastCalculator: SmartCastCalculator = SmartCastCalculator(bindingContext, moduleDescriptor, expression)
|
public val smartCastCalculator: SmartCastCalculator = SmartCastCalculator(bindingContext, moduleDescriptor, expression)
|
||||||
|
|
||||||
public class Result(
|
public val descriptorFilter: ((DeclarationDescriptor) -> Collection<LookupElement>)?
|
||||||
val declarationFilter: ((DeclarationDescriptor) -> Collection<LookupElement>)?,
|
= { descriptor: DeclarationDescriptor -> filterDescriptor(descriptor).map { postProcess(it) } }.check { expectedInfos.isNotEmpty() }
|
||||||
val additionalItems: Collection<LookupElement>,
|
|
||||||
val inheritanceSearcher: InheritanceItemsSearcher?)
|
|
||||||
|
|
||||||
public fun execute(): Result? {
|
private fun filterDescriptor(descriptor: DeclarationDescriptor): Collection<LookupElement> {
|
||||||
fun postProcess(item: LookupElement): LookupElement {
|
if (descriptor in descriptorsToSkip) return emptyList()
|
||||||
return if (item.getUserData(KEEP_OLD_ARGUMENT_LIST_ON_TAB_KEY) == null) {
|
|
||||||
object : LookupElementDecorator<LookupElement>(item) {
|
|
||||||
override fun handleInsert(context: InsertionContext) {
|
|
||||||
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
|
|
||||||
val offset = context.getOffsetMap().getOffset(OLD_ARGUMENTS_REPLACEMENT_OFFSET)
|
|
||||||
if (offset != -1) {
|
|
||||||
context.getDocument().deleteString(context.getTailOffset(), offset)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super.handleInsert(context)
|
val result = SmartList<LookupElement>()
|
||||||
}
|
val types = descriptor.fuzzyTypesForSmartCompletion(smartCastCalculator)
|
||||||
}
|
val infoClassifier = { expectedInfo: ExpectedInfo -> types.classifyExpectedInfo(expectedInfo) }
|
||||||
}
|
|
||||||
else {
|
result.addLookupElements(descriptor, expectedInfos, infoClassifier, noNameSimilarityForReturnItself = receiver == null) { descriptor ->
|
||||||
item
|
lookupElementFactory.createLookupElement(descriptor, bindingContext, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (descriptor is PropertyDescriptor) {
|
||||||
|
result.addLookupElements(descriptor, expectedInfos, infoClassifier) { descriptor ->
|
||||||
|
lookupElementFactory.createBackingFieldLookupElement(descriptor, inDescriptor, resolutionFacade)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val result = executeInternal() ?: return null
|
if (receiver == null) {
|
||||||
// TODO: code could be more simple, see KT-5726
|
toFunctionReferenceLookupElement(descriptor, expectedInfos.filterFunctionExpected())?.let { result.add(it) }
|
||||||
val additionalItems = result.additionalItems.map(::postProcess)
|
}
|
||||||
val inheritanceSearcher = result.inheritanceSearcher?.let {
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
public fun additionalItems(): Pair<Collection<LookupElement>, InheritanceItemsSearcher?> {
|
||||||
|
val (items, inheritanceSearcher) = additionalItemsNoPostProcess()
|
||||||
|
val postProcessedItems = items.map { postProcess(it) }
|
||||||
|
val postProcessedSearcher = inheritanceSearcher?.let {
|
||||||
object : InheritanceItemsSearcher {
|
object : InheritanceItemsSearcher {
|
||||||
override fun search(nameFilter: (String) -> Boolean, consumer: (LookupElement) -> Unit) {
|
override fun search(nameFilter: (String) -> Boolean, consumer: (LookupElement) -> Unit) {
|
||||||
it.search(nameFilter, { consumer(postProcess(it)) })
|
it.search(nameFilter, { consumer(postProcess(it)) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val filter = result.declarationFilter
|
return postProcessedItems to postProcessedSearcher
|
||||||
return if (filter != null)
|
|
||||||
Result({ filter(it).map(::postProcess) }, additionalItems, inheritanceSearcher)
|
|
||||||
else
|
|
||||||
Result(null, additionalItems, inheritanceSearcher)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun executeInternal(): Result? {
|
private fun additionalItemsNoPostProcess(): Pair<Collection<LookupElement>, InheritanceItemsSearcher?> {
|
||||||
val (additionalItems, inheritanceSearcher) = additionalItems()
|
|
||||||
|
|
||||||
if (expectedInfos.isEmpty()) {
|
|
||||||
return Result(null, additionalItems, inheritanceSearcher)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun filterDeclaration(descriptor: DeclarationDescriptor): Collection<LookupElement> {
|
|
||||||
if (descriptor in descriptorsToSkip) return emptyList()
|
|
||||||
|
|
||||||
val result = SmartList<LookupElement>()
|
|
||||||
val types = descriptor.fuzzyTypesForSmartCompletion(smartCastCalculator)
|
|
||||||
val infoClassifier = { expectedInfo: ExpectedInfo -> types.classifyExpectedInfo(expectedInfo) }
|
|
||||||
|
|
||||||
result.addLookupElements(descriptor, expectedInfos, infoClassifier, noNameSimilarityForReturnItself = receiver == null) { descriptor ->
|
|
||||||
lookupElementFactory.createLookupElement(descriptor, bindingContext, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (descriptor is PropertyDescriptor) {
|
|
||||||
result.addLookupElements(descriptor, expectedInfos, infoClassifier) { descriptor ->
|
|
||||||
lookupElementFactory.createBackingFieldLookupElement(descriptor, inDescriptor, resolutionFacade)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (receiver == null) {
|
|
||||||
toFunctionReferenceLookupElement(descriptor, expectedInfos.filterFunctionExpected())?.let { result.add(it) }
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
return Result(::filterDeclaration, additionalItems, inheritanceSearcher)
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun additionalItems(): Pair<Collection<LookupElement>, InheritanceItemsSearcher?> {
|
|
||||||
val asTypePositionItems = buildForAsTypePosition()
|
val asTypePositionItems = buildForAsTypePosition()
|
||||||
if (asTypePositionItems != null) {
|
if (asTypePositionItems != null) {
|
||||||
assert(expectedInfos.isEmpty())
|
assert(expectedInfos.isEmpty())
|
||||||
@@ -185,6 +149,28 @@ class SmartCompletion(
|
|||||||
return Pair(items, inheritanceSearcher)
|
return Pair(items, inheritanceSearcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun postProcess(item: LookupElement): LookupElement {
|
||||||
|
if (forBasicCompletion) return item
|
||||||
|
|
||||||
|
return if (item.getUserData(KEEP_OLD_ARGUMENT_LIST_ON_TAB_KEY) == null) {
|
||||||
|
object : LookupElementDecorator<LookupElement>(item) {
|
||||||
|
override fun handleInsert(context: InsertionContext) {
|
||||||
|
if (context.getCompletionChar() == Lookup.REPLACE_SELECT_CHAR) {
|
||||||
|
val offset = context.getOffsetMap().getOffset(OLD_ARGUMENTS_REPLACEMENT_OFFSET)
|
||||||
|
if (offset != -1) {
|
||||||
|
context.getDocument().deleteString(context.getTailOffset(), offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
super.handleInsert(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
item
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun MutableCollection<LookupElement>.addThisItems(place: JetExpression, expectedInfos: Collection<ExpectedInfo>, smartCastCalculator: SmartCastCalculator) {
|
private fun MutableCollection<LookupElement>.addThisItems(place: JetExpression, expectedInfos: Collection<ExpectedInfo>, smartCastCalculator: SmartCastCalculator) {
|
||||||
if (shouldCompleteThisItems(prefixMatcher)) {
|
if (shouldCompleteThisItems(prefixMatcher)) {
|
||||||
val items = thisExpressionItems(bindingContext, place, prefixMatcher.getPrefix())
|
val items = thisExpressionItems(bindingContext, place, prefixMatcher.getPrefix())
|
||||||
|
|||||||
+19
-20
@@ -50,33 +50,32 @@ class SmartCompletionSession(configuration: CompletionSessionConfiguration, para
|
|||||||
val completion = SmartCompletion(expression, resolutionFacade, moduleDescriptor,
|
val completion = SmartCompletion(expression, resolutionFacade, moduleDescriptor,
|
||||||
bindingContext, isVisibleFilter, inDescriptor, prefixMatcher, originalSearchScope,
|
bindingContext, isVisibleFilter, inDescriptor, prefixMatcher, originalSearchScope,
|
||||||
toFromOriginalFileMapper, lookupElementFactory)
|
toFromOriginalFileMapper, lookupElementFactory)
|
||||||
val result = completion.execute()
|
|
||||||
if (result != null) {
|
|
||||||
collector.addElements(result.additionalItems)
|
|
||||||
|
|
||||||
if (nameExpression != null) {
|
val (additionalItems, inheritanceSearcher) = completion.additionalItems()
|
||||||
val filter = result.declarationFilter
|
collector.addElements(additionalItems)
|
||||||
if (filter != null) {
|
|
||||||
referenceVariants.forEach { collector.addElements(filter(it)) }
|
|
||||||
flushToResultSet()
|
|
||||||
|
|
||||||
processNonImported { collector.addElements(filter(it)) }
|
if (nameExpression != null) {
|
||||||
flushToResultSet()
|
val filter = completion.descriptorFilter
|
||||||
|
if (filter != null) {
|
||||||
|
referenceVariants.forEach { collector.addElements(filter(it)) }
|
||||||
|
flushToResultSet()
|
||||||
|
|
||||||
if (position.getContainingFile() is JetCodeFragment) {
|
processNonImported { collector.addElements(filter(it)) }
|
||||||
getRuntimeReceiverTypeReferenceVariants().forEach {
|
flushToResultSet()
|
||||||
collector.addElements(filter(it).map { it.withReceiverCast() })
|
|
||||||
}
|
if (position.getContainingFile() is JetCodeFragment) {
|
||||||
flushToResultSet()
|
getRuntimeReceiverTypeReferenceVariants().forEach {
|
||||||
|
collector.addElements(filter(it).map { it.withReceiverCast() })
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// it makes no sense to search inheritors if there is no reference because it means that we have prefix like "this@"
|
|
||||||
result.inheritanceSearcher?.search({ prefixMatcher.prefixMatches(it) }) {
|
|
||||||
collector.addElement(it)
|
|
||||||
flushToResultSet()
|
flushToResultSet()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// it makes no sense to search inheritors if there is no reference because it means that we have prefix like "this@"
|
||||||
|
inheritanceSearcher?.search({ prefixMatcher.prefixMatches(it) }) {
|
||||||
|
collector.addElement(it)
|
||||||
|
flushToResultSet()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user