Search in sources :

Example 26 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.

the class GradleDslParser method getExpressionElement.

@Nullable
private static GradleDslExpression getExpressionElement(@NotNull GradleDslElement parentElement, @NotNull GroovyPsiElement psiElement, @NotNull String propertyName, @NotNull GrExpression propertyExpression) {
    if (propertyExpression instanceof GrLiteral) {
        // ex: compileSdkVersion 23 or compileSdkVersion = "android-23"
        return new GradleDslLiteral(parentElement, psiElement, propertyName, (GrLiteral) propertyExpression);
    }
    if (propertyExpression instanceof GrReferenceExpression) {
        // ex: compileSdkVersion SDK_VERSION or sourceCompatibility = VERSION_1_5
        return new GradleDslReference(parentElement, psiElement, propertyName, (GrReferenceExpression) propertyExpression);
    }
    if (propertyExpression instanceof GrMethodCallExpression) {
        // ex: compile project("someProject")
        GrMethodCallExpression methodCall = (GrMethodCallExpression) propertyExpression;
        GrReferenceExpression callReferenceExpression = getChildOfType(methodCall, GrReferenceExpression.class);
        if (callReferenceExpression != null) {
            String referenceName = callReferenceExpression.getText();
            if (!isEmpty(referenceName)) {
                GrArgumentList argumentList = methodCall.getArgumentList();
                if (argumentList.getAllArguments().length > 0) {
                    return getMethodCall(parentElement, methodCall, referenceName, argumentList);
                } else {
                    return new GradleDslMethodCall(parentElement, methodCall, referenceName);
                }
            }
        }
    }
    if (propertyExpression instanceof GrNewExpression) {
        GrNewExpression newExpression = (GrNewExpression) propertyExpression;
        GrCodeReferenceElement referenceElement = newExpression.getReferenceElement();
        if (referenceElement != null) {
            String objectName = referenceElement.getText();
            if (!isEmpty(objectName)) {
                GrArgumentList argumentList = newExpression.getArgumentList();
                if (argumentList != null) {
                    if (argumentList.getAllArguments().length > 0) {
                        return getNewExpression(parentElement, newExpression, objectName, argumentList);
                    }
                }
            }
        }
    }
    return null;
}
Also used : GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrMethodCallExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) Nullable(org.jetbrains.annotations.Nullable)

Example 27 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.

the class JavaVersionDslElement method getVersionText.

@Nullable
private String getVersionText() {
    GradleDslLiteral valueLiteral = null;
    if (myInternalVersionElement instanceof GradleDslReference) {
        GradleDslReference referenceElement = (GradleDslReference) myInternalVersionElement;
        valueLiteral = referenceElement.getValue(GradleDslLiteral.class);
        if (valueLiteral == null) {
            String resolvedReference = referenceElement.getValue(String.class);
            if (resolvedReference != null) {
                return resolvedReference;
            } else {
                return referenceElement.getReferenceText();
            }
        }
    }
    if (myInternalVersionElement instanceof GradleDslLiteral) {
        valueLiteral = (GradleDslLiteral) myInternalVersionElement;
    }
    if (valueLiteral != null) {
        GrLiteral literal = valueLiteral.getLiteral();
        if (literal != null) {
            return literal.getText();
        }
    }
    return null;
}
Also used : 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) Nullable(org.jetbrains.annotations.Nullable)

Example 28 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.

the class GradleDslLiteral method create.

@Override
@Nullable
public GroovyPsiElement create() {
    if (!(myParent instanceof GradleDslExpressionMap)) {
        return super.create();
    }
    // This is a value in the map element we need to create a named argument for it.
    GroovyPsiElement parentPsiElement = myParent.create();
    if (parentPsiElement == null) {
        return null;
    }
    setPsiElement(parentPsiElement);
    GrLiteral newLiteral = createLiteral();
    if (newLiteral == null) {
        return null;
    }
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(newLiteral.getProject());
    GrNamedArgument namedArgument = factory.createNamedArgument(myName, newLiteral);
    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;
        GrLiteral literal = getChildOfType(addedNameArgument, GrLiteral.class);
        if (literal != null) {
            myExpression = literal;
            setModified(false);
            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) 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 29 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project android by JetBrains.

the class GradleDslLiteral method apply.

@Override
protected void apply() {
    GroovyPsiElement psiElement = getPsiElement();
    if (psiElement == null) {
        return;
    }
    GrLiteral newLiteral = createLiteral();
    if (newLiteral == null) {
        return;
    }
    if (myExpression != null) {
        PsiElement replace = myExpression.replace(newLiteral);
        if (replace instanceof GrLiteral) {
            myExpression = (GrLiteral) replace;
        }
    } else {
        PsiElement added;
        if (// Entries in [].
        psiElement instanceof GrListOrMap || (psiElement instanceof GrArgumentList && !(psiElement instanceof GrCommandArgumentList))) {
            // Method call arguments in ().
            // add before ) or ]
            added = psiElement.addBefore(newLiteral, psiElement.getLastChild());
        } else {
            added = psiElement.addAfter(newLiteral, psiElement.getLastChild());
        }
        if (added instanceof GrLiteral) {
            myExpression = (GrLiteral) added;
        }
        if (myUnsavedConfigBlock != null) {
            addConfigBlock();
        }
    }
}
Also used : GrCommandArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrCommandArgumentList) 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) 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)

Example 30 with GrLiteral

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral in project intellij-plugins by JetBrains.

the class GrStepDefinitionCreator method createStepDefinition.

@Override
public boolean createStepDefinition(@NotNull GherkinStep step, @NotNull final PsiFile file) {
    if (!(file instanceof GroovyFile))
        return false;
    final Project project = file.getProject();
    final VirtualFile vFile = ObjectUtils.assertNotNull(file.getVirtualFile());
    final OpenFileDescriptor descriptor = new OpenFileDescriptor(project, vFile);
    FileEditorManager.getInstance(project).getAllEditors(vFile);
    FileEditorManager.getInstance(project).openTextEditor(descriptor, true);
    final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (editor != null) {
        final TemplateManager templateManager = TemplateManager.getInstance(file.getProject());
        final TemplateState templateState = TemplateManagerImpl.getTemplateState(editor);
        final Template template = templateManager.getActiveTemplate(editor);
        if (templateState != null && template != null) {
            templateState.gotoEnd();
        }
    }
    // snippet text
    final GrMethodCall element = buildStepDefinitionByStep(step);
    GrMethodCall methodCall = (GrMethodCall) ((GroovyFile) file).addStatementBefore(element, null);
    JavaCodeStyleManager.getInstance(project).shortenClassReferences(methodCall);
    methodCall = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(methodCall);
    PsiDocumentManager.getInstance(project).commitAllDocuments();
    if (ApplicationManager.getApplication().isUnitTestMode())
        return true;
    final TemplateBuilderImpl builder = (TemplateBuilderImpl) TemplateBuilderFactory.getInstance().createTemplateBuilder(methodCall);
    // regexp str
    GrLiteral pattern = GrCucumberUtil.getStepDefinitionPattern(methodCall);
    assert pattern != null;
    String patternText = pattern.getText();
    builder.replaceElement(pattern, new TextRange(1, patternText.length() - 1), patternText.substring(1, patternText.length() - 1));
    // block vars
    GrClosableBlock closure = methodCall.getClosureArguments()[0];
    final GrParameter[] blockVars = closure.getAllParameters();
    for (GrParameter var : blockVars) {
        PsiElement identifier = var.getNameIdentifierGroovy();
        builder.replaceElement(identifier, identifier.getText());
    }
    TemplateManager manager = TemplateManager.getInstance(project);
    final Editor editorToRunTemplate;
    if (editor == null) {
        editorToRunTemplate = IntentionUtils.positionCursor(project, file, methodCall);
    } else {
        editorToRunTemplate = editor;
    }
    Template template = builder.buildTemplate();
    TextRange range = methodCall.getTextRange();
    editorToRunTemplate.getDocument().deleteString(range.getStartOffset(), range.getEndOffset());
    editorToRunTemplate.getCaretModel().moveToOffset(range.getStartOffset());
    manager.startTemplate(editorToRunTemplate, template, new TemplateEditingAdapter() {

        @Override
        public void templateFinished(Template template, boolean brokenOff) {
            if (brokenOff)
                return;
            ApplicationManager.getApplication().runWriteAction(() -> {
                PsiDocumentManager.getInstance(project).commitDocument(editorToRunTemplate.getDocument());
                final int offset = editorToRunTemplate.getCaretModel().getOffset();
                GrMethodCall methodCall1 = PsiTreeUtil.findElementOfClassAtOffset(file, offset - 1, GrMethodCall.class, false);
                if (methodCall1 != null) {
                    GrClosableBlock[] closures = methodCall1.getClosureArguments();
                    if (closures.length == 1) {
                        GrClosableBlock closure1 = closures[0];
                        selectBody(closure1, editor);
                    }
                }
            });
        }
    });
    return true;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GrMethodCall(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrMethodCall) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) TextRange(com.intellij.openapi.util.TextRange) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) TemplateState(com.intellij.codeInsight.template.impl.TemplateState) Project(com.intellij.openapi.project.Project) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Editor(com.intellij.openapi.editor.Editor) GrLiteral(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrLiteral (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrLiteral)45 Nullable (org.jetbrains.annotations.Nullable)14 PsiElement (com.intellij.psi.PsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)10 NonNls (org.jetbrains.annotations.NonNls)9 PsiType (com.intellij.psi.PsiType)6 GrArgumentList (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList)6 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)5 TextRange (com.intellij.openapi.util.TextRange)4 BigInteger (java.math.BigInteger)4 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)4 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrStringInjection (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)3 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)3 GrMethodCallExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.path.GrMethodCallExpression)3