Name Suggester: Allow any Kotlin identifier in suggested names
#KT-10332 Fixed
This commit is contained in:
@@ -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) {
|
||||
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
fun main(args: Array<String>) {
|
||||
val a = a1
|
||||
println(a)
|
||||
val a1 = a1
|
||||
println(a1)
|
||||
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
|
||||
*/
|
||||
@@ -0,0 +1,10 @@
|
||||
fun foo(): (Int) -> Int = { 1 }
|
||||
|
||||
fun test() {
|
||||
<selection>foo()</selection>
|
||||
}
|
||||
|
||||
/*
|
||||
foo
|
||||
function
|
||||
*/
|
||||
@@ -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
|
||||
*/
|
||||
+12
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user