Refactoring Util: Implement PsiElement.getLineCount()

This commit is contained in:
Alexey Sedunov
2014-09-09 16:16:29 +04:00
parent b59da7686c
commit 1a63f9b815
4 changed files with 42 additions and 19 deletions
@@ -31,6 +31,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetBlockExpression; import org.jetbrains.jet.lang.psi.JetBlockExpression;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetFunctionLiteral; import org.jetbrains.jet.lang.psi.JetFunctionLiteral;
import org.jetbrains.jet.plugin.refactoring.RefactoringPackage;
public abstract class AbstractJetUpDownMover extends LineMover { public abstract class AbstractJetUpDownMover extends LineMover {
protected AbstractJetUpDownMover() { protected AbstractJetUpDownMover() {
@@ -83,7 +84,7 @@ public abstract class AbstractJetUpDownMover extends LineMover {
} }
if (comment != null) { if (comment != null) {
int extension = getElementLineCount(comment, editor); int extension = RefactoringPackage.getLineCount(comment);
if (extendDown) { if (extendDown) {
bottomExtension = extension; bottomExtension = extension;
} }
@@ -132,16 +133,6 @@ public abstract class AbstractJetUpDownMover extends LineMover {
return sourceRange; return sourceRange;
} }
protected static int getElementLineCount(PsiElement element, Editor editor) {
Document doc = editor.getDocument();
TextRange spaceRange = element.getTextRange();
int startLine = doc.getLineNumber(spaceRange.getStartOffset());
int endLine = doc.getLineNumber(spaceRange.getEndOffset());
return endLine - startLine;
}
protected static int getElementLine(PsiElement element, Editor editor, boolean first) { protected static int getElementLine(PsiElement element, Editor editor, boolean first) {
if (element == null) return -1; if (element == null) return -1;
@@ -27,6 +27,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.plugin.refactoring.RefactoringPackage;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -182,7 +183,7 @@ public class JetDeclarationMover extends AbstractJetUpDownMover {
@Override @Override
protected PsiElement adjustElement(PsiElement element, Editor editor, boolean first) { protected PsiElement adjustElement(PsiElement element, Editor editor, boolean first) {
return first ? getTopmostSiblingCommentOrOriginal(element, editor) : element; return first ? getTopmostSiblingCommentOrOriginal(element) : element;
} }
@Nullable @Nullable
@@ -255,7 +256,7 @@ public class JetDeclarationMover extends AbstractJetUpDownMover {
if (target instanceof JetPropertyAccessor && !(sibling instanceof JetPropertyAccessor)) return null; if (target instanceof JetPropertyAccessor && !(sibling instanceof JetPropertyAccessor)) return null;
if (!down && start instanceof JetDeclaration) { if (!down && start instanceof JetDeclaration) {
start = getTopmostSiblingCommentOrOriginal(start, editor); start = getTopmostSiblingCommentOrOriginal(start);
} }
else if (start != null && start.getFirstChild() != null) { else if (start != null && start.getFirstChild() != null) {
start = skipInsignificantElements(start.getFirstChild(), true); start = skipInsignificantElements(start.getFirstChild(), true);
@@ -311,11 +312,11 @@ public class JetDeclarationMover extends AbstractJetUpDownMover {
} }
@NotNull @NotNull
private static PsiElement getTopmostSiblingCommentOrOriginal(@NotNull PsiElement originalElement, @NotNull Editor editor) { private static PsiElement getTopmostSiblingCommentOrOriginal(@NotNull PsiElement originalElement) {
PsiElement element = originalElement; PsiElement element = originalElement;
PsiElement sibling = element.getPrevSibling(); PsiElement sibling = element.getPrevSibling();
while (sibling instanceof PsiComment while (sibling instanceof PsiComment
|| (sibling instanceof PsiWhiteSpace && getElementLineCount(sibling, editor) <= 1) || (sibling instanceof PsiWhiteSpace && !RefactoringPackage.isMultiLine(sibling))
|| (sibling != null && (sibling.getText() == null || sibling.getText().isEmpty()))) { || (sibling != null && (sibling.getText() == null || sibling.getText().isEmpty()))) {
if (sibling instanceof PsiComment) { if (sibling instanceof PsiComment) {
element = sibling; element = sibling;
@@ -31,6 +31,7 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage; import org.jetbrains.jet.lang.psi.psiUtil.PsiUtilPackage;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.plugin.refactoring.RefactoringPackage;
import java.util.List; import java.util.List;
@@ -431,7 +432,6 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
} }
protected static PsiElement adjustSibling( protected static PsiElement adjustSibling(
@NotNull Editor editor,
@NotNull LineRange sourceRange, @NotNull LineRange sourceRange,
@NotNull MoveInfo info, @NotNull MoveInfo info,
boolean down boolean down
@@ -448,7 +448,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
} }
if (whiteSpaceTestSubject instanceof PsiWhiteSpace) { if (whiteSpaceTestSubject instanceof PsiWhiteSpace) {
if (getElementLineCount(whiteSpaceTestSubject, editor) > 1) { if (RefactoringPackage.isMultiLine(whiteSpaceTestSubject)) {
int nearLine = down ? sourceRange.endLine : sourceRange.startLine - 1; int nearLine = down ? sourceRange.endLine : sourceRange.startLine - 1;
info.toMove = sourceRange; info.toMove = sourceRange;
@@ -524,7 +524,7 @@ public class JetExpressionMover extends AbstractJetUpDownMover {
LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange); LineRange sourceRange = getSourceRange(firstElement, lastElement, editor, oldRange);
if (sourceRange == null) return false; if (sourceRange == null) return false;
PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(editor, sourceRange, info, down), editor, down); PsiElement sibling = getLastNonWhiteSiblingInLine(adjustSibling(sourceRange, info, down), editor, down);
// Either reached last sibling, or jumped over multi-line whitespace // Either reached last sibling, or jumped over multi-line whitespace
if (sibling == null) return true; if (sibling == null) return true;
@@ -62,6 +62,10 @@ import com.intellij.openapi.editor.colors.EditorColorsManager
import com.intellij.ui.components.JBList import com.intellij.ui.components.JBList
import com.intellij.openapi.ui.popup.JBPopupAdapter import com.intellij.openapi.ui.popup.JBPopupAdapter
import com.intellij.openapi.ui.popup.LightweightWindowEvent import com.intellij.openapi.ui.popup.LightweightWindowEvent
import com.intellij.openapi.editor.Document
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiWhiteSpace
/** /**
* Replace [[JetSimpleNameExpression]] (and its enclosing qualifier) with qualified element given by FqName * Replace [[JetSimpleNameExpression]] (and its enclosing qualifier) with qualified element given by FqName
@@ -255,3 +259,30 @@ public class SelectionAwareScopeHighlighter(val editor: Editor) {
highlighters.clear() highlighters.clear()
} }
} }
fun PsiElement.getLineCount(): Int {
val doc = getContainingFile()?.let { file -> PsiDocumentManager.getInstance(getProject()).getDocument(file) }
if (doc != null) {
val spaceRange = getTextRange() ?: TextRange.EMPTY_RANGE
val startLine = doc.getLineNumber(spaceRange.getStartOffset())
val endLine = doc.getLineNumber(spaceRange.getEndOffset())
return endLine - startLine
}
var lineCount = 1
accept(
object: JetTreeVisitorVoid() {
override fun visitWhiteSpace(space: PsiWhiteSpace) {
if ("\n" in space.getText() ?: "") {
lineCount++
}
}
}
)
return lineCount
}
fun PsiElement.isMultiLine(): Boolean = getLineCount() > 1