Name Suggester: Allow any Kotlin identifier in suggested names

#KT-10332 Fixed
This commit is contained in:
Alexey Sedunov
2015-12-11 17:41:13 +03:00
parent ee9eb55b59
commit 04c6ea78c7
9 changed files with 87 additions and 8 deletions
@@ -32,7 +32,6 @@ import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.builtIns
import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeSmart
import java.util.*
import java.util.regex.Pattern
public object KotlinNameSuggester {
public fun suggestNamesByExpressionAndType(
@@ -216,6 +215,9 @@ public object KotlinNameSuggester {
}
}
}
else if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) {
addName("function", validator)
}
else {
val descriptor = type.getConstructor().getDeclarationDescriptor()
if (descriptor != null) {
@@ -237,7 +239,7 @@ public object KotlinNameSuggester {
private fun MutableCollection<String>.addCamelNames(name: String, validator: (String) -> Boolean, startLowerCase: Boolean = true) {
if (name === "") return
var s = deleteNonLetterFromString(name)
var s = extractIdentifiers(name)
for (prefix in ACCESSOR_PREFIXES) {
if (!s.startsWith(prefix)) continue
@@ -268,10 +270,17 @@ public object KotlinNameSuggester {
}
}
private fun deleteNonLetterFromString(s: String): String {
val pattern = Pattern.compile("[^a-zA-Z]")
val matcher = pattern.matcher(s)
return matcher.replaceAll("")
private fun extractIdentifiers(s: String): String {
return buildString {
val lexer = KotlinLexer()
lexer.start(s)
while (lexer.tokenType != null) {
if (lexer.tokenType == KtTokens.IDENTIFIER) {
append(lexer.tokenText)
}
lexer.advance()
}
}
}
private fun MutableCollection<String>.addNamesByExpressionPSI(expression: KtExpression?, validator: (String) -> Boolean) {