Minor: refactoring KotlinNameSuggester

This commit is contained in:
Dmitry Gridin
2019-03-18 19:43:03 +07:00
parent 0d2426b081
commit ff34788eb3
@@ -37,11 +37,11 @@ import java.util.*
object KotlinNameSuggester {
fun suggestNamesByExpressionAndType(
expression: KtExpression,
type: KotlinType?,
bindingContext: BindingContext?,
validator: (String) -> Boolean,
defaultName: String?
expression: KtExpression,
type: KotlinType?,
bindingContext: BindingContext?,
validator: (String) -> Boolean,
defaultName: String?
): Collection<String> {
val result = LinkedHashSet<String>()
@@ -71,9 +71,10 @@ object KotlinNameSuggester {
}
fun suggestNamesByExpressionOnly(
expression: KtExpression,
bindingContext: BindingContext?,
validator: (String) -> Boolean, defaultName: String? = null): List<String> {
expression: KtExpression,
bindingContext: BindingContext?,
validator: (String) -> Boolean, defaultName: String? = null
): List<String> {
val result = ArrayList<String>()
result.addNamesByExpression(expression, bindingContext, validator)
@@ -86,15 +87,16 @@ object KotlinNameSuggester {
}
fun suggestIterationVariableNames(
collection: KtExpression,
elementType: KotlinType,
bindingContext: BindingContext?,
validator: (String) -> Boolean, defaultName: String?): Collection<String> {
collection: KtExpression,
elementType: KotlinType,
bindingContext: BindingContext?,
validator: (String) -> Boolean, defaultName: String?
): Collection<String> {
val result = LinkedHashSet<String>()
suggestNamesByExpressionOnly(collection, bindingContext, { true })
.mapNotNull { StringUtil.unpluralize(it) }
.mapTo(result) { suggestNameByName(it, validator) }
.mapNotNull { StringUtil.unpluralize(it) }
.mapTo(result) { suggestNameByName(it, validator) }
result.addNamesByType(elementType, validator)
@@ -106,11 +108,11 @@ object KotlinNameSuggester {
}
private val COMMON_TYPE_PARAMETER_NAMES = listOf("T", "U", "V", "W", "X", "Y", "Z")
private val MAX_NUMBER_OF_SUGGESTED_NAME_CHECKS = 1000
private const val MAX_NUMBER_OF_SUGGESTED_NAME_CHECKS = 1000
fun suggestNamesForTypeParameters(count: Int, validator: (String) -> Boolean): List<String> {
val result = ArrayList<String>()
for (i in 0..count - 1) {
for (i in 0 until count) {
result.add(suggestNameByMultipleNames(COMMON_TYPE_PARAMETER_NAMES, validator))
}
return result
@@ -121,7 +123,7 @@ object KotlinNameSuggester {
return when (this) {
is KtNullableType -> "Nullable${innerType?.render() ?: ""}"
is KtFunctionType -> {
val arguments = listOf(receiverTypeReference).filterNotNull() + parameters.mapNotNull { it.typeReference }
val arguments = listOfNotNull(receiverTypeReference) + parameters.mapNotNull { it.typeReference }
val argText = arguments.joinToString(separator = "") { it.typeElement?.render() ?: "" }
val returnText = returnTypeReference?.typeElement?.render() ?: "Unit"
"${argText}To$returnText"
@@ -170,23 +172,23 @@ object KotlinNameSuggester {
}
private fun MutableCollection<String>.addNamesByType(type: KotlinType, validator: (String) -> Boolean) {
var type = TypeUtils.makeNotNullable(type) // wipe out '?'
val builtIns = type.builtIns
val myType = TypeUtils.makeNotNullable(type) // wipe out '?'
val builtIns = myType.builtIns
val typeChecker = KotlinTypeChecker.DEFAULT
if (ErrorUtils.containsErrorType(type)) return
if (ErrorUtils.containsErrorType(myType)) return
when {
typeChecker.equalTypes(builtIns.booleanType, type) -> addName("b", validator)
typeChecker.equalTypes(builtIns.intType, type) -> addName("i", validator)
typeChecker.equalTypes(builtIns.byteType, type) -> addName("byte", validator)
typeChecker.equalTypes(builtIns.longType, type) -> addName("l", validator)
typeChecker.equalTypes(builtIns.floatType, type) -> addName("fl", validator)
typeChecker.equalTypes(builtIns.doubleType, type) -> addName("d", validator)
typeChecker.equalTypes(builtIns.shortType, type) -> addName("sh", validator)
typeChecker.equalTypes(builtIns.charType, type) -> addName("c", validator)
typeChecker.equalTypes(builtIns.stringType, type) -> addName("s", validator)
KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) -> {
val elementType = builtIns.getArrayElementType(type)
typeChecker.equalTypes(builtIns.booleanType, myType) -> addName("b", validator)
typeChecker.equalTypes(builtIns.intType, myType) -> addName("i", validator)
typeChecker.equalTypes(builtIns.byteType, myType) -> addName("byte", validator)
typeChecker.equalTypes(builtIns.longType, myType) -> addName("l", validator)
typeChecker.equalTypes(builtIns.floatType, myType) -> addName("fl", validator)
typeChecker.equalTypes(builtIns.doubleType, myType) -> addName("d", validator)
typeChecker.equalTypes(builtIns.shortType, myType) -> addName("sh", validator)
typeChecker.equalTypes(builtIns.charType, myType) -> addName("c", validator)
typeChecker.equalTypes(builtIns.stringType, myType) -> addName("s", validator)
KotlinBuiltIns.isArray(myType) || KotlinBuiltIns.isPrimitiveArray(myType) -> {
val elementType = builtIns.getArrayElementType(myType)
when {
typeChecker.equalTypes(builtIns.booleanType, elementType) -> addName("booleans", validator)
typeChecker.equalTypes(builtIns.intType, elementType) -> addName("ints", validator)
@@ -206,9 +208,9 @@ object KotlinNameSuggester {
}
}
}
type.isFunctionType -> addName("function", validator)
myType.isFunctionType -> addName("function", validator)
else -> {
val descriptor = type.constructor.declarationDescriptor
val descriptor = myType.constructor.declarationDescriptor
if (descriptor != null) {
val className = descriptor.name
if (!className.isSpecial) {
@@ -242,14 +244,13 @@ object KotlinNameSuggester {
}
var upperCaseLetterBefore = false
for (i in 0..s.length - 1) {
for (i in 0 until s.length) {
val c = s[i]
val upperCaseLetter = Character.isUpperCase(c)
if (i == 0) {
addName(if (startLowerCase) s.decapitalizeSmart() else s, validator)
}
else {
} else {
if (upperCaseLetter && !upperCaseLetterBefore) {
val substring = s.substring(i)
addName(if (startLowerCase) substring.decapitalizeSmart() else substring, validator)
@@ -275,8 +276,7 @@ object KotlinNameSuggester {
private fun MutableCollection<String>.addNamesByExpressionPSI(expression: KtExpression?, validator: (String) -> Boolean) {
if (expression == null) return
val deparenthesized = KtPsiUtil.safeDeparenthesize(expression)
when (deparenthesized) {
when (val deparenthesized = KtPsiUtil.safeDeparenthesize(expression)) {
is KtSimpleNameExpression -> addCamelNames(deparenthesized.getReferencedName(), validator)
is KtQualifiedExpression -> addNamesByExpressionPSI(deparenthesized.selectorExpression, validator)
is KtCallExpression -> addNamesByExpressionPSI(deparenthesized.calleeExpression, validator)
@@ -285,9 +285,9 @@ object KotlinNameSuggester {
}
private fun MutableCollection<String>.addNamesByExpression(
expression: KtExpression?,
bindingContext: BindingContext?,
validator: (String) -> Boolean
expression: KtExpression?,
bindingContext: BindingContext?,
validator: (String) -> Boolean
) {
if (expression == null) return
@@ -296,9 +296,9 @@ object KotlinNameSuggester {
}
private fun MutableCollection<String>.addNamesByValueArgument(
expression: KtExpression,
bindingContext: BindingContext?,
validator: (String) -> Boolean
expression: KtExpression,
bindingContext: BindingContext?,
validator: (String) -> Boolean
) {
if (bindingContext == null) return
val argumentExpression = expression.getOutermostParenthesizerOrThis()