Use of expressions instead of plain text when converting when's

This commit is contained in:
Valentin Kipyatkov
2015-05-06 12:16:39 +03:00
parent 66a90916fe
commit ada11ca5e9
14 changed files with 84 additions and 92 deletions
@@ -63,7 +63,7 @@ public class JetPsiFactory(private val project: Project) {
}
public fun createExpression(text: String): JetExpression {
return createProperty("val x = $text").getInitializer()!!
return createProperty("val x = $text").getInitializer() ?: error("Failed to create expression from text: '$text'")
}
public fun createClassLiteral(className: String): JetClassLiteralExpression =
@@ -1,51 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.psi;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JetPsiUnparsingUtils {
private JetPsiUnparsingUtils() {
}
@NotNull
public static String toBinaryExpression(@Nullable JetExpression left, @NotNull String op, @Nullable JetElement right) {
return toBinaryExpression(JetPsiUtil.getText(left), op, JetPsiUtil.getText(right));
}
@NotNull
public static String toBinaryExpression(@NotNull String left, @NotNull String op, @NotNull String right) {
return left + " " + op + " " + right;
}
@NotNull
public static String parenthesizeIfNeeded(@Nullable JetExpression expression) {
String text = JetPsiUtil.getText(expression);
return (expression instanceof JetParenthesizedExpression ||
expression instanceof JetConstantExpression ||
expression instanceof JetSimpleNameExpression ||
expression instanceof JetDotQualifiedExpression)
? text : "(" + text + ")";
}
@NotNull
public static String parenthesizeTextIfNeeded(@NotNull String expressionText) {
return (expressionText.startsWith("(") && expressionText.endsWith(")")) ? expressionText : "(" + expressionText + ")";
}
}
@@ -52,6 +52,7 @@ public class JetWhenConditionInRange extends JetWhenCondition {
return visitor.visitWhenConditionInRange(this, data);
}
@NotNull
public JetSimpleNameExpression getOperationReference() {
return (JetSimpleNameExpression) findChildByType(JetNodeTypes.OPERATION_REFERENCE);
}
@@ -72,6 +72,7 @@ public fun JetPsiFactory.createExpressionByPattern(pattern: String, vararg args:
.filter { args[it.getKey()] is String }
.flatMap { it.getValue() }
.map { it.range }
.filterNot { it.isEmpty() }
.sortBy { -it.getStartOffset() }
// reformat whole text except for String arguments (as they can contain user's formatting to be preserved)