Refactored SplitIfIntention + JetPsiFactory.createIf uses createExpressionByPattern and does not reformat its arguments

This commit is contained in:
Valentin Kipyatkov
2015-05-05 14:06:19 +03:00
parent de8601bb5d
commit 7499c4bc19
14 changed files with 49 additions and 78 deletions
@@ -349,8 +349,11 @@ public class JetPsiFactory(private val project: Project) {
return createReturn(JetPsiUtil.getText(expression))
}
public fun createIf(condition: JetExpression?, thenExpr: JetExpression?, elseExpr: JetExpression?): JetIfExpression {
return createExpression(JetPsiUnparsingUtils.toIf(condition, thenExpr, elseExpr)) as JetIfExpression
public fun createIf(condition: JetExpression, thenExpr: JetExpression, elseExpr: JetExpression?): JetIfExpression {
return (if (elseExpr != null)
createExpressionByPattern("if ($0) $1 else $2", condition, thenExpr, elseExpr) as JetIfExpression
else
createExpressionByPattern("if ($0) $1", condition, thenExpr)) as JetIfExpression
}
public fun createArgumentWithName(name: String?, argumentExpression: JetExpression): JetValueArgument {
@@ -23,20 +23,6 @@ public class JetPsiUnparsingUtils {
private JetPsiUnparsingUtils() {
}
@NotNull
public static String toIf(@Nullable JetExpression condition, @Nullable JetExpression thenExpression, @Nullable JetExpression elseExpression) {
return toIf(
JetPsiUtil.getText(condition),
JetPsiUtil.getText(thenExpression),
elseExpression != null ? elseExpression.getText() : null
);
}
@NotNull
public static String toIf(@NotNull String condition, @NotNull String thenExpression, @Nullable String elseExpression) {
return "if " + parenthesizeTextIfNeeded(condition) + " " + thenExpression + (elseExpression != null ? " else " + elseExpression : "");
}
@NotNull
public static String toBinaryExpression(@Nullable JetExpression left, @NotNull String op, @Nullable JetElement right) {
return toBinaryExpression(JetPsiUtil.getText(left), op, JetPsiUtil.getText(right));
@@ -80,9 +80,6 @@ public fun JetPsiFactory.createExpressionByPattern(pattern: String, vararg args:
bound = range.getStartOffset() + start
}
expression = codeStyleManager.reformatRange(expression, start, bound + 1, true) as JetExpression
// we need to adjust indent of all lines within expression
codeStyleManager.adjustLineIndent(expression.getContainingFile(), expression.getTextRange())
}
// do not reformat the whole expression in PostprocessReformattingAspect
@@ -97,6 +94,8 @@ public fun JetPsiFactory.createExpressionByPattern(pattern: String, vararg args:
element.replace(arg)
}
codeStyleManager.adjustLineIndent(expression.getContainingFile(), expression.getTextRange())
return expression
}
@@ -46,8 +46,7 @@ import org.jetbrains.kotlin.JetNodeTypes
import org.jetbrains.kotlin.name.FqName
public fun JetCallElement.getCallNameExpression(): JetSimpleNameExpression? {
val calleeExpression = getCalleeExpression()
if (calleeExpression == null) return null
val calleeExpression = getCalleeExpression() ?: return null
return when (calleeExpression) {
is JetSimpleNameExpression -> calleeExpression