Search in sources :

Example 76 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class GradleDslExpressionList method createNamedArgumentList.

@Nullable
private GroovyPsiElement createNamedArgumentList() {
    assert myParent instanceof GradleDslExpressionMap;
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(parentPsiElement.getProject());
    GrExpression expressionFromText = factory.createExpressionFromText("[]");
    if (expressionFromText instanceof GrListOrMap) {
        // Elements need to be added to the list before adding the list to the named argument.
        GrListOrMap list = (GrListOrMap) expressionFromText;
        for (GradleDslExpression expression : myToBeAddedExpressions) {
            expression.setPsiElement(list);
            expression.applyChanges();
            myExpressions.add(expression);
        }
        myToBeAddedExpressions.clear();
    }
    GrNamedArgument namedArgument = factory.createNamedArgument(myName, expressionFromText);
    PsiElement added;
    if (parentPsiElement instanceof GrArgumentList) {
        added = ((GrArgumentList) parentPsiElement).addNamedArgument(namedArgument);
    } else {
        added = parentPsiElement.addAfter(namedArgument, parentPsiElement.getLastChild());
    }
    if (added instanceof GrNamedArgument) {
        GrNamedArgument addedNameArgument = (GrNamedArgument) added;
        setPsiElement(addedNameArgument.getExpression());
        return getPsiElement();
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 77 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class GradleDslExpressionMap method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = super.create();
    if (psiElement == null) {
        return null;
    }
    if (psiElement instanceof GrListOrMap || psiElement instanceof GrArgumentList) {
        return psiElement;
    }
    if (psiElement instanceof GrApplicationStatement) {
        GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
        GrArgumentList argumentList = factory.createArgumentListFromText("xyz");
        // Workaround to get an empty argument list.
        argumentList.getFirstChild().delete();
        PsiElement added = psiElement.addAfter(argumentList, psiElement.getLastChild());
        if (added instanceof GrArgumentList) {
            GrArgumentList addedArgumentList = (GrArgumentList) added;
            setPsiElement(addedArgumentList);
            return addedArgumentList;
        }
    }
    return null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 78 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class GradleDslLiteral method addConfigBlock.

private void addConfigBlock() {
    if (myUnsavedConfigBlock == null) {
        return;
    }
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null) {
        return;
    }
    GroovyPsiElementFactory factory = getPsiElementFactory();
    if (factory == null) {
        return;
    }
    // For now, this is only reachable for newly added dependencies, which means psiElement is an application statement with three children:
    // the configuration name, whitespace, dependency in compact notation. Let's add some more: comma, whitespace and finally the config
    // block.
    GrApplicationStatement methodCallStatement = (GrApplicationStatement) factory.createStatementFromText("foo 1, 2");
    PsiElement comma = methodCallStatement.getArgumentList().getFirstChild().getNextSibling();
    psiElement.addAfter(comma, psiElement.getLastChild());
    psiElement.addAfter(factory.createWhiteSpace(), psiElement.getLastChild());
    psiElement.addAfter(myUnsavedConfigBlock, psiElement.getLastChild());
    myUnsavedConfigBlock = null;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrApplicationStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 79 with GroovyPsiElementFactory

use of org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory in project android by JetBrains.

the class GradleDslLiteral method createLiteral.

@Nullable
private GrLiteral createLiteral() {
    if (myUnsavedValue == null) {
        return null;
    }
    CharSequence unsavedValueText = null;
    if (myUnsavedValue instanceof String) {
        unsavedValueText = GrStringUtil.getLiteralTextByValue((String) myUnsavedValue);
    } else if (myUnsavedValue instanceof Integer || myUnsavedValue instanceof Boolean) {
        unsavedValueText = myUnsavedValue.toString();
    }
    myUnsavedValue = null;
    if (unsavedValueText == null) {
        return null;
    }
    GroovyPsiElementFactory factory = getPsiElementFactory();
    if (factory == null) {
        return null;
    }
    GrExpression newExpression = factory.createExpressionFromText(unsavedValueText);
    if (!(newExpression instanceof GrLiteral)) {
        return null;
    }
    return (GrLiteral) newExpression;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with GroovyPsiElementFactory

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

the class GrStepDefinitionCreator method buildStepDefinitionByStep.

private static GrMethodCall buildStepDefinitionByStep(@NotNull final GherkinStep step) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(step.getProject());
    final Step cucumberStep = new Step(Collections.emptyList(), step.getKeyword().getText(), step.getStepName(), 0, null, null);
    SnippetGenerator generator = new SnippetGenerator(new GroovySnippet());
    final String fqnPendingException;
    if (GrCucumberUtil.isCucumber_1_1_orAbove(step)) {
        fqnPendingException = "cucumber.api.PendingException";
    } else {
        fqnPendingException = "cucumber.runtime.PendingException";
    }
    String snippet = generator.getSnippet(cucumberStep, null).replace("PendingException", fqnPendingException);
    return (GrMethodCall) factory.createStatementFromText(snippet, step);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) SnippetGenerator(cucumber.runtime.snippets.SnippetGenerator) Step(gherkin.formatter.model.Step) GherkinStep(org.jetbrains.plugins.cucumber.psi.GherkinStep) GroovySnippet(cucumber.runtime.groovy.GroovySnippet)

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