Search in sources :

Example 11 with GroovyPsiElement

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

the class ConvertConcatenationToGstringIntention method containsMultilineStrings.

private static boolean containsMultilineStrings(GrExpression expr) {
    final Ref<Boolean> result = Ref.create(false);
    expr.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitLiteralExpression(@NotNull GrLiteral literal) {
            if (GrStringUtil.isMultilineStringLiteral(literal) && literal.getText().contains("\n")) {
                result.set(true);
            }
        }

        @Override
        public void visitElement(@NotNull GroovyPsiElement element) {
            if (!result.get()) {
                super.visitElement(element);
            }
        }
    });
    return result.get();
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)

Example 12 with GroovyPsiElement

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

the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.

private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
    final StringPartInfo part = context.getStringPart();
    GrExpression expression = context.getExpression();
    if (expression != null) {
        expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
        if (expression == null)
            return false;
        if (expression instanceof GrReferenceExpression) {
            final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
            if (PsiUtil.isLocalVariable(resolved)) {
                expression = ((GrVariable) resolved).getInitializerGroovy();
                if (expression == null)
                    return false;
            }
        }
        ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
        expression.accept(visitor);
        return visitor.isResult();
    }
    if (part != null) {
        for (GrStringInjection injection : part.getInjections()) {
            GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
            assert scope != null;
            ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
            scope.accept(visitor);
            if (!visitor.isResult()) {
                return visitor.isResult();
            }
        }
        return true;
    } else {
        return false;
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrStringInjection(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrStringInjection) StringPartInfo(org.jetbrains.plugins.groovy.refactoring.introduce.StringPartInfo) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 13 with GroovyPsiElement

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

the class GroovyParameterInfoHandler method getCurrentParameterIndex.

private static int getCurrentParameterIndex(GroovyPsiElement place, int offset) {
    if (place instanceof GrArgumentList) {
        GrArgumentList list = (GrArgumentList) place;
        int idx = (list.getNamedArguments().length > 0) ? 1 : 0;
        for (PsiElement child = list.getFirstChild(); child != null; child = child.getNextSibling()) {
            if (child.getTextRange().contains(offset)) {
                if (child instanceof GrNamedArgument)
                    return 0;
                return idx;
            }
            if (child.getNode().getElementType() == GroovyTokenTypes.mCOMMA)
                idx++;
            if (isNamedArgWithPriorComma(child))
                idx--;
        }
    }
    return -1;
}
Also used : GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) GrArgumentList(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentList) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 14 with GroovyPsiElement

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

the class GroovyChangeContextUtil method encodeContextInfo.

public static void encodeContextInfo(PsiElement element, PsiElement scope) {
    if (!(element instanceof GroovyPsiElement))
        return;
    if (PsiUtil.isThisReference(element)) {
        GrReferenceExpression thisExpr = (GrReferenceExpression) element;
        final PsiClass containingClass = PsiTreeUtil.getParentOfType(thisExpr, PsiClass.class);
        element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
        thisExpr.putCopyableUserData(QUALIFIER_CLASS_KEY, containingClass);
    } else if (element instanceof GrReferenceExpression) {
        GrReferenceExpression refExpr = (GrReferenceExpression) element;
        final GrExpression qualifier = refExpr.getQualifierExpression();
        if (qualifier == null) {
            PsiElement refElement = refExpr.resolve();
            element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
            if (refElement != null && !PsiTreeUtil.isContextAncestor(scope, refElement, false)) {
                if (refElement instanceof GrAccessorMethod)
                    refElement = ((GrAccessorMethod) refElement).getProperty();
                if (refElement instanceof PsiClass) {
                    refExpr.putCopyableUserData(REF_TO_CLASS, (PsiClass) refElement);
                } else if (refElement instanceof PsiMember) {
                    refExpr.putCopyableUserData(REF_TO_MEMBER, (PsiMember) refElement);
                }
            }
        }
    } else if (element instanceof GrCodeReferenceElement) {
        final PsiElement resolvedElement = ((GrCodeReferenceElement) element).resolve();
        element.putCopyableUserData(KEY_ENCODED, KEY_ENCODED);
        if (resolvedElement instanceof PsiClass && !PsiTreeUtil.isContextAncestor(scope, resolvedElement, false)) {
            element.putCopyableUserData(REF_TO_CLASS, (PsiClass) resolvedElement);
        }
    }
    for (PsiElement child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        encodeContextInfo(child, scope);
    }
}
Also used : GrAccessorMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrAccessorMethod) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Example 15 with GroovyPsiElement

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

the class GroovyNameSuggestionProvider method getSuggestedNames.

@Override
public SuggestedNameInfo getSuggestedNames(final PsiElement element, @Nullable PsiElement nameSuggestionContext, Set<String> result) {
    if (nameSuggestionContext == null)
        nameSuggestionContext = element;
    if (element instanceof GrVariable && nameSuggestionContext instanceof GroovyPsiElement) {
        final PsiType type = ((GrVariable) element).getTypeGroovy();
        if (type != null) {
            final String[] names = GroovyNameSuggestionUtil.suggestVariableNameByType(type, new DefaultGroovyVariableNameValidator((GroovyPsiElement) nameSuggestionContext));
            result.addAll(Arrays.asList(names));
            return new SuggestedNameInfo(names) {

                @Override
                public void nameChosen(String name) {
                    JavaStatisticsManager.incVariableNameUseCount(name, JavaCodeStyleManager.getInstance(element.getProject()).getVariableKind((GrVariable) element), ((GrVariable) element).getName(), type);
                }
            };
        }
    }
    return null;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) PsiType(com.intellij.psi.PsiType)

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