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