Moved some code from DeclarationUtils to JetDeclarationJoinLinesHandler and refactored it
This commit is contained in:
@@ -17,7 +17,6 @@
|
||||
package org.jetbrains.jet.plugin.intentions.declarations;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiWhiteSpace;
|
||||
@@ -54,32 +53,6 @@ public class DeclarationUtils {
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
public static Pair<JetProperty, JetBinaryExpression> checkAndGetPropertyAndInitializer(@NotNull PsiElement element) {
|
||||
JetProperty property = null;
|
||||
JetExpression initializer = null;
|
||||
|
||||
if (element instanceof JetProperty) {
|
||||
PsiElement nextElement = JetPsiUtil.skipSiblingsForwardByPredicate(element, SKIP_DELIMITERS);
|
||||
if (nextElement instanceof JetExpression) {
|
||||
property = (JetProperty) element;
|
||||
initializer = (JetExpression) nextElement;
|
||||
}
|
||||
}
|
||||
|
||||
if (property == null) return null;
|
||||
if (property.hasInitializer()) return null;
|
||||
if (!JetPsiUtil.isOrdinaryAssignment(initializer)) return null;
|
||||
|
||||
JetBinaryExpression assignment = (JetBinaryExpression) initializer;
|
||||
|
||||
if (!(assignment.getLeft() instanceof JetSimpleNameExpression)) return null;
|
||||
if (assignment.getRight() == null) return null;
|
||||
if (!JetPsiUtil.unquoteIdentifier(assignment.getLeft().getText()).equals(property.getName())) return null;
|
||||
|
||||
return new Pair<JetProperty, JetBinaryExpression>(property, assignment);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property) {
|
||||
if (property.getTypeRef() != null) return null;
|
||||
@@ -139,30 +112,4 @@ public class DeclarationUtils {
|
||||
);
|
||||
}
|
||||
|
||||
// Returns joined property
|
||||
@NotNull
|
||||
public static JetProperty joinPropertyDeclarationWithInitializer(
|
||||
@NotNull Pair<JetProperty, JetBinaryExpression> propertyAndInitializer
|
||||
) {
|
||||
JetProperty property = propertyAndInitializer.first;
|
||||
assertNotNull(property);
|
||||
|
||||
JetBinaryExpression assignment = propertyAndInitializer.second;
|
||||
assertNotNull(assignment);
|
||||
|
||||
JetProperty newProperty = changePropertyInitializer(property, assignment.getRight());
|
||||
|
||||
property.getParent().deleteChildRange(property.getNextSibling(), assignment);
|
||||
return (JetProperty) property.replace(newProperty);
|
||||
}
|
||||
|
||||
// Returns joined property
|
||||
@NotNull
|
||||
public static JetProperty joinPropertyDeclarationWithInitializer(@NotNull PsiElement element) {
|
||||
Pair<JetProperty, JetBinaryExpression> propertyAndInitializer = checkAndGetPropertyAndInitializer(element);
|
||||
assertNotNull(propertyAndInitializer);
|
||||
|
||||
//noinspection ConstantConditions
|
||||
return joinPropertyDeclarationWithInitializer(propertyAndInitializer);
|
||||
}
|
||||
}
|
||||
|
||||
+32
-6
@@ -20,22 +20,48 @@ import com.intellij.codeInsight.editorActions.JoinRawLinesHandlerDelegate
|
||||
import com.intellij.openapi.editor.Document
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.PsiFile
|
||||
import com.intellij.util.ArrayUtil
|
||||
import org.jetbrains.jet.lang.psi.JetPsiUtil
|
||||
import org.jetbrains.jet.lang.psi.psiUtil.*
|
||||
import org.jetbrains.jet.lang.psi.JetProperty
|
||||
import org.jetbrains.jet.lang.psi.JetBinaryExpression
|
||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.jet.lexer.JetTokens
|
||||
|
||||
public class JetDeclarationJoinLinesHandler : JoinRawLinesHandlerDelegate {
|
||||
|
||||
override fun tryJoinRawLines(document: Document, file: PsiFile, start: Int, end: Int): Int {
|
||||
val element = JetPsiUtil.skipSiblingsBackwardByPredicate(file.findElementAt(start), DeclarationUtils.SKIP_DELIMITERS)
|
||||
val element = JetPsiUtil.skipSiblingsBackwardByPredicate(file.findElementAt(start), DeclarationUtils.SKIP_DELIMITERS) ?: return -1
|
||||
|
||||
val target = element?.getParentByTypesAndPredicate<PsiElement>(strict = false) {
|
||||
DeclarationUtils.checkAndGetPropertyAndInitializer(it) != null
|
||||
} ?: return -1
|
||||
val pair = element.parents(withItself = true)
|
||||
.map { getPropertyAndAssignment(it) }
|
||||
.filterNotNull()
|
||||
.firstOrNull() ?: return -1
|
||||
val (property, assignment) = pair
|
||||
|
||||
return DeclarationUtils.joinPropertyDeclarationWithInitializer(target).getTextRange()!!.getStartOffset()
|
||||
return doJoin(property, assignment).getTextRange()!!.getStartOffset()
|
||||
}
|
||||
|
||||
override fun tryJoinLines(document: Document, file: PsiFile, start: Int, end: Int)
|
||||
= -1
|
||||
|
||||
private fun getPropertyAndAssignment(element: PsiElement): Pair<JetProperty, JetBinaryExpression>? {
|
||||
val property = element as? JetProperty ?: return null
|
||||
if (property.hasInitializer()) return null
|
||||
|
||||
val assignment = JetPsiUtil.skipSiblingsForwardByPredicate(element, DeclarationUtils.SKIP_DELIMITERS) as? JetBinaryExpression ?: return null
|
||||
if (assignment.getOperationToken() != JetTokens.EQ) return null
|
||||
|
||||
val left = assignment.getLeft() as? JetSimpleNameExpression ?: return null
|
||||
if (assignment.getRight() == null) return null
|
||||
if (left.getReferencedName() != property.getName()) return null
|
||||
|
||||
return property to assignment
|
||||
}
|
||||
|
||||
private fun doJoin(property: JetProperty, assignment: JetBinaryExpression): JetProperty {
|
||||
val newProperty = DeclarationUtils.changePropertyInitializer(property, assignment.getRight())
|
||||
property.getParent()!!.deleteChildRange(property.getNextSibling(), assignment)
|
||||
return property.replace(newProperty) as JetProperty
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user