use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinDeclarationMover method getElementSourceLineRange.
@Override
protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) {
PsiElement first;
PsiElement last;
if (element instanceof KtDeclaration) {
first = element.getFirstChild();
last = element.getLastChild();
if (first == null || last == null)
return null;
} else {
first = last = element;
}
TextRange textRange1 = first.getTextRange();
TextRange textRange2 = last.getTextRange();
Document doc = editor.getDocument();
if (doc.getTextLength() < textRange2.getEndOffset())
return null;
int startLine = editor.offsetToLogicalPosition(textRange1.getStartOffset()).line;
int endLine = editor.offsetToLogicalPosition(textRange2.getEndOffset()).line + 1;
if (element instanceof PsiComment || startLine == oldRange.startLine || startLine == oldRange.endLine || endLine == oldRange.startLine || endLine == oldRange.endLine) {
return new LineRange(startLine, endLine);
}
TextRange lineTextRange = new TextRange(doc.getLineStartOffset(oldRange.startLine), doc.getLineEndOffset(oldRange.endLine));
if (element instanceof KtDeclaration) {
for (PsiElement anchor : getDeclarationAnchors((KtDeclaration) element)) {
TextRange suspectTextRange = anchor.getTextRange();
if (suspectTextRange != null && lineTextRange.intersects(suspectTextRange))
return new LineRange(startLine, endLine);
}
}
return null;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
parametersOrArgsToMove = null;
if (!super.checkAvailable(editor, file, info, down))
return false;
switch(checkForMovableClosingBrace(editor, file, info, down)) {
case NOT_MOVABLE:
{
info.toMove2 = null;
return true;
}
case MOVABLE:
return true;
default:
break;
}
LineRange oldRange = info.toMove;
Pair<PsiElement, PsiElement> psiRange = getElementRange(editor, file, oldRange);
if (psiRange == null)
return false;
//noinspection unchecked
PsiElement firstElement = getMovableElement(psiRange.getFirst(), false);
PsiElement lastElement = getMovableElement(psiRange.getSecond(), true);
if (firstElement == null || lastElement == null)
return false;
if (isForbiddenMove(firstElement, down) || isForbiddenMove(lastElement, down)) {
info.toMove2 = null;
return true;
}
if ((firstElement instanceof KtParameter || firstElement instanceof KtValueArgument) && PsiTreeUtil.isAncestor(lastElement, firstElement, false)) {
lastElement = firstElement;
}
LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange);
if (sourceRange == null)
return false;
PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(sourceRange, info, down), editor, down);
// Either reached last sibling, or jumped over multi-line whitespace
if (sibling == null)
return true;
info.toMove = sourceRange;
info.toMove2 = getTargetRange(editor, sourceRange.firstElement, sibling, down);
return true;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method checkForMovableUpClosingBrace.
private static BraceStatus checkForMovableUpClosingBrace(@NotNull PsiElement closingBrace, PsiElement block, @NotNull Editor editor, @NotNull MoveInfo info) {
//noinspection unchecked
PsiElement prev = KtPsiUtil.getLastChildByType(block, KtExpression.class);
if (prev == null)
return BraceStatus.NOT_MOVABLE;
Document doc = editor.getDocument();
info.toMove = new LineRange(closingBrace, closingBrace, doc);
info.toMove2 = new LineRange(prev, prev, doc);
info.indentSource = true;
return BraceStatus.MOVABLE;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method getElementSourceLineRange.
@Override
protected LineRange getElementSourceLineRange(@NotNull PsiElement element, @NotNull Editor editor, @NotNull LineRange oldRange) {
TextRange textRange = element.getTextRange();
if (editor.getDocument().getTextLength() < textRange.getEndOffset())
return null;
int startLine = editor.offsetToLogicalPosition(textRange.getStartOffset()).line;
int endLine = editor.offsetToLogicalPosition(textRange.getEndOffset()).line + 1;
return new LineRange(startLine, endLine);
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project kotlin by JetBrains.
the class KotlinExpressionMover method getStandaloneClosingBrace.
@Nullable
private static PsiElement getStandaloneClosingBrace(@NotNull PsiFile file, @NotNull Editor editor) {
LineRange range = getLineRangeFromSelection(editor);
if (range.endLine - range.startLine != 1)
return null;
int offset = editor.getCaretModel().getOffset();
Document document = editor.getDocument();
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