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.types.typeUtil.builtIns
import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeSmart import org.jetbrains.kotlin.util.capitalizeDecapitalize.decapitalizeSmart
import java.util.* import java.util.*
import java.util.regex.Pattern
public object KotlinNameSuggester { public object KotlinNameSuggester {
public fun suggestNamesByExpressionAndType( public fun suggestNamesByExpressionAndType(
@@ -216,6 +215,9 @@ public object KotlinNameSuggester {
} }
} }
} }
else if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) {
addName("function", validator)
}
else { else {
val descriptor = type.getConstructor().getDeclarationDescriptor() val descriptor = type.getConstructor().getDeclarationDescriptor()
if (descriptor != null) { if (descriptor != null) {
@@ -237,7 +239,7 @@ public object KotlinNameSuggester {
private fun MutableCollection<String>.addCamelNames(name: String, validator: (String) -> Boolean, startLowerCase: Boolean = true) { private fun MutableCollection<String>.addCamelNames(name: String, validator: (String) -> Boolean, startLowerCase: Boolean = true) {
if (name === "") return if (name === "") return
var s = deleteNonLetterFromString(name) var s = extractIdentifiers(name)
for (prefix in ACCESSOR_PREFIXES) { for (prefix in ACCESSOR_PREFIXES) {
if (!s.startsWith(prefix)) continue if (!s.startsWith(prefix)) continue
@@ -268,10 +270,17 @@ public object KotlinNameSuggester {
} }
} }
private fun deleteNonLetterFromString(s: String): String { private fun extractIdentifiers(s: String): String {
val pattern = Pattern.compile("[^a-zA-Z]") return buildString {
val matcher = pattern.matcher(s) val lexer = KotlinLexer()
return matcher.replaceAll("") 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) { private fun MutableCollection<String>.addNamesByExpressionPSI(expression: KtExpression?, validator: (String) -> Boolean) {
@@ -1,5 +1,5 @@
fun main(args: Array<String>) { fun main(args: Array<String>) {
val a = a1 val a1 = a1
println(a) println(a1)
println(a2) println(a2)
} }
@@ -0,0 +1,10 @@
fun foo(): Int.(Int) -> Int = { 1 }
fun test() {
<selection>foo()</selection>
}
/*
foo
function
*/
@@ -0,0 +1,10 @@
fun foo(): Int.(Int, Int) -> Int = { 1 }
fun test() {
<selection>foo()</selection>
}
/*
foo
function
*/
+10
View File
@@ -0,0 +1,10 @@
fun foo(): (Int) -> Int = { 1 }
fun test() {
<selection>foo()</selection>
}
/*
foo
function
*/
+10
View File
@@ -0,0 +1,10 @@
fun foo(): (Int, Int) -> Int = { 1 }
fun test() {
<selection>foo()</selection>
}
/*
foo
function
*/
@@ -0,0 +1,9 @@
class Test123
fun foo() {
<selection>Test123()</selection>
}
/*
test123
*/
@@ -0,0 +1,9 @@
class Тест123
fun тест() {
<selection>Тест123()</selection>
}
/*
тест123
*/
@@ -62,6 +62,18 @@ public class KotlinNameSuggesterTest : LightCodeInsightFixtureTestCase() {
public fun testParameterNameByParenthesizedArgumentExpression() { doTest() } public fun testParameterNameByParenthesizedArgumentExpression() { doTest() }
public fun testIdWithDigits() { doTest() }
public fun testIdWithNonASCII() { doTest() }
public fun testFunction1() { doTest() }
public fun testFunction2() { doTest() }
public fun testExtensionFunction1() { doTest() }
public fun testExtensionFunction2() { doTest() }
override fun setUp() { override fun setUp() {
super.setUp() super.setUp()
myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/refactoring/nameSuggester") myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/refactoring/nameSuggester")