diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java index 020114648b5..3fe21f12735 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/AbstractKotlinUpDownMover.java @@ -119,7 +119,7 @@ public abstract class AbstractKotlinUpDownMover extends LineMover { return sourceRange; } - protected static int getElementLine(PsiElement element, Editor editor, boolean first) { + protected static int getElementLine(@Nullable PsiElement element, @NotNull Editor editor, boolean first) { if (element == null) return -1; Document doc = editor.getDocument(); diff --git a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.java b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.java index a77085e063a..c7ecb17d65e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.java +++ b/idea/src/org/jetbrains/kotlin/idea/codeInsight/upDownMover/KotlinExpressionMover.java @@ -229,9 +229,8 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover { @Nullable private static LineRange getExpressionTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) { - - PsiElement start = sibling; - PsiElement end = sibling; + @Nullable PsiElement start = sibling; + @Nullable PsiElement end = sibling; if (!down) { if (sibling instanceof KtIfExpression) { @@ -350,31 +349,59 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover { } } - if (start == sibling && end == sibling) { - PsiElement comment = sibling; - while (true) { - int nextLine = getElementLine(comment, editor, !down) + (down ? 1 : -1); - comment = firstNonWhiteSibling(comment, down); - if (comment instanceof PsiComment && getElementLine(comment, editor, down) == nextLine) { - if (down) { - end = comment; - } else { - start = comment; - } - } - else break; - } - if (down && end instanceof PsiComment) { - PsiElement next = firstNonWhiteSibling(end, true); - if (getElementLine(next, editor, true) == getElementLine(end, editor, false) + 1) { - end = next; - } - } + Pair extended = extendForSiblingComments(start, end, sibling, editor, down); + if (extended != null) { + start = extended.first; + end = extended.second; } return start != null && end != null ? new LineRange(start, end, editor.getDocument()) : null; } + private static @Nullable Pair extendForSiblingComments( + @Nullable PsiElement start, @Nullable PsiElement end, @NotNull PsiElement sibling, + @NotNull Editor editor, boolean down + ) { + if (!(start == end && start == sibling)) { + return null; + } + + boolean hasUpdate = false; + + PsiElement current = sibling; + while (true) { + int nextLine = getElementLine(current, editor, !down) + (down ? 1 : -1); + + current = firstNonWhiteSibling(current, down); + if (!(current instanceof PsiComment)) { + break; + } + + if (getElementLine(current, editor, down) != nextLine) { + // An empty line is between current element and next sibling + break; + } + + hasUpdate = true; + if (down) { + end = current; + } + else { + start = current; + } + } + + if (down && end instanceof PsiComment) { + PsiElement next = firstNonWhiteSibling(end, true); + if (getElementLine(next, editor, true) == getElementLine(end, editor, false) + 1) { + hasUpdate = true; + end = next; + } + } + + return hasUpdate ? Pair.create(start, end) : null; + } + @Nullable private static LineRange getWhenEntryTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) { if (sibling.getNode().getElementType() == (down ? KtTokens.RBRACE : KtTokens.LBRACE) &&