use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
if (!(file instanceof GoFile && super.checkAvailable(editor, file, info, down)))
return false;
Couple<PsiElement> primeElementRange = getElementRange(editor, file);
if (primeElementRange == null)
return false;
PsiElement commonParent = primeElementRange.first.isEquivalentTo(primeElementRange.second) ? primeElementRange.first.getParent() : PsiTreeUtil.findCommonParent(primeElementRange.first, primeElementRange.second);
if (commonParent == null)
return false;
Couple<PsiElement> elementRange = getTopmostElementRange(primeElementRange, commonParent);
if (elementRange == null)
return false;
if (commonParent == elementRange.first)
commonParent = commonParent.getParent();
info.toMove = new LineRange(elementRange.first, elementRange.second);
if (elementRange.first instanceof GoTopLevelDeclaration && commonParent instanceof GoFile) {
PsiElement toMove2 = getNeighborOfType(elementRange, GoTopLevelDeclaration.class, down);
info.toMove2 = toMove2 != null ? new LineRange(toMove2) : null;
return true;
}
if (commonParent instanceof GoImportList) {
PsiElement toMove2 = getNeighborOfType(elementRange, GoImportDeclaration.class, down);
info.toMove2 = toMove2 != null ? new LineRange(toMove2) : null;
return true;
}
return setUpInfo(info, elementRange, commonParent, down);
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoStatementMover method setUpInfo.
private static boolean setUpInfo(@NotNull MoveInfo info, @NotNull Couple<PsiElement> range, @NotNull PsiElement commonParent, boolean down) {
info.toMove = new LineRange(range.first, range.second);
info.toMove2 = null;
if (range.first instanceof GoPackageClause)
return true;
PsiElement topLevelElement = PsiTreeUtil.findPrevParent(commonParent.getContainingFile(), commonParent);
int nearLine = down ? info.toMove.endLine : info.toMove.startLine - 1;
LineRange lineRange = new LineRange(topLevelElement);
if (!lineRange.containsLine(down ? info.toMove.endLine + 1 : info.toMove.startLine - 2)) {
return true;
}
info.toMove2 = lineRange.containsLine(down ? info.toMove.endLine + 1 : info.toMove.startLine - 2) ? new LineRange(nearLine, nearLine + 1) : null;
return true;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project intellij-community by JetBrains.
the class GroovyStatementMover method checkAvailable.
@Override
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
final Project project = file.getProject();
if (!HandlerUtils.canBeInvoked(editor, project) || !(file instanceof GroovyFileBase))
return false;
LineRange range = getLineRangeFromSelection(editor);
final Document document = editor.getDocument();
final int offset = document.getLineStartOffset(range.startLine);
final GrLiteral literal = PsiTreeUtil.findElementOfClassAtOffset(file, offset, GrLiteral.class, false);
//multiline string
if (literal != null && literal.textContains('\n'))
return false;
final GroovyPsiElement pivot = getElementToMove((GroovyFileBase) file, offset);
if (pivot == null)
return false;
final LineRange pivotRange = getLineRange(pivot);
range = new LineRange(Math.min(range.startLine, pivotRange.startLine), Math.max(range.endLine, pivotRange.endLine));
final GroovyPsiElement scope = PsiTreeUtil.getParentOfType(pivot, GrMethod.class, GrTypeDefinitionBody.class, GroovyFileBase.class);
final boolean stmtLevel = isStatement(pivot);
boolean topLevel = pivot instanceof GrTypeDefinition && pivot.getParent() instanceof GroovyFileBase;
final List<LineRange> allRanges = allRanges(scope, stmtLevel, topLevel);
LineRange prev = null;
LineRange next = null;
for (LineRange each : allRanges) {
if (each.endLine <= range.startLine) {
prev = each;
}
if (each.containsLine(range.startLine)) {
range = new LineRange(each.startLine, range.endLine);
}
if (each.startLine < range.endLine && each.endLine > range.endLine) {
range = new LineRange(range.startLine, each.endLine);
}
if (each.startLine >= range.endLine && next == null) {
next = each;
}
}
info.toMove = range;
info.toMove2 = down ? next : prev;
return true;
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project intellij-community by JetBrains.
the class PyStatementMover method beforeMove.
@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
final LineRange toMove = info.toMove;
final LineRange toMove2 = info.toMove2;
if (toMove instanceof MyLineRange && toMove2 instanceof ScopeRange) {
PostprocessReformattingAspect.getInstance(editor.getProject()).disablePostprocessFormattingInside(() -> {
final PsiElement startToMove = ((MyLineRange) toMove).myStartElement;
final PsiElement endToMove = ((MyLineRange) toMove).myEndElement;
final PsiFile file = startToMove.getContainingFile();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
final int selectionStart = selectionModel.getSelectionStart();
boolean isSelectionStartAtCaret = caretModel.getOffset() == selectionStart;
final SelectionContainer selectionLen = getSelectionLenContainer(editor, ((MyLineRange) toMove));
int shift = getCaretShift(startToMove, endToMove, caretModel, isSelectionStartAtCaret);
final boolean hasSelection = selectionModel.hasSelection();
int offset;
if (((ScopeRange) toMove2).isTheSameLevel()) {
offset = moveTheSameLevel((ScopeRange) toMove2, (MyLineRange) toMove);
} else {
offset = moveInOut(((MyLineRange) toMove), editor, info);
}
restoreCaretAndSelection(file, editor, isSelectionStartAtCaret, hasSelection, selectionLen, shift, offset, (MyLineRange) toMove);
//do not move further
info.toMove2 = info.toMove;
});
}
}
use of com.intellij.codeInsight.editorActions.moveUpDown.LineRange in project intellij-community by JetBrains.
the class PyStatementMover method getDestinationScope.
@Nullable
private static LineRange getDestinationScope(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final PsiElement elementToMove, boolean down) {
final Document document = file.getViewProvider().getDocument();
if (document == null)
return null;
final int offset = down ? elementToMove.getTextRange().getEndOffset() : elementToMove.getTextRange().getStartOffset();
int lineNumber = down ? document.getLineNumber(offset) + 1 : document.getLineNumber(offset) - 1;
if (moveOutsideFile(document, lineNumber))
return null;
int lineEndOffset = document.getLineEndOffset(lineNumber);
final int startOffset = document.getLineStartOffset(lineNumber);
final PyStatementList statementList = getStatementList(elementToMove);
final PsiElement destination = getDestinationElement(elementToMove, document, lineEndOffset, down);
final int start = destination != null ? destination.getTextRange().getStartOffset() : lineNumber;
final int end = destination != null ? destination.getTextRange().getEndOffset() : lineNumber;
final int startLine = document.getLineNumber(start);
final int endLine = document.getLineNumber(end);
if (elementToMove instanceof PyClass || elementToMove instanceof PyFunction) {
PyElement scope = statementList == null ? (PyElement) elementToMove.getContainingFile() : statementList;
if (destination != null)
return new ScopeRange(scope, destination, !down, true);
}
final String lineText = document.getText(TextRange.create(startOffset, lineEndOffset));
final boolean isEmptyLine = StringUtil.isEmptyOrSpaces(lineText);
if (isEmptyLine && moveToEmptyLine(elementToMove, down))
return new LineRange(lineNumber, lineNumber + 1);
LineRange scopeRange = moveOut(elementToMove, editor, down);
if (scopeRange != null)
return scopeRange;
scopeRange = moveInto(elementToMove, file, editor, down, lineEndOffset);
if (scopeRange != null)
return scopeRange;
if (elementToMove instanceof PsiComment && (PsiTreeUtil.isAncestor(destination, elementToMove, true)) || destination instanceof PsiComment) {
return new LineRange(lineNumber, lineNumber + 1);
}
final PyElement scope = statementList == null ? (PyElement) elementToMove.getContainingFile() : statementList;
if ((elementToMove instanceof PyClass) || (elementToMove instanceof PyFunction))
return new ScopeRange(scope, scope.getFirstChild(), !down, true);
return new LineRange(startLine, endLine + 1);
}
Aggregations