Code refactoring
This commit is contained in:
+35
-40
@@ -193,23 +193,22 @@ abstract class CompletionSessionBase(protected val configuration: CompletionSess
|
|||||||
|
|
||||||
protected abstract fun doComplete()
|
protected abstract fun doComplete()
|
||||||
|
|
||||||
// set is used only for completion in code fragments
|
protected abstract val descriptorKindFilter: DescriptorKindFilter?
|
||||||
private var alreadyAddedDescriptors: Collection<DeclarationDescriptor> by Delegates.notNull()
|
|
||||||
|
|
||||||
protected fun getReferenceVariants(kindFilter: DescriptorKindFilter, runtimeReceiverType: Boolean): Collection<DeclarationDescriptor> {
|
// set is used only for completion in code fragments
|
||||||
val descriptors = referenceVariantsHelper.getReferenceVariants(reference!!.expression, kindFilter, runtimeReceiverType, prefixMatcher.asNameFilter())
|
protected val referenceVariants: Collection<DeclarationDescriptor> by Delegates.lazy {
|
||||||
if (!runtimeReceiverType) {
|
if (descriptorKindFilter != null) {
|
||||||
if (position.getContainingFile() is JetCodeFragment) {
|
referenceVariantsHelper.getReferenceVariants(reference!!.expression, descriptorKindFilter!!, false, prefixMatcher.asNameFilter())
|
||||||
alreadyAddedDescriptors = descriptors
|
|
||||||
}
|
|
||||||
return descriptors
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return descriptors.filter { desc ->
|
emptyList()
|
||||||
!alreadyAddedDescriptors.any {
|
}
|
||||||
comparePossiblyOverridingDescriptors(project, it, desc)
|
}
|
||||||
}
|
|
||||||
}
|
protected fun getRuntimeReceiverTypeReferenceVariants(): Collection<DeclarationDescriptor> {
|
||||||
|
val descriptors = referenceVariantsHelper.getReferenceVariants(reference!!.expression, descriptorKindFilter!!, true, prefixMatcher.asNameFilter())
|
||||||
|
return descriptors.filter { descriptor ->
|
||||||
|
referenceVariants.none { comparePossiblyOverridingDescriptors(project, it, descriptor) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,6 +254,20 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
|
|||||||
|
|
||||||
public val completionKind: CompletionKind = calcCompletionKind()
|
public val completionKind: CompletionKind = calcCompletionKind()
|
||||||
|
|
||||||
|
protected override val descriptorKindFilter = when (completionKind) {
|
||||||
|
CompletionKind.TYPES ->
|
||||||
|
DescriptorKindFilter(DescriptorKindFilter.CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude DescriptorKindExclude.EnumEntry
|
||||||
|
|
||||||
|
CompletionKind.ANNOTATION_TYPES, CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME ->
|
||||||
|
DescriptorKindFilter(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude NonAnnotationClassifierExclude
|
||||||
|
|
||||||
|
CompletionKind.ALL ->
|
||||||
|
DescriptorKindFilter(DescriptorKindFilter.ALL_KINDS_MASK)
|
||||||
|
|
||||||
|
CompletionKind.NAMED_PARAMETERS_ONLY, CompletionKind.KEYWORDS_ONLY ->
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
private fun calcCompletionKind(): CompletionKind {
|
private fun calcCompletionKind(): CompletionKind {
|
||||||
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) {
|
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) {
|
||||||
return CompletionKind.NAMED_PARAMETERS_ONLY
|
return CompletionKind.NAMED_PARAMETERS_ONLY
|
||||||
@@ -293,20 +306,7 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
|
|||||||
assert(parameters.getCompletionType() == CompletionType.BASIC)
|
assert(parameters.getCompletionType() == CompletionType.BASIC)
|
||||||
|
|
||||||
if (completionKind != CompletionKind.NAMED_PARAMETERS_ONLY) {
|
if (completionKind != CompletionKind.NAMED_PARAMETERS_ONLY) {
|
||||||
val kindFilter = when (completionKind) {
|
collector.addDescriptorElements(referenceVariants, suppressAutoInsertion = false)
|
||||||
CompletionKind.TYPES ->
|
|
||||||
DescriptorKindFilter(DescriptorKindFilter.CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude DescriptorKindExclude.EnumEntry
|
|
||||||
|
|
||||||
CompletionKind.ANNOTATION_TYPES, CompletionKind.ANNOTATION_TYPES_OR_PARAMETER_NAME ->
|
|
||||||
DescriptorKindFilter(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS_MASK or DescriptorKindFilter.PACKAGES_MASK) exclude NonAnnotationClassifierExclude
|
|
||||||
|
|
||||||
else ->
|
|
||||||
DescriptorKindFilter(DescriptorKindFilter.ALL_KINDS_MASK)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (completionKind != CompletionKind.KEYWORDS_ONLY) {
|
|
||||||
addReferenceVariants(kindFilter, runtimeReceiverType = false)
|
|
||||||
}
|
|
||||||
|
|
||||||
val keywordsPrefix = prefix.substringBefore('@') // if there is '@' in the prefix - use shorter prefix to not loose 'this' etc
|
val keywordsPrefix = prefix.substringBefore('@') // if there is '@' in the prefix - use shorter prefix to not loose 'this' etc
|
||||||
KeywordCompletion.complete(expression ?: parameters.getPosition(), keywordsPrefix) { lookupElement ->
|
KeywordCompletion.complete(expression ?: parameters.getPosition(), keywordsPrefix) { lookupElement ->
|
||||||
@@ -350,7 +350,7 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
|
|||||||
|
|
||||||
if (position.getContainingFile() is JetCodeFragment) {
|
if (position.getContainingFile() is JetCodeFragment) {
|
||||||
flushToResultSet()
|
flushToResultSet()
|
||||||
addReferenceVariants(kindFilter, runtimeReceiverType = true)
|
collector.addDescriptorElements(getRuntimeReceiverTypeReferenceVariants(), suppressAutoInsertion = false, withReceiverCast = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -387,20 +387,13 @@ class BasicCompletionSession(configuration: CompletionSessionConfiguration,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addReferenceVariants(kindFilter: DescriptorKindFilter, runtimeReceiverType: Boolean) {
|
|
||||||
collector.addDescriptorElements(
|
|
||||||
getReferenceVariants(kindFilter, runtimeReceiverType),
|
|
||||||
suppressAutoInsertion = false,
|
|
||||||
withReceiverCast = runtimeReceiverType)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SmartCompletionSession(configuration: CompletionSessionConfiguration, parameters: CompletionParameters, resultSet: CompletionResultSet)
|
class SmartCompletionSession(configuration: CompletionSessionConfiguration, parameters: CompletionParameters, resultSet: CompletionResultSet)
|
||||||
: CompletionSessionBase(configuration, parameters, resultSet) {
|
: CompletionSessionBase(configuration, parameters, resultSet) {
|
||||||
|
|
||||||
// we do not include SAM-constructors because they are handled separately and adding them requires iterating of java classes
|
// we do not include SAM-constructors because they are handled separately and adding them requires iterating of java classes
|
||||||
private val DESCRIPTOR_KIND_MASK = DescriptorKindFilter.VALUES exclude SamConstructorDescriptorKindExclude
|
override val descriptorKindFilter: DescriptorKindFilter? = DescriptorKindFilter.VALUES exclude SamConstructorDescriptorKindExclude
|
||||||
|
|
||||||
override fun doComplete() {
|
override fun doComplete() {
|
||||||
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) {
|
if (NamedParametersCompletion.isOnlyNamedParameterExpected(position)) {
|
||||||
@@ -423,14 +416,16 @@ class SmartCompletionSession(configuration: CompletionSessionConfiguration, para
|
|||||||
if (reference != null) {
|
if (reference != null) {
|
||||||
val filter = result.declarationFilter
|
val filter = result.declarationFilter
|
||||||
if (filter != null) {
|
if (filter != null) {
|
||||||
getReferenceVariants(DESCRIPTOR_KIND_MASK, runtimeReceiverType = false).forEach { collector.addElements(filter(it)) }
|
referenceVariants.forEach { collector.addElements(filter(it)) }
|
||||||
flushToResultSet()
|
flushToResultSet()
|
||||||
|
|
||||||
processNonImported { collector.addElements(filter(it)) }
|
processNonImported { collector.addElements(filter(it)) }
|
||||||
flushToResultSet()
|
flushToResultSet()
|
||||||
|
|
||||||
if (position.getContainingFile() is JetCodeFragment) {
|
if (position.getContainingFile() is JetCodeFragment) {
|
||||||
getReferenceVariants(DESCRIPTOR_KIND_MASK, runtimeReceiverType = true).forEach { collector.addElements(filter(it).map { it.withReceiverCast() }) }
|
getRuntimeReceiverTypeReferenceVariants().forEach {
|
||||||
|
collector.addElements(filter(it).map { it.withReceiverCast() })
|
||||||
|
}
|
||||||
flushToResultSet()
|
flushToResultSet()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -461,7 +456,7 @@ class SmartCompletionSession(configuration: CompletionSessionConfiguration, para
|
|||||||
override fun getSpreadElement(): LeafPsiElement? = null
|
override fun getSpreadElement(): LeafPsiElement? = null
|
||||||
override fun isExternal() = false
|
override fun isExternal() = false
|
||||||
}
|
}
|
||||||
val dummyArguments = call!!.getValueArguments() + listOf(dummyArgument)
|
val dummyArguments = call.getValueArguments() + listOf(dummyArgument)
|
||||||
val dummyCall = object : DelegatingCall(call) {
|
val dummyCall = object : DelegatingCall(call) {
|
||||||
override fun getValueArguments() = dummyArguments
|
override fun getValueArguments() = dummyArguments
|
||||||
override fun getFunctionLiteralArguments() = listOf(dummyArgument)
|
override fun getFunctionLiteralArguments() = listOf(dummyArgument)
|
||||||
|
|||||||
+3
@@ -33,6 +33,7 @@ import org.jetbrains.kotlin.kdoc.psi.impl.KDocName
|
|||||||
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getParentOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
|
||||||
|
|
||||||
class KDocCompletionContributor(): CompletionContributor() {
|
class KDocCompletionContributor(): CompletionContributor() {
|
||||||
init {
|
init {
|
||||||
@@ -57,6 +58,8 @@ object KDocNameCompletionProvider: CompletionProvider<CompletionParameters>() {
|
|||||||
|
|
||||||
class KDocNameCompletionSession(parameters: CompletionParameters,
|
class KDocNameCompletionSession(parameters: CompletionParameters,
|
||||||
resultSet: CompletionResultSet): CompletionSessionBase(CompletionSessionConfiguration(parameters), parameters, resultSet) {
|
resultSet: CompletionResultSet): CompletionSessionBase(CompletionSessionConfiguration(parameters), parameters, resultSet) {
|
||||||
|
override val descriptorKindFilter: DescriptorKindFilter? get() = null
|
||||||
|
|
||||||
override fun doComplete() {
|
override fun doComplete() {
|
||||||
val position = parameters.getPosition().getParentOfType<KDocName>(false) ?: return
|
val position = parameters.getPosition().getParentOfType<KDocName>(false) ?: return
|
||||||
val declaration = position.getContainingDoc().getOwner() ?: return
|
val declaration = position.getContainingDoc().getOwner() ?: return
|
||||||
|
|||||||
Reference in New Issue
Block a user