Added error 'public member should specify type' and quick fix
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package org.jetbrains.jet.plugin.quickfix;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticParameters;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithParameters;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithPsiElement;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.types.JetType;
|
||||
|
||||
/**
|
||||
* @author svtk
|
||||
*/
|
||||
public class AddReturnTypeFix extends JetIntentionAction<JetNamedDeclaration> {
|
||||
private JetType type;
|
||||
|
||||
public AddReturnTypeFix(@NotNull JetNamedDeclaration element, JetType type) {
|
||||
super(element);
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getText() {
|
||||
return "Add return type declaration";
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
return "Add return type declaration";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
|
||||
PsiElement newElement;
|
||||
if (element instanceof JetProperty) {
|
||||
newElement = addPropertyType(project, (JetProperty) element, type);
|
||||
}
|
||||
else {
|
||||
assert element instanceof JetFunction;
|
||||
newElement = addFunctionType(project, (JetFunction) element, type);
|
||||
}
|
||||
ImportClassHelper.perform(type, element, newElement);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean startInWriteAction() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static JetProperty addPropertyType(Project project, JetProperty property, JetType type) {
|
||||
JetProperty newProperty = (JetProperty) property.copy();
|
||||
JetTypeReference typeReference = JetPsiFactory.createType(project, type.toString());
|
||||
Pair<PsiElement, PsiElement> colon = JetPsiFactory.createColon(project);
|
||||
PsiElement nameIdentifier = newProperty.getNameIdentifier();
|
||||
addTypeReference(newProperty, typeReference, colon, nameIdentifier);
|
||||
return newProperty;
|
||||
}
|
||||
|
||||
public static JetFunction addFunctionType(Project project, JetFunction function, JetType type) {
|
||||
JetFunction newFunction = (JetFunction) function.copy();
|
||||
JetTypeReference typeReference = JetPsiFactory.createType(project, type.toString());
|
||||
Pair<PsiElement, PsiElement> colon = JetPsiFactory.createColon(project);
|
||||
JetParameterList valueParameterList = newFunction.getValueParameterList();
|
||||
addTypeReference(newFunction, typeReference, colon, valueParameterList);
|
||||
return newFunction;
|
||||
}
|
||||
|
||||
private static void addTypeReference(JetNamedDeclaration element, JetTypeReference typeReference, Pair<PsiElement, PsiElement> colon, PsiElement anchor) {
|
||||
assert anchor != null;
|
||||
element.addAfter(typeReference, anchor);
|
||||
element.addRangeAfter(colon.getFirst(), colon.getSecond(), anchor);
|
||||
}
|
||||
|
||||
public static JetIntentionActionFactory<JetNamedDeclaration> createFactory() {
|
||||
return new JetIntentionActionFactory<JetNamedDeclaration>() {
|
||||
@Override
|
||||
public JetIntentionAction<JetNamedDeclaration> createAction(DiagnosticWithPsiElement diagnostic) {
|
||||
assert diagnostic.getPsiElement() instanceof JetNamedDeclaration;
|
||||
DiagnosticWithParameters<PsiElement> diagnosticWithParameters = assertAndCastToDiagnosticWithParameters(diagnostic, DiagnosticParameters.TYPE);
|
||||
JetType type = diagnosticWithParameters.getParameter(DiagnosticParameters.TYPE);
|
||||
return new AddReturnTypeFix((JetNamedDeclaration) diagnostic.getPsiElement(), type);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -103,5 +103,7 @@ public class QuickFixes {
|
||||
add(Errors.GETTER_VISIBILITY_DIFFERS_FROM_PROPERTY_VISIBILITY, removeModifierFactory);
|
||||
add(Errors.REDUNDANT_MODIFIER_IN_GETTER, removeRedundantModifierFactory);
|
||||
add(Errors.ILLEGAL_MODIFIER, removeModifierFactory);
|
||||
|
||||
add(Errors.PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE, AddReturnTypeFix.createFactory());
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package org.jetbrains.jet.plugin.quickfix;
|
||||
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
@@ -97,7 +96,7 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction<JetProperty>
|
||||
newElement.deleteChildRange(nextSibling, initializer);
|
||||
|
||||
if (newElement.getPropertyTypeRef() == null && type != null) {
|
||||
newElement = addPropertyType(project, newElement, type);
|
||||
newElement = AddReturnTypeFix.addPropertyType(project, newElement, type);
|
||||
needImport = true;
|
||||
}
|
||||
}
|
||||
@@ -108,17 +107,6 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction<JetProperty>
|
||||
}
|
||||
}
|
||||
|
||||
public static JetProperty addPropertyType(Project project, JetProperty property, JetType type) {
|
||||
JetProperty newProperty = (JetProperty) property.copy();
|
||||
JetTypeReference typeReference = JetPsiFactory.createType(project, type.toString());
|
||||
Pair<PsiElement, PsiElement> colon = JetPsiFactory.createColon(project);
|
||||
PsiElement nameIdentifier = newProperty.getNameIdentifier();
|
||||
assert nameIdentifier != null;
|
||||
newProperty.addAfter(typeReference, nameIdentifier);
|
||||
newProperty.addRangeAfter(colon.getFirst(), colon.getSecond(), nameIdentifier);
|
||||
return newProperty;
|
||||
}
|
||||
|
||||
public static JetIntentionActionFactory<JetProperty> createFactory() {
|
||||
return new JetIntentionActionFactory<JetProperty>() {
|
||||
@Override
|
||||
|
||||
@@ -164,3 +164,8 @@ fun f(): Int {
|
||||
}
|
||||
|
||||
fun f(): Int = if (1 < 2) 1 else returnNothing()
|
||||
|
||||
public fun <!PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE!>f<!>() = 1
|
||||
class B() {
|
||||
protected fun <!PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE!>f<!>() = "ss"
|
||||
}
|
||||
@@ -25,4 +25,5 @@ class Test() {
|
||||
<!UNRESOLVED_REFERENCE!>$b<!> = <!UNRESOLVED_REFERENCE!>$a<!>
|
||||
a = <!UNRESOLVED_REFERENCE!>$b<!>
|
||||
}
|
||||
public val <!PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE!>i<!> = 1
|
||||
}
|
||||
@@ -3,7 +3,7 @@ class Outer() {
|
||||
val outer: Outer get() = this@Outer
|
||||
}
|
||||
|
||||
public val x = Inner()
|
||||
public val x : Inner = Inner()
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
open class Base() {
|
||||
public val plain = 239
|
||||
val plain = 239
|
||||
public val read : Int
|
||||
get() = 239
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Outer() {
|
||||
public val s = "xyzzy"
|
||||
val s = "xyzzy"
|
||||
|
||||
open class InnerBase(public val name: String) {
|
||||
}
|
||||
@@ -7,7 +7,7 @@ class Outer() {
|
||||
class InnerDerived(): InnerBase(s) {
|
||||
}
|
||||
|
||||
public val x = InnerDerived()
|
||||
val x = InnerDerived()
|
||||
}
|
||||
|
||||
fun box() : String {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
protected fun <caret>foo() : Int = 1
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
public fun <caret>foo() : String = "a"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
import java.util.List
|
||||
|
||||
public val <caret>l : List<Int>? = java.util.Collections.emptyList<Int>()
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "false"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
internal protected fun <caret>foo() = 1
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
protected fun <caret>foo() = 1
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "false"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
public fun <caret>foo()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
class A() {
|
||||
public fun <caret>foo() = "a"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// "Add return type declaration" "true"
|
||||
namespace a
|
||||
|
||||
public val <caret>l = java.util.Collections.emptyList<Int>()
|
||||
@@ -34,7 +34,7 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testPublicVar() throws Exception {
|
||||
loadText("class PublicVar() { public var foo = 0; }");
|
||||
loadText("class PublicVar() { public var foo : Int = 0; }");
|
||||
final Class aClass = loadImplementationClass(generateClassesInFile(), "PublicVar");
|
||||
final Object instance = aClass.newInstance();
|
||||
Method setter = findMethodByName(aClass, "setFoo");
|
||||
@@ -44,7 +44,7 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testAccessorsInInterface() {
|
||||
loadText("class AccessorsInInterface() { public var foo = 0; }");
|
||||
loadText("class AccessorsInInterface() { public var foo : Int = 0; }");
|
||||
final Class aClass = loadClass("AccessorsInInterface", generateClassesInFile());
|
||||
assertNotNull(findMethodByName(aClass, "getFoo"));
|
||||
assertNotNull(findMethodByName(aClass, "setFoo"));
|
||||
@@ -108,7 +108,7 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testInitializersForNamespaceProperties() throws Exception {
|
||||
loadText("public val x = System.currentTimeMillis()");
|
||||
loadText("val x = System.currentTimeMillis()");
|
||||
final Method method = generateFunction("getX");
|
||||
assertIsCurrentTime((Long) method.invoke(null));
|
||||
}
|
||||
@@ -136,7 +136,7 @@ public class PropertyGenTest extends CodegenTestCase {
|
||||
}
|
||||
|
||||
public void testKt160() throws Exception {
|
||||
loadText("public val s = java.lang.Double.toString(1.0)");
|
||||
loadText("internal val s = java.lang.Double.toString(1.0)");
|
||||
final Method method = generateFunction("getS");
|
||||
assertEquals(method.invoke(null), "1.0");
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.jetbrains.jet.plugin.quickfix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import org.jetbrains.jet.JetTestCaseBase;
|
||||
|
||||
/**
|
||||
@@ -21,5 +22,10 @@ public class TypeAdditionFixTests extends LightQuickFixTestCase {
|
||||
protected String getTestDataPath() {
|
||||
return JetTestCaseBase.getTestDataPathBase();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Sdk getProjectJDK() {
|
||||
return JetTestCaseBase.jdkFromIdeaHome();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user