Move statement up/down fails for multiline declarations with lambdas (KT-21013)
#KT-21013 Fixed
This commit is contained in:
committed by
Nikolay Krasko
parent
f41165c566
commit
3f96e1dabc
+11
-5
@@ -209,7 +209,7 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static KtBlockExpression getDSLLambdaBlock(@NotNull PsiElement element, boolean down) {
|
private static KtBlockExpression getDSLLambdaBlock(@NotNull Editor editor, @NotNull PsiElement element, boolean down) {
|
||||||
KtCallExpression callExpression =
|
KtCallExpression callExpression =
|
||||||
(KtCallExpression) KtPsiUtil.getOutermostDescendantElement(element, down, IS_CALL_EXPRESSION);
|
(KtCallExpression) KtPsiUtil.getOutermostDescendantElement(element, down, IS_CALL_EXPRESSION);
|
||||||
if (callExpression == null) return null;
|
if (callExpression == null) return null;
|
||||||
@@ -219,6 +219,11 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
|
|
||||||
KtLambdaExpression lambdaExpression = functionLiterals.get(0).getLambdaExpression();
|
KtLambdaExpression lambdaExpression = functionLiterals.get(0).getLambdaExpression();
|
||||||
if (lambdaExpression == null) return null;
|
if (lambdaExpression == null) return null;
|
||||||
|
|
||||||
|
Document document = editor.getDocument();
|
||||||
|
TextRange range = lambdaExpression.getTextRange();
|
||||||
|
if (document.getLineNumber(range.getStartOffset()) == document.getLineNumber(range.getEndOffset())) return null;
|
||||||
|
|
||||||
return lambdaExpression.getBodyExpression();
|
return lambdaExpression.getBodyExpression();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -279,7 +284,7 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
else {
|
else {
|
||||||
PsiElement blockLikeElement;
|
PsiElement blockLikeElement;
|
||||||
|
|
||||||
KtBlockExpression dslBlock = getDSLLambdaBlock(sibling, down);
|
KtBlockExpression dslBlock = getDSLLambdaBlock(editor, sibling, down);
|
||||||
if (dslBlock != null) {
|
if (dslBlock != null) {
|
||||||
// Use JetFunctionLiteral (since it contains braces)
|
// Use JetFunctionLiteral (since it contains braces)
|
||||||
blockLikeElement = dslBlock.getParent();
|
blockLikeElement = dslBlock.getParent();
|
||||||
@@ -417,7 +422,8 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
return block.getLBrace() == null && block.getRBrace() == null;
|
return block.getLBrace() == null && block.getRBrace() == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static PsiElement adjustSibling(
|
private static PsiElement adjustSibling(
|
||||||
|
@NotNull Editor editor,
|
||||||
@NotNull LineRange sourceRange,
|
@NotNull LineRange sourceRange,
|
||||||
@NotNull MoveInfo info,
|
@NotNull MoveInfo info,
|
||||||
boolean down
|
boolean down
|
||||||
@@ -452,7 +458,7 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
if (sibling == null) {
|
if (sibling == null) {
|
||||||
KtCallExpression callExpression = PsiTreeUtil.getParentOfType(element, KtCallExpression.class);
|
KtCallExpression callExpression = PsiTreeUtil.getParentOfType(element, KtCallExpression.class);
|
||||||
if (callExpression != null) {
|
if (callExpression != null) {
|
||||||
KtBlockExpression dslBlock = getDSLLambdaBlock(callExpression, down);
|
KtBlockExpression dslBlock = getDSLLambdaBlock(editor, callExpression, down);
|
||||||
if (PsiTreeUtil.isAncestor(dslBlock, element, false)) {
|
if (PsiTreeUtil.isAncestor(dslBlock, element, false)) {
|
||||||
//noinspection ConstantConditions
|
//noinspection ConstantConditions
|
||||||
PsiElement blockParent = dslBlock.getParent();
|
PsiElement blockParent = dslBlock.getParent();
|
||||||
@@ -510,7 +516,7 @@ public class KotlinExpressionMover extends AbstractKotlinUpDownMover {
|
|||||||
LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange);
|
LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange);
|
||||||
if (sourceRange == null) return false;
|
if (sourceRange == null) return false;
|
||||||
|
|
||||||
PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(sourceRange, info, down), editor, down);
|
PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(editor, sourceRange, info, down), editor, down);
|
||||||
|
|
||||||
// Either reached last sibling, or jumped over multi-line whitespace
|
// Either reached last sibling, or jumped over multi-line whitespace
|
||||||
if (sibling == null) return true;
|
if (sibling == null) return true;
|
||||||
|
|||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
fun test() {
|
||||||
|
(0..10)
|
||||||
|
.map { it }
|
||||||
|
<caret>println()
|
||||||
|
}
|
||||||
+6
@@ -0,0 +1,6 @@
|
|||||||
|
// MOVE: up
|
||||||
|
fun test() {
|
||||||
|
<caret>println()
|
||||||
|
(0..10)
|
||||||
|
.map { it }
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// MOVE: up
|
||||||
|
fun test() {
|
||||||
|
(0..10)
|
||||||
|
.map {
|
||||||
|
it
|
||||||
|
}
|
||||||
|
<caret>println()
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
// MOVE: up
|
||||||
|
fun test() {
|
||||||
|
(0..10)
|
||||||
|
.map {
|
||||||
|
it
|
||||||
|
<caret>println()
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+12
@@ -1089,6 +1089,18 @@ public class MoveStatementTestGenerated extends AbstractMoveStatementTest {
|
|||||||
doTestExpression(fileName);
|
doTestExpression(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineExpressionWithClosure1.kt")
|
||||||
|
public void testMultilineExpressionWithClosure1() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/multilineExpressionWithClosure1.kt");
|
||||||
|
doTestExpression(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("multilineExpressionWithClosure2.kt")
|
||||||
|
public void testMultilineExpressionWithClosure2() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/multilineExpressionWithClosure2.kt");
|
||||||
|
doTestExpression(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("outOfClosure1.kt")
|
@TestMetadata("outOfClosure1.kt")
|
||||||
public void testOutOfClosure1() throws Exception {
|
public void testOutOfClosure1() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/outOfClosure1.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/codeInsight/moveUpDown/expressions/outOfClosure1.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user