Add postfix template for listOf/setOf/etc

#KT-25239 Fixed
This commit is contained in:
Denis Zharkov
2018-07-04 12:14:01 +03:00
parent 4ab97e8454
commit 0d00eb7de3
23 changed files with 119 additions and 1 deletions
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>arrayOf(s)</spot>
}
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>s</spot>$key
}
@@ -0,0 +1,5 @@
<html>
<body>
Wraps expression into 'arrayOf()'
</body>
</html>
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>listOf(s)</spot>
}
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>s</spot>$key
}
@@ -0,0 +1,5 @@
<html>
<body>
Wraps expression into 'listOf()'
</body>
</html>
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>sequenceOf(s)</spot>
}
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>s</spot>$key
}
@@ -0,0 +1,5 @@
<html>
<body>
Wraps expression into 'sequenceOf()'
</body>
</html>
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>setOf(s)</spot>
}
@@ -0,0 +1,3 @@
fun foo(s: String) {
val x = <spot>s</spot>$key
}
@@ -0,0 +1,5 @@
<html>
<body>
Wraps expression into 'setOf()'
</body>
</html>
@@ -58,7 +58,11 @@ class KtPostfixTemplateProvider : PostfixTemplateProvider {
KtParenthesizedPostfixTemplate,
KtSoutPostfixTemplate,
KtReturnPostfixTemplate,
KtWhilePostfixTemplate
KtWhilePostfixTemplate,
KtWrapWithListOfPostfixTemplate,
KtWrapWithSetOfPostfixTemplate,
KtWrapWithArrayOfPostfixTemplate,
KtWrapWithSequenceOfPostfixTemplate
)
override fun isTerminalSymbol(currentChar: Char) = currentChar == '.' || currentChar == '!'
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.idea.liveTemplates.macro.SuggestVariableNameMacro
import org.jetbrains.kotlin.idea.resolve.ideService
import org.jetbrains.kotlin.idea.util.getResolutionScope
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtReturnExpression
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getType
import org.jetbrains.kotlin.types.KotlinType
@@ -44,6 +45,18 @@ internal abstract class ConstantStringBasedPostfixTemplate(
override fun getElementToRemove(expr: PsiElement?) = expr
}
internal abstract class KtWrapWithCallPostfixTemplate(val functionName: String) : ConstantStringBasedPostfixTemplate(
functionName,
"$functionName(expr)",
"$functionName(\$expr$)\$END$",
createExpressionSelectorWithComplexFilter { expression, _ -> expression !is KtReturnExpression }
)
internal object KtWrapWithListOfPostfixTemplate : KtWrapWithCallPostfixTemplate("listOf")
internal object KtWrapWithSetOfPostfixTemplate : KtWrapWithCallPostfixTemplate("setOf")
internal object KtWrapWithArrayOfPostfixTemplate : KtWrapWithCallPostfixTemplate("arrayOf")
internal object KtWrapWithSequenceOfPostfixTemplate : KtWrapWithCallPostfixTemplate("sequenceOf")
internal class KtForEachPostfixTemplate(
name: String
) : ConstantStringBasedPostfixTemplate(
@@ -0,0 +1,3 @@
fun foo(x: String) {
x.arrayOf<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String) {
arrayOf(x)
}
@@ -0,0 +1,3 @@
fun foo(x: String) {
val y = x.listOf<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String) {
val y = listOf(x)<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String): Any {
return x.sequenceOf<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String): Any {
return sequenceOf(x)<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String) {
val y = x.setOf<caret>
}
@@ -0,0 +1,3 @@
fun foo(x: String) {
val y = setOf(x)
}
@@ -213,4 +213,37 @@ public class PostfixTemplateProviderTestGenerated extends AbstractPostfixTemplat
public void testWhile() throws Exception {
runTest("idea/testData/codeInsight/postfix/while.kt");
}
@TestMetadata("idea/testData/codeInsight/postfix/wrapWithCall")
@TestDataPath("$PROJECT_ROOT")
@RunWith(JUnit3RunnerWithInners.class)
public static class WrapWithCall extends AbstractPostfixTemplateProviderTest {
private void runTest(String testDataFilePath) throws Exception {
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
}
public void testAllFilesPresentInWrapWithCall() throws Exception {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/codeInsight/postfix/wrapWithCall"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("arrayOfStatement.kt")
public void testArrayOfStatement() throws Exception {
runTest("idea/testData/codeInsight/postfix/wrapWithCall/arrayOfStatement.kt");
}
@TestMetadata("listOf.kt")
public void testListOf() throws Exception {
runTest("idea/testData/codeInsight/postfix/wrapWithCall/listOf.kt");
}
@TestMetadata("returnSequenceOf.kt")
public void testReturnSequenceOf() throws Exception {
runTest("idea/testData/codeInsight/postfix/wrapWithCall/returnSequenceOf.kt");
}
@TestMetadata("setOf.kt")
public void testSetOf() throws Exception {
runTest("idea/testData/codeInsight/postfix/wrapWithCall/setOf.kt");
}
}
}