Search in sources :

Example 1 with KtBlockExpression

use of org.jetbrains.kotlin.psi.KtBlockExpression 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 2 with KtBlockExpression

use of org.jetbrains.kotlin.psi.KtBlockExpression in project kotlin by JetBrains.

the class AbstractKotlinUpDownMover method getSourceRange.

@Nullable
protected LineRange getSourceRange(@NotNull PsiElement firstElement, @NotNull PsiElement lastElement, @NotNull Editor editor, LineRange oldRange) {
    PsiElement parent = PsiTreeUtil.findCommonParent(firstElement, lastElement);
    int topExtension = 0;
    int bottomExtension = 0;
    if (parent instanceof KtFunctionLiteral) {
        KtBlockExpression block = ((KtFunctionLiteral) parent).getBodyExpression();
        if (block != null) {
            PsiElement comment = null;
            boolean extendDown = false;
            if (checkCommentAtBlockBound(firstElement, lastElement, block)) {
                comment = lastElement;
                extendDown = true;
                lastElement = block.getLastChild();
            } else if (checkCommentAtBlockBound(lastElement, firstElement, block)) {
                comment = firstElement;
                firstElement = block.getFirstChild();
            }
            if (comment != null) {
                int extension = KotlinRefactoringUtilKt.getLineCount(comment);
                if (extendDown) {
                    bottomExtension = extension;
                } else {
                    topExtension = extension;
                }
            }
            parent = PsiTreeUtil.findCommonParent(firstElement, lastElement);
        }
    }
    if (parent == null)
        return null;
    Pair<PsiElement, PsiElement> originalRange = getElementRange(parent, firstElement, lastElement);
    if (!checkSourceElement(originalRange.first) || !checkSourceElement(originalRange.second))
        return null;
    LineRange lineRange1 = getElementSourceLineRange(originalRange.first, editor, oldRange);
    if (lineRange1 == null)
        return null;
    LineRange lineRange2 = getElementSourceLineRange(originalRange.second, editor, oldRange);
    if (lineRange2 == null)
        return null;
    LineRange parentLineRange = getElementSourceLineRange(parent, editor, oldRange);
    LineRange sourceRange = new LineRange(lineRange1.startLine - topExtension, lineRange2.endLine + bottomExtension);
    if (parentLineRange != null && sourceRange.startLine == parentLineRange.startLine && sourceRange.endLine == parentLineRange.endLine) {
        sourceRange.firstElement = sourceRange.lastElement = parent;
    } else {
        sourceRange.firstElement = originalRange.first;
        sourceRange.lastElement = originalRange.second;
    }
    return sourceRange;
}
Also used : KtFunctionLiteral(org.jetbrains.kotlin.psi.KtFunctionLiteral) KtBlockExpression(org.jetbrains.kotlin.psi.KtBlockExpression) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Nullable (org.jetbrains.annotations.Nullable)2 KtBlockExpression (org.jetbrains.kotlin.psi.KtBlockExpression)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 KtFunctionLiteral (org.jetbrains.kotlin.psi.KtFunctionLiteral)1 KtIfExpression (org.jetbrains.kotlin.psi.KtIfExpression)1