Search in sources :

Example 1 with KtExpression

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);
    }
}
Also used : Type(org.jetbrains.org.objectweb.asm.Type) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtSimpleNameExpression(org.jetbrains.kotlin.psi.KtSimpleNameExpression) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)

Example 2 with KtExpression

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);
}
Also used : KtPrefixExpression(org.jetbrains.kotlin.psi.KtPrefixExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) TextRange(com.intellij.openapi.util.TextRange) KtParenthesizedExpression(org.jetbrains.kotlin.psi.KtParenthesizedExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with KtExpression

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);
}
Also used : KtExpression(org.jetbrains.kotlin.psi.KtExpression) TextRange(com.intellij.openapi.util.TextRange) KtParenthesizedExpression(org.jetbrains.kotlin.psi.KtParenthesizedExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with KtExpression

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;
}
Also used : KtIfExpression(org.jetbrains.kotlin.psi.KtIfExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtBlockExpression(org.jetbrains.kotlin.psi.KtBlockExpression) TextRange(com.intellij.openapi.util.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with KtExpression

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();
}
Also used : KtFinallySection(org.jetbrains.kotlin.psi.KtFinallySection) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KtExpression (org.jetbrains.kotlin.psi.KtExpression)28 NotNull (org.jetbrains.annotations.NotNull)13 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)6 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 Nullable (org.jetbrains.annotations.Nullable)4 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)3 HashMap (java.util.HashMap)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 KtElement (org.jetbrains.kotlin.psi.KtElement)2 KtParenthesizedExpression (org.jetbrains.kotlin.psi.KtParenthesizedExpression)2 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)2 ExpressionValueArgument (org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument)2 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)2 Type (org.jetbrains.org.objectweb.asm.Type)2 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 IElementType (com.intellij.psi.tree.IElementType)1 ParameterTableModelItemBase (com.intellij.refactoring.changeSignature.ParameterTableModelItemBase)1