Improve positioning strategy for delegation calls

This commit is contained in:
Denis Zharkov
2015-03-04 16:00:04 +03:00
parent 4022982760
commit aa5599f6ca
6 changed files with 55 additions and 16 deletions
@@ -155,12 +155,19 @@ public interface Errors {
// Secondary constructors
DiagnosticFactory0<JetConstructorDelegationCall> CYCLIC_CONSTRUCTOR_DELEGATION_CALL = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetConstructorDelegationCall> CYCLIC_CONSTRUCTOR_DELEGATION_CALL =
DiagnosticFactory0.create(ERROR, PositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL);
DiagnosticFactory0<JetSecondaryConstructor> SECONDARY_CONSTRUCTOR_IN_OBJECT = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetDelegatorToSuperCall> SUPERTYPE_INITIALIZED_WITHOUT_PRIMARY_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetConstructorDelegationCall> PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<PsiElement> INIT_KEYWORD_BEFORE_CLASS_INITIALIZER_EXPECTED = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetConstructorDelegationCall> DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetConstructorDelegationCall> PRIMARY_CONSTRUCTOR_DELEGATION_CALL_EXPECTED =
DiagnosticFactory0.create(ERROR, PositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL);
DiagnosticFactory0<JetConstructorDelegationCall> DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR =
DiagnosticFactory0.create(ERROR, PositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL);
DiagnosticFactory0<PsiElement> PRIMARY_CONSTRUCTOR_REQUIRED_FOR_DATA_CLASS = DiagnosticFactory0.create(ERROR);
// Trait-specific
@@ -392,7 +399,8 @@ public interface Errors {
DiagnosticFactory0<JetExpression> NOT_A_CLASS = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> OVERLOAD_RESOLUTION_AMBIGUITY = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> NONE_APPLICABLE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> NONE_APPLICABLE =
DiagnosticFactory1.create(ERROR, PositioningStrategies.SECONDARY_CONSTRUCTOR_DELEGATION_CALL_OR_DEFAULT);
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> CANNOT_COMPLETE_RESOLVE = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<PsiElement, Collection<? extends ResolvedCall<?>>> UNRESOLVED_REFERENCE_WRONG_RECEIVER = DiagnosticFactory1.create(ERROR);
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.lexer.JetKeywordToken
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.utils.sure
import kotlin.platform.platformStatic
import org.jetbrains.kotlin.psi.psiUtil.*
public object PositioningStrategies {
private open class DeclarationHeader<T : JetDeclaration> : PositioningStrategy<T>() {
@@ -373,4 +374,28 @@ public object PositioningStrategies {
return DEFAULT.mark(element)
}
}
public val SECONDARY_CONSTRUCTOR_DELEGATION_CALL: PositioningStrategy<JetConstructorDelegationCall> =
object : PositioningStrategy<JetConstructorDelegationCall>() {
override fun mark(element: JetConstructorDelegationCall): List<TextRange> {
if (element.getCalleeExpression()?.isEmpty() ?: false) {
val constructor = element.getStrictParentOfType<JetSecondaryConstructor>()!!
return markElement(constructor.getConstructorKeyword())
}
return markElement(element.getCalleeExpression() ?: element)
}
}
public val SECONDARY_CONSTRUCTOR_DELEGATION_CALL_OR_DEFAULT: PositioningStrategy<PsiElement> =
object : PositioningStrategy<PsiElement>() {
override fun mark(element: PsiElement): List<TextRange> {
val parent = element.getParent()
if (parent is JetConstructorDelegationCall) {
return SECONDARY_CONSTRUCTOR_DELEGATION_CALL.mark(parent)
}
else {
return DEFAULT.mark(element)
}
}
}
}
@@ -21,6 +21,7 @@ import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
import org.jetbrains.kotlin.name.FqName;
import org.jetbrains.kotlin.name.Name;
import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
@@ -180,4 +181,9 @@ public class JetSecondaryConstructor extends JetDeclarationStub<KotlinPlaceHolde
public JetClassOrObject getClassOrObject() {
return (JetClassOrObject) getParent().getParent();
}
@NotNull
public PsiElement getConstructorKeyword() {
return findNotNullChildByType(JetTokens.CONSTRUCTOR_KEYWORD);
}
}
@@ -1,12 +1,12 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER
class A1 {
constructor(): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this()<!> {}
constructor(): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>() {}
}
class A2(x: Byte) {
constructor(x1: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, 1)<!> {}
constructor(x1: Int, x2: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, x2, 2)<!> {}
constructor(x1: Int, x2: Int, x3: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1)<!> {}
constructor(x1: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, 1) {}
constructor(x1: Int, x2: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, x2, 2) {}
constructor(x1: Int, x2: Int, x3: Int): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1) {}
// delegating to previously declared cycle
constructor(x1: Double): this(1) {}
@@ -15,9 +15,9 @@ class A2(x: Byte) {
// delegating to cycle declared after
constructor(x1: String): this(1L) {}
constructor(x1: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, 1L)<!> {}
constructor(x1: Long, x2: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, x2, 2L)<!> {}
constructor(x1: Long, x2: Long, x3: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1)<!> {}
constructor(x1: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, 1L) {}
constructor(x1: Long, x2: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, x2, 2L) {}
constructor(x1: Long, x2: Long, x3: Long): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1) {}
// no cycle, just call to primary constuctor
constructor(x1: Double, x2: Double): this(x1, x2, 1.0) {}
@@ -26,8 +26,8 @@ class A2(x: Byte) {
constructor(): this("x", "y") {}
constructor(x1: String, x2: String): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, x2, "")<!> {}
constructor(x1: String, x2: String, x3: String): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this(x1, x2)<!> {}
constructor(x1: String, x2: String): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, x2, "") {}
constructor(x1: String, x2: String, x3: String): <!CYCLIC_CONSTRUCTOR_DELEGATION_CALL!>this<!>(x1, x2) {}
}
open class B(x: Byte)
@@ -6,7 +6,7 @@ enum class A {
constructor(x: Int) {}
constructor(x: Int, y: Int): this(x+y) {}
constructor(x: Double): this(x.toInt(), 1) {}
constructor(x: String): <!DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR!>super(x, 1)<!> {}
constructor(x: String): <!DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR!>super<!>(x, 1) {}
}
enum class B(x: Int) {
@@ -14,7 +14,7 @@ enum class B(x: Int) {
constructor(x: Int, y: Int): this(x+y) {}
constructor(x: Double): this(x.toInt(), 1) {}
constructor(x: String): <!DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR!>super(x, 1)<!> {}
constructor(x: String): <!DELEGATION_SUPER_CALL_IN_ENUM_CONSTRUCTOR!>super<!>(x, 1) {}
}
enum class C {
@@ -6,5 +6,5 @@ open class B(x: Double) {
trait C
class A : B, C {
constructor(): <!NONE_APPLICABLE!>super<!>(' ') { }
constructor(x: Int) <!NONE_APPLICABLE!><!>{ }
<!NONE_APPLICABLE!>constructor<!>(x: Int) { }
}