KT-9839 related, secondary constructor to primary: comment restoration

(cherry picked from commit 998e39e)
This commit is contained in:
Mikhail Glukhikh
2016-09-30 11:23:46 +03:00
committed by Mikhail Glukhikh
parent 725df49c8c
commit d1958be2a8
4 changed files with 39 additions and 2 deletions
@@ -20,6 +20,7 @@ import com.intellij.openapi.editor.Editor
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.analyzeFully
import org.jetbrains.kotlin.idea.util.CommentSaver
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
@@ -83,6 +84,7 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingIntention<KtS
val context = klass.analyzeFully()
val constructorDescriptor = context[BindingContext.CONSTRUCTOR, element] ?: return
val factory = KtPsiFactory(klass)
val constructorCommentSaver = CommentSaver(element)
val constructorInClass = klass.createPrimaryConstructorIfAbsent()
val constructor = factory.createPrimaryConstructor(element.modifierList?.text?.replace("\n", " "))
val parameterList = constructor.valueParameterList!!
@@ -105,10 +107,12 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingIntention<KtS
val newParameter = factory.createParameter(parameter.text)
val parameterDescriptor = context[BindingContext.VALUE_PARAMETER, parameter]
val propertyDescriptor = parameterToPropertyMap[parameterDescriptor]
var propertyCommentSaver: CommentSaver? = null
if (parameterDescriptor != null && propertyDescriptor != null) {
val property = DescriptorToSourceUtils.descriptorToDeclaration(propertyDescriptor) as? KtProperty
if (property != null) {
if (propertyDescriptor.name == parameterDescriptor.name && propertyDescriptor.type == parameterDescriptor.type) {
propertyCommentSaver = CommentSaver(property)
val valOrVar = if (property.isVar) factory.createVarKeyword() else factory.createValKeyword()
newParameter.addBefore(valOrVar, newParameter.nameIdentifier)
val propertyModifiers = property.modifierList?.text
@@ -123,7 +127,9 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingIntention<KtS
}
}
}
parameterList.addParameter(newParameter)
with (parameterList.addParameter(newParameter)) {
propertyCommentSaver?.restore(this)
}
}
val delegationCall = element.getDelegationCall()
@@ -140,7 +146,9 @@ class ConvertSecondaryConstructorToPrimaryIntention : SelfTargetingIntention<KtS
}
}
constructorInClass.replace(constructor)
with (constructorInClass.replace(constructor)) {
constructorCommentSaver.restore(this)
}
element.delete()
if ((initializer?.body as? KtBlockExpression)?.statements?.isNotEmpty() ?: false) {
initializer?.let { klass.addDeclaration(it) }
@@ -0,0 +1,13 @@
class WithComments {
/**
* A very important property
*/
val veryImportant: Any
/**
* A constructor
*/
constructor<caret>(/* Some parameter */veryImportant: Any) {
this.veryImportant = veryImportant
}
}
@@ -0,0 +1,10 @@
class WithComments/* Some parameter */
/**
* A constructor
*/(
/**
* A very important property
*/
val veryImportant: Any) {
}
@@ -4774,6 +4774,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("withComments.kt")
public void testWithComments() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertSecondaryConstructorToPrimary/withComments.kt");
doTest(fileName);
}
@TestMetadata("withDelegation.kt")
public void testWithDelegation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertSecondaryConstructorToPrimary/withDelegation.kt");