Minor refactoring for KotlinExpressionMover (KT-23461)
Extract function, some better naming.
This commit is contained in:
+1
-1
@@ -119,7 +119,7 @@ public abstract class AbstractKotlinUpDownMover extends LineMover {
|
|||||||
return sourceRange;
|
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;
|
if (element == null) return -1;
|
||||||
|
|
||||||
Document doc = editor.getDocument();
|
Document doc = editor.getDocument();
|
||||||
|
|||||||
+50
-23
@@ -229,9 +229,8 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static LineRange getExpressionTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
|
private static LineRange getExpressionTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
|
||||||
|
@Nullable PsiElement start = sibling;
|
||||||
PsiElement start = sibling;
|
@Nullable PsiElement end = sibling;
|
||||||
PsiElement end = sibling;
|
|
||||||
|
|
||||||
if (!down) {
|
if (!down) {
|
||||||
if (sibling instanceof KtIfExpression) {
|
if (sibling instanceof KtIfExpression) {
|
||||||
@@ -350,31 +349,59 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start == sibling && end == sibling) {
|
Pair<PsiElement, PsiElement> extended = extendForSiblingComments(start, end, sibling, editor, down);
|
||||||
PsiElement comment = sibling;
|
if (extended != null) {
|
||||||
while (true) {
|
start = extended.first;
|
||||||
int nextLine = getElementLine(comment, editor, !down) + (down ? 1 : -1);
|
end = extended.second;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return start != null && end != null ? new LineRange(start, end, editor.getDocument()) : null;
|
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
|
@Nullable
|
||||||
private static LineRange getWhenEntryTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
|
private static LineRange getWhenEntryTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
|
||||||
if (sibling.getNode().getElementType() == (down ? KtTokens.RBRACE : KtTokens.LBRACE) &&
|
if (sibling.getNode().getElementType() == (down ? KtTokens.RBRACE : KtTokens.LBRACE) &&
|
||||||
|
|||||||
Reference in New Issue
Block a user