diff --git a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggester.kt b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggester.kt index c001bd2c092..8c46c6ba46f 100644 --- a/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggester.kt +++ b/idea/idea-core/src/org/jetbrains/kotlin/idea/core/KotlinNameSuggester.kt @@ -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.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.addNamesByExpressionPSI(expression: KtExpression?, validator: (String) -> Boolean) { diff --git a/idea/testData/refactoring/introduceVariable/NoExplicitReceiversUnresolved.kt.after b/idea/testData/refactoring/introduceVariable/NoExplicitReceiversUnresolved.kt.after index 0a35a0444ac..8823f7e7e96 100644 --- a/idea/testData/refactoring/introduceVariable/NoExplicitReceiversUnresolved.kt.after +++ b/idea/testData/refactoring/introduceVariable/NoExplicitReceiversUnresolved.kt.after @@ -1,5 +1,5 @@ fun main(args: Array) { - val a = a1 - println(a) + val a1 = a1 + println(a1) println(a2) } \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/ExtensionFunction1.kt b/idea/testData/refactoring/nameSuggester/ExtensionFunction1.kt new file mode 100644 index 00000000000..2f856045930 --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/ExtensionFunction1.kt @@ -0,0 +1,10 @@ +fun foo(): Int.(Int) -> Int = { 1 } + +fun test() { + foo() +} + +/* +foo +function +*/ \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/ExtensionFunction2.kt b/idea/testData/refactoring/nameSuggester/ExtensionFunction2.kt new file mode 100644 index 00000000000..00456d4d416 --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/ExtensionFunction2.kt @@ -0,0 +1,10 @@ +fun foo(): Int.(Int, Int) -> Int = { 1 } + +fun test() { + foo() +} + +/* +foo +function +*/ \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/Function1.kt b/idea/testData/refactoring/nameSuggester/Function1.kt new file mode 100644 index 00000000000..d865cdabf41 --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/Function1.kt @@ -0,0 +1,10 @@ +fun foo(): (Int) -> Int = { 1 } + +fun test() { + foo() +} + +/* +foo +function +*/ \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/Function2.kt b/idea/testData/refactoring/nameSuggester/Function2.kt new file mode 100644 index 00000000000..64c82b42b6e --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/Function2.kt @@ -0,0 +1,10 @@ +fun foo(): (Int, Int) -> Int = { 1 } + +fun test() { + foo() +} + +/* +foo +function +*/ \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/IdWithDigits.kt b/idea/testData/refactoring/nameSuggester/IdWithDigits.kt new file mode 100644 index 00000000000..83abc156c0e --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/IdWithDigits.kt @@ -0,0 +1,9 @@ +class Test123 + +fun foo() { + Test123() +} + +/* +test123 +*/ \ No newline at end of file diff --git a/idea/testData/refactoring/nameSuggester/IdWithNonASCII.kt b/idea/testData/refactoring/nameSuggester/IdWithNonASCII.kt new file mode 100644 index 00000000000..be185974f36 --- /dev/null +++ b/idea/testData/refactoring/nameSuggester/IdWithNonASCII.kt @@ -0,0 +1,9 @@ +class Тест123 + +fun тест() { + Тест123() +} + +/* +тест123 +*/ \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/refactoring/nameSuggester/KotlinNameSuggesterTest.kt b/idea/tests/org/jetbrains/kotlin/idea/refactoring/nameSuggester/KotlinNameSuggesterTest.kt index 63ca4586ac6..fc27850a76f 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/refactoring/nameSuggester/KotlinNameSuggesterTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/refactoring/nameSuggester/KotlinNameSuggesterTest.kt @@ -62,6 +62,18 @@ public class KotlinNameSuggesterTest : LightCodeInsightFixtureTestCase() { 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() { super.setUp() myFixture.setTestDataPath(PluginTestCaseBase.getTestDataPathBase() + "/refactoring/nameSuggester")