Correctness check in JetPsiFactory.createExpression
This commit is contained in:
@@ -64,7 +64,11 @@ public class JetPsiFactory(private val project: Project) {
|
||||
|
||||
public fun createExpression(text: String): JetExpression {
|
||||
//TODO: '\n' below if important - some strange code indenting problems appear without it
|
||||
return createProperty("val x =\n$text").getInitializer() ?: error("Failed to create expression from text: '$text'")
|
||||
val expression = createProperty("val x =\n$text").getInitializer() ?: error("Failed to create expression from text: '$text'")
|
||||
assert(expression.getText() == text) {
|
||||
"Failed to create expression from text: '$text', resulting expression's text was: '${expression.getText()}'"
|
||||
}
|
||||
return expression
|
||||
}
|
||||
|
||||
public fun createClassLiteral(className: String): JetClassLiteralExpression =
|
||||
|
||||
@@ -33,7 +33,7 @@ import java.util.LinkedHashMap
|
||||
public fun JetPsiFactory.createExpressionByPattern(pattern: String, vararg args: Any): JetExpression {
|
||||
val (processedText, allPlaceholders) = processPattern(pattern, args)
|
||||
|
||||
var expression = createExpression(processedText)
|
||||
var expression = createExpression(processedText.trim())
|
||||
val project = expression.getProject()
|
||||
|
||||
val start = expression.startOffset
|
||||
|
||||
@@ -149,11 +149,11 @@ public class JetTypeCheckerTest extends JetLiteFixture {
|
||||
}
|
||||
|
||||
public void testWhen() throws Exception {
|
||||
assertType("when (1) { is 1 -> 2; } ", "Int");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'} ", "Comparable<out Any?>");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; is 1 -> null} ", "Comparable<out Any?>?");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; else -> null} ", "Comparable<out Any?>?");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; is 1 -> when(2) {is 1 -> null}} ", "Comparable<out Any?>?");
|
||||
assertType("when (1) { is 1 -> 2; }", "Int");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'}", "Comparable<out Any?>");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; is 1 -> null}", "Comparable<out Any?>?");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; else -> null}", "Comparable<out Any?>?");
|
||||
assertType("when (1) { is 1 -> 2; is 1 -> '2'; is 1 -> when(2) {is 1 -> null}}", "Comparable<out Any?>?");
|
||||
}
|
||||
|
||||
public void testTry() throws Exception {
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ public class MoveDeclarationsOutHelper {
|
||||
|
||||
// Dummy element to add new declarations at the beginning
|
||||
JetPsiFactory psiFactory = JetPsiFactory(project);
|
||||
PsiElement dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement "), statements[0]);
|
||||
PsiElement dummyFirstStatement = container.addBefore(psiFactory.createExpression("dummyStatement"), statements[0]);
|
||||
|
||||
try {
|
||||
SearchScope scope = new LocalSearchScope(container);
|
||||
|
||||
+8
-5
@@ -77,17 +77,20 @@ public class ReplaceWithOperatorAssignmentIntention : JetSelfTargetingOffsetInde
|
||||
element.replace(JetPsiFactory(element).createExpression(replacement))
|
||||
}
|
||||
|
||||
[tailRecursive]
|
||||
@tailRecursive
|
||||
private fun buildOperatorAssignmentText(variableExpression: JetSimpleNameExpression, expression: JetBinaryExpression, tail: String): String {
|
||||
val operationText = expression.getOperationReference().getText()
|
||||
val variableName = variableExpression.getText()
|
||||
return when {
|
||||
variableExpression.matches(expression.getLeft()) -> "$variableName $operationText= ${expression.getRight()!!.getText()} $tail"
|
||||
|
||||
variableExpression.matches(expression.getRight()) -> "$variableName $operationText= ${expression.getLeft()!!.getText()} $tail"
|
||||
fun String.appendTail() = if (tail.isEmpty()) this else "$this $tail"
|
||||
|
||||
return when {
|
||||
variableExpression.matches(expression.getLeft()) -> "$variableName $operationText= ${expression.getRight()!!.getText()}".appendTail()
|
||||
|
||||
variableExpression.matches(expression.getRight()) -> "$variableName $operationText= ${expression.getLeft()!!.getText()}".appendTail()
|
||||
|
||||
expression.getLeft() is JetBinaryExpression ->
|
||||
buildOperatorAssignmentText(variableExpression, expression.getLeft() as JetBinaryExpression, "$operationText ${expression.getRight()!!.getText()} $tail")
|
||||
buildOperatorAssignmentText(variableExpression, expression.getLeft() as JetBinaryExpression, "$operationText ${expression.getRight()!!.getText()}".appendTail())
|
||||
|
||||
else -> tail
|
||||
}
|
||||
|
||||
+4
-2
@@ -34,14 +34,14 @@ public class WhenToIfIntention : JetSelfTargetingRangeIntention<JetWhenExpressio
|
||||
override fun applyTo(element: JetWhenExpression, editor: Editor) {
|
||||
val factory = JetPsiFactory(element)
|
||||
val ifExpression = factory.buildExpression {
|
||||
for ((i, entry) in element.getEntries().withIndex()) {
|
||||
val entries = element.getEntries()
|
||||
for ((i, entry) in entries.withIndex()) {
|
||||
if (i > 0) {
|
||||
appendFixedText("else ")
|
||||
}
|
||||
val branch = entry.getExpression()
|
||||
if (entry.isElse()) {
|
||||
appendExpression(branch)
|
||||
appendFixedText("\n")
|
||||
}
|
||||
else {
|
||||
val condition = factory.combineWhenConditions(entry.getConditions(), element.getSubjectExpression())
|
||||
@@ -49,6 +49,8 @@ public class WhenToIfIntention : JetSelfTargetingRangeIntention<JetWhenExpressio
|
||||
appendExpression(condition)
|
||||
appendFixedText(")")
|
||||
appendExpression(branch)
|
||||
}
|
||||
if (i != entries.lastIndex) {
|
||||
appendFixedText("\n")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
fun test(n: Int): String {
|
||||
return if (n is ) "String"
|
||||
return if (n is) "String"
|
||||
else if (n in) "1..10"
|
||||
else "unknown"
|
||||
}
|
||||
Reference in New Issue
Block a user