Minor: make JetClass::createPrimaryConstructorIfAbsent & JetClass::createPrimaryConstructorParameterListIfAbsent extension function and move to idea module

This commit is contained in:
Zalim Bashorov
2015-09-15 19:52:50 +03:00
parent 08a267adaa
commit c82e8f45ea
7 changed files with 32 additions and 34 deletions
@@ -17,8 +17,6 @@
package org.jetbrains.kotlin.psi
import com.intellij.lang.ASTNode
import com.intellij.navigation.ItemPresentation
import com.intellij.navigation.ItemPresentationProviders
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiElement
import com.intellij.psi.tree.TokenSet
@@ -26,8 +24,7 @@ import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.stubs.KotlinClassStub
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
import java.util.ArrayList
import java.util.Collections
import java.util.*
public open class JetClass : JetClassOrObject {
public constructor(node: ASTNode) : super(node)
@@ -39,22 +36,6 @@ public open class JetClass : JetClassOrObject {
return visitor.visitClass(this, data)
}
public fun createPrimaryConstructorIfAbsent(): JetPrimaryConstructor {
val constructor = getPrimaryConstructor()
if (constructor != null) return constructor
var anchor: PsiElement? = getTypeParameterList()
if (anchor == null) anchor = getNameIdentifier()
if (anchor == null) anchor = getLastChild()
return addAfter(JetPsiFactory(getProject()).createPrimaryConstructor(), anchor) as JetPrimaryConstructor
}
public fun createPrimaryConstructorParameterListIfAbsent(): JetParameterList {
val constructor = createPrimaryConstructorIfAbsent()
val parameterList = constructor.getValueParameterList()
if (parameterList != null) return parameterList
return constructor.add(JetPsiFactory(getProject()).createParameterList("()")) as JetParameterList
}
public fun getColon(): PsiElement? = findChildByType(JetTokens.COLON)
public fun getProperties(): List<JetProperty> = getBody()?.getProperties().orEmpty()
@@ -31,18 +31,18 @@ import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.core.isVisible
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorParameterListIfAbsent
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.ShortenReferences
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.IndexedParametersSubstitution
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
import java.util.ArrayList
import java.util.*
public object SuperClassNotInitialized : JetIntentionActionsFactory() {
private val DISPLAY_MAX_PARAMS = 5
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.types.substitutions.SubstitutionUtilsKt;
import java.util.List;
import static org.jetbrains.kotlin.idea.core.refactoring.JetRefactoringUtilKt.createPrimaryConstructorIfAbsent;
import static org.jetbrains.kotlin.psi.PsiPackage.JetPsiFactory;
public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageInfo<T> {
@@ -295,7 +296,7 @@ public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageIn
}
else {
if (element instanceof JetClass) {
JetPrimaryConstructor constructor = ((JetClass) element).createPrimaryConstructorIfAbsent();
JetPrimaryConstructor constructor = createPrimaryConstructorIfAbsent((JetClass) element);
JetParameterList oldParameterList = constructor.getValueParameterList();
assert oldParameterList != null : "primary constructor from factory has parameter list";
newParameterList = (JetParameterList) oldParameterList.replace(newParameterList);
@@ -351,7 +352,7 @@ public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageIn
PsiModificationUtilsKt.setVisibility((JetCallableDeclaration)element, newVisibilityToken);
}
else if (element instanceof JetClass) {
PsiModificationUtilsKt.setVisibility(((JetClass) element).createPrimaryConstructorIfAbsent(), newVisibilityToken);
PsiModificationUtilsKt.setVisibility(createPrimaryConstructorIfAbsent((JetClass) element), newVisibilityToken);
}
else throw new AssertionError("Invalid element: " + PsiUtilPackage.getElementTextWithContext(element));
}
@@ -18,12 +18,12 @@ package org.jetbrains.kotlin.idea.refactoring.changeSignature.usages
import com.intellij.usageView.UsageInfo
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorParameterListIfAbsent
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetChangeInfo
import org.jetbrains.kotlin.idea.refactoring.changeSignature.getAffectedCallables
import org.jetbrains.kotlin.idea.refactoring.changeSignature.isInsideOfCallerBody
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getValueParameterList
public class KotlinCallerUsage(element: JetNamedDeclaration): JetUsageInfo<JetNamedDeclaration>(element) {
override fun processUsage(changeInfo: JetChangeInfo, element: JetNamedDeclaration, allUsages: Array<out UsageInfo>): Boolean {
@@ -74,4 +74,4 @@ public class KotlinCallerCallUsage(element: JetCallElement): JetUsageInfo<JetCal
return true
}
}
}
@@ -28,22 +28,21 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.ui.JBColor
import com.intellij.ui.NonFocusableCheckBox
import com.intellij.util.ui.FormBuilder
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorParameterListIfAbsent
import org.jetbrains.kotlin.idea.refactoring.changeSignature.JetValVar
import org.jetbrains.kotlin.idea.refactoring.introduce.AbstractKotlinInplaceIntroducer
import org.jetbrains.kotlin.idea.refactoring.introduce.introduceVariable.KotlinInplaceVariableIntroducer
import org.jetbrains.kotlin.idea.util.application.runWriteAction
import org.jetbrains.kotlin.idea.util.psi.patternMatching.toRange
import org.jetbrains.kotlin.types.typeUtil.supertypes
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.psi.psiUtil.getValueParameterList
import org.jetbrains.kotlin.psi.psiUtil.getValueParameters
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.typeUtil.supertypes
import org.jetbrains.kotlin.utils.addToStdlib.singletonList
import java.awt.BorderLayout
import java.awt.Color
import java.util.ArrayList
import java.util.*
import javax.swing.JCheckBox
public class KotlinInplaceParameterIntroducer(
@@ -263,4 +262,4 @@ public class KotlinInplaceParameterIntroducer(
listOf(parameterType) + parameterType.supertypes(),
KotlinIntroduceParameterHelper.Default).show()
}
}
}
@@ -689,4 +689,20 @@ public fun invokeOnceOnCommandFinish(action: () -> Unit) {
public fun String.quoteIfNeeded(): String = if (KotlinNameSuggester.isIdentifier(this)) this else "`$this`"
public fun FqNameBase.hasIdentifiersOnly(): Boolean = pathSegments().all { KotlinNameSuggester.isIdentifier(it.asString()) }
public fun FqNameBase.hasIdentifiersOnly(): Boolean = pathSegments().all { KotlinNameSuggester.isIdentifier(it.asString()) }
public fun JetClass.createPrimaryConstructorIfAbsent(): JetPrimaryConstructor {
val constructor = getPrimaryConstructor()
if (constructor != null) return constructor
var anchor: PsiElement? = typeParameterList
if (anchor == null) anchor = nameIdentifier
if (anchor == null) anchor = lastChild
return addAfter(JetPsiFactory(project).createPrimaryConstructor(), anchor) as JetPrimaryConstructor
}
public fun JetClass.createPrimaryConstructorParameterListIfAbsent(): JetParameterList {
val constructor = createPrimaryConstructorIfAbsent()
val parameterList = constructor.valueParameterList
if (parameterList != null) return parameterList
return constructor.add(JetPsiFactory(project).createParameterList("()")) as JetParameterList
}
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.idea.JetLanguage
import org.jetbrains.kotlin.idea.codeInsight.shorten.addToShorteningWaitSet
import org.jetbrains.kotlin.idea.core.refactoring.createJavaField
import org.jetbrains.kotlin.idea.core.refactoring.createJavaMethod
import org.jetbrains.kotlin.idea.core.refactoring.createPrimaryConstructorIfAbsent
import org.jetbrains.kotlin.idea.intentions.setType
import org.jetbrains.kotlin.idea.refactoring.safeDelete.removeOverrideModifier
import org.jetbrains.kotlin.idea.util.anonymousObjectSuperTypeOrNull