mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Cleanup (warnings; formatting)
This commit is contained in:
+85
-73
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,16 +17,17 @@ package com.intellij.codeInsight.editorActions.moveUpDown;
|
||||
|
||||
import com.intellij.codeInsight.CodeInsightUtil;
|
||||
import com.intellij.codeInsight.CodeInsightUtilCore;
|
||||
import com.intellij.lang.StdLanguages;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.editor.LogicalPosition;
|
||||
import com.intellij.openapi.editor.RangeMarker;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.TextRange;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.PsiDocumentManagerImpl;
|
||||
import com.intellij.psi.impl.PsiDocumentManagerBase;
|
||||
import com.intellij.psi.impl.source.jsp.jspJava.JspClassLevelDeclarationStatement;
|
||||
import com.intellij.psi.impl.source.jsp.jspJava.JspTemplateStatement;
|
||||
import com.intellij.psi.search.PsiElementProcessor;
|
||||
@@ -37,42 +38,46 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class StatementMover extends LineMover {
|
||||
private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.editor.actions.moveUpDown.StatementMover");
|
||||
|
||||
private PsiElement statementToSurroundWithCodeBlock;
|
||||
private static final Key<PsiElement> STATEMENT_TO_SURROUND_WITH_CODE_BLOCK_KEY = Key.create("STATEMENT_TO_SURROUND_WITH_CODE_BLOCK_KEY");
|
||||
|
||||
@Override
|
||||
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
|
||||
public void beforeMove(@NotNull Editor editor, @NotNull MoveInfo info, boolean down) {
|
||||
super.beforeMove(editor, info, down);
|
||||
if (statementToSurroundWithCodeBlock != null) {
|
||||
surroundWithCodeBlock(info, down);
|
||||
|
||||
PsiElement statement = STATEMENT_TO_SURROUND_WITH_CODE_BLOCK_KEY.get(info);
|
||||
if (statement != null) {
|
||||
surroundWithCodeBlock(info, down, statement);
|
||||
}
|
||||
}
|
||||
|
||||
private void surroundWithCodeBlock(@NotNull final MoveInfo info, final boolean down) {
|
||||
private static void surroundWithCodeBlock(MoveInfo info, boolean down, PsiElement statement) {
|
||||
try {
|
||||
final Document document = PsiDocumentManager.getInstance(statementToSurroundWithCodeBlock.getProject()).getDocument(statementToSurroundWithCodeBlock.getContainingFile());
|
||||
Document document = PsiDocumentManager.getInstance(statement.getProject()).getDocument(statement.getContainingFile());
|
||||
assert document != null : statement.getContainingFile();
|
||||
int startOffset = document.getLineStartOffset(info.toMove.startLine);
|
||||
int endOffset = getLineStartSafeOffset(document, info.toMove.endLine);
|
||||
if (document.getText().charAt(endOffset-1) == '\n') endOffset--;
|
||||
final RangeMarker lineRangeMarker = document.createRangeMarker(startOffset, endOffset);
|
||||
RangeMarker lineRangeMarker = document.createRangeMarker(startOffset, endOffset);
|
||||
|
||||
final PsiElementFactory factory = JavaPsiFacade.getInstance(statementToSurroundWithCodeBlock.getProject()).getElementFactory();
|
||||
PsiElementFactory factory = JavaPsiFacade.getInstance(statement.getProject()).getElementFactory();
|
||||
PsiCodeBlock codeBlock = factory.createCodeBlock();
|
||||
codeBlock.add(statementToSurroundWithCodeBlock);
|
||||
final PsiBlockStatement blockStatement = (PsiBlockStatement)factory.createStatementFromText("{}", statementToSurroundWithCodeBlock);
|
||||
codeBlock.add(statement);
|
||||
PsiBlockStatement blockStatement = (PsiBlockStatement)factory.createStatementFromText("{}", statement);
|
||||
blockStatement.getCodeBlock().replace(codeBlock);
|
||||
PsiBlockStatement newStatement = (PsiBlockStatement)statementToSurroundWithCodeBlock.replace(blockStatement);
|
||||
PsiBlockStatement newStatement = (PsiBlockStatement)statement.replace(blockStatement);
|
||||
newStatement = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(newStatement);
|
||||
info.toMove = new LineRange(document.getLineNumber(lineRangeMarker.getStartOffset()), document.getLineNumber(lineRangeMarker.getEndOffset())+1);
|
||||
PsiCodeBlock newCodeBlock = newStatement.getCodeBlock();
|
||||
if (down) {
|
||||
PsiElement blockChild = firstNonWhiteElement(newCodeBlock.getFirstBodyElement(), true);
|
||||
if (blockChild == null) blockChild = newCodeBlock.getRBrace();
|
||||
info.toMove2 = new LineRange(info.toMove2.startLine, //document.getLineNumber(newCodeBlock.getParent().getTextRange().getStartOffset()),
|
||||
document.getLineNumber(blockChild.getTextRange().getStartOffset()));
|
||||
assert blockChild != null : newCodeBlock;
|
||||
info.toMove2 = new LineRange(info.toMove2.startLine, document.getLineNumber(blockChild.getTextRange().getStartOffset()));
|
||||
}
|
||||
else {
|
||||
int start = document.getLineNumber(newCodeBlock.getRBrace().getTextRange().getStartOffset());
|
||||
PsiJavaToken brace = newCodeBlock.getRBrace();
|
||||
assert brace != null : newCodeBlock;
|
||||
int start = document.getLineNumber(brace.getTextRange().getStartOffset());
|
||||
int end = info.toMove.startLine;
|
||||
if (start > end) end = start;
|
||||
info.toMove2 = new LineRange(start, end);
|
||||
@@ -84,71 +89,69 @@ class StatementMover extends LineMover {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkAvailable(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down) {
|
||||
//if (!(file instanceof PsiJavaFile)) return false;
|
||||
final boolean available = super.checkAvailable(editor, file, info, down);
|
||||
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
|
||||
boolean available = super.checkAvailable(editor, file, info, down);
|
||||
if (!available) return false;
|
||||
LineRange range = info.toMove;
|
||||
|
||||
range = expandLineRangeToCoverPsiElements(range, editor, file);
|
||||
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));
|
||||
final PsiElement[] statements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
|
||||
int startOffset = editor.logicalPositionToOffset(new LogicalPosition(range.startLine, 0));
|
||||
int endOffset = editor.logicalPositionToOffset(new LogicalPosition(range.endLine, 0));
|
||||
PsiElement[] statements = CodeInsightUtil.findStatementsInRange(file, startOffset, endOffset);
|
||||
if (statements.length == 0) return false;
|
||||
|
||||
range.firstElement = statements[0];
|
||||
range.lastElement = statements[statements.length-1];
|
||||
|
||||
if (!checkMovingInsideOutside(file, editor, info, down)) {
|
||||
info.toMove2 = null;
|
||||
return true;
|
||||
return info.prohibitMove();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static int getDestLineForAnon(Editor editor, LineRange range, boolean down) {
|
||||
int destLine = down ? range.endLine+1 : range.startLine - 1;
|
||||
private static int getDestLineForAnonymous(Editor editor, LineRange range, boolean down) {
|
||||
int destLine = down ? range.endLine + 1 : range.startLine - 1;
|
||||
if (!(range.firstElement instanceof PsiStatement)) {
|
||||
return destLine;
|
||||
}
|
||||
PsiElement sibling =
|
||||
StatementUpDownMover.firstNonWhiteElement(down ? range.lastElement.getNextSibling() : range.firstElement.getPrevSibling(), down);
|
||||
final PsiClass aClass = findChildOfType(sibling, PsiClass.class, PsiStatement.class);
|
||||
if (aClass != null && PsiTreeUtil.getParentOfType(aClass, PsiStatement.class) == sibling) {
|
||||
destLine =
|
||||
editor.getDocument().getLineNumber(down ? sibling.getTextRange().getEndOffset() + 1 : sibling.getTextRange().getStartOffset());
|
||||
|
||||
PsiElement sibling = firstNonWhiteElement(down ? range.lastElement.getNextSibling() : range.firstElement.getPrevSibling(), down);
|
||||
if (sibling != null) {
|
||||
PsiClass aClass = findChildOfType(sibling, PsiClass.class, PsiStatement.class);
|
||||
if (aClass != null && PsiTreeUtil.getParentOfType(aClass, PsiStatement.class) == sibling) {
|
||||
destLine = editor.getDocument().getLineNumber(down ? sibling.getTextRange().getEndOffset() + 1 : sibling.getTextRange().getStartOffset());
|
||||
}
|
||||
}
|
||||
|
||||
return destLine;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static <T extends PsiElement> T findChildOfType(@Nullable final PsiElement element,
|
||||
@NotNull final Class<T> aClass,
|
||||
@Nullable final Class<? extends PsiElement> stopAt) {
|
||||
final PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {
|
||||
private static <T extends PsiElement> T findChildOfType(PsiElement element, Class<T> aClass, Class<? extends PsiElement> stopAt) {
|
||||
PsiElementProcessor.FindElement<PsiElement> processor = new PsiElementProcessor.FindElement<PsiElement>() {
|
||||
@Override
|
||||
public boolean execute(@NotNull PsiElement each) {
|
||||
if (each == element) return true; // strict
|
||||
if (aClass.isInstance(each)) {
|
||||
return setFound(each);
|
||||
}
|
||||
if (aClass.isInstance(each)) return setFound(each);
|
||||
return stopAt == null || !stopAt.isInstance(each);
|
||||
}
|
||||
};
|
||||
|
||||
PsiTreeUtil.processElements(element, processor);
|
||||
//noinspection unchecked
|
||||
return (T)processor.getFoundElement();
|
||||
@SuppressWarnings("unchecked") T result = (T)processor.getFoundElement();
|
||||
return result;
|
||||
}
|
||||
|
||||
private boolean calcInsertOffset(@NotNull PsiFile file, @NotNull Editor editor, @NotNull LineRange range, @NotNull final MoveInfo info, final boolean down) {
|
||||
int destLine = getDestLineForAnon(editor, range, down);
|
||||
private static boolean calcInsertOffset(PsiFile file, Editor editor, LineRange range, MoveInfo info, boolean down) {
|
||||
int destLine = getDestLineForAnonymous(editor, range, down);
|
||||
|
||||
int startLine = down ? range.endLine : range.startLine - 1;
|
||||
if (destLine < 0 || startLine < 0) return false;
|
||||
while (true) {
|
||||
final int offset = editor.logicalPositionToOffset(new LogicalPosition(destLine, 0));
|
||||
int offset = editor.logicalPositionToOffset(new LogicalPosition(destLine, 0));
|
||||
PsiElement element = firstNonWhiteElement(offset, file, true);
|
||||
|
||||
while (element != null && !(element instanceof PsiFile)) {
|
||||
@@ -170,7 +173,7 @@ class StatementMover extends LineMover {
|
||||
found = true;
|
||||
}
|
||||
if (found) {
|
||||
statementToSurroundWithCodeBlock = elementToSurround;
|
||||
STATEMENT_TO_SURROUND_WITH_CODE_BLOCK_KEY.set(info, elementToSurround);
|
||||
info.toMove = range;
|
||||
int endLine = destLine;
|
||||
if (startLine > endLine) {
|
||||
@@ -192,14 +195,14 @@ class StatementMover extends LineMover {
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean statementCanBePlacedAlong(final PsiElement element) {
|
||||
private static boolean statementCanBePlacedAlong(PsiElement element) {
|
||||
if (element instanceof JspTemplateStatement) {
|
||||
PsiElement neighbour = element.getPrevSibling();
|
||||
// we can place statement inside scriptlet only
|
||||
return neighbour != null && !(neighbour instanceof JspTemplateStatement);
|
||||
}
|
||||
if (element instanceof PsiBlockStatement) return false;
|
||||
final PsiElement parent = element.getParent();
|
||||
PsiElement parent = element.getParent();
|
||||
if (parent instanceof JspClassLevelDeclarationStatement) return false;
|
||||
if (parent instanceof PsiCodeBlock) return true;
|
||||
if (parent instanceof PsiIfStatement &&
|
||||
@@ -216,10 +219,10 @@ class StatementMover extends LineMover {
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean checkMovingInsideOutside(PsiFile file, final Editor editor, @NotNull final MoveInfo info, final boolean down) {
|
||||
final int offset = editor.getCaretModel().getOffset();
|
||||
private static boolean checkMovingInsideOutside(PsiFile file, Editor editor, @NotNull MoveInfo info, boolean down) {
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
|
||||
PsiElement elementAtOffset = file.getViewProvider().findElementAt(offset, StdLanguages.JAVA);
|
||||
PsiElement elementAtOffset = file.getViewProvider().findElementAt(offset, JavaLanguage.INSTANCE);
|
||||
if (elementAtOffset == null) return false;
|
||||
|
||||
PsiElement guard = elementAtOffset;
|
||||
@@ -231,23 +234,24 @@ class StatementMover extends LineMover {
|
||||
PsiElement brace = itIsTheClosingCurlyBraceWeAreMoving(file, editor);
|
||||
if (brace != null) {
|
||||
int line = editor.getDocument().getLineNumber(offset);
|
||||
final LineRange toMove = new LineRange(line, line + 1);
|
||||
LineRange toMove = new LineRange(line, line + 1);
|
||||
toMove.firstElement = toMove.lastElement = brace;
|
||||
info.toMove = toMove;
|
||||
}
|
||||
|
||||
// cannot move in/outside method/class/initializer/comment
|
||||
if (!calcInsertOffset(file, editor, info.toMove, info, down)) return false;
|
||||
int insertOffset = down ? getLineStartSafeOffset(editor.getDocument(), info.toMove2.endLine) : editor.getDocument().getLineStartOffset(info.toMove2.startLine);
|
||||
PsiElement elementAtInsertOffset = file.getViewProvider().findElementAt(insertOffset, StdLanguages.JAVA);
|
||||
int insertOffset = down ? getLineStartSafeOffset(editor.getDocument(), info.toMove2.endLine)
|
||||
: editor.getDocument().getLineStartOffset(info.toMove2.startLine);
|
||||
PsiElement elementAtInsertOffset = file.getViewProvider().findElementAt(insertOffset, JavaLanguage.INSTANCE);
|
||||
PsiElement newGuard = elementAtInsertOffset;
|
||||
do {
|
||||
newGuard = PsiTreeUtil.getParentOfType(newGuard, PsiMethod.class, PsiClassInitializer.class, PsiClass.class, PsiComment.class);
|
||||
}
|
||||
while (newGuard instanceof PsiAnonymousClass);
|
||||
|
||||
if (brace != null && PsiTreeUtil.getParentOfType(brace, PsiCodeBlock.class, false) !=
|
||||
PsiTreeUtil.getParentOfType(elementAtInsertOffset, PsiCodeBlock.class, false)) {
|
||||
if (brace != null &&
|
||||
PsiTreeUtil.getParentOfType(brace, PsiCodeBlock.class, false) != PsiTreeUtil.getParentOfType(elementAtInsertOffset, PsiCodeBlock.class, false)) {
|
||||
info.indentSource = true;
|
||||
}
|
||||
if (newGuard == guard && isInside(insertOffset, newGuard) == isInside(offset, guard)) return true;
|
||||
@@ -259,28 +263,38 @@ class StatementMover extends LineMover {
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isInside(final int offset, final PsiElement guard) {
|
||||
private static boolean isInside(int offset, PsiElement guard) {
|
||||
if (guard == null) return false;
|
||||
TextRange inside = guard instanceof PsiMethod
|
||||
? ((PsiMethod)guard).getBody().getTextRange()
|
||||
: guard instanceof PsiClassInitializer
|
||||
? ((PsiClassInitializer)guard).getBody().getTextRange()
|
||||
: guard instanceof PsiClass ? new TextRange(((PsiClass)guard).getLBrace().getTextOffset(),
|
||||
((PsiClass)guard).getRBrace().getTextOffset()) : guard.getTextRange();
|
||||
|
||||
TextRange inside;
|
||||
if (guard instanceof PsiMethod) {
|
||||
PsiCodeBlock body = ((PsiMethod)guard).getBody();
|
||||
inside = body != null ? body.getTextRange() : null;
|
||||
}
|
||||
else if (guard instanceof PsiClassInitializer) {
|
||||
inside = ((PsiClassInitializer)guard).getBody().getTextRange();
|
||||
}
|
||||
else if (guard instanceof PsiClass) {
|
||||
PsiElement left = ((PsiClass)guard).getLBrace(), right = ((PsiClass)guard).getRBrace();
|
||||
inside = left != null && right != null ? new TextRange(left.getTextOffset(), right.getTextOffset()) : null;
|
||||
}
|
||||
else {
|
||||
inside = guard.getTextRange();
|
||||
}
|
||||
return inside != null && inside.contains(offset);
|
||||
}
|
||||
|
||||
private static LineRange expandLineRangeToCoverPsiElements(final LineRange range, Editor editor, final PsiFile file) {
|
||||
private static LineRange expandLineRangeToCoverPsiElements(LineRange range, Editor editor, PsiFile file) {
|
||||
Pair<PsiElement, PsiElement> psiRange = getElementRange(editor, file, range);
|
||||
if (psiRange == null) return null;
|
||||
final PsiElement parent = PsiTreeUtil.findCommonParent(psiRange.getFirst(), psiRange.getSecond());
|
||||
PsiElement parent = PsiTreeUtil.findCommonParent(psiRange.getFirst(), psiRange.getSecond());
|
||||
Pair<PsiElement, PsiElement> elementRange = getElementRange(parent, psiRange.getFirst(), psiRange.getSecond());
|
||||
if (elementRange == null) return null;
|
||||
int endOffset = elementRange.getSecond().getTextRange().getEndOffset();
|
||||
Document document = editor.getDocument();
|
||||
if (endOffset > document.getTextLength()) {
|
||||
LOG.assertTrue(!PsiDocumentManager.getInstance(file.getProject()).isUncommited(document));
|
||||
LOG.assertTrue(PsiDocumentManagerImpl.checkConsistency(file, document));
|
||||
LOG.assertTrue(PsiDocumentManagerBase.checkConsistency(file, document));
|
||||
}
|
||||
int endLine;
|
||||
if (endOffset == document.getTextLength()) {
|
||||
@@ -295,7 +309,7 @@ class StatementMover extends LineMover {
|
||||
return new LineRange(startLine, endLine);
|
||||
}
|
||||
|
||||
private static PsiElement itIsTheClosingCurlyBraceWeAreMoving(final PsiFile file, final Editor editor) {
|
||||
private static PsiElement itIsTheClosingCurlyBraceWeAreMoving(PsiFile file, Editor editor) {
|
||||
LineRange range = getLineRangeFromSelection(editor);
|
||||
if (range.endLine - range.startLine != 1) return null;
|
||||
int offset = editor.getCaretModel().getOffset();
|
||||
@@ -304,8 +318,6 @@ class StatementMover extends LineMover {
|
||||
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('}'));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+13
-17
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.codeInsight.editorActions.moveUpDown;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -25,13 +24,12 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class LineMover extends StatementUpDownMover {
|
||||
|
||||
@Override
|
||||
public boolean checkAvailable(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down) {
|
||||
public boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) {
|
||||
LineRange range = StatementUpDownMover.getLineRangeFromSelection(editor);
|
||||
|
||||
LogicalPosition maxLinePos = editor.offsetToLogicalPosition(editor.getDocument().getTextLength());
|
||||
int maxLine = maxLinePos.column == 0? maxLinePos.line : maxLinePos.line + 1;
|
||||
int maxLine = maxLinePos.column == 0 ? maxLinePos.line : maxLinePos.line + 1;
|
||||
if (range.startLine == 0 && !down) return false;
|
||||
if (range.endLine >= maxLine && down) return false;
|
||||
|
||||
@@ -42,23 +40,21 @@ public class LineMover extends StatementUpDownMover {
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static Pair<PsiElement, PsiElement> getElementRange(final PsiElement parent,
|
||||
PsiElement element1,
|
||||
PsiElement element2) {
|
||||
protected static Pair<PsiElement, PsiElement> getElementRange(PsiElement parent, PsiElement element1, PsiElement element2) {
|
||||
if (PsiTreeUtil.isAncestor(element1, element2, false) || PsiTreeUtil.isAncestor(element2, element1, false)) {
|
||||
return Pair.create(parent, parent);
|
||||
}
|
||||
// find nearset children that are parents of elements
|
||||
while (element1 != null && element1.getParent() != parent) {
|
||||
element1 = element1.getParent();
|
||||
}
|
||||
while (element2 != null && element2.getParent() != parent) {
|
||||
element2 = element2.getParent();
|
||||
}
|
||||
|
||||
// find nearest children that are parents of elements
|
||||
while (element1 != null && element1.getParent() != parent) element1 = element1.getParent();
|
||||
while (element2 != null && element2.getParent() != parent) element2 = element2.getParent();
|
||||
if (element1 == null || element2 == null) return null;
|
||||
|
||||
if (element1 != element2) {
|
||||
assert element1.getTextRange().getEndOffset() <= element2.getTextRange().getStartOffset() : element1.getTextRange() + "-"+element2.getTextRange()+element1+element2;
|
||||
assert element1.getTextRange().getEndOffset() <= element2.getTextRange().getStartOffset() :
|
||||
element1.getTextRange() + "-" + element2.getTextRange() + element1 + element2;
|
||||
}
|
||||
|
||||
return Pair.create(element1, element2);
|
||||
}
|
||||
}
|
||||
}
|
||||
+23
-23
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -13,7 +13,6 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.intellij.codeInsight.editorActions.moveUpDown;
|
||||
|
||||
import com.intellij.lang.ASTNode;
|
||||
@@ -32,15 +31,18 @@ import org.jetbrains.annotations.Nullable;
|
||||
* @author spleaner
|
||||
*/
|
||||
public abstract class StatementUpDownMover {
|
||||
public static final ExtensionPointName<StatementUpDownMover> STATEMENT_UP_DOWN_MOVER_EP = ExtensionPointName.create("com.intellij.statementUpDownMover");
|
||||
public static final ExtensionPointName<StatementUpDownMover> STATEMENT_UP_DOWN_MOVER_EP =
|
||||
ExtensionPointName.create("com.intellij.statementUpDownMover");
|
||||
|
||||
public static class MoveInfo extends UserDataHolderBase {
|
||||
/** Source line range */
|
||||
@NotNull
|
||||
/**
|
||||
* Source line range
|
||||
*/
|
||||
public LineRange toMove;
|
||||
|
||||
/**
|
||||
* Target line range, or {@code null} if move not available
|
||||
*
|
||||
* @see #prohibitMove()
|
||||
*/
|
||||
public LineRange toMove2;
|
||||
@@ -52,7 +54,8 @@ public abstract class StatementUpDownMover {
|
||||
public boolean indentTarget = true;
|
||||
|
||||
/**
|
||||
* Use this method in {@link StatementUpDownMover#checkAvailable(com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile, com.intellij.codeInsight.editorActions.moveUpDown.StatementUpDownMover.MoveInfo, boolean)}
|
||||
* Use this method in {@link StatementUpDownMover#checkAvailable(Editor, PsiFile, StatementUpDownMover.MoveInfo, boolean)}.
|
||||
*
|
||||
* @return true to suppress further movers processing
|
||||
*/
|
||||
public final boolean prohibitMove() {
|
||||
@@ -61,28 +64,25 @@ public abstract class StatementUpDownMover {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract boolean checkAvailable(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down);
|
||||
public abstract boolean checkAvailable(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down);
|
||||
|
||||
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
|
||||
}
|
||||
public void beforeMove(@NotNull Editor editor, @NotNull MoveInfo info, boolean down) { }
|
||||
|
||||
public void afterMove(@NotNull final Editor editor, @NotNull final PsiFile file, @NotNull final MoveInfo info, final boolean down) {
|
||||
}
|
||||
public void afterMove(@NotNull Editor editor, @NotNull PsiFile file, @NotNull MoveInfo info, boolean down) { }
|
||||
|
||||
public static int getLineStartSafeOffset(@NotNull Document document, int line) {
|
||||
if (line == document.getLineCount()) return document.getTextLength();
|
||||
return document.getLineStartOffset(line);
|
||||
return line == document.getLineCount() ? document.getTextLength() : document.getLineStartOffset(line);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static LineRange getLineRangeFromSelection(@NotNull Editor editor) {
|
||||
final int startLine;
|
||||
final int endLine;
|
||||
final SelectionModel selectionModel = editor.getSelectionModel();
|
||||
int startLine;
|
||||
int endLine;
|
||||
SelectionModel selectionModel = editor.getSelectionModel();
|
||||
LineRange range;
|
||||
if (selectionModel.hasSelection()) {
|
||||
startLine = editor.offsetToLogicalPosition(selectionModel.getSelectionStart()).line;
|
||||
final LogicalPosition endPos = editor.offsetToLogicalPosition(selectionModel.getSelectionEnd());
|
||||
LogicalPosition endPos = editor.offsetToLogicalPosition(selectionModel.getSelectionEnd());
|
||||
endLine = endPos.column == 0 ? endPos.line : endPos.line+1;
|
||||
range = new LineRange(startLine, endLine);
|
||||
}
|
||||
@@ -96,10 +96,10 @@ public abstract class StatementUpDownMover {
|
||||
|
||||
@Nullable
|
||||
protected static Pair<PsiElement, PsiElement> getElementRange(@NotNull Editor editor, @NotNull PsiFile file, @NotNull LineRange range) {
|
||||
final int startOffset = editor.logicalPositionToOffset(new LogicalPosition(range.startLine, 0));
|
||||
int startOffset = editor.logicalPositionToOffset(new LogicalPosition(range.startLine, 0));
|
||||
PsiElement startingElement = firstNonWhiteElement(startOffset, file, true);
|
||||
if (startingElement == null) return null;
|
||||
final int endOffset = editor.logicalPositionToOffset(new LogicalPosition(range.endLine, 0)) -1;
|
||||
int endOffset = editor.logicalPositionToOffset(new LogicalPosition(range.endLine, 0)) -1;
|
||||
|
||||
PsiElement endingElement = firstNonWhiteElement(endOffset, file, false);
|
||||
if (endingElement == null) return null;
|
||||
@@ -114,16 +114,16 @@ public abstract class StatementUpDownMover {
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static PsiElement firstNonWhiteElement(int offset, @NotNull PsiFile file, final boolean lookRight) {
|
||||
final ASTNode leafElement = file.getNode().findLeafElementAt(offset);
|
||||
protected static PsiElement firstNonWhiteElement(int offset, @NotNull PsiFile file, boolean lookRight) {
|
||||
ASTNode leafElement = file.getNode().findLeafElementAt(offset);
|
||||
return leafElement == null ? null : firstNonWhiteElement(leafElement.getPsi(), lookRight);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
protected static PsiElement firstNonWhiteElement(PsiElement element, final boolean lookRight) {
|
||||
protected static PsiElement firstNonWhiteElement(PsiElement element, boolean lookRight) {
|
||||
if (element instanceof PsiWhiteSpace) {
|
||||
element = lookRight ? element.getNextSibling() : element.getPrevSibling();
|
||||
}
|
||||
return element;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user