idea: cleanup 'public', property access syntax
This commit is contained in:
@@ -142,7 +142,7 @@ class ExpectedInfos(
|
||||
private val useHeuristicSignatures: Boolean = true,
|
||||
private val useOuterCallsExpectedTypeCount: Int = 0
|
||||
) {
|
||||
public fun calculate(expressionWithType: KtExpression): Collection<ExpectedInfo> {
|
||||
fun calculate(expressionWithType: KtExpression): Collection<ExpectedInfo> {
|
||||
val expectedInfos = calculateForArgument(expressionWithType)
|
||||
?: calculateForFunctionLiteralArgument(expressionWithType)
|
||||
?: calculateForIndexingArgument(expressionWithType)
|
||||
@@ -163,16 +163,16 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForArgument(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val argument = expressionWithType.getParent() as? KtValueArgument ?: return null
|
||||
val argumentList = argument.getParent() as? KtValueArgumentList ?: return null
|
||||
val callElement = argumentList.getParent() as? KtCallElement ?: return null
|
||||
val argument = expressionWithType.parent as? KtValueArgument ?: return null
|
||||
val argumentList = argument.parent as? KtValueArgumentList ?: return null
|
||||
val callElement = argumentList.parent as? KtCallElement ?: return null
|
||||
return calculateForArgument(callElement, argument)
|
||||
}
|
||||
|
||||
private fun calculateForFunctionLiteralArgument(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val functionLiteralArgument = expressionWithType.getParent() as? KtLambdaArgument
|
||||
val callExpression = functionLiteralArgument?.getParent() as? KtCallExpression ?: return null
|
||||
val literalArgument = callExpression.getLambdaArguments().firstOrNull() ?: return null
|
||||
val functionLiteralArgument = expressionWithType.parent as? KtLambdaArgument
|
||||
val callExpression = functionLiteralArgument?.parent as? KtCallExpression ?: return null
|
||||
val literalArgument = callExpression.lambdaArguments.firstOrNull() ?: return null
|
||||
if (literalArgument.getArgumentExpression() != expressionWithType) return null
|
||||
return calculateForArgument(callExpression, literalArgument)
|
||||
}
|
||||
@@ -191,7 +191,7 @@ class ExpectedInfos(
|
||||
return calculateForArgument(call, argument)
|
||||
}
|
||||
|
||||
public fun calculateForArgument(call: Call, argument: ValueArgument): Collection<ExpectedInfo> {
|
||||
fun calculateForArgument(call: Call, argument: ValueArgument): Collection<ExpectedInfo> {
|
||||
val results = calculateForArgument(call, TypeUtils.NO_EXPECTED_TYPE, argument)
|
||||
|
||||
fun makesSenseToUseOuterCallExpectedType(info: ExpectedInfo): Boolean {
|
||||
@@ -218,14 +218,14 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForArgument(call: Call, callExpectedType: KotlinType, argument: ValueArgument): Collection<ExpectedInfo> {
|
||||
val argumentIndex = call.getValueArguments().indexOf(argument)
|
||||
val argumentIndex = call.valueArguments.indexOf(argument)
|
||||
assert(argumentIndex >= 0) {
|
||||
"Could not find argument '$argument(${argument.asElement().text})' among arguments of call: $call"
|
||||
}
|
||||
|
||||
// leave only arguments before the current one
|
||||
val truncatedCall = object : DelegatingCall(call) {
|
||||
val arguments = call.getValueArguments().subList(0, argumentIndex)
|
||||
val arguments = call.valueArguments.subList(0, argumentIndex)
|
||||
|
||||
override fun getValueArguments() = arguments
|
||||
override fun getFunctionLiteralArguments() = emptyList<LambdaArgument>()
|
||||
@@ -262,7 +262,7 @@ class ExpectedInfos(
|
||||
// check that all arguments before the current one matched
|
||||
if (checkPrevArgumentsMatched && !candidate.allArgumentsMatched()) return
|
||||
|
||||
var descriptor = candidate.getResultingDescriptor()
|
||||
var descriptor = candidate.resultingDescriptor
|
||||
if (descriptor.valueParameters.isEmpty()) return
|
||||
|
||||
var argumentToParameter = call.mapArgumentsToParameters(descriptor)
|
||||
@@ -311,13 +311,13 @@ class ExpectedInfos(
|
||||
return
|
||||
}
|
||||
|
||||
val expectedName = if (descriptor.hasSynthesizedParameterNames()) null else parameter.getName().asString()
|
||||
val expectedName = if (descriptor.hasSynthesizedParameterNames()) null else parameter.name.asString()
|
||||
|
||||
fun needCommaForParameter(parameter: ValueParameterDescriptor): Boolean {
|
||||
if (parameter.hasDefaultValue()) return false // parameter is optional
|
||||
if (parameter.varargElementType != null) return false // vararg arguments list can be empty
|
||||
// last parameter of functional type can be placed outside parenthesis:
|
||||
if (!isArrayAccess && parameter == parameters.last() && KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(parameter.getType())) return false
|
||||
if (!isArrayAccess && parameter == parameters.last() && KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(parameter.type)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -349,16 +349,16 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
val starOptions = if (!alreadyHasStar) ItemOptions.STAR_PREFIX else ItemOptions.DEFAULT
|
||||
add(ExpectedInfo.createForArgument(parameter.getType(), expectedName, varargTail, argumentPositionData, starOptions))
|
||||
add(ExpectedInfo.createForArgument(parameter.type, expectedName, varargTail, argumentPositionData, starOptions))
|
||||
}
|
||||
else {
|
||||
if (alreadyHasStar) return
|
||||
|
||||
val parameterType = if (useHeuristicSignatures)
|
||||
resolutionFacade.ideService<HeuristicSignatures>().
|
||||
correctedParameterType(descriptor, parameter) ?: parameter.getType()
|
||||
correctedParameterType(descriptor, parameter) ?: parameter.type
|
||||
else
|
||||
parameter.getType()
|
||||
parameter.type
|
||||
|
||||
if (isFunctionLiteralArgument) {
|
||||
if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(parameterType)) {
|
||||
@@ -378,8 +378,8 @@ class ExpectedInfos(
|
||||
= getArgumentExpression()?.let { bindingContext.getType(it) }?.isError ?: true
|
||||
|
||||
private fun namedArgumentTail(argumentToParameter: Map<ValueArgument, ValueParameterDescriptor>, argumentName: Name, descriptor: FunctionDescriptor): Tail? {
|
||||
val usedParameterNames = (argumentToParameter.values.map { it.getName() } + listOf(argumentName)).toSet()
|
||||
val notUsedParameters = descriptor.getValueParameters().filter { it.getName() !in usedParameterNames }
|
||||
val usedParameterNames = (argumentToParameter.values.map { it.name } + listOf(argumentName)).toSet()
|
||||
val notUsedParameters = descriptor.valueParameters.filter { it.name !in usedParameterNames }
|
||||
return if (notUsedParameters.isEmpty())
|
||||
Tail.RPARENTH // named arguments no supported for []
|
||||
else if (notUsedParameters.all { it.hasDefaultValue() })
|
||||
@@ -389,11 +389,11 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForEqAndAssignment(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val binaryExpression = expressionWithType.getParent() as? KtBinaryExpression
|
||||
val binaryExpression = expressionWithType.parent as? KtBinaryExpression
|
||||
if (binaryExpression != null) {
|
||||
val operationToken = binaryExpression.getOperationToken()
|
||||
val operationToken = binaryExpression.operationToken
|
||||
if (operationToken == KtTokens.EQ || operationToken in COMPARISON_TOKENS) {
|
||||
val otherOperand = if (expressionWithType == binaryExpression.getRight()) binaryExpression.getLeft() else binaryExpression.getRight()
|
||||
val otherOperand = if (expressionWithType == binaryExpression.right) binaryExpression.left else binaryExpression.right
|
||||
if (otherOperand != null) {
|
||||
var expectedType = bindingContext.getType(otherOperand) ?: return null
|
||||
|
||||
@@ -421,16 +421,16 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForIf(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val ifExpression = (expressionWithType.getParent() as? KtContainerNode)?.getParent() as? KtIfExpression ?: return null
|
||||
val ifExpression = (expressionWithType.parent as? KtContainerNode)?.parent as? KtIfExpression ?: return null
|
||||
return when (expressionWithType) {
|
||||
ifExpression.getCondition() -> listOf(ExpectedInfo(resolutionFacade.moduleDescriptor.builtIns.booleanType, null, Tail.RPARENTH, additionalData = IfConditionAdditionalData))
|
||||
ifExpression.condition -> listOf(ExpectedInfo(resolutionFacade.moduleDescriptor.builtIns.booleanType, null, Tail.RPARENTH, additionalData = IfConditionAdditionalData))
|
||||
|
||||
ifExpression.getThen() -> calculate(ifExpression).map { ExpectedInfo(it.filter, it.expectedName, Tail.ELSE) }
|
||||
ifExpression.then -> calculate(ifExpression).map { ExpectedInfo(it.filter, it.expectedName, Tail.ELSE) }
|
||||
|
||||
ifExpression.getElse() -> {
|
||||
ifExpression.`else` -> {
|
||||
val ifExpectedInfo = calculate(ifExpression)
|
||||
val thenType = ifExpression.getThen()?.let { bindingContext.getType(it) }
|
||||
val filteredInfo = if (thenType != null && !thenType.isError())
|
||||
val thenType = ifExpression.then?.let { bindingContext.getType(it) }
|
||||
val filteredInfo = if (thenType != null && !thenType.isError)
|
||||
ifExpectedInfo.filter { it.matchingSubstitutor(thenType) != null }
|
||||
else
|
||||
ifExpectedInfo
|
||||
@@ -442,11 +442,11 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForElvis(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val binaryExpression = expressionWithType.getParent() as? KtBinaryExpression
|
||||
val binaryExpression = expressionWithType.parent as? KtBinaryExpression
|
||||
if (binaryExpression != null) {
|
||||
val operationToken = binaryExpression.getOperationToken()
|
||||
if (operationToken == KtTokens.ELVIS && expressionWithType == binaryExpression.getRight()) {
|
||||
val leftExpression = binaryExpression.getLeft() ?: return null
|
||||
val operationToken = binaryExpression.operationToken
|
||||
if (operationToken == KtTokens.ELVIS && expressionWithType == binaryExpression.right) {
|
||||
val leftExpression = binaryExpression.left ?: return null
|
||||
val leftType = bindingContext.getType(leftExpression)
|
||||
val leftTypeNotNullable = leftType?.makeNotNullable()
|
||||
val expectedInfos = calculate(binaryExpression)
|
||||
@@ -486,10 +486,10 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForWhenEntryValue(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val condition = expressionWithType.getParent() as? KtWhenConditionWithExpression ?: return null
|
||||
val entry = condition.getParent() as KtWhenEntry
|
||||
val whenExpression = entry.getParent() as KtWhenExpression
|
||||
val subject = whenExpression.getSubjectExpression()
|
||||
val condition = expressionWithType.parent as? KtWhenConditionWithExpression ?: return null
|
||||
val entry = condition.parent as KtWhenEntry
|
||||
val whenExpression = entry.parent as KtWhenExpression
|
||||
val subject = whenExpression.subjectExpression
|
||||
if (subject != null) {
|
||||
val subjectType = bindingContext.getType(subject) ?: return null
|
||||
return listOf(ExpectedInfo(subjectType, null, null, additionalData = WhenEntryAdditionalData(whenWithSubject = true)))
|
||||
@@ -500,32 +500,32 @@ class ExpectedInfos(
|
||||
}
|
||||
|
||||
private fun calculateForExclOperand(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val prefixExpression = expressionWithType.getParent() as? KtPrefixExpression ?: return null
|
||||
if (prefixExpression.getOperationToken() != KtTokens.EXCL) return null
|
||||
val prefixExpression = expressionWithType.parent as? KtPrefixExpression ?: return null
|
||||
if (prefixExpression.operationToken != KtTokens.EXCL) return null
|
||||
return listOf(ExpectedInfo(resolutionFacade.moduleDescriptor.builtIns.booleanType, null, null))
|
||||
}
|
||||
|
||||
private fun calculateForInitializer(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val property = expressionWithType.getParent() as? KtProperty ?: return null
|
||||
if (expressionWithType != property.getInitializer()) return null
|
||||
val property = expressionWithType.parent as? KtProperty ?: return null
|
||||
if (expressionWithType != property.initializer) return null
|
||||
val propertyDescriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, property] as? VariableDescriptor ?: return null
|
||||
val expectedName = propertyDescriptor.name.asString()
|
||||
val expectedInfo = if (property.typeReference != null)
|
||||
ExpectedInfo(propertyDescriptor.getType(), expectedName, null)
|
||||
ExpectedInfo(propertyDescriptor.type, expectedName, null)
|
||||
else
|
||||
ExpectedInfo(ByTypeFilter.All, expectedName, null) // no explicit type - only expected name known
|
||||
return listOf(expectedInfo)
|
||||
}
|
||||
|
||||
private fun calculateForExpressionBody(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val declaration = expressionWithType.getParent() as? KtDeclarationWithBody ?: return null
|
||||
if (expressionWithType != declaration.getBodyExpression() || declaration.hasBlockBody()) return null
|
||||
val declaration = expressionWithType.parent as? KtDeclarationWithBody ?: return null
|
||||
if (expressionWithType != declaration.bodyExpression || declaration.hasBlockBody()) return null
|
||||
val descriptor = bindingContext[BindingContext.DECLARATION_TO_DESCRIPTOR, declaration] as? FunctionDescriptor ?: return null
|
||||
return functionReturnValueExpectedInfo(descriptor, expectType = declaration.hasDeclaredReturnType()).singletonOrEmptyList()
|
||||
}
|
||||
|
||||
private fun calculateForReturn(expressionWithType: KtExpression): Collection<ExpectedInfo>? {
|
||||
val returnExpression = expressionWithType.getParent() as? KtReturnExpression ?: return null
|
||||
val returnExpression = expressionWithType.parent as? KtReturnExpression ?: return null
|
||||
val descriptor = returnExpression.getTargetFunctionDescriptor(bindingContext) ?: return null
|
||||
return functionReturnValueExpectedInfo(descriptor, expectType = true).singletonOrEmptyList()
|
||||
}
|
||||
@@ -539,7 +539,7 @@ class ExpectedInfos(
|
||||
|
||||
is PropertyGetterDescriptor -> {
|
||||
if (descriptor !is PropertyGetterDescriptor) return null
|
||||
val property = descriptor.getCorrespondingProperty()
|
||||
val property = descriptor.correspondingProperty
|
||||
val expectedType = if (expectType) property.type else null
|
||||
ExpectedInfo.createForReturnValue(expectedType, property)
|
||||
}
|
||||
@@ -595,9 +595,9 @@ class ExpectedInfos(
|
||||
private fun expectedNameFromExpression(expression: KtExpression?): String? {
|
||||
return when (expression) {
|
||||
is KtSimpleNameExpression -> expression.getReferencedName()
|
||||
is KtQualifiedExpression -> expectedNameFromExpression(expression.getSelectorExpression())
|
||||
is KtCallExpression -> expectedNameFromExpression(expression.getCalleeExpression())
|
||||
is KtArrayAccessExpression -> expectedNameFromExpression(expression.getArrayExpression())?.unpluralize()
|
||||
is KtQualifiedExpression -> expectedNameFromExpression(expression.selectorExpression)
|
||||
is KtCallExpression -> expectedNameFromExpression(expression.calleeExpression)
|
||||
is KtArrayAccessExpression -> expectedNameFromExpression(expression.arrayExpression)?.unpluralize()
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,16 +41,16 @@ internal class HeuristicSignatures(
|
||||
private val project: Project,
|
||||
private val typeResolver: TypeResolver
|
||||
) {
|
||||
public fun correctedParameterType(function: FunctionDescriptor, parameter: ValueParameterDescriptor): KotlinType? {
|
||||
val parameterIndex = function.getValueParameters().indexOf(parameter)
|
||||
fun correctedParameterType(function: FunctionDescriptor, parameter: ValueParameterDescriptor): KotlinType? {
|
||||
val parameterIndex = function.valueParameters.indexOf(parameter)
|
||||
assert(parameterIndex >= 0)
|
||||
return correctedParameterType(function, parameterIndex)
|
||||
}
|
||||
|
||||
private fun correctedParameterType(function: FunctionDescriptor, parameterIndex: Int): KotlinType? {
|
||||
val ownerType = function.getDispatchReceiverParameter()?.getType() ?: return null
|
||||
val ownerType = function.dispatchReceiverParameter?.type ?: return null
|
||||
|
||||
val superFunctions = function.getOverriddenDescriptors()
|
||||
val superFunctions = function.overriddenDescriptors
|
||||
if (superFunctions.isNotEmpty()) {
|
||||
for (superFunction in superFunctions) {
|
||||
val correctedType = correctedParameterType(superFunction, parameterIndex) ?: continue
|
||||
@@ -60,12 +60,12 @@ internal class HeuristicSignatures(
|
||||
return null
|
||||
}
|
||||
else {
|
||||
val ownerClass = ownerType.getConstructor().getDeclarationDescriptor() ?: return null
|
||||
val ownerClass = ownerType.constructor.declarationDescriptor ?: return null
|
||||
val classFqName = DescriptorUtils.getFqName(ownerClass)
|
||||
if (!classFqName.isSafe()) return null
|
||||
val parameterTypes = signatures[classFqName.toSafe() to function.getName()] ?: return null
|
||||
if (!classFqName.isSafe) return null
|
||||
val parameterTypes = signatures[classFqName.toSafe() to function.name] ?: return null
|
||||
val typeStr = parameterTypes[parameterIndex]
|
||||
val typeParameters = ownerClass.getTypeConstructor().getParameters()
|
||||
val typeParameters = ownerClass.typeConstructor.parameters
|
||||
|
||||
val type = typeFromText(typeStr, typeParameters)
|
||||
|
||||
@@ -81,7 +81,7 @@ internal class HeuristicSignatures(
|
||||
typeParameters.forEach { addClassifierDescriptor(it) }
|
||||
}
|
||||
val type = typeResolver.resolveType(scope, typeRef, BindingTraceContext(), false)
|
||||
assert(!type.isError()) { "No type resolved from '$text'" }
|
||||
assert(!type.isError) { "No type resolved from '$text'" }
|
||||
return type
|
||||
}
|
||||
|
||||
|
||||
@@ -30,10 +30,10 @@ class ImportableFqNameClassifier(private val file: KtFile) {
|
||||
private val allUnderImports = HashSet<FqName>()
|
||||
|
||||
init {
|
||||
for (import in file.getImportDirectives()) {
|
||||
val importPath = import.getImportPath() ?: continue
|
||||
for (import in file.importDirectives) {
|
||||
val importPath = import.importPath ?: continue
|
||||
val fqName = importPath.fqnPart()
|
||||
if (importPath.isAllUnder()) {
|
||||
if (importPath.isAllUnder) {
|
||||
allUnderImports.add(fqName)
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -34,7 +34,7 @@ import org.jetbrains.kotlin.types.expressions.ForLoopConventionsChecker
|
||||
import org.jetbrains.kotlin.util.isValidOperator
|
||||
import java.util.*
|
||||
|
||||
public class IterableTypesDetection(
|
||||
class IterableTypesDetection(
|
||||
private val project: Project,
|
||||
private val forLoopConventionsChecker: ForLoopConventionsChecker
|
||||
) {
|
||||
@@ -42,7 +42,7 @@ public class IterableTypesDetection(
|
||||
private val iteratorName = Name.identifier("iterator")
|
||||
}
|
||||
|
||||
public fun createDetector(scope: LexicalScope): IterableTypesDetector {
|
||||
fun createDetector(scope: LexicalScope): IterableTypesDetector {
|
||||
return Detector(scope)
|
||||
}
|
||||
private inner class Detector(private val scope: LexicalScope): IterableTypesDetector {
|
||||
@@ -89,10 +89,10 @@ public class IterableTypesDetection(
|
||||
}
|
||||
}
|
||||
|
||||
public interface IterableTypesDetector {
|
||||
public fun isIterable(type: KotlinType, loopVarType: KotlinType? = null): Boolean
|
||||
interface IterableTypesDetector {
|
||||
fun isIterable(type: KotlinType, loopVarType: KotlinType? = null): Boolean
|
||||
|
||||
public fun isIterable(type: FuzzyType, loopVarType: KotlinType? = null): Boolean
|
||||
fun isIterable(type: FuzzyType, loopVarType: KotlinType? = null): Boolean
|
||||
|
||||
public fun elementType(type: KotlinType): FuzzyType?
|
||||
fun elementType(type: KotlinType): FuzzyType?
|
||||
}
|
||||
@@ -58,7 +58,7 @@ import org.jetbrains.kotlin.utils.addIfNotNull
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList
|
||||
import java.util.*
|
||||
|
||||
public class KotlinIndicesHelper(
|
||||
class KotlinIndicesHelper(
|
||||
private val resolutionFacade: ResolutionFacade,
|
||||
private val scope: GlobalSearchScope,
|
||||
visibilityFilter: (DeclarationDescriptor) -> Boolean,
|
||||
@@ -76,7 +76,7 @@ public class KotlinIndicesHelper(
|
||||
true
|
||||
}
|
||||
|
||||
public fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> {
|
||||
fun getTopLevelCallablesByName(name: String): Collection<CallableDescriptor> {
|
||||
val declarations = HashSet<KtCallableDeclaration>()
|
||||
declarations.addTopLevelNonExtensionCallablesByName(KotlinFunctionShortNameIndex.getInstance(), name)
|
||||
declarations.addTopLevelNonExtensionCallablesByName(KotlinPropertyShortNameIndex.getInstance(), name)
|
||||
@@ -89,10 +89,10 @@ public class KotlinIndicesHelper(
|
||||
index: StringStubIndexExtension<out KtCallableDeclaration>,
|
||||
name: String
|
||||
) {
|
||||
index.get(name, project, scope).filterTo(this) { it.getParent() is KtFile && it.getReceiverTypeReference() == null }
|
||||
index.get(name, project, scope).filterTo(this) { it.parent is KtFile && it.receiverTypeReference == null }
|
||||
}
|
||||
|
||||
public fun processTopLevelCallables(nameFilter: (String) -> Boolean, processor: (CallableDescriptor) -> Unit) {
|
||||
fun processTopLevelCallables(nameFilter: (String) -> Boolean, processor: (CallableDescriptor) -> Unit) {
|
||||
fun processIndex(index: StringStubIndexExtension<out KtCallableDeclaration>) {
|
||||
for (key in index.getAllKeys(project)) {
|
||||
ProgressManager.checkCanceled()
|
||||
@@ -114,7 +114,7 @@ public class KotlinIndicesHelper(
|
||||
processIndex(KotlinTopLevelPropertyFqnNameIndex.getInstance())
|
||||
}
|
||||
|
||||
public fun getCallableTopLevelExtensions(
|
||||
fun getCallableTopLevelExtensions(
|
||||
callTypeAndReceiver: CallTypeAndReceiver<*, *>,
|
||||
position: KtExpression,
|
||||
bindingContext: BindingContext,
|
||||
@@ -125,7 +125,7 @@ public class KotlinIndicesHelper(
|
||||
return getCallableTopLevelExtensions(callTypeAndReceiver, receiverTypes, nameFilter)
|
||||
}
|
||||
|
||||
public fun getCallableTopLevelExtensions(
|
||||
fun getCallableTopLevelExtensions(
|
||||
callTypeAndReceiver: CallTypeAndReceiver<*, *>,
|
||||
receiverTypes: Collection<KotlinType>,
|
||||
nameFilter: (String) -> Boolean
|
||||
@@ -161,9 +161,9 @@ public class KotlinIndicesHelper(
|
||||
}
|
||||
|
||||
private fun MutableCollection<String>.addTypeNames(type: KotlinType) {
|
||||
val constructor = type.getConstructor()
|
||||
addIfNotNull(constructor.getDeclarationDescriptor()?.getName()?.asString())
|
||||
constructor.getSupertypes().forEach { addTypeNames(it) }
|
||||
val constructor = type.constructor
|
||||
addIfNotNull(constructor.declarationDescriptor?.name?.asString())
|
||||
constructor.supertypes.forEach { addTypeNames(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -187,14 +187,14 @@ public class KotlinIndicesHelper(
|
||||
return result
|
||||
}
|
||||
|
||||
public fun getJvmClassesByName(name: String): Collection<ClassDescriptor> {
|
||||
fun getJvmClassesByName(name: String): Collection<ClassDescriptor> {
|
||||
return PsiShortNamesCache.getInstance(project).getClassesByName(name, scope)
|
||||
.mapNotNull { it.resolveToDescriptor(resolutionFacade) }
|
||||
.filter(descriptorFilter)
|
||||
.toSet()
|
||||
}
|
||||
|
||||
public fun getKotlinClasses(nameFilter: (String) -> Boolean, kindFilter: (ClassKind) -> Boolean): Collection<ClassDescriptor> {
|
||||
fun getKotlinClasses(nameFilter: (String) -> Boolean, kindFilter: (ClassKind) -> Boolean): Collection<ClassDescriptor> {
|
||||
return KotlinFullClassNameIndex.getInstance().getAllKeys(project).asSequence()
|
||||
.map { FqName(it) }
|
||||
.filter {
|
||||
@@ -214,11 +214,11 @@ public class KotlinIndicesHelper(
|
||||
}
|
||||
|
||||
// Note: Can't search with psi element as analyzer could be built over temp files
|
||||
return ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(moduleDescriptor, classFQName) { kindFilter(it.getKind()) }
|
||||
return ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(moduleDescriptor, classFQName) { kindFilter(it.kind) }
|
||||
.filter(descriptorFilter)
|
||||
}
|
||||
|
||||
public fun processObjectMembers(
|
||||
fun processObjectMembers(
|
||||
descriptorKindFilter: DescriptorKindFilter,
|
||||
nameFilter: (String) -> Boolean,
|
||||
filter: (KtCallableDeclaration, KtObjectDeclaration) -> Boolean,
|
||||
@@ -251,7 +251,7 @@ public class KotlinIndicesHelper(
|
||||
}
|
||||
}
|
||||
|
||||
public fun processJavaStaticMembers(
|
||||
fun processJavaStaticMembers(
|
||||
descriptorKindFilter: DescriptorKindFilter,
|
||||
nameFilter: (String) -> Boolean,
|
||||
processor: (DeclarationDescriptor) -> Unit
|
||||
@@ -308,7 +308,7 @@ public class KotlinIndicesHelper(
|
||||
}
|
||||
|
||||
private fun KtCallableDeclaration.resolveToDescriptorsWithHack(): Collection<CallableDescriptor> {
|
||||
if (getContainingKtFile().isCompiled()) { //TODO: it's temporary while resolveToDescriptor does not work for compiled declarations
|
||||
if (getContainingKtFile().isCompiled) { //TODO: it's temporary while resolveToDescriptor does not work for compiled declarations
|
||||
return resolutionFacade.resolveImportReference(moduleDescriptor, fqName!!).filterIsInstance<CallableDescriptor>()
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -33,8 +33,8 @@ import org.jetbrains.kotlin.types.typeUtil.builtIns
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeSmart
|
||||
import java.util.*
|
||||
|
||||
public object KotlinNameSuggester {
|
||||
public fun suggestNamesByExpressionAndType(
|
||||
object KotlinNameSuggester {
|
||||
fun suggestNamesByExpressionAndType(
|
||||
expression: KtExpression,
|
||||
type: KotlinType?,
|
||||
bindingContext: BindingContext?,
|
||||
@@ -56,7 +56,7 @@ public object KotlinNameSuggester {
|
||||
return result
|
||||
}
|
||||
|
||||
public fun suggestNamesByType(type: KotlinType, validator: (String) -> Boolean, defaultName: String? = null): List<String> {
|
||||
fun suggestNamesByType(type: KotlinType, validator: (String) -> Boolean, defaultName: String? = null): List<String> {
|
||||
val result = ArrayList<String>()
|
||||
|
||||
result.addNamesByType(type, validator)
|
||||
@@ -68,7 +68,7 @@ public object KotlinNameSuggester {
|
||||
return result
|
||||
}
|
||||
|
||||
public fun suggestNamesByExpressionOnly(
|
||||
fun suggestNamesByExpressionOnly(
|
||||
expression: KtExpression,
|
||||
bindingContext: BindingContext?,
|
||||
validator: (String) -> Boolean, defaultName: String? = null): List<String> {
|
||||
@@ -83,7 +83,7 @@ public object KotlinNameSuggester {
|
||||
return result
|
||||
}
|
||||
|
||||
public fun suggestIterationVariableNames(
|
||||
fun suggestIterationVariableNames(
|
||||
collection: KtExpression,
|
||||
elementType: KotlinType,
|
||||
bindingContext: BindingContext?,
|
||||
@@ -105,7 +105,7 @@ public object KotlinNameSuggester {
|
||||
|
||||
private val COMMON_TYPE_PARAMETER_NAMES = listOf("T", "U", "V", "W", "X", "Y", "Z")
|
||||
|
||||
public fun suggestNamesForTypeParameters(count: Int, validator: (String) -> Boolean): List<String> {
|
||||
fun suggestNamesForTypeParameters(count: Int, validator: (String) -> Boolean): List<String> {
|
||||
val result = ArrayList<String>()
|
||||
for (i in 0..count - 1) {
|
||||
result.add(suggestNameByMultipleNames(COMMON_TYPE_PARAMETER_NAMES, validator))
|
||||
@@ -118,7 +118,7 @@ public object KotlinNameSuggester {
|
||||
* @param name to check it in scope
|
||||
* @return name or nameI, where I is number
|
||||
*/
|
||||
public fun suggestNameByName(name: String, validator: (String) -> Boolean): String {
|
||||
fun suggestNameByName(name: String, validator: (String) -> Boolean): String {
|
||||
if (validator(name)) return name
|
||||
var i = 1
|
||||
while (!validator(name + i)) {
|
||||
@@ -134,7 +134,7 @@ public object KotlinNameSuggester {
|
||||
* @param names to check it in scope
|
||||
* @return name or nameI, where name is one of variants and I is a number
|
||||
*/
|
||||
public fun suggestNameByMultipleNames(names: Collection<String>, validator: (String) -> Boolean): String {
|
||||
fun suggestNameByMultipleNames(names: Collection<String>, validator: (String) -> Boolean): String {
|
||||
var i = 0
|
||||
while (true) {
|
||||
for (name in names) {
|
||||
@@ -151,66 +151,66 @@ public object KotlinNameSuggester {
|
||||
val typeChecker = KotlinTypeChecker.DEFAULT
|
||||
if (ErrorUtils.containsErrorType(type)) return
|
||||
|
||||
if (typeChecker.equalTypes(builtIns.getBooleanType(), type)) {
|
||||
if (typeChecker.equalTypes(builtIns.booleanType, type)) {
|
||||
addName("b", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getIntType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.intType, type)) {
|
||||
addName("i", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getByteType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.byteType, type)) {
|
||||
addName("byte", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getLongType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.longType, type)) {
|
||||
addName("l", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getFloatType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.floatType, type)) {
|
||||
addName("fl", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getDoubleType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.doubleType, type)) {
|
||||
addName("d", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getShortType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.shortType, type)) {
|
||||
addName("sh", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getCharType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.charType, type)) {
|
||||
addName("c", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getStringType(), type)) {
|
||||
else if (typeChecker.equalTypes(builtIns.stringType, type)) {
|
||||
addName("s", validator)
|
||||
}
|
||||
else if (KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) {
|
||||
val elementType = builtIns.getArrayElementType(type)
|
||||
if (typeChecker.equalTypes(builtIns.getBooleanType(), elementType)) {
|
||||
if (typeChecker.equalTypes(builtIns.booleanType, elementType)) {
|
||||
addName("booleans", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getIntType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.intType, elementType)) {
|
||||
addName("ints", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getByteType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.byteType, elementType)) {
|
||||
addName("bytes", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getLongType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.longType, elementType)) {
|
||||
addName("longs", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getFloatType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.floatType, elementType)) {
|
||||
addName("floats", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getDoubleType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.doubleType, elementType)) {
|
||||
addName("doubles", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getShortType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.shortType, elementType)) {
|
||||
addName("shorts", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getCharType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.charType, elementType)) {
|
||||
addName("chars", validator)
|
||||
}
|
||||
else if (typeChecker.equalTypes(builtIns.getStringType(), elementType)) {
|
||||
else if (typeChecker.equalTypes(builtIns.stringType, elementType)) {
|
||||
addName("strings", validator)
|
||||
}
|
||||
else {
|
||||
val classDescriptor = TypeUtils.getClassDescriptor(elementType)
|
||||
if (classDescriptor != null) {
|
||||
val className = classDescriptor.getName()
|
||||
val className = classDescriptor.name
|
||||
addName("arrayOf" + StringUtil.capitalize(className.asString()) + "s", validator)
|
||||
}
|
||||
}
|
||||
@@ -219,10 +219,10 @@ public object KotlinNameSuggester {
|
||||
addName("function", validator)
|
||||
}
|
||||
else {
|
||||
val descriptor = type.getConstructor().getDeclarationDescriptor()
|
||||
val descriptor = type.constructor.declarationDescriptor
|
||||
if (descriptor != null) {
|
||||
val className = descriptor.getName()
|
||||
if (!className.isSpecial()) {
|
||||
val className = descriptor.name
|
||||
if (!className.isSpecial) {
|
||||
addCamelNames(className.asString(), validator)
|
||||
}
|
||||
}
|
||||
@@ -231,7 +231,7 @@ public object KotlinNameSuggester {
|
||||
|
||||
private val ACCESSOR_PREFIXES = arrayOf("get", "is", "set")
|
||||
|
||||
public fun getCamelNames(name: String, validator: (String) -> Boolean, startLowerCase: Boolean): List<String> {
|
||||
fun getCamelNames(name: String, validator: (String) -> Boolean, startLowerCase: Boolean): List<String> {
|
||||
val result = ArrayList<String>()
|
||||
result.addCamelNames(name, validator, startLowerCase)
|
||||
return result
|
||||
@@ -331,13 +331,13 @@ public object KotlinNameSuggester {
|
||||
add(suggestNameByName(correctedName, validator))
|
||||
}
|
||||
|
||||
public fun isIdentifier(name: String?): Boolean {
|
||||
fun isIdentifier(name: String?): Boolean {
|
||||
if (name == null || name.isEmpty()) return false
|
||||
|
||||
val lexer = KotlinLexer()
|
||||
lexer.start(name, 0, name.length)
|
||||
if (lexer.getTokenType() !== KtTokens.IDENTIFIER) return false
|
||||
if (lexer.tokenType !== KtTokens.IDENTIFIER) return false
|
||||
lexer.advance()
|
||||
return lexer.getTokenType() == null
|
||||
return lexer.tokenType == null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import java.util.*
|
||||
|
||||
public class CollectingNameValidator @JvmOverloads constructor(
|
||||
class CollectingNameValidator @JvmOverloads constructor(
|
||||
existingNames: Collection<String> = Collections.emptySet(),
|
||||
private val filter: (String) -> Boolean = { true }
|
||||
): (String) -> Boolean {
|
||||
@@ -52,24 +52,24 @@ public class CollectingNameValidator @JvmOverloads constructor(
|
||||
return false
|
||||
}
|
||||
|
||||
public fun addName(name: String) {
|
||||
fun addName(name: String) {
|
||||
existingNames.add(name)
|
||||
}
|
||||
}
|
||||
|
||||
public class NewDeclarationNameValidator(
|
||||
class NewDeclarationNameValidator(
|
||||
private val visibleDeclarationsContext: KtElement?,
|
||||
private val checkDeclarationsIn: Sequence<PsiElement>,
|
||||
private val target: NewDeclarationNameValidator.Target
|
||||
) : (String) -> Boolean {
|
||||
|
||||
public constructor(container: PsiElement, anchor: PsiElement?, target: NewDeclarationNameValidator.Target)
|
||||
constructor(container: PsiElement, anchor: PsiElement?, target: NewDeclarationNameValidator.Target)
|
||||
: this(
|
||||
(anchor ?: container).parentsWithSelf.firstIsInstanceOrNull<KtElement>(),
|
||||
anchor?.siblings() ?: container.allChildren,
|
||||
target)
|
||||
|
||||
public enum class Target {
|
||||
enum class Target {
|
||||
VARIABLES,
|
||||
FUNCTIONS_AND_CLASSES
|
||||
}
|
||||
@@ -106,8 +106,8 @@ public class NewDeclarationNameValidator(
|
||||
}
|
||||
|
||||
private fun KtNamedDeclaration.isConflicting(name: Name): Boolean {
|
||||
if (getNameAsName() != name) return false
|
||||
if (this is KtCallableDeclaration && getReceiverTypeReference() != null) return false
|
||||
if (nameAsName != name) return false
|
||||
if (this is KtCallableDeclaration && receiverTypeReference != null) return false
|
||||
return when(target) {
|
||||
Target.VARIABLES -> this is KtVariableDeclaration
|
||||
Target.FUNCTIONS_AND_CLASSES -> this is KtNamedFunction || this is KtClassOrObject
|
||||
|
||||
@@ -37,23 +37,23 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
|
||||
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import java.util.*
|
||||
|
||||
public object OptionalParametersHelper {
|
||||
public fun detectArgumentsToDropForDefaults(
|
||||
object OptionalParametersHelper {
|
||||
fun detectArgumentsToDropForDefaults(
|
||||
resolvedCall: ResolvedCall<out CallableDescriptor>,
|
||||
project: Project,
|
||||
canDrop: (ValueArgument) -> Boolean = { true }
|
||||
): Collection<ValueArgument> {
|
||||
if (!resolvedCall.isReallySuccess()) return emptyList()
|
||||
val descriptor = resolvedCall.getResultingDescriptor()
|
||||
val descriptor = resolvedCall.resultingDescriptor
|
||||
|
||||
val parameterToDefaultValue = descriptor.getValueParameters()
|
||||
val parameterToDefaultValue = descriptor.valueParameters
|
||||
.mapNotNull { parameter -> defaultParameterValue(parameter, project)?.let { parameter to it } }
|
||||
.toMap()
|
||||
if (parameterToDefaultValue.isEmpty()) return emptyList()
|
||||
|
||||
//TODO: drop functional literal out of parenthesis too
|
||||
|
||||
val arguments = resolvedCall.getCall().getValueArgumentsInParentheses()
|
||||
val arguments = resolvedCall.call.getValueArgumentsInParentheses()
|
||||
val argumentsToDrop = ArrayList<ValueArgument>()
|
||||
for (argument in arguments.asReversed()) {
|
||||
if (!canDrop(argument) || !argument.matchesDefault(resolvedCall, parameterToDefaultValue)) {
|
||||
@@ -70,7 +70,7 @@ public object OptionalParametersHelper {
|
||||
val defaultValue = parameterToDefaultValue[parameter] ?: return false
|
||||
val expression = defaultValue.substituteArguments(resolvedCall)
|
||||
val argumentExpression = getArgumentExpression()!!
|
||||
return argumentExpression.getText() == expression.getText() //TODO
|
||||
return argumentExpression.text == expression.text //TODO
|
||||
}
|
||||
|
||||
private fun DefaultValue.substituteArguments(resolvedCall: ResolvedCall<out CallableDescriptor>): KtExpression {
|
||||
@@ -79,9 +79,9 @@ public object OptionalParametersHelper {
|
||||
val key = Key<KtExpression>("SUBSTITUTION")
|
||||
|
||||
for ((parameter, usages) in parameterUsages) {
|
||||
val resolvedArgument = resolvedCall.getValueArguments()[parameter]!!
|
||||
val resolvedArgument = resolvedCall.valueArguments[parameter]!!
|
||||
if (resolvedArgument is ExpressionValueArgument) {
|
||||
val argument = resolvedArgument.getValueArgument()!!.getArgumentExpression()!!
|
||||
val argument = resolvedArgument.valueArgument!!.getArgumentExpression()!!
|
||||
usages.forEach { it.putCopyableUserData(key, argument) }
|
||||
}
|
||||
//TODO: vararg
|
||||
@@ -109,31 +109,31 @@ public object OptionalParametersHelper {
|
||||
return expressionCopy
|
||||
}
|
||||
|
||||
public data class DefaultValue(
|
||||
public val expression: KtExpression,
|
||||
public val parameterUsages: Map<ValueParameterDescriptor, Collection<KtExpression>>
|
||||
data class DefaultValue(
|
||||
val expression: KtExpression,
|
||||
val parameterUsages: Map<ValueParameterDescriptor, Collection<KtExpression>>
|
||||
)
|
||||
|
||||
public fun defaultParameterValueExpression(parameter: ValueParameterDescriptor, project: Project): KtExpression? {
|
||||
fun defaultParameterValueExpression(parameter: ValueParameterDescriptor, project: Project): KtExpression? {
|
||||
if (!parameter.hasDefaultValue()) return null
|
||||
|
||||
if (!parameter.declaresDefaultValue()) {
|
||||
val overridden = parameter.getOverriddenDescriptors().firstOrNull { it.hasDefaultValue() } ?: return null
|
||||
val overridden = parameter.overriddenDescriptors.firstOrNull { it.hasDefaultValue() } ?: return null
|
||||
return defaultParameterValueExpression(overridden, project)
|
||||
}
|
||||
|
||||
//TODO: parameter in overriding method!
|
||||
//TODO: it's a temporary code while we don't have default values accessible from descriptors
|
||||
val declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, parameter)?.getNavigationElement() as? KtParameter
|
||||
val declaration = DescriptorToSourceUtilsIde.getAnyDeclaration(project, parameter)?.navigationElement as? KtParameter
|
||||
return declaration?.defaultValue
|
||||
}
|
||||
|
||||
//TODO: handle imports
|
||||
//TODO: handle implicit receivers
|
||||
public fun defaultParameterValue(parameter: ValueParameterDescriptor, project: Project): DefaultValue? {
|
||||
fun defaultParameterValue(parameter: ValueParameterDescriptor, project: Project): DefaultValue? {
|
||||
val expression = defaultParameterValueExpression(parameter, project) ?: return null
|
||||
|
||||
val allParameters = parameter.getContainingDeclaration().getValueParameters().toSet()
|
||||
val allParameters = parameter.containingDeclaration.valueParameters.toSet()
|
||||
|
||||
val parameterUsages = HashMap<ValueParameterDescriptor, MutableCollection<KtExpression>>()
|
||||
|
||||
|
||||
@@ -43,10 +43,10 @@ internal class TypesWithContainsDetector(
|
||||
|
||||
private val typesWithExtensionContains: Collection<KotlinType> = scope
|
||||
.collectFunctions(containsName, NoLookupLocation.FROM_IDE)
|
||||
.filter { it.getExtensionReceiverParameter() != null && isGoodContainsFunction(it, listOf()) }
|
||||
.map { it.getExtensionReceiverParameter()!!.getType() }
|
||||
.filter { it.extensionReceiverParameter != null && isGoodContainsFunction(it, listOf()) }
|
||||
.map { it.extensionReceiverParameter!!.type }
|
||||
|
||||
public fun hasContains(type: FuzzyType): Boolean {
|
||||
fun hasContains(type: FuzzyType): Boolean {
|
||||
return cache.getOrPut(type, { hasContainsNoCache(type) })
|
||||
}
|
||||
|
||||
@@ -57,10 +57,10 @@ internal class TypesWithContainsDetector(
|
||||
}
|
||||
|
||||
private fun isGoodContainsFunction(function: FunctionDescriptor, freeTypeParams: Collection<TypeParameterDescriptor>): Boolean {
|
||||
if (!TypeUtils.equalTypes(function.getReturnType()!!, booleanType)) return false
|
||||
val parameter = function.getValueParameters().singleOrNull() ?: return false
|
||||
val parameterType = heuristicSignatures.correctedParameterType(function, parameter) ?: parameter.getType()
|
||||
val fuzzyParameterType = FuzzyType(parameterType, function.getTypeParameters() + freeTypeParams)
|
||||
if (!TypeUtils.equalTypes(function.returnType!!, booleanType)) return false
|
||||
val parameter = function.valueParameters.singleOrNull() ?: return false
|
||||
val parameterType = heuristicSignatures.correctedParameterType(function, parameter) ?: parameter.type
|
||||
val fuzzyParameterType = FuzzyType(parameterType, function.typeParameters + freeTypeParams)
|
||||
return fuzzyParameterType.checkIsSuperTypeOf(argumentType) != null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,16 +47,16 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
|
||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||
import java.util.*
|
||||
|
||||
public fun Call.mapArgumentsToParameters(targetDescriptor: CallableDescriptor): Map<ValueArgument, ValueParameterDescriptor> {
|
||||
val parameters = targetDescriptor.getValueParameters()
|
||||
fun Call.mapArgumentsToParameters(targetDescriptor: CallableDescriptor): Map<ValueArgument, ValueParameterDescriptor> {
|
||||
val parameters = targetDescriptor.valueParameters
|
||||
if (parameters.isEmpty()) return emptyMap()
|
||||
|
||||
val map = HashMap<ValueArgument, ValueParameterDescriptor>()
|
||||
val parametersByName = parameters.toMapBy { it.getName() }
|
||||
val parametersByName = parameters.toMapBy { it.name }
|
||||
|
||||
var positionalArgumentIndex: Int? = 0
|
||||
|
||||
for (argument in getValueArguments()) {
|
||||
for (argument in valueArguments) {
|
||||
if (argument is LambdaArgument) {
|
||||
map[argument] = parameters.last()
|
||||
}
|
||||
@@ -88,22 +88,22 @@ public fun Call.mapArgumentsToParameters(targetDescriptor: CallableDescriptor):
|
||||
return map
|
||||
}
|
||||
|
||||
public fun ImplicitReceiver.asExpression(resolutionScope: LexicalScope, psiFactory: KtPsiFactory): KtExpression? {
|
||||
fun ImplicitReceiver.asExpression(resolutionScope: LexicalScope, psiFactory: KtPsiFactory): KtExpression? {
|
||||
val expressionFactory = resolutionScope.getImplicitReceiversWithInstanceToExpression()
|
||||
.entries
|
||||
.firstOrNull { it.key.getContainingDeclaration() == this.declarationDescriptor }
|
||||
.firstOrNull { it.key.containingDeclaration == this.declarationDescriptor }
|
||||
?.value ?: return null
|
||||
return expressionFactory.createExpression(psiFactory)
|
||||
}
|
||||
|
||||
public fun KtImportDirective.targetDescriptors(resolutionFacade: ResolutionFacade = this.getResolutionFacade()): Collection<DeclarationDescriptor> {
|
||||
fun KtImportDirective.targetDescriptors(resolutionFacade: ResolutionFacade = this.getResolutionFacade()): Collection<DeclarationDescriptor> {
|
||||
// For codeFragments imports are created in dummy file
|
||||
if (this.getContainingKtFile().doNotAnalyze != null) return emptyList()
|
||||
val nameExpression = importedReference?.getQualifiedElementSelector() as? KtSimpleNameExpression ?: return emptyList()
|
||||
return nameExpression.mainReference.resolveToDescriptors(resolutionFacade.analyze(nameExpression))
|
||||
}
|
||||
|
||||
public fun Call.resolveCandidates(
|
||||
fun Call.resolveCandidates(
|
||||
bindingContext: BindingContext,
|
||||
resolutionFacade: ResolutionFacade,
|
||||
expectedType: KotlinType = expectedType(this, bindingContext),
|
||||
|
||||
+7
-7
@@ -23,15 +23,15 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
public interface DeclarationLookupObject : Iconable {
|
||||
public val psiElement: PsiElement?
|
||||
public val descriptor: DeclarationDescriptor?
|
||||
public val name: Name?
|
||||
public val importableFqName: FqName?
|
||||
public val isDeprecated: Boolean
|
||||
interface DeclarationLookupObject : Iconable {
|
||||
val psiElement: PsiElement?
|
||||
val descriptor: DeclarationDescriptor?
|
||||
val name: Name?
|
||||
val importableFqName: FqName?
|
||||
val isDeprecated: Boolean
|
||||
}
|
||||
|
||||
public data class PackageLookupObject(val fqName: FqName) : DeclarationLookupObject {
|
||||
data class PackageLookupObject(val fqName: FqName) : DeclarationLookupObject {
|
||||
override val psiElement: PsiElement? get() = null
|
||||
|
||||
override val descriptor: DeclarationDescriptor? get() = null
|
||||
|
||||
@@ -85,7 +85,7 @@ private fun compareDescriptorsText(project: Project, d1: DeclarationDescriptor,
|
||||
return false
|
||||
}
|
||||
|
||||
public fun compareDescriptors(project: Project, currentDescriptor: DeclarationDescriptor?, originalDescriptor: DeclarationDescriptor?): Boolean {
|
||||
fun compareDescriptors(project: Project, currentDescriptor: DeclarationDescriptor?, originalDescriptor: DeclarationDescriptor?): Boolean {
|
||||
if (currentDescriptor == originalDescriptor) return true
|
||||
if (currentDescriptor == null || originalDescriptor == null) return false
|
||||
|
||||
@@ -110,7 +110,7 @@ public fun compareDescriptors(project: Project, currentDescriptor: DeclarationDe
|
||||
return false
|
||||
}
|
||||
|
||||
public fun Visibility.toKeywordToken(): KtModifierKeywordToken {
|
||||
fun Visibility.toKeywordToken(): KtModifierKeywordToken {
|
||||
val normalized = normalize()
|
||||
when (normalized) {
|
||||
Visibilities.PUBLIC -> return KtTokens.PUBLIC_KEYWORD
|
||||
|
||||
+2
-2
@@ -29,7 +29,7 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtEnumEntry
|
||||
import org.jetbrains.kotlin.resolve.OverrideResolver
|
||||
|
||||
public open class ImplementMembersHandler : OverrideImplementMembersHandler(), IntentionAction {
|
||||
open class ImplementMembersHandler : OverrideImplementMembersHandler(), IntentionAction {
|
||||
override fun collectMembersToGenerate(descriptor: ClassDescriptor, project: Project): Collection<OverrideMemberChooserObject> {
|
||||
return OverrideResolver.getMissingImplementations(descriptor)
|
||||
.map { OverrideMemberChooserObject.create(project, it, it, OverrideMemberChooserObject.BodyType.EMPTY) }
|
||||
@@ -45,7 +45,7 @@ public open class ImplementMembersHandler : OverrideImplementMembersHandler(), I
|
||||
override fun isAvailable(project: Project, editor: Editor, file: PsiFile) = isValidFor(editor, file)
|
||||
}
|
||||
|
||||
public class ImplementAsConstructorParameter : ImplementMembersHandler() {
|
||||
class ImplementAsConstructorParameter : ImplementMembersHandler() {
|
||||
override fun getText() = "Implement as constructor parameters"
|
||||
|
||||
override fun isValidForClass(classOrObject: KtClassOrObject): Boolean {
|
||||
|
||||
+4
-4
@@ -32,9 +32,9 @@ import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
|
||||
public abstract class OverrideImplementMembersHandler : LanguageCodeInsightActionHandler {
|
||||
abstract class OverrideImplementMembersHandler : LanguageCodeInsightActionHandler {
|
||||
|
||||
public fun collectMembersToGenerate(classOrObject: KtClassOrObject): Collection<OverrideMemberChooserObject> {
|
||||
fun collectMembersToGenerate(classOrObject: KtClassOrObject): Collection<OverrideMemberChooserObject> {
|
||||
val descriptor = classOrObject.resolveToDescriptor() as? ClassDescriptor ?: return emptySet()
|
||||
return collectMembersToGenerate(descriptor, classOrObject.project)
|
||||
}
|
||||
@@ -62,7 +62,7 @@ public abstract class OverrideImplementMembersHandler : LanguageCodeInsightActio
|
||||
|
||||
protected abstract fun getNoMembersFoundHint(): String
|
||||
|
||||
public fun invoke(project: Project, editor: Editor, file: PsiFile, implementAll: Boolean) {
|
||||
fun invoke(project: Project, editor: Editor, file: PsiFile, implementAll: Boolean) {
|
||||
val elementAtCaret = file.findElementAt(editor.caretModel.offset)
|
||||
val classOrObject = elementAtCaret?.getNonStrictParentOfType<KtClassOrObject>()!!
|
||||
|
||||
@@ -93,7 +93,7 @@ public abstract class OverrideImplementMembersHandler : LanguageCodeInsightActio
|
||||
override fun startInWriteAction(): Boolean = false
|
||||
|
||||
companion object {
|
||||
public fun generateMembers(editor: Editor?, classOrObject: KtClassOrObject, selectedElements: Collection<OverrideMemberChooserObject>) {
|
||||
fun generateMembers(editor: Editor?, classOrObject: KtClassOrObject, selectedElements: Collection<OverrideMemberChooserObject>) {
|
||||
val project = classOrObject.project
|
||||
insertMembersAfter(editor, classOrObject, selectedElements.map { it.generateMember(project) })
|
||||
}
|
||||
|
||||
+1
-1
@@ -143,7 +143,7 @@ private fun generateFunction(project: Project, descriptor: FunctionDescriptor, b
|
||||
return KtPsiFactory(project).createFunction(OVERRIDE_RENDERER.render(newDescriptor) + body)
|
||||
}
|
||||
|
||||
public fun generateUnsupportedOrSuperCall(descriptor: CallableMemberDescriptor, bodyType: OverrideMemberChooserObject.BodyType): String {
|
||||
fun generateUnsupportedOrSuperCall(descriptor: CallableMemberDescriptor, bodyType: OverrideMemberChooserObject.BodyType): String {
|
||||
if (bodyType == OverrideMemberChooserObject.BodyType.EMPTY) {
|
||||
return "throw UnsupportedOperationException()"
|
||||
}
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
|
||||
import java.util.*
|
||||
|
||||
public class OverrideMembersHandler(private val preferConstructorParameters: Boolean = false) : OverrideImplementMembersHandler() {
|
||||
class OverrideMembersHandler(private val preferConstructorParameters: Boolean = false) : OverrideImplementMembersHandler() {
|
||||
override fun collectMembersToGenerate(descriptor: ClassDescriptor, project: Project): Collection<OverrideMemberChooserObject> {
|
||||
val result = ArrayList<OverrideMemberChooserObject>()
|
||||
for (member in descriptor.unsubstitutedMemberScope.getContributedDescriptors()) {
|
||||
|
||||
@@ -23,11 +23,11 @@ import com.intellij.psi.PsiPackage
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
|
||||
public fun PsiDirectory.getPackage(): PsiPackage? = JavaDirectoryService.getInstance()!!.getPackage(this)
|
||||
fun PsiDirectory.getPackage(): PsiPackage? = JavaDirectoryService.getInstance()!!.getPackage(this)
|
||||
|
||||
public fun PsiFile.getFqNameByDirectory(): FqName {
|
||||
val qualifiedNameByDirectory = getParent()?.getPackage()?.getQualifiedName()
|
||||
fun PsiFile.getFqNameByDirectory(): FqName {
|
||||
val qualifiedNameByDirectory = parent?.getPackage()?.qualifiedName
|
||||
return qualifiedNameByDirectory?.let { FqName(it) } ?: FqName.ROOT
|
||||
}
|
||||
|
||||
public fun KtFile.packageMatchesDirectory(): Boolean = getPackageFqName() == getFqNameByDirectory()
|
||||
fun KtFile.packageMatchesDirectory(): Boolean = packageFqName == getFqNameByDirectory()
|
||||
@@ -20,17 +20,17 @@ import com.intellij.psi.filters.position.PositionElementFilter
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
|
||||
public class FirstChildInParentFilter(val level: Int = 1) : PositionElementFilter() {
|
||||
class FirstChildInParentFilter(val level: Int = 1) : PositionElementFilter() {
|
||||
override fun isAcceptable(element: Any?, context: PsiElement?): Boolean {
|
||||
if (element !is PsiElement) return false
|
||||
|
||||
var parent: PsiElement? = element
|
||||
for (i in 1..level) {
|
||||
if (parent == null) break
|
||||
parent = parent.getContext()
|
||||
parent = parent.context
|
||||
}
|
||||
|
||||
return (parent != null) && PsiTreeUtil.isAncestor(parent.getFirstChild(), element, true)
|
||||
return (parent != null) && PsiTreeUtil.isAncestor(parent.firstChild, element, true)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -33,34 +33,33 @@ import org.jetbrains.kotlin.resolve.OverridingUtil
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getValueArgumentsInParentheses
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public inline fun <reified T: PsiElement> PsiElement.replaced(newElement: T): T {
|
||||
inline fun <reified T: PsiElement> PsiElement.replaced(newElement: T): T {
|
||||
val result = replace(newElement)
|
||||
return if (result is T)
|
||||
result
|
||||
else
|
||||
(result as KtParenthesizedExpression).getExpression() as T
|
||||
(result as KtParenthesizedExpression).expression as T
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
public fun <T: PsiElement> T.copied(): T = copy() as T
|
||||
@Suppress("UNCHECKED_CAST") fun <T: PsiElement> T.copied(): T = copy() as T
|
||||
|
||||
public fun KtLambdaArgument.moveInsideParentheses(bindingContext: BindingContext): KtCallExpression {
|
||||
fun KtLambdaArgument.moveInsideParentheses(bindingContext: BindingContext): KtCallExpression {
|
||||
return moveInsideParenthesesAndReplaceWith(this.getArgumentExpression(), bindingContext)
|
||||
}
|
||||
|
||||
public fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
replacement: KtExpression,
|
||||
bindingContext: BindingContext
|
||||
): KtCallExpression = moveInsideParenthesesAndReplaceWith(replacement, getLambdaArgumentName(bindingContext))
|
||||
|
||||
public fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
replacement: KtExpression,
|
||||
functionLiteralArgumentName: Name?
|
||||
): KtCallExpression {
|
||||
val oldCallExpression = getParent() as KtCallExpression
|
||||
val oldCallExpression = parent as KtCallExpression
|
||||
val newCallExpression = oldCallExpression.copy() as KtCallExpression
|
||||
|
||||
val psiFactory = KtPsiFactory(getProject())
|
||||
val psiFactory = KtPsiFactory(project)
|
||||
val argument = if (newCallExpression.getValueArgumentsInParentheses().any { it.isNamed() }) {
|
||||
psiFactory.createArgument(replacement, functionLiteralArgumentName)
|
||||
}
|
||||
@@ -68,13 +67,13 @@ public fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
psiFactory.createArgument(replacement)
|
||||
}
|
||||
|
||||
val functionLiteralArgument = newCallExpression.getLambdaArguments().firstOrNull()!!
|
||||
val valueArgumentList = newCallExpression.getValueArgumentList() ?: psiFactory.createCallArguments("()")
|
||||
val functionLiteralArgument = newCallExpression.lambdaArguments.firstOrNull()!!
|
||||
val valueArgumentList = newCallExpression.valueArgumentList ?: psiFactory.createCallArguments("()")
|
||||
|
||||
valueArgumentList.addArgument(argument)
|
||||
|
||||
(functionLiteralArgument.getPrevSibling() as? PsiWhiteSpace)?.delete()
|
||||
if (newCallExpression.getValueArgumentList() != null) {
|
||||
(functionLiteralArgument.prevSibling as? PsiWhiteSpace)?.delete()
|
||||
if (newCallExpression.valueArgumentList != null) {
|
||||
functionLiteralArgument.delete()
|
||||
}
|
||||
else {
|
||||
@@ -83,17 +82,17 @@ public fun KtLambdaArgument.moveInsideParenthesesAndReplaceWith(
|
||||
return oldCallExpression.replace(newCallExpression) as KtCallExpression
|
||||
}
|
||||
|
||||
public fun KtCallExpression.moveFunctionLiteralOutsideParentheses() {
|
||||
assert(getLambdaArguments().isEmpty())
|
||||
val argumentList = getValueArgumentList()!!
|
||||
val argument = argumentList.getArguments().last()
|
||||
fun KtCallExpression.moveFunctionLiteralOutsideParentheses() {
|
||||
assert(lambdaArguments.isEmpty())
|
||||
val argumentList = valueArgumentList!!
|
||||
val argument = argumentList.arguments.last()
|
||||
val expression = argument.getArgumentExpression()!!
|
||||
assert(expression.unpackFunctionLiteral() != null)
|
||||
|
||||
val dummyCall = KtPsiFactory(this).createExpressionByPattern("foo()$0:'{}'", expression) as KtCallExpression
|
||||
val functionLiteralArgument = dummyCall.getLambdaArguments().single()
|
||||
val functionLiteralArgument = dummyCall.lambdaArguments.single()
|
||||
this.add(functionLiteralArgument)
|
||||
if (argumentList.getArguments().size > 1) {
|
||||
if (argumentList.arguments.size > 1) {
|
||||
argumentList.removeArgument(argument)
|
||||
}
|
||||
else {
|
||||
@@ -101,15 +100,15 @@ public fun KtCallExpression.moveFunctionLiteralOutsideParentheses() {
|
||||
}
|
||||
}
|
||||
|
||||
public fun KtBlockExpression.appendElement(element: KtElement, addNewLine: Boolean = false): KtElement {
|
||||
val rBrace = getRBrace()
|
||||
fun KtBlockExpression.appendElement(element: KtElement, addNewLine: Boolean = false): KtElement {
|
||||
val rBrace = rBrace
|
||||
val newLine = KtPsiFactory(this).createNewLine()
|
||||
val anchor = if (rBrace == null) {
|
||||
val lastChild = getLastChild()
|
||||
val lastChild = lastChild
|
||||
if (lastChild !is PsiWhiteSpace) addAfter(newLine, lastChild)!! else lastChild
|
||||
}
|
||||
else {
|
||||
rBrace.getPrevSibling()!!
|
||||
rBrace.prevSibling!!
|
||||
}
|
||||
val addedElement = addAfter(element, anchor)!! as KtElement
|
||||
if (addNewLine) {
|
||||
@@ -119,8 +118,8 @@ public fun KtBlockExpression.appendElement(element: KtElement, addNewLine: Boole
|
||||
}
|
||||
|
||||
//TODO: git rid of this method
|
||||
public fun PsiElement.deleteElementAndCleanParent() {
|
||||
val parent = getParent()
|
||||
fun PsiElement.deleteElementAndCleanParent() {
|
||||
val parent = parent
|
||||
|
||||
deleteElementWithDelimiters(this)
|
||||
deleteChildlessElement(parent, this.javaClass)
|
||||
@@ -140,32 +139,32 @@ private fun deleteElementWithDelimiters(element: PsiElement) {
|
||||
val from: PsiElement
|
||||
val to: PsiElement
|
||||
if (paramBefore != null) {
|
||||
from = paramBefore.getNextSibling()
|
||||
from = paramBefore.nextSibling
|
||||
to = element
|
||||
}
|
||||
else {
|
||||
val paramAfter = PsiTreeUtil.getNextSiblingOfType<PsiElement>(element, element.javaClass)
|
||||
|
||||
from = element
|
||||
to = if (paramAfter != null) paramAfter.getPrevSibling() else element
|
||||
to = if (paramAfter != null) paramAfter.prevSibling else element
|
||||
}
|
||||
|
||||
val parent = element.getParent()
|
||||
val parent = element.parent
|
||||
|
||||
parent.deleteChildRange(from, to)
|
||||
}
|
||||
|
||||
public fun PsiElement.deleteSingle() {
|
||||
CodeEditUtil.removeChild(getParent()?.getNode() ?: return, getNode() ?: return)
|
||||
fun PsiElement.deleteSingle() {
|
||||
CodeEditUtil.removeChild(parent?.node ?: return, node ?: return)
|
||||
}
|
||||
|
||||
public fun KtClass.getOrCreateCompanionObject() : KtObjectDeclaration {
|
||||
fun KtClass.getOrCreateCompanionObject() : KtObjectDeclaration {
|
||||
getCompanionObjects().firstOrNull()?.let { return it }
|
||||
return addDeclaration(KtPsiFactory(this).createCompanionObject()) as KtObjectDeclaration
|
||||
}
|
||||
|
||||
//TODO: code style option whether to insert redundant 'public' keyword or not
|
||||
public fun KtDeclaration.setVisibility(visibilityModifier: KtModifierKeywordToken) {
|
||||
fun KtDeclaration.setVisibility(visibilityModifier: KtModifierKeywordToken) {
|
||||
val defaultVisibilityKeyword = implicitVisibility()
|
||||
|
||||
if (visibilityModifier == defaultVisibilityKeyword) {
|
||||
|
||||
+4
-4
@@ -37,9 +37,9 @@ import org.jetbrains.kotlin.renderer.render
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import javax.swing.Icon
|
||||
|
||||
public open class DescriptorMemberChooserObject(
|
||||
open class DescriptorMemberChooserObject(
|
||||
psiElement: PsiElement,
|
||||
public open val descriptor: DeclarationDescriptor
|
||||
open val descriptor: DeclarationDescriptor
|
||||
) : PsiElementMemberChooserObject(psiElement, DescriptorMemberChooserObject.getText(descriptor), DescriptorMemberChooserObject.getIcon(psiElement, descriptor)), ClassMemberWithElement {
|
||||
|
||||
override fun getParentNodeDelegate(): MemberChooserObject {
|
||||
@@ -73,14 +73,14 @@ public open class DescriptorMemberChooserObject(
|
||||
nameShortness = NameShortness.SHORT
|
||||
}
|
||||
|
||||
public fun getText(descriptor: DeclarationDescriptor): String {
|
||||
fun getText(descriptor: DeclarationDescriptor): String {
|
||||
return if (descriptor is ClassDescriptor)
|
||||
descriptor.fqNameUnsafe.render()
|
||||
else
|
||||
MEMBER_RENDERER.render(descriptor)
|
||||
}
|
||||
|
||||
public fun getIcon(declaration: PsiElement?, descriptor: DeclarationDescriptor): Icon {
|
||||
fun getIcon(declaration: PsiElement?, descriptor: DeclarationDescriptor): Icon {
|
||||
if (declaration != null && declaration.isValid) {
|
||||
val isClass = declaration is PsiClass || declaration is KtClass
|
||||
val flags = if (isClass) 0 else Iconable.ICON_FLAG_VISIBILITY
|
||||
|
||||
Reference in New Issue
Block a user