IterateExpressionIntention uses collection expression for name suggestions too
This commit is contained in:
@@ -72,6 +72,23 @@ public object KotlinNameSuggester {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun suggestIterationVariableNames(collection: JetExpression, elementType: JetType, validator: (String) -> Boolean, defaultName: String?): Collection<String> {
|
||||||
|
val result = LinkedHashSet<String>()
|
||||||
|
|
||||||
|
suggestNamesByExpressionOnly(collection, { true })
|
||||||
|
.map { StringUtil.unpluralize(it) }
|
||||||
|
.filterNotNull()
|
||||||
|
.mapTo(result) { suggestNameByName(it, validator) }
|
||||||
|
|
||||||
|
result.addNamesByType(elementType, validator)
|
||||||
|
|
||||||
|
if (result.isEmpty()) {
|
||||||
|
result.addName(defaultName, validator)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
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")
|
||||||
|
|
||||||
public fun suggestNamesForTypeParameters(count: Int, validator: (String) -> Boolean): List<String> {
|
public fun suggestNamesForTypeParameters(count: Int, validator: (String) -> Boolean): List<String> {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil
|
|||||||
|
|
||||||
//TODO: move it somewhere else and reuse
|
//TODO: move it somewhere else and reuse
|
||||||
public abstract class ChooseValueExpression<T : Any>(
|
public abstract class ChooseValueExpression<T : Any>(
|
||||||
lookupItems: List<T>,
|
lookupItems: Collection<T>,
|
||||||
protected val defaultItem: T,
|
protected val defaultItem: T,
|
||||||
private val advertisementText: String? = null
|
private val advertisementText: String? = null
|
||||||
) : Expression() {
|
) : Expression() {
|
||||||
@@ -65,8 +65,8 @@ public abstract class ChooseValueExpression<T : Any>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class ChooseStringExpression(
|
public class ChooseStringExpression(
|
||||||
suggestions: List<String>,
|
suggestions: Collection<String>,
|
||||||
default: String = suggestions[0],
|
default: String = suggestions.first(),
|
||||||
advertisementText: String? = null
|
advertisementText: String? = null
|
||||||
) : ChooseValueExpression<String>(suggestions, default, advertisementText) {
|
) : ChooseValueExpression<String>(suggestions, default, advertisementText) {
|
||||||
override fun getLookupString(element: String) = element
|
override fun getLookupString(element: String) = element
|
||||||
|
|||||||
@@ -57,11 +57,10 @@ public class IterateExpressionIntention : JetSelfTargetingIntention<JetExpressio
|
|||||||
//TODO: multi-declaration (when?)
|
//TODO: multi-declaration (when?)
|
||||||
|
|
||||||
val elementType = data(element)!!.elementType
|
val elementType = data(element)!!.elementType
|
||||||
//TODO: base on expression too
|
|
||||||
//TODO: name validation
|
//TODO: name validation
|
||||||
val names = KotlinNameSuggester.suggestNamesByType(elementType, { true }, "e")
|
val names = KotlinNameSuggester.suggestIterationVariableNames(element, elementType, { true }, "e")
|
||||||
|
|
||||||
var forExpression = JetPsiFactory(element).createExpressionByPattern("for($0 in $1) {\nx\n}", names[0], element) as JetForExpression
|
var forExpression = JetPsiFactory(element).createExpressionByPattern("for($0 in $1) {\nx\n}", names.first(), element) as JetForExpression
|
||||||
forExpression = element.replaced(forExpression)
|
forExpression = element.replaced(forExpression)
|
||||||
|
|
||||||
PsiDocumentManager.getInstance(forExpression.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument())
|
PsiDocumentManager.getInstance(forExpression.getProject()).doPostponedOperationsAndUnblockDocument(editor.getDocument())
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fun foo(names: List<String>) {
|
||||||
|
names<caret>
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
fun foo(names: List<String>) {
|
||||||
|
for (name in names) {
|
||||||
|
<caret>
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5235,6 +5235,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("nameSuggestion.kt")
|
||||||
|
public void testNameSuggestion() throws Exception {
|
||||||
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/iterateExpression/nameSuggestion.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("simple.kt")
|
@TestMetadata("simple.kt")
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/iterateExpression/simple.kt");
|
String fileName = JetTestUtils.navigationMetadata("idea/testData/intentions/iterateExpression/simple.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user