Search in sources :

Example 21 with GroovyPsiElement

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

the class GrBracesSurrounder method doSurroundElements.

@Override
protected GroovyPsiElement doSurroundElements(PsiElement[] elements, PsiElement context) throws IncorrectOperationException {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(elements[0].getProject());
    final PsiElement e0 = elements[0];
    final PsiElement parent = e0.getParent();
    final GrCodeBlock block;
    if (parent instanceof GrControlStatement) {
        block = factory.createMethodBodyFromText("\n");
        final PsiElement prev = e0.getPrevSibling();
        if (prev != null && prev.getNode().getElementType().equals(GroovyTokenTypes.mNLS)) {
            final ASTNode parentNode = e0.getParent().getNode();
            parentNode.addLeaf(TokenType.WHITE_SPACE, " ", prev.getNode());
            parentNode.removeChild(prev.getNode());
        }
    } else {
        block = factory.createClosureFromText("{}");
    }
    GroovyManyStatementsSurrounder.addStatements(block, elements);
    return block;
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) ASTNode(com.intellij.lang.ASTNode) GrControlStatement(org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrCodeBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)

Example 22 with GroovyPsiElement

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

the class IfSurrounder method getSurroundSelectionRange.

@Override
protected TextRange getSurroundSelectionRange(GroovyPsiElement element) {
    assert element instanceof GrIfStatement;
    GrCondition condition = ((GrIfStatement) element).getCondition();
    int endOffset = element.getTextRange().getEndOffset();
    if (condition != null) {
        PsiElement child = condition.getFirstChild();
        assert child != null;
        endOffset = child.getTextRange().getStartOffset();
        condition.getParent().getNode().removeChild(condition.getNode());
    }
    return new TextRange(endOffset, endOffset);
}
Also used : GrIfStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement) TextRange(com.intellij.openapi.util.TextRange) GrCondition(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 23 with GroovyPsiElement

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

the class GradleEditorValueExtractor method visitGStringExpression.

@Override
public void visitGStringExpression(GrString expression) {
    GroovyPsiElement[] parts = expression.getAllContentParts();
    boolean registeredAssignment = false;
    for (GroovyPsiElement part : parts) {
        if (part instanceof GrStringContent) {
            if (!myInterestedInReferencesOnly && !registeredAssignment && !part.getTextRange().isEmpty()) {
                registeredAssignment = true;
                String text = expression.getText();
                TextRange range = expression.getTextRange();
                if (text.startsWith("'") || text.startsWith("\"")) {
                    text = text.substring(1);
                    range = TextRange.create(range.getStartOffset() + 1, range.getEndOffset());
                }
                if (text.endsWith("'") || text.endsWith("\"")) {
                    text = text.substring(0, text.length() - 1);
                    range = TextRange.create(range.getStartOffset(), range.getEndOffset() - 1);
                }
                myContext.addCachedValue(text, range);
            }
        } else if (part instanceof GrStringInjection) {
            GrExpression injectedExpression = ((GrStringInjection) part).getExpression();
            if (injectedExpression != null) {
                injectedExpression.accept(myVisitor);
            }
        }
    }
}
Also used : GrStringContent(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringContent) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) TextRange(com.intellij.openapi.util.TextRange) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrString(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)

Example 24 with GroovyPsiElement

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

the class ApplyPluginTest method verifyAppliedPlugins.

private static void verifyAppliedPlugins(GradleBuildModel buildModel, String buildText) {
    assertEquals("apply", ImmutableList.of("com.android.application", "com.android.library"), buildModel.appliedPlugins());
    GroovyPsiElement buildFilePsiElement = buildModel.getPsiElement();
    assertNotNull(buildFilePsiElement);
    assertEquals("buildText", buildText, buildFilePsiElement.getText());
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 25 with GroovyPsiElement

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

the class GradleFileModelTestCase method verifyGradleValue.

public static void verifyGradleValue(@NotNull GradleNullableValue gradleValue, @NotNull String propertyName, @NotNull String propertyText, @NotNull String propertyFilePath) {
    GroovyPsiElement psiElement = gradleValue.getPsiElement();
    assertNotNull(psiElement);
    assertEquals(propertyText, psiElement.getText());
    assertEquals(propertyFilePath, toSystemIndependentName(gradleValue.getFile().getPath()));
    assertEquals(propertyName, gradleValue.getPropertyName());
    assertEquals(propertyText, gradleValue.getDslText());
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Aggregations

GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)98 PsiElement (com.intellij.psi.PsiElement)34 Nullable (org.jetbrains.annotations.Nullable)17 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)17 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)16 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)13 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)12 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)11 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)9 GrListOrMap (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap)8 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)8 GrApplicationStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrApplicationStatement)8 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)8 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)8 Project (com.intellij.openapi.project.Project)7 TextRange (com.intellij.openapi.util.TextRange)7 NotNull (org.jetbrains.annotations.NotNull)7 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)7 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)7 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)6