use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method getValueParamOrArgTargetRange.
@Nullable
private LineRange getValueParamOrArgTargetRange(@NotNull Editor editor, @NotNull PsiElement elementToCheck, @NotNull PsiElement sibling, boolean down) {
PsiElement next = sibling;
if (next.getNode().getElementType() == KtTokens.COMMA) {
next = firstNonWhiteSibling(next, down);
}
LineRange range = (next instanceof KtParameter || next instanceof KtValueArgument) ? new LineRange(next, next, editor.getDocument()) : null;
if (range != null) {
parametersOrArgsToMove = new Pair<PsiElement, PsiElement>(elementToCheck, next);
}
return range;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method getExpressionTargetRange.
@Nullable
private static LineRange getExpressionTargetRange(@NotNull Editor editor, @NotNull PsiElement sibling, boolean down) {
if (sibling instanceof KtIfExpression && !down) {
KtExpression elseBranch = ((KtIfExpression) sibling).getElse();
if (elseBranch instanceof KtBlockExpression) {
sibling = elseBranch;
}
}
PsiElement start = sibling;
PsiElement end = sibling;
// moving out of code block
if (sibling.getNode().getElementType() == (down ? KtTokens.RBRACE : KtTokens.LBRACE)) {
PsiElement parent = sibling.getParent();
if (!(parent instanceof KtBlockExpression || parent instanceof KtFunctionLiteral))
return null;
KtBlockExpression newBlock;
if (parent instanceof KtFunctionLiteral) {
//noinspection ConstantConditions
newBlock = findClosestBlock(((KtFunctionLiteral) parent).getBodyExpression(), down, false);
if (!down) {
PsiElement arrow = ((KtFunctionLiteral) parent).getArrow();
if (arrow != null) {
end = arrow;
}
}
} else {
newBlock = findClosestBlock(sibling, down, true);
}
if (newBlock == null)
return null;
if (PsiTreeUtil.isAncestor(newBlock, parent, true)) {
PsiElement outermostParent = KtPsiUtil.getOutermostParent(parent, newBlock, true);
if (down) {
end = outermostParent;
} else {
start = outermostParent;
}
} else {
if (down) {
end = newBlock.getLBrace();
} else {
start = newBlock.getRBrace();
}
}
} else // moving into code block
{
PsiElement blockLikeElement;
KtBlockExpression dslBlock = getDSLLambdaBlock(sibling, down);
if (dslBlock != null) {
// Use JetFunctionLiteral (since it contains braces)
blockLikeElement = dslBlock.getParent();
} else {
// JetBlockExpression and other block-like elements
blockLikeElement = KtPsiUtil.getOutermostDescendantElement(sibling, down, CHECK_BLOCK_LIKE_ELEMENT);
}
if (blockLikeElement != null) {
if (down) {
end = KtPsiUtil.findChildByType(blockLikeElement, KtTokens.LBRACE);
if (blockLikeElement instanceof KtFunctionLiteral) {
PsiElement arrow = ((KtFunctionLiteral) blockLikeElement).getArrow();
if (arrow != null) {
end = arrow;
}
}
} else {
start = KtPsiUtil.findChildByType(blockLikeElement, KtTokens.RBRACE);
}
}
}
return start != null && end != null ? new LineRange(start, end, editor.getDocument()) : null;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method adjustSibling.
protected static PsiElement adjustSibling(@NotNull LineRange sourceRange, @NotNull MoveInfo info, boolean down) {
PsiElement element = down ? sourceRange.lastElement : sourceRange.firstElement;
PsiElement sibling = down ? element.getNextSibling() : element.getPrevSibling();
PsiElement whiteSpaceTestSubject = sibling;
if (sibling == null) {
PsiElement parent = element.getParent();
if (parent != null && isBracelessBlock(parent)) {
whiteSpaceTestSubject = down ? parent.getNextSibling() : parent.getPrevSibling();
}
}
if (whiteSpaceTestSubject instanceof PsiWhiteSpace) {
if (KotlinRefactoringUtilKt.isMultiLine(whiteSpaceTestSubject)) {
int nearLine = down ? sourceRange.endLine : sourceRange.startLine - 1;
info.toMove = sourceRange;
info.toMove2 = new LineRange(nearLine, nearLine + 1);
info.indentTarget = false;
return null;
}
if (sibling != null) {
sibling = firstNonWhiteElement(sibling, down);
}
}
if (sibling == null) {
KtCallExpression callExpression = PsiTreeUtil.getParentOfType(element, KtCallExpression.class);
if (callExpression != null) {
KtBlockExpression dslBlock = getDSLLambdaBlock(callExpression, down);
if (PsiTreeUtil.isAncestor(dslBlock, element, false)) {
//noinspection ConstantConditions
PsiElement blockParent = dslBlock.getParent();
return down ? KtPsiUtil.findChildByType(blockParent, KtTokens.RBRACE) : KtPsiUtil.findChildByType(blockParent, KtTokens.LBRACE);
}
}
info.toMove2 = null;
return null;
}
return sibling;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project intellij-plugins by JetBrains.
the class DartStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
if (!(file instanceof DartFile))
return false;
if (!super.checkAvailable(editor, file, info, down))
return false;
LineRange range = expandLineRangeToCoverPsiElements(info.toMove, editor, file);
if (range == null)
return false;
info.toMove = range;
final int startOffset = editor.logicalPositionToOffset(new LogicalPosition(range.startLine, 0));
final int endOffset = editor.logicalPositionToOffset(new LogicalPosition(range.endLine, 0));
PsiElement[] statements = DartRefactoringUtil.findListExpressionInRange(file, startOffset, endOffset);
if (statements.length == 1) {
// Disallow component mover
info.toMove2 = null;
// Require trailing comma
return true;
}
if (statements.length == 0) {
statements = DartRefactoringUtil.findStatementsInRange(file, startOffset, endOffset);
}
if (statements.length == 0)
return false;
range.firstElement = statements[0];
range.lastElement = statements[statements.length - 1];
info.indentTarget = true;
if (!checkMovingInsideOutside(file, editor, info, down)) {
info.toMove2 = null;
}
return true;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project intellij-plugins by JetBrains.
the class DartStatementMover method soloRightBraceBeingMoved.
private static PsiElement soloRightBraceBeingMoved(final PsiFile file, final Editor editor) {
// Return the right brace on the line with the cursor, or null if the line is not a single brace.
LineRange range = getLineRangeFromSelection(editor);
if (range.endLine - range.startLine != 1) {
return null;
}
Document document = editor.getDocument();
int offset = editor.getCaretModel().getOffset();
int line = document.getLineNumber(offset);
int lineStartOffset = document.getLineStartOffset(line);
String lineText = document.getText().substring(lineStartOffset, document.getLineEndOffset(line));
if (!lineText.trim().equals("}")) {
return null;
}
return file.findElementAt(lineStartOffset + lineText.indexOf('}'));
}
Aggregations