@NotNull in JetPsiFactory
This commit is contained in:
@@ -36,20 +36,24 @@ import java.util.List;
|
|||||||
|
|
||||||
public class JetPsiFactory {
|
public class JetPsiFactory {
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static ASTNode createValNode(Project project) {
|
public static ASTNode createValNode(Project project) {
|
||||||
JetProperty property = createProperty(project, "val x = 1");
|
JetProperty property = createProperty(project, "val x = 1");
|
||||||
return property.getValOrVarNode();
|
return property.getValOrVarNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static ASTNode createVarNode(Project project) {
|
public static ASTNode createVarNode(Project project) {
|
||||||
JetProperty property = createProperty(project, "var x = 1");
|
JetProperty property = createProperty(project, "var x = 1");
|
||||||
return property.getValOrVarNode();
|
return property.getValOrVarNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static ASTNode createValOrVarNode(Project project, String text) {
|
public static ASTNode createValOrVarNode(Project project, String text) {
|
||||||
return createParameterList(project, "(" + text + " int x)").getParameters().get(0).getValOrVarNode();
|
return createParameterList(project, "(" + text + " int x)").getParameters().get(0).getValOrVarNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetExpression createExpression(Project project, String text) {
|
public static JetExpression createExpression(Project project, String text) {
|
||||||
JetProperty property = createProperty(project, "val x = " + text);
|
JetProperty property = createProperty(project, "val x = " + text);
|
||||||
return property.getInitializer();
|
return property.getInitializer();
|
||||||
@@ -63,6 +67,7 @@ public class JetPsiFactory {
|
|||||||
return callExpression.getValueArgumentList();
|
return callExpression.getValueArgumentList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetTypeArgumentList createTypeArguments(Project project, String text) {
|
public static JetTypeArgumentList createTypeArguments(Project project, String text) {
|
||||||
JetProperty property = createProperty(project, "val x = foo" + text + "()");
|
JetProperty property = createProperty(project, "val x = foo" + text + "()");
|
||||||
JetExpression initializer = property.getInitializer();
|
JetExpression initializer = property.getInitializer();
|
||||||
@@ -70,6 +75,7 @@ public class JetPsiFactory {
|
|||||||
return callExpression.getTypeArgumentList();
|
return callExpression.getTypeArgumentList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetTypeReference createType(Project project, String type) {
|
public static JetTypeReference createType(Project project, String type) {
|
||||||
JetProperty property = createProperty(project, "val x : " + type);
|
JetProperty property = createProperty(project, "val x : " + type);
|
||||||
return property.getTypeRef();
|
return property.getTypeRef();
|
||||||
@@ -90,17 +96,20 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//the pair contains the first and the last elements of a range
|
//the pair contains the first and the last elements of a range
|
||||||
|
@NotNull
|
||||||
public static Pair<PsiElement, PsiElement> createColonAndWhiteSpaces(Project project) {
|
public static Pair<PsiElement, PsiElement> createColonAndWhiteSpaces(Project project) {
|
||||||
JetProperty property = createProperty(project, "val x : Int");
|
JetProperty property = createProperty(project, "val x : Int");
|
||||||
return Pair.create(property.findElementAt(5), property.findElementAt(7));
|
return Pair.create(property.findElementAt(5), property.findElementAt(7));
|
||||||
}
|
}
|
||||||
|
|
||||||
//the pair contains the first and the last elements of a range
|
//the pair contains the first and the last elements of a range
|
||||||
|
@NotNull
|
||||||
public static Pair<PsiElement, PsiElement> createTypeWhiteSpaceAndColon(Project project, String type) {
|
public static Pair<PsiElement, PsiElement> createTypeWhiteSpaceAndColon(Project project, String type) {
|
||||||
JetProperty property = createProperty(project, "val x: " + type);
|
JetProperty property = createProperty(project, "val x: " + type);
|
||||||
return Pair.create(property.findElementAt(5), (PsiElement) property.getTypeRef());
|
return Pair.create(property.findElementAt(5), (PsiElement) property.getTypeRef());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static ASTNode createColonNode(Project project) {
|
public static ASTNode createColonNode(Project project) {
|
||||||
JetProperty property = createProperty(project, "val x: Int");
|
JetProperty property = createProperty(project, "val x: Int");
|
||||||
return property.getNode().findChildByType(JetTokens.COLON);
|
return property.getNode().findChildByType(JetTokens.COLON);
|
||||||
@@ -127,6 +136,7 @@ public class JetPsiFactory {
|
|||||||
return createWhiteSpace(project, " ");
|
return createWhiteSpace(project, " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private static PsiElement createWhiteSpace(Project project, String text) {
|
private static PsiElement createWhiteSpace(Project project, String text) {
|
||||||
JetProperty property = createProperty(project, "val" + text + "x");
|
JetProperty property = createProperty(project, "val" + text + "x");
|
||||||
return property.findElementAt(3);
|
return property.findElementAt(3);
|
||||||
@@ -137,6 +147,7 @@ public class JetPsiFactory {
|
|||||||
return createWhiteSpace(project, "\n");
|
return createWhiteSpace(project, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetClass createClass(Project project, String text) {
|
public static JetClass createClass(Project project, String text) {
|
||||||
return createDeclaration(project, text, JetClass.class);
|
return createDeclaration(project, text, JetClass.class);
|
||||||
}
|
}
|
||||||
@@ -158,19 +169,23 @@ public class JetPsiFactory {
|
|||||||
LocalTimeCounter.currentTime(), true);
|
LocalTimeCounter.currentTime(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetProperty createProperty(Project project, String name, String type, boolean isVar, @Nullable String initializer) {
|
public static JetProperty createProperty(Project project, String name, String type, boolean isVar, @Nullable String initializer) {
|
||||||
String text = (isVar ? "var " : "val ") + name + (type != null ? ":" + type : "") + (initializer == null ? "" : " = " + initializer);
|
String text = (isVar ? "var " : "val ") + name + (type != null ? ":" + type : "") + (initializer == null ? "" : " = " + initializer);
|
||||||
return createProperty(project, text);
|
return createProperty(project, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetProperty createProperty(Project project, String name, String type, boolean isVar) {
|
public static JetProperty createProperty(Project project, String name, String type, boolean isVar) {
|
||||||
return createProperty(project, name, type, isVar, null);
|
return createProperty(project, name, type, isVar, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetProperty createProperty(Project project, String text) {
|
public static JetProperty createProperty(Project project, String text) {
|
||||||
return createDeclaration(project, text, JetProperty.class);
|
return createDeclaration(project, text, JetProperty.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static <T> T createDeclaration(Project project, String text, Class<T> clazz) {
|
public static <T> T createDeclaration(Project project, String text, Class<T> clazz) {
|
||||||
JetFile file = createFile(project, text);
|
JetFile file = createFile(project, text);
|
||||||
List<JetDeclaration> dcls = file.getDeclarations();
|
List<JetDeclaration> dcls = file.getDeclarations();
|
||||||
@@ -180,26 +195,32 @@ public class JetPsiFactory {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static PsiElement createNameIdentifier(Project project, String name) {
|
public static PsiElement createNameIdentifier(Project project, String name) {
|
||||||
return createProperty(project, name, null, false).getNameIdentifier();
|
return createProperty(project, name, null, false).getNameIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetSimpleNameExpression createSimpleName(Project project, String name) {
|
public static JetSimpleNameExpression createSimpleName(Project project, String name) {
|
||||||
return (JetSimpleNameExpression) createProperty(project, name, null, false, name).getInitializer();
|
return (JetSimpleNameExpression) createProperty(project, name, null, false, name).getInitializer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static PsiElement createIdentifier(Project project, String name) {
|
public static PsiElement createIdentifier(Project project, String name) {
|
||||||
return createSimpleName(project, name).getIdentifier();
|
return createSimpleName(project, name).getIdentifier();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetNamedFunction createFunction(Project project, String funDecl) {
|
public static JetNamedFunction createFunction(Project project, String funDecl) {
|
||||||
return createDeclaration(project, funDecl, JetNamedFunction.class);
|
return createDeclaration(project, funDecl, JetNamedFunction.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetModifierList createModifierList(Project project, JetKeywordToken modifier) {
|
public static JetModifierList createModifierList(Project project, JetKeywordToken modifier) {
|
||||||
return createModifierList(project, modifier.getValue());
|
return createModifierList(project, modifier.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetModifierList createModifierList(Project project, String text) {
|
public static JetModifierList createModifierList(Project project, String text) {
|
||||||
JetProperty property = createProperty(project, text + " val x");
|
JetProperty property = createProperty(project, text + " val x");
|
||||||
return property.getModifierList();
|
return property.getModifierList();
|
||||||
@@ -213,26 +234,31 @@ public class JetPsiFactory {
|
|||||||
return modifierList.getAnnotations().get(0);
|
return modifierList.getAnnotations().get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetModifierList createConstructorModifierList(Project project, JetKeywordToken modifier) {
|
public static JetModifierList createConstructorModifierList(Project project, JetKeywordToken modifier) {
|
||||||
JetClass aClass = createClass(project, "class C " + modifier.getValue() + " (){}");
|
JetClass aClass = createClass(project, "class C " + modifier.getValue() + " (){}");
|
||||||
return aClass.getPrimaryConstructorModifierList();
|
return aClass.getPrimaryConstructorModifierList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetExpression createEmptyBody(Project project) {
|
public static JetExpression createEmptyBody(Project project) {
|
||||||
JetNamedFunction function = createFunction(project, "fun foo() {}");
|
JetNamedFunction function = createFunction(project, "fun foo() {}");
|
||||||
return function.getBodyExpression();
|
return function.getBodyExpression();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetClassBody createEmptyClassBody(Project project) {
|
public static JetClassBody createEmptyClassBody(Project project) {
|
||||||
JetClass aClass = createClass(project, "class A(){}");
|
JetClass aClass = createClass(project, "class A(){}");
|
||||||
return aClass.getBody();
|
return aClass.getBody();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetParameter createParameter(Project project, String name, String type) {
|
public static JetParameter createParameter(Project project, String name, String type) {
|
||||||
JetNamedFunction function = createFunction(project, "fun foo(" + name + " : " + type + ") {}");
|
JetNamedFunction function = createFunction(project, "fun foo(" + name + " : " + type + ") {}");
|
||||||
return function.getValueParameters().get(0);
|
return function.getValueParameters().get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetParameterList createParameterList(Project project, String text) {
|
public static JetParameterList createParameterList(Project project, String text) {
|
||||||
JetNamedFunction function = createFunction(project, "fun foo" + text + "{}");
|
JetNamedFunction function = createFunction(project, "fun foo" + text + "{}");
|
||||||
return function.getValueParameterList();
|
return function.getValueParameterList();
|
||||||
@@ -249,6 +275,7 @@ public class JetPsiFactory {
|
|||||||
return whenEntry;
|
return whenEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetStringTemplateEntryWithExpression createBlockStringTemplateEntry(@NotNull Project project, @NotNull JetExpression expression) {
|
public static JetStringTemplateEntryWithExpression createBlockStringTemplateEntry(@NotNull Project project, @NotNull JetExpression expression) {
|
||||||
JetStringTemplateExpression stringTemplateExpression = (JetStringTemplateExpression) createExpression(project,
|
JetStringTemplateExpression stringTemplateExpression = (JetStringTemplateExpression) createExpression(project,
|
||||||
"\"${" + expression.getText() + "}\"");
|
"\"${" + expression.getText() + "}\"");
|
||||||
@@ -278,16 +305,19 @@ public class JetPsiFactory {
|
|||||||
return namespace.getImportDirectives().iterator().next();
|
return namespace.getImportDirectives().iterator().next();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static PsiElement createPrimaryConstructor(Project project) {
|
public static PsiElement createPrimaryConstructor(Project project) {
|
||||||
JetClass aClass = createClass(project, "class A()");
|
JetClass aClass = createClass(project, "class A()");
|
||||||
return aClass.findElementAt(7).getParent();
|
return aClass.findElementAt(7).getParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetSimpleNameExpression createClassLabel(Project project, @NotNull String labelName) {
|
public static JetSimpleNameExpression createClassLabel(Project project, @NotNull String labelName) {
|
||||||
JetThisExpression expression = (JetThisExpression) createExpression(project, "this@" + labelName);
|
JetThisExpression expression = (JetThisExpression) createExpression(project, "this@" + labelName);
|
||||||
return expression.getTargetLabel();
|
return expression.getTargetLabel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetExpression createFieldIdentifier(Project project, @NotNull String fieldName) {
|
public static JetExpression createFieldIdentifier(Project project, @NotNull String fieldName) {
|
||||||
return createExpression(project, "$" + fieldName);
|
return createExpression(project, "$" + fieldName);
|
||||||
}
|
}
|
||||||
@@ -302,10 +332,12 @@ public class JetPsiFactory {
|
|||||||
return createBinaryExpression(project, JetPsiUtil.getText(lhs), op, JetPsiUtil.getText(rhs));
|
return createBinaryExpression(project, JetPsiUtil.getText(lhs), op, JetPsiUtil.getText(rhs));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetTypeCodeFragment createTypeCodeFragment(Project project, String text, PsiElement context) {
|
public static JetTypeCodeFragment createTypeCodeFragment(Project project, String text, PsiElement context) {
|
||||||
return new JetTypeCodeFragmentImpl(project, "fragment.kt", text, context);
|
return new JetTypeCodeFragmentImpl(project, "fragment.kt", text, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetExpressionCodeFragment createExpressionCodeFragment(Project project, String text, PsiElement context) {
|
public static JetExpressionCodeFragment createExpressionCodeFragment(Project project, String text, PsiElement context) {
|
||||||
return new JetExpressionCodeFragmentImpl(project, "fragment.kt", text, context);
|
return new JetExpressionCodeFragmentImpl(project, "fragment.kt", text, context);
|
||||||
}
|
}
|
||||||
@@ -490,16 +522,19 @@ public class JetPsiFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetExpression createFunctionBody(Project project, @NotNull String bodyText) {
|
public static JetExpression createFunctionBody(Project project, @NotNull String bodyText) {
|
||||||
JetFunction func = createFunction(project, "fun foo() {\n" + bodyText + "\n}");
|
JetFunction func = createFunction(project, "fun foo() {\n" + bodyText + "\n}");
|
||||||
return func.getBodyExpression();
|
return func.getBodyExpression();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetClassObject createEmptyClassObject(Project project) {
|
public static JetClassObject createEmptyClassObject(Project project) {
|
||||||
JetClass klass = createClass(project, "class foo { class object { } }");
|
JetClass klass = createClass(project, "class foo { class object { } }");
|
||||||
return klass.getClassObject();
|
return klass.getClassObject();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public static JetBlockExpression wrapInABlock(@NotNull final JetExpression expression) {
|
public static JetBlockExpression wrapInABlock(@NotNull final JetExpression expression) {
|
||||||
if (expression instanceof JetBlockExpression) {
|
if (expression instanceof JetBlockExpression) {
|
||||||
return (JetBlockExpression) expression;
|
return (JetBlockExpression) expression;
|
||||||
|
|||||||
Reference in New Issue
Block a user