Search in sources :

Example 1 with KtFunctionLiteral

use of org.jetbrains.kotlin.psi.KtFunctionLiteral 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

LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 PsiElement (com.intellij.psi.PsiElement)1 Nullable (org.jetbrains.annotations.Nullable)1 KtBlockExpression (org.jetbrains.kotlin.psi.KtBlockExpression)1 KtFunctionLiteral (org.jetbrains.kotlin.psi.KtFunctionLiteral)1