Create From Usage: Add ability to select val/var when creating property

This commit is contained in:
Alexey Sedunov
2014-09-29 16:29:48 +04:00
parent e9b16f3e1b
commit e85d34b445
2 changed files with 17 additions and 0 deletions
@@ -410,6 +410,10 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
return returnTypeExpression
}
private fun setupValVarTemplate(builder: TemplateBuilder, property: JetProperty) {
builder.replaceElement(property.getValOrVarNode().getPsi()!!, ValVarExpression)
}
private fun setupTypeParameterListTemplate(builder: TemplateBuilderImpl, declaration: JetCallableDeclaration): TypeParameterListExpression {
val typeParameterMap = HashMap<String, Array<String>>()
val receiverTypeParameterNames = receiverTypeCandidate?.let { it.typeParameterNames!! } ?: ArrayUtil.EMPTY_STRING_ARRAY
@@ -481,6 +485,9 @@ class CallableBuilder(val config: CallableBuilderConfiguration) {
caretModel.moveToOffset(containingFile.getNode().getStartOffset())
val builder = TemplateBuilderImpl(containingFile)
if (declaration is JetProperty) {
setupValVarTemplate(builder, declaration)
}
val returnTypeExpression = if (isUnit) null else setupReturnTypeTemplate(builder, declaration)
val parameterTypeExpressions =
setupParameterTypeTemplates(builder, declaration.getValueParameterList()?.getParameters() ?: Collections.emptyList())
@@ -140,3 +140,13 @@ private class TypeParameterListExpression(private val typeParameterNamesFromRece
// do not offer the user any choices
override fun calculateLookupItems(context: ExpressionContext?) = array<LookupElement>()
}
private object ValVarExpression: Expression() {
private val cachedLookupElements = listOf("val", "var").map { LookupElementBuilder.create(it) }.copyToArray<LookupElement>()
override fun calculateResult(context: ExpressionContext?): Result? = TextResult("val")
override fun calculateQuickResult(context: ExpressionContext?): Result? = calculateResult(context)
override fun calculateLookupItems(context: ExpressionContext?): Array<LookupElement>? = cachedLookupElements
}