diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java index 1d929afc7e8..7150a5f9191 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java @@ -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 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(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 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 propertyAndInitializer = checkAndGetPropertyAndInitializer(element); - assertNotNull(propertyAndInitializer); - - //noinspection ConstantConditions - return joinPropertyDeclarationWithInitializer(propertyAndInitializer); - } } diff --git a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt index 81392d0380f..4932cd4db42 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt @@ -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(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? { + 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 + } + } \ No newline at end of file