Minor refactoring for KotlinExpressionMover (KT-23461)

Extract function, some better naming.
This commit is contained in:
Nikolay Krasko
2019-09-13 16:05:25 +03:00
parent ed28a06285
commit 11de99f9d7
2 changed files with 51 additions and 24 deletions
@@ -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();
@@ -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<PsiElement, PsiElement> 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<PsiElement, PsiElement> 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) &&