use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class TailRecursionCodegen method assignParameterValues.
private void assignParameterValues(CallableDescriptor fd, CallableMethod callableMethod, List<ResolvedValueArgument> valueArguments) {
List<Type> types = callableMethod.getValueParameterTypes();
for (ValueParameterDescriptor parameterDescriptor : Lists.reverse(fd.getValueParameters())) {
ResolvedValueArgument arg = valueArguments.get(parameterDescriptor.getIndex());
Type type = types.get(parameterDescriptor.getIndex());
if (arg instanceof ExpressionValueArgument) {
ExpressionValueArgument ev = (ExpressionValueArgument) arg;
ValueArgument argument = ev.getValueArgument();
KtExpression argumentExpression = argument == null ? null : argument.getArgumentExpression();
if (argumentExpression instanceof KtSimpleNameExpression) {
ResolvedCall<?> resolvedCall = CallUtilKt.getResolvedCall(argumentExpression, state.getBindingContext());
if (resolvedCall != null && resolvedCall.getResultingDescriptor().equals(parameterDescriptor.getOriginal())) {
// do nothing: we shouldn't store argument to itself again
AsmUtil.pop(v, type);
continue;
}
}
//assign the parameter below
} else if (arg instanceof DefaultValueArgument) {
AsmUtil.pop(v, type);
DefaultParameterValueLoader.DEFAULT.genValue(parameterDescriptor, codegen).put(type, v);
} else if (arg instanceof VarargValueArgument) {
// assign the parameter below
} else {
throw new UnsupportedOperationException("Unknown argument type: " + arg + " in " + fd);
}
store(parameterDescriptor, type);
}
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class KotlinNotSurrounder method surroundExpression.
@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
KtPrefixExpression prefixExpr = (KtPrefixExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression("!(a)");
KtParenthesizedExpression parenthesizedExpression = (KtParenthesizedExpression) prefixExpr.getBaseExpression();
assert parenthesizedExpression != null : "JetParenthesizedExpression should exists for " + prefixExpr.getText() + " expression";
KtExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
expressionWithoutParentheses.replace(expression);
expression = (KtExpression) expression.replace(prefixExpr);
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
int offset = expression.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class KotlinParenthesesSurrounder method surroundExpression.
@Nullable
@Override
public TextRange surroundExpression(@NotNull Project project, @NotNull Editor editor, @NotNull KtExpression expression) {
KtParenthesizedExpression parenthesizedExpression = (KtParenthesizedExpression) KtPsiFactoryKt.KtPsiFactory(expression).createExpression("(a)");
KtExpression expressionWithoutParentheses = parenthesizedExpression.getExpression();
assert expressionWithoutParentheses != null : "JetExpression should exists for " + parenthesizedExpression.getText() + " expression";
expressionWithoutParentheses.replace(expression);
expression = (KtExpression) expression.replace(parenthesizedExpression);
CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(expression);
int offset = expression.getTextRange().getEndOffset();
return new TextRange(offset, offset);
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class KotlinIfSurrounderBase method surroundStatements.
@Nullable
@Override
protected TextRange surroundStatements(Project project, Editor editor, PsiElement container, PsiElement[] statements) {
statements = MoveDeclarationsOutHelper.move(container, statements, isGenerateDefaultInitializers());
if (statements.length == 0) {
KotlinSurrounderUtils.showErrorHint(project, editor, KotlinSurrounderUtils.SURROUND_WITH_ERROR);
return null;
}
KtIfExpression ifExpression = (KtIfExpression) KtPsiFactoryKt.KtPsiFactory(project).createExpression(getCodeTemplate());
ifExpression = (KtIfExpression) container.addAfter(ifExpression, statements[statements.length - 1]);
// TODO move a comment for first statement
KtBlockExpression thenBranch = (KtBlockExpression) ifExpression.getThen();
assert thenBranch != null : "Then branch should exist for created if expression: " + ifExpression.getText();
// Add statements in then branch of created if
KotlinSurrounderUtils.addStatementsInBlock(thenBranch, statements);
// Delete statements from original code
container.deleteChildRange(statements[0], statements[statements.length - 1]);
ifExpression = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(ifExpression);
KtExpression condition = ifExpression.getCondition();
assert condition != null : "Condition should exists for created if expression: " + ifExpression.getText();
// Delete condition from created if
TextRange range = condition.getTextRange();
TextRange textRange = new TextRange(range.getStartOffset(), range.getStartOffset());
editor.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
return textRange;
}
use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.
the class KotlinTryFinallySurrounder method getTextRangeForCaret.
@NotNull
@Override
protected TextRange getTextRangeForCaret(@NotNull KtTryExpression expression) {
KtFinallySection block = expression.getFinallyBlock();
assert block != null : "Finally block should exists for " + expression.getText();
KtExpression blockExpression = block.getFinalExpression().getStatements().get(0);
return blockExpression.getTextRange();
}
Aggregations