Change Signature: Add/remove constructor keyword on primary constructor when changing modifier list
This commit is contained in:
@@ -17,9 +17,10 @@
|
||||
package org.jetbrains.kotlin.psi
|
||||
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.lexer.JetModifierKeywordToken
|
||||
import org.jetbrains.kotlin.lexer.JetTokens
|
||||
import org.jetbrains.kotlin.psi.addRemoveModifier.*
|
||||
import org.jetbrains.kotlin.psi.addRemoveModifier.addModifier
|
||||
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub
|
||||
import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes
|
||||
|
||||
@@ -31,16 +32,29 @@ public class JetPrimaryConstructor : JetConstructor<JetPrimaryConstructor> {
|
||||
|
||||
override fun getContainingClassOrObject() = getParent() as JetClassOrObject
|
||||
|
||||
private fun getOrCreateConstructorKeyword(): PsiElement {
|
||||
return getConstructorKeyword() ?: addBefore(JetPsiFactory(this).createConstructorKeyword(), valueParameterList!!)
|
||||
}
|
||||
|
||||
override fun addModifier(modifier: JetModifierKeywordToken) {
|
||||
val modifierList = getModifierList()
|
||||
val modifierList = modifierList
|
||||
if (modifierList != null) {
|
||||
addModifier(modifierList, modifier, JetTokens.PUBLIC_KEYWORD)
|
||||
if (this.modifierList == null) {
|
||||
getConstructorKeyword()?.delete()
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (modifier == JetTokens.PUBLIC_KEYWORD) return
|
||||
val parameterList = getValueParameterList()!!
|
||||
val newModifierList = JetPsiFactory(getProject()).createModifierList(modifier)
|
||||
addBefore(newModifierList, parameterList)
|
||||
val newModifierList = JetPsiFactory(this).createModifierList(modifier)
|
||||
addBefore(newModifierList, getOrCreateConstructorKeyword())
|
||||
}
|
||||
}
|
||||
|
||||
override fun removeModifier(modifier: JetModifierKeywordToken) {
|
||||
super.removeModifier(modifier)
|
||||
if (modifierList == null) {
|
||||
getConstructorKeyword()?.delete()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -66,6 +66,9 @@ internal fun addModifier(modifierList: JetModifierList, modifier: JetModifierKey
|
||||
if (modifier == defaultVisibilityModifier) { // do not insert explicit 'internal' keyword (or 'public' for primary constructor)
|
||||
//TODO: code style option
|
||||
modifierToReplace?.delete()
|
||||
if (modifierList.firstChild == null) {
|
||||
modifierList.delete()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -98,7 +101,12 @@ internal fun addModifier(modifierList: JetModifierList, modifier: JetModifierKey
|
||||
}
|
||||
|
||||
internal fun removeModifier(owner: JetModifierListOwner, modifier: JetModifierKeywordToken) {
|
||||
owner.getModifierList()?.getModifier(modifier)?.delete()
|
||||
owner.getModifierList()?.let {
|
||||
it.getModifier(modifier)?.delete()
|
||||
if (it.firstChild == null) {
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val MODIFIERS_TO_REPLACE = mapOf(
|
||||
|
||||
@@ -194,7 +194,7 @@ public open class JetChangeInfo(
|
||||
buffer.append(name)
|
||||
|
||||
if (newVisibility != defaultVisibility) {
|
||||
buffer.append(' ').append(newVisibility).append(' ')
|
||||
buffer.append(' ').append(newVisibility).append(" constructor ")
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
+5
-15
@@ -293,18 +293,10 @@ public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageIn
|
||||
}
|
||||
else {
|
||||
if (element instanceof JetClass) {
|
||||
PsiElement anchor = ((JetClass) element).getTypeParameterList();
|
||||
|
||||
if (anchor == null) {
|
||||
anchor = ((JetClass) element).getNameIdentifier();
|
||||
}
|
||||
if (anchor != null) {
|
||||
JetPrimaryConstructor constructor =
|
||||
(JetPrimaryConstructor) element.addAfter(psiFactory.createPrimaryConstructor(), anchor);
|
||||
JetParameterList oldParameterList = constructor.getValueParameterList();
|
||||
assert oldParameterList != null : "primary constructor from factory has parameter list";
|
||||
newParameterList = (JetParameterList) oldParameterList.replace(newParameterList);
|
||||
}
|
||||
JetPrimaryConstructor constructor = ((JetClass) element).createPrimaryConstructorIfAbsent();
|
||||
JetParameterList oldParameterList = constructor.getValueParameterList();
|
||||
assert oldParameterList != null : "primary constructor from factory has parameter list";
|
||||
newParameterList = (JetParameterList) oldParameterList.replace(newParameterList);
|
||||
}
|
||||
else if (isLambda) {
|
||||
//noinspection ConstantConditions
|
||||
@@ -357,9 +349,7 @@ public class JetCallableDefinitionUsage<T extends PsiElement> extends JetUsageIn
|
||||
((JetCallableDeclaration)element).addModifier(newVisibilityToken);
|
||||
}
|
||||
else if (element instanceof JetClass) {
|
||||
JetPrimaryConstructor constructor = ((JetClass) element).getPrimaryConstructor();
|
||||
assert constructor != null : "Primary constructor should be created before changing visibility";
|
||||
constructor.addModifier(newVisibilityToken);
|
||||
((JetClass) element).createPrimaryConstructorIfAbsent().addModifier(newVisibilityToken);
|
||||
}
|
||||
else throw new AssertionError("Invalid element: " + PsiUtilPackage.getElementTextWithContext(element));
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -1,2 +1,2 @@
|
||||
// INTENTION_TEXT: Make primary constructor private
|
||||
class C<caret> private (val v: Int)
|
||||
class C<caret> private constructor(val v: Int)
|
||||
@@ -1,4 +1,4 @@
|
||||
class C1 protected (val x: Any) {}
|
||||
class C1 protected constructor(val x: Any) {}
|
||||
|
||||
fun f() {
|
||||
val c = C1(12);
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
class C1 protected (){}
|
||||
class C1 protected constructor(){}
|
||||
|
||||
+1
@@ -0,0 +1 @@
|
||||
class X private constructor()
|
||||
+1
@@ -0,0 +1 @@
|
||||
class <caret>X
|
||||
+1
@@ -0,0 +1 @@
|
||||
class X (n: Int)
|
||||
+1
@@ -0,0 +1 @@
|
||||
class <caret>X private constructor(n: Int)
|
||||
+12
@@ -1306,6 +1306,18 @@ public class JetChangeSignatureTest extends KotlinCodeInsightTestCase {
|
||||
doTest(changeInfo);
|
||||
}
|
||||
|
||||
public void testMakePrimaryConstructorPrivateNoParams() throws Exception {
|
||||
JetChangeInfo changeInfo = getChangeInfo();
|
||||
changeInfo.setNewVisibility(Visibilities.PRIVATE);
|
||||
doTest(changeInfo);
|
||||
}
|
||||
|
||||
public void testMakePrimaryConstructorPublic() throws Exception {
|
||||
JetChangeInfo changeInfo = getChangeInfo();
|
||||
changeInfo.setNewVisibility(Visibilities.PUBLIC);
|
||||
doTest(changeInfo);
|
||||
}
|
||||
|
||||
private List<Editor> editors = null;
|
||||
|
||||
private static final String[] EXTENSIONS = {".kt", ".java"};
|
||||
|
||||
Reference in New Issue
Block a user