Extract Function: Allow to choose between function and property extraction
This commit is contained in:
@@ -25,6 +25,7 @@ import com.intellij.util.LocalTimeCounter
|
||||
import org.jetbrains.jet.lang.resolve.ImportPath
|
||||
import org.jetbrains.jet.lexer.JetKeywordToken
|
||||
import org.jetbrains.jet.plugin.JetFileType
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory.CallableBuilder.Target
|
||||
|
||||
public fun JetPsiFactory(project: Project?): JetPsiFactory = JetPsiFactory(project!!)
|
||||
public fun JetPsiFactory(contextElement: JetElement): JetPsiFactory = JetPsiFactory(contextElement.getProject())
|
||||
@@ -424,7 +425,12 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return WhenBuilder(subject?.getText())
|
||||
}
|
||||
|
||||
public class FunctionBuilder() {
|
||||
public class CallableBuilder(private val target: Target) {
|
||||
public enum class Target {
|
||||
FUNCTION
|
||||
READ_ONLY_PROPERTY
|
||||
}
|
||||
|
||||
enum class State {
|
||||
MODIFIERS
|
||||
NAME
|
||||
@@ -440,25 +446,35 @@ public class JetPsiFactory(private val project: Project) {
|
||||
private var state = State.MODIFIERS
|
||||
|
||||
private fun closeParams() {
|
||||
assert(state == State.FIRST_PARAM || state == State.REST_PARAMS)
|
||||
|
||||
sb.append(")")
|
||||
if (target == Target.FUNCTION) {
|
||||
assert(state == State.FIRST_PARAM || state == State.REST_PARAMS)
|
||||
sb.append(")")
|
||||
}
|
||||
|
||||
state = State.TYPE_CONSTRAINTS
|
||||
}
|
||||
|
||||
private fun placeFun() {
|
||||
private fun placeKeyword() {
|
||||
assert(state == State.MODIFIERS)
|
||||
|
||||
if (sb.length() != 0) {
|
||||
sb.append(" ")
|
||||
}
|
||||
sb.append("fun ")
|
||||
val keyword = when (target) {
|
||||
Target.FUNCTION -> "fun"
|
||||
Target.READ_ONLY_PROPERTY -> "val"
|
||||
}
|
||||
sb.append("$keyword ")
|
||||
|
||||
state = State.RECEIVER
|
||||
}
|
||||
|
||||
public fun modifier(modifier: String): FunctionBuilder {
|
||||
private fun blockPrefix() = when (target) {
|
||||
Target.FUNCTION -> ""
|
||||
Target.READ_ONLY_PROPERTY -> "\nget()"
|
||||
}
|
||||
|
||||
public fun modifier(modifier: String): CallableBuilder {
|
||||
assert(state == State.MODIFIERS)
|
||||
|
||||
sb.append(modifier)
|
||||
@@ -466,8 +482,8 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return this
|
||||
}
|
||||
|
||||
public fun typeParams(values: Collection<String>): FunctionBuilder {
|
||||
placeFun()
|
||||
public fun typeParams(values: Collection<String>): CallableBuilder {
|
||||
placeKeyword()
|
||||
if (!values.isEmpty()) {
|
||||
sb.append(values.joinToString(", ", "<", "> ", -1, ""))
|
||||
}
|
||||
@@ -475,7 +491,7 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return this
|
||||
}
|
||||
|
||||
public fun receiver(receiverType: String): FunctionBuilder {
|
||||
public fun receiver(receiverType: String): CallableBuilder {
|
||||
assert(state == State.RECEIVER)
|
||||
|
||||
sb.append(receiverType).append(".")
|
||||
@@ -484,16 +500,24 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return this
|
||||
}
|
||||
|
||||
public fun name(name: String): FunctionBuilder {
|
||||
public fun name(name: String): CallableBuilder {
|
||||
assert(state == State.NAME || state == State.RECEIVER)
|
||||
|
||||
sb.append(name).append("(")
|
||||
state = State.FIRST_PARAM
|
||||
sb.append(name)
|
||||
when (target) {
|
||||
Target.FUNCTION -> {
|
||||
sb.append("(")
|
||||
state = State.FIRST_PARAM
|
||||
}
|
||||
else ->
|
||||
state = State.TYPE_CONSTRAINTS
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public fun param(name: String, `type`: String): FunctionBuilder {
|
||||
public fun param(name: String, `type`: String): CallableBuilder {
|
||||
assert(target == Target.FUNCTION)
|
||||
assert(state == State.FIRST_PARAM || state == State.REST_PARAMS)
|
||||
|
||||
if (state == State.REST_PARAMS) {
|
||||
@@ -507,20 +531,20 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return this
|
||||
}
|
||||
|
||||
public fun returnType(`type`: String): FunctionBuilder {
|
||||
public fun returnType(`type`: String): CallableBuilder {
|
||||
closeParams()
|
||||
sb.append(": ").append(`type`)
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public fun noReturnType(): FunctionBuilder {
|
||||
public fun noReturnType(): CallableBuilder {
|
||||
closeParams()
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public fun typeConstraints(values: Collection<String>): FunctionBuilder {
|
||||
public fun typeConstraints(values: Collection<String>): CallableBuilder {
|
||||
assert(state == State.TYPE_CONSTRAINTS)
|
||||
|
||||
if (!values.isEmpty()) {
|
||||
@@ -531,25 +555,25 @@ public class JetPsiFactory(private val project: Project) {
|
||||
return this
|
||||
}
|
||||
|
||||
public fun simpleBody(body: String): FunctionBuilder {
|
||||
public fun simpleBody(body: String): CallableBuilder {
|
||||
assert(state == State.BODY || state == State.TYPE_CONSTRAINTS)
|
||||
|
||||
sb.append(" = ").append(body)
|
||||
sb.append(blockPrefix()).append(" = ").append(body)
|
||||
state = State.DONE
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public fun blockBody(body: String): FunctionBuilder {
|
||||
public fun blockBody(body: String): CallableBuilder {
|
||||
assert(state == State.BODY || state == State.TYPE_CONSTRAINTS)
|
||||
|
||||
sb.append(" {\n").append(body).append("\n}")
|
||||
sb.append(blockPrefix()).append(" {\n").append(body).append("\n}")
|
||||
state = State.DONE
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
public fun toFunctionText(): String {
|
||||
public fun asString(): String {
|
||||
if (state != State.DONE) {
|
||||
state = State.DONE
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user