idea: cleanup code
This commit is contained in:
+12
-22
@@ -1,17 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.liveTemplates.macro
|
||||
@@ -20,7 +9,6 @@ import com.intellij.codeInsight.lookup.LookupElement
|
||||
import com.intellij.codeInsight.lookup.LookupElementBuilder
|
||||
import com.intellij.codeInsight.template.Expression
|
||||
import com.intellij.codeInsight.template.ExpressionContext
|
||||
import com.intellij.codeInsight.template.Macro
|
||||
import com.intellij.codeInsight.template.Result
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiNamedElement
|
||||
@@ -60,23 +48,25 @@ class AnonymousSuperMacro : KotlinMacro() {
|
||||
}
|
||||
|
||||
private fun getSupertypes(params: Array<Expression>, context: ExpressionContext): Collection<PsiNamedElement> {
|
||||
if (params.size != 0) return emptyList()
|
||||
if (params.isNotEmpty()) return emptyList()
|
||||
|
||||
val psiDocumentManager = PsiDocumentManager.getInstance(context.project)
|
||||
psiDocumentManager.commitAllDocuments()
|
||||
|
||||
val psiFile = psiDocumentManager.getPsiFile(context.editor!!.document) as? KtFile ?: return emptyList()
|
||||
|
||||
val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java) ?: return emptyList()
|
||||
val expression = PsiTreeUtil.getParentOfType(psiFile.findElementAt(context.startOffset), KtExpression::class.java)
|
||||
?: return emptyList()
|
||||
|
||||
val bindingContext = expression.analyze(BodyResolveMode.FULL)
|
||||
val resolutionScope = expression.getResolutionScope(bindingContext, expression.getResolutionFacade())
|
||||
|
||||
return resolutionScope
|
||||
.collectDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)
|
||||
.filter { it is ClassDescriptor &&
|
||||
(it.modality == Modality.OPEN || it.modality == Modality.ABSTRACT) &&
|
||||
(it.kind == ClassKind.CLASS || it.kind == ClassKind.INTERFACE) }
|
||||
.mapNotNull { DescriptorToSourceUtils.descriptorToDeclaration(it) as PsiNamedElement? }
|
||||
return resolutionScope.collectDescriptorsFiltered(DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS)
|
||||
.filter {
|
||||
it is ClassDescriptor &&
|
||||
(it.modality == Modality.OPEN || it.modality == Modality.ABSTRACT) &&
|
||||
(it.kind == ClassKind.CLASS || it.kind == ClassKind.INTERFACE)
|
||||
}
|
||||
.mapNotNull { DescriptorToSourceUtils.descriptorToDeclaration(it) as PsiNamedElement? }
|
||||
}
|
||||
}
|
||||
|
||||
+20
-8
@@ -59,24 +59,36 @@ abstract class BaseKotlinVariableMacro<TState> : KotlinMacro() {
|
||||
val bindingContext = resolutionFacade.analyze(contextElement, BodyResolveMode.PARTIAL_FOR_COMPLETION)
|
||||
|
||||
fun isVisible(descriptor: DeclarationDescriptor): Boolean {
|
||||
return descriptor !is DeclarationDescriptorWithVisibility || descriptor.isVisible(contextElement, null, bindingContext, resolutionFacade)
|
||||
return descriptor !is DeclarationDescriptorWithVisibility || descriptor.isVisible(
|
||||
contextElement,
|
||||
null,
|
||||
bindingContext,
|
||||
resolutionFacade
|
||||
)
|
||||
}
|
||||
|
||||
val state = initState(contextElement, bindingContext)
|
||||
|
||||
val helper = ReferenceVariantsHelper(bindingContext, resolutionFacade, resolutionFacade.moduleDescriptor, ::isVisible, NotPropertiesService.getNotProperties(contextElement))
|
||||
val helper = ReferenceVariantsHelper(
|
||||
bindingContext,
|
||||
resolutionFacade,
|
||||
resolutionFacade.moduleDescriptor,
|
||||
::isVisible,
|
||||
NotPropertiesService.getNotProperties(contextElement)
|
||||
)
|
||||
return helper
|
||||
.getReferenceVariants(contextElement, CallTypeAndReceiver.DEFAULT, DescriptorKindFilter.VARIABLES, { true })
|
||||
.map { it as VariableDescriptor }
|
||||
.filter { isSuitable(it, project, state) }
|
||||
.getReferenceVariants(contextElement, CallTypeAndReceiver.DEFAULT, DescriptorKindFilter.VARIABLES, { true })
|
||||
.map { it as VariableDescriptor }
|
||||
.filter { isSuitable(it, project, state) }
|
||||
}
|
||||
|
||||
protected abstract fun initState(contextElement: KtElement, bindingContext: BindingContext): TState
|
||||
|
||||
protected abstract fun isSuitable(
|
||||
variableDescriptor: VariableDescriptor,
|
||||
project: Project,
|
||||
state: TState): Boolean
|
||||
variableDescriptor: VariableDescriptor,
|
||||
project: Project,
|
||||
state: TState
|
||||
): Boolean
|
||||
|
||||
override fun calculateResult(params: Array<Expression>, context: ExpressionContext): Result? {
|
||||
val vars = getVariables(params, context)
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
package org.jetbrains.kotlin.idea.liveTemplates.macro
|
||||
|
||||
import com.intellij.codeInsight.template.Expression
|
||||
|
||||
+4
-2
@@ -43,7 +43,8 @@ class SuitableVariableMacro : BaseKotlinVariableMacro<SuitableVariableMacro.Stat
|
||||
val expectedInfos = ExpectedInfos(bindingContext, resolutionFacade, null).calculate(contextElement)
|
||||
if (expectedInfos.isNotEmpty()) {
|
||||
val scope = contextElement.getResolutionScope(bindingContext, resolutionFacade)
|
||||
val smartCastCalculator = SmartCastCalculator(bindingContext, scope.ownerDescriptor, contextElement, null, resolutionFacade)
|
||||
val smartCastCalculator =
|
||||
SmartCastCalculator(bindingContext, scope.ownerDescriptor, contextElement, null, resolutionFacade)
|
||||
return State(expectedInfos, smartCastCalculator)
|
||||
}
|
||||
}
|
||||
@@ -55,6 +56,7 @@ class SuitableVariableMacro : BaseKotlinVariableMacro<SuitableVariableMacro.Stat
|
||||
override fun isSuitable(variableDescriptor: VariableDescriptor, project: Project, state: State?): Boolean {
|
||||
if (state == null) return true
|
||||
val types = state.smartCastCalculator.types(variableDescriptor)
|
||||
return state.expectedInfos.any { expectedInfo -> types.any { expectedInfo.filter.matchingSubstitutor(it.toFuzzyType(emptyList())) != null } }
|
||||
return state.expectedInfos
|
||||
.any { expectedInfo -> types.any { expectedInfo.filter.matchingSubstitutor(it.toFuzzyType(emptyList())) != null } }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user