Search in sources :

Example 61 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrReferenceExpressionImpl method bindToElementViaStaticImport.

@Override
public GrReferenceExpression bindToElementViaStaticImport(@NotNull PsiMember member) {
    if (getQualifier() != null) {
        throw new IncorrectOperationException("Reference has qualifier");
    }
    if (StringUtil.isEmpty(getReferenceName())) {
        throw new IncorrectOperationException("Reference has empty name");
    }
    PsiClass containingClass = member.getContainingClass();
    if (containingClass == null) {
        throw new IncorrectOperationException("Member has no containing class");
    }
    final PsiFile file = getContainingFile();
    if (file instanceof GroovyFile) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
        String text = "import static " + containingClass.getQualifiedName() + "." + member.getName();
        final GrImportStatement statement = factory.createImportStatementFromText(text);
        ((GroovyFile) file).addImport(statement);
    }
    return this;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrImportStatement(org.jetbrains.plugins.groovy.lang.psi.api.toplevel.imports.GrImportStatement) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 62 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrEnumConstantImpl method addNamedArgument.

@Override
public GrNamedArgument addNamedArgument(final GrNamedArgument namedArgument) throws IncorrectOperationException {
    GrArgumentList list = getArgumentList();
    assert list != null;
    if (list.getText().trim().isEmpty()) {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
        final GrArgumentList newList = factory.createArgumentList();
        list = (GrArgumentList) list.replace(newList);
    }
    return list.addNamedArgument(namedArgument);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)

Example 63 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrClassReferenceTypePointer method calcType.

@Nullable
@Override
protected GrClassReferenceType calcType() {
    final GrReferenceElement reference = mySmartPsiElementPointer.getElement();
    if (reference != null) {
        return new GrClassReferenceType(reference);
    }
    try {
        final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
        GrTypeElement typeElement = factory.createTypeElement(myReferenceText, null);
        return (GrClassReferenceType) typeElement.getType();
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrClassReferenceType(org.jetbrains.plugins.groovy.lang.psi.impl.GrClassReferenceType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement) Nullable(org.jetbrains.annotations.Nullable)

Example 64 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GroovyPropertyUtils method generateSetterPrototype.

public static GrMethod generateSetterPrototype(PsiField field) {
    Project project = field.getProject();
    JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    String name = field.getName();
    boolean isStatic = field.hasModifierProperty(PsiModifier.STATIC);
    VariableKind kind = codeStyleManager.getVariableKind(field);
    String propertyName = codeStyleManager.variableNameToPropertyName(name, kind);
    String setName = getSetterName(field.getName());
    final PsiClass containingClass = field.getContainingClass();
    try {
        GrMethod setMethod = factory.createMethod(setName, PsiType.VOID);
        String parameterName = codeStyleManager.propertyNameToVariableName(propertyName, VariableKind.PARAMETER);
        final PsiType type = field instanceof GrField ? ((GrField) field).getDeclaredType() : field.getType();
        GrParameter param = factory.createParameter(parameterName, type);
        annotateWithNullableStuff(field, param);
        setMethod.getParameterList().add(param);
        PsiUtil.setModifierProperty(setMethod, PsiModifier.STATIC, isStatic);
        @NonNls StringBuilder builder = new StringBuilder();
        if (name.equals(parameterName)) {
            if (!isStatic) {
                builder.append("this.");
            } else {
                String className = containingClass.getName();
                if (className != null) {
                    builder.append(className);
                    builder.append(".");
                }
            }
        }
        builder.append(name);
        builder.append("=");
        builder.append(parameterName);
        builder.append("\n");
        GrCodeBlock body = factory.createMethodBodyFromText(builder.toString());
        setMethod.getBlock().replace(body);
        return setMethod;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        return null;
    }
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) NonNls(org.jetbrains.annotations.NonNls) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) Project(com.intellij.openapi.project.Project) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) VariableKind(com.intellij.psi.codeStyle.VariableKind) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 65 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project intellij-community by JetBrains.

the class GrLightAnnotation method addAttribute.

public void addAttribute(PsiNameValuePair pair) {
    if (pair instanceof GrAnnotationNameValuePair) {
        myAnnotationArgList.addAttribute((GrAnnotationNameValuePair) pair);
    } else {
        GrAnnotationMemberValue newValue = new AnnotationArgConverter().convert(pair.getValue());
        if (newValue == null)
            return;
        String name = pair.getName();
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(pair.getProject());
        String annotationText;
        annotationText = name != null ? "@A(" + name + "=" + newValue.getText() + ")" : "@A(" + newValue.getText() + ")";
        GrAnnotation annotation = factory.createAnnotationFromText(annotationText);
        myAnnotationArgList.addAttribute(annotation.getParameterList().getAttributes()[0]);
    }
}
Also used : GrAnnotationMemberValue(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationMemberValue) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrAnnotationNameValuePair(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotationNameValuePair) AnnotationArgConverter(org.jetbrains.plugins.groovy.lang.psi.impl.AnnotationArgConverter)

Aggregations

GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)159 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)42 PsiElement (com.intellij.psi.PsiElement)30 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)23 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)22 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)21 IncorrectOperationException (com.intellij.util.IncorrectOperationException)20 Nullable (org.jetbrains.annotations.Nullable)20 GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)18 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)17 NotNull (org.jetbrains.annotations.NotNull)16 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)15 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)14 Project (com.intellij.openapi.project.Project)8 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)8 ASTNode (com.intellij.lang.ASTNode)7 ArrayList (java.util.ArrayList)7 GrCodeReferenceElement (org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6