From 593e93b88b0a9a8a6d29873b6d62f135af82ed8d Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 1 Oct 2014 15:16:21 +0400 Subject: [PATCH] Moved code from DeclarationUtils and refactored code --- .../intentions/declarations/DeclarationUtils.java | 13 ------------- .../declarations/JetDeclarationJoinLinesHandler.kt | 12 ++++++++++-- 2 files changed, 10 insertions(+), 15 deletions(-) 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 7d8b4eaaab7..79f247c20d9 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java +++ b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/DeclarationUtils.java @@ -16,16 +16,12 @@ package org.jetbrains.jet.plugin.intentions.declarations; -import com.google.common.base.Predicate; -import com.intellij.psi.PsiComment; import com.intellij.psi.PsiElement; -import com.intellij.psi.PsiWhiteSpace; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.types.JetType; -import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.plugin.codeInsight.ShortenReferences; import org.jetbrains.jet.plugin.project.AnalyzerFacadeWithCache; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -44,15 +40,6 @@ public class DeclarationUtils { return property.hasInitializer() && property.isLocal(); } - public static final Predicate SKIP_DELIMITERS = new Predicate() { - @Override - public boolean apply(@Nullable PsiElement input) { - return input == null - || input instanceof PsiWhiteSpace || input instanceof PsiComment - || input.getNode().getElementType() == JetTokens.SEMICOLON; - } - }; - @Nullable private static JetType getPropertyTypeIfNeeded(@NotNull JetProperty property) { if (property.getTypeRef() != null) return null; 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 3c2df089c8b..68819acc529 100644 --- a/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt +++ b/idea/src/org/jetbrains/jet/plugin/intentions/declarations/JetDeclarationJoinLinesHandler.kt @@ -26,11 +26,16 @@ 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 +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.PsiComment +import com.google.common.base.Predicate 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) ?: return -1 + val element = file.findElementAt(start) + ?.siblings(forward = false, withItself = false) + ?.firstOrNull { !isToSkip(it) } ?: return -1 val pair = element.parents(withItself = true) .map { getPropertyAndAssignment(it) } @@ -49,7 +54,8 @@ public class JetDeclarationJoinLinesHandler : JoinRawLinesHandlerDelegate { val property = element as? JetProperty ?: return null if (property.hasInitializer()) return null - val assignment = JetPsiUtil.skipSiblingsForwardByPredicate(element, DeclarationUtils.SKIP_DELIMITERS) as? JetBinaryExpression ?: return null + val assignment = element.siblings(forward = true, withItself = false) + .firstOrNull { !isToSkip(it) } as? JetBinaryExpression ?: return null if (assignment.getOperationToken() != JetTokens.EQ) return null val left = assignment.getLeft() as? JetSimpleNameExpression ?: return null @@ -64,4 +70,6 @@ public class JetDeclarationJoinLinesHandler : JoinRawLinesHandlerDelegate { property.getParent()!!.deleteChildRange(property.getNextSibling(), assignment) //TODO: should we remove range? } + private fun isToSkip(element: PsiElement) + = element is PsiWhiteSpace || element is PsiComment || element.getNode()!!.getElementType() == JetTokens.SEMICOLON } \ No newline at end of file