Search in sources :

Example 46 with GroovyPsiElementFactory

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

the class NotAndParenthesesSurrounder method surroundExpression.

@Override
protected TextRange surroundExpression(@NotNull GrExpression expression, @Nullable PsiElement context) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
    final GrUnaryExpression template = (GrUnaryExpression) factory.createExpressionFromText("!(a)", context);
    assert template.getOperand() != null;
    GroovyExpressionSurrounder.replaceToOldExpression(((GrParenthesizedExpression) template.getOperand()).getOperand(), expression);
    final GrExpression result = expression.replaceWithExpression(template, true);
    final int endOffset = result.getTextRange().getEndOffset();
    return new TextRange(endOffset, endOffset);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrUnaryExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrUnaryExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) TextRange(com.intellij.openapi.util.TextRange)

Example 47 with GroovyPsiElementFactory

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

the class JavaVersionDslElement method create.

@Override
@Nullable
public GroovyPsiElement create() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement != null) {
        return psiElement;
    }
    GradlePropertiesDslElement parent = (GradlePropertiesDslElement) myParent;
    assert parent != null;
    GroovyPsiElement javaPsiElement = parent.create();
    assert javaPsiElement != null;
    ApplicationManager.getApplication().assertWriteAccessAllowed();
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(javaPsiElement.getProject());
    // Tries to create the new element close to targetCompatibility or sourceCompatibility, if neither of them exists, create it at the
    // end of the file.
    // Also, tries to copy the value from targetCompatibility or sourceCompatibility if possible to keep consistency.
    JavaVersionDslElement anchor = null;
    if (SOURCE_COMPATIBILITY_ATTRIBUTE_NAME.equals(myName)) {
        anchor = parent.getPropertyElement(TARGET_COMPATIBILITY_ATTRIBUTE_NAME, JavaVersionDslElement.class);
    } else if (TARGET_COMPATIBILITY_ATTRIBUTE_NAME.equals(myName)) {
        anchor = parent.getPropertyElement(SOURCE_COMPATIBILITY_ATTRIBUTE_NAME, JavaVersionDslElement.class);
    }
    PsiElement anchorPsiElement = null;
    String anchorText = null;
    if (anchor != null) {
        anchorPsiElement = anchor.getPsiElement();
        anchorText = anchor.getVersionText();
    }
    if (anchorPsiElement == null) {
        anchorPsiElement = javaPsiElement.getLastChild();
    }
    if (anchorText == null) {
        anchorText = "1.6";
    }
    GrExpression newExpressionPsi;
    GroovyPsiElement valuePsi;
    if (myUseAssignment) {
        GrExpression expression = factory.createExpressionFromText(myName + " = " + anchorText);
        newExpressionPsi = (GrExpression) javaPsiElement.addBefore(expression, anchorPsiElement);
        valuePsi = ((GrAssignmentExpression) newExpressionPsi).getRValue();
    } else {
        GrExpression expression = factory.createExpressionFromText(myName + " " + anchorText);
        newExpressionPsi = (GrExpression) javaPsiElement.addBefore(expression, anchorPsiElement);
        valuePsi = ((GrApplicationStatement) newExpressionPsi).getExpressionArguments()[0];
    }
    if (valuePsi instanceof GrLiteral) {
        myInternalVersionElement = new GradleDslLiteral(parent, newExpressionPsi, myName, (GrLiteral) valuePsi);
    } else if (valuePsi instanceof GrReferenceExpression) {
        myInternalVersionElement = new GradleDslReference(parent, newExpressionPsi, myName, (GrReferenceExpression) valuePsi);
    }
    return getPsiElement();
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LanguageLevelUtil.parseFromGradleString(com.android.tools.idea.gradle.dsl.parser.java.LanguageLevelUtil.parseFromGradleString) LanguageLevelUtil.convertToGradleString(com.android.tools.idea.gradle.dsl.parser.java.LanguageLevelUtil.convertToGradleString) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with GroovyPsiElementFactory

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

the class GradleFilePsiMerger method mergeDependencies.

private static void mergeDependencies(@NotNull PsiElement fromRoot, @NotNull PsiElement toRoot, @NotNull Project project, @Nullable String supportLibVersionFilter) {
    Map<String, Multimap<String, GradleCoordinate>> dependencies = Maps.newHashMap();
    final List<String> unparsedDependencies = Lists.newArrayList();
    // Load existing dependencies into the map for the existing build.gradle
    pullDependenciesIntoMap(toRoot, dependencies, null);
    // Load dependencies into the map for the new build.gradle
    pullDependenciesIntoMap(fromRoot, dependencies, unparsedDependencies);
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    RepositoryUrlManager urlManager = RepositoryUrlManager.get();
    ImmutableList<String> configurations = CONFIGURATION_ORDERING.immutableSortedCopy(dependencies.keySet());
    AndroidSdkData sdk = AndroidSdks.getInstance().tryToChooseAndroidSdk();
    if (sdk != null) {
        for (String configurationName : configurations) {
            List<GradleCoordinate> resolved = urlManager.resolveDynamicSdkDependencies(dependencies.get(configurationName), supportLibVersionFilter, sdk, FileOpUtils.create());
            for (GradleCoordinate dependency : resolved) {
                PsiElement dependencyElement = factory.createStatementFromText(String.format("%s '%s'\n", configurationName, dependency.toString()));
                toRoot.addBefore(dependencyElement, toRoot.getLastChild());
            }
        }
    }
    for (String dependency : unparsedDependencies) {
        PsiElement dependencyElement = factory.createStatementFromText(dependency);
        toRoot.addBefore(dependencyElement, toRoot.getLastChild());
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) AndroidSdkData(org.jetbrains.android.sdk.AndroidSdkData) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) PsiElement(com.intellij.psi.PsiElement)

Example 49 with GroovyPsiElementFactory

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

the class MultipleRepositoryUrlsFix method doFix.

@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
    List<GrCallExpression> statements = MultipleRepositoryUrlsInspection.findUrlCallExpressions(myClosure);
    if (statements.size() <= 1)
        return;
    statements.remove(0);
    List<PsiElement> elements = new ArrayList<>(statements);
    for (GrCallExpression statement : statements) {
        PsiElement newLineCandidate = statement.getNextSibling();
        if (PsiUtil.isNewLine(newLineCandidate)) {
            elements.add(newLineCandidate);
        }
    }
    myClosure.removeElements(elements.toArray(new PsiElement[elements.size()]));
    GrClosableBlock closableBlock = PsiTreeUtil.getParentOfType(myClosure, GrClosableBlock.class);
    if (closableBlock == null)
        return;
    GroovyPsiElementFactory elementFactory = GroovyPsiElementFactory.getInstance(project);
    for (GrCallExpression statement : statements) {
        closableBlock.addStatementBefore(elementFactory.createStatementFromText(myRepoType + '{' + statement.getText() + '}'), null);
    }
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrCallExpression) ArrayList(java.util.ArrayList) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) PsiElement(com.intellij.psi.PsiElement)

Example 50 with GroovyPsiElementFactory

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

the class GrInspectionUtil method replaceExpression.

public static void replaceExpression(GrExpression expression, String newExpression) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
    final GrExpression newCall = factory.createExpressionFromText(newExpression);
    expression.replaceWithExpression(newCall, true);
}
Also used : GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)

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