Search in sources :

Example 6 with LineRange

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;
}
Also used : PsiComment(com.intellij.psi.PsiComment) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) PsiElement(com.intellij.psi.PsiElement)

Example 7 with LineRange

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;
}
Also used : LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange)

Example 8 with LineRange

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;
}
Also used : Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange)

Example 9 with LineRange

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);
}
Also used : TextRange(com.intellij.openapi.util.TextRange) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange)

Example 10 with LineRange

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('}'));
}
Also used : Document(com.intellij.openapi.editor.Document) LineRange(com.intellij.codeInsight.editorActions.moveUpDown.LineRange) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)24 Document (com.intellij.openapi.editor.Document)8 PsiElement (com.intellij.psi.PsiElement)8 Nullable (org.jetbrains.annotations.Nullable)6 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 TextRange (com.intellij.openapi.util.TextRange)3 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 PsiComment (com.intellij.psi.PsiComment)2 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)2 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)2 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)2 CaretModel (com.intellij.openapi.editor.CaretModel)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 Project (com.intellij.openapi.project.Project)1 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)1 ArrayList (java.util.ArrayList)1 KtBlockExpression (org.jetbrains.kotlin.psi.KtBlockExpression)1 KtFunctionLiteral (org.jetbrains.kotlin.psi.KtFunctionLiteral)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1