Search in sources :

Example 96 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GrMainCompletionProvider method suggestVariableNames.

private static void suggestVariableNames(PsiElement context, CompletionResultSet result) {
    final PsiElement parent = context.getParent();
    if (GroovyCompletionUtil.isWildcardCompletion(context))
        return;
    if (parent instanceof GrVariable) {
        final GrVariable variable = (GrVariable) parent;
        if (context.equals(variable.getNameIdentifierGroovy())) {
            final PsiType type = variable.getTypeGroovy();
            if (type != null) {
                final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(context.getProject());
                VariableKind kind = variable instanceof GrParameter ? VariableKind.PARAMETER : variable instanceof GrField ? VariableKind.FIELD : VariableKind.LOCAL_VARIABLE;
                SuggestedNameInfo suggestedNameInfo = codeStyleManager.suggestVariableName(kind, null, null, type);
                String[] names = suggestedNameInfo.names;
                if (names.length > 0) {
                    String name = names[0];
                    String newName = InlineMethodConflictSolver.suggestNewName(name, null, parent);
                    if (!name.equals(newName)) {
                        result.addElement(LookupElementBuilder.create(newName));
                        return;
                    }
                }
                for (String name : names) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
            GrExpression initializer = variable.getInitializerGroovy();
            if (initializer != null) {
                for (String name : GroovyNameSuggestionUtil.suggestVariableNames(initializer, new DefaultGroovyVariableNameValidator(variable), variable.hasModifierProperty(PsiModifier.STATIC))) {
                    result.addElement(LookupElementBuilder.create(name));
                }
            }
        }
    }
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) DefaultGroovyVariableNameValidator(org.jetbrains.plugins.groovy.refactoring.DefaultGroovyVariableNameValidator) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) SuggestedNameInfo(com.intellij.psi.codeStyle.SuggestedNameInfo) VariableKind(com.intellij.psi.codeStyle.VariableKind)

Example 97 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyDocumentationProvider method getQuickNavigateInfo.

@Override
@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    if (element instanceof GrVariable || element instanceof GrImplicitVariable) {
        StringBuilder buffer = new StringBuilder();
        PsiVariable variable = (PsiVariable) element;
        if (originalElement instanceof GrVariableDeclaration && ((GrVariableDeclaration) originalElement).getVariables().length > 1) {
            for (GrVariable var : ((GrVariableDeclaration) originalElement).getVariables()) {
                generateVariableInfo(originalElement, buffer, var);
                buffer.append("\n\n");
            }
        } else {
            generateVariableInfo(originalElement, buffer, variable);
        }
        return buffer.toString();
    } else if (element instanceof PsiMethod) {
        StringBuilder buffer = new StringBuilder();
        PsiMethod method = (PsiMethod) element;
        if (method instanceof GrGdkMethod) {
            buffer.append("[GDK] ");
        } else {
            PsiClass hisClass = method.getContainingClass();
            if (hisClass != null) {
                String qName = hisClass.getQualifiedName();
                if (qName != null) {
                    buffer.append(qName).append("\n");
                }
            }
        }
        PsiSubstitutor substitutor = calcSubstitutor(originalElement);
        if (!method.isConstructor()) {
            final PsiType substituted = substitutor.substitute(PsiUtil.getSmartReturnType(method));
            PsiImplUtil.appendTypeString(buffer, substituted, originalElement);
            buffer.append(" ");
        }
        buffer.append(method.getName()).append(" ");
        buffer.append("(");
        PsiParameter[] parameters = method.getParameterList().getParameters();
        for (int i = 0; i < parameters.length; i++) {
            PsiParameter parameter = parameters[i];
            if (i > 0)
                buffer.append(", ");
            if (parameter instanceof GrParameter) {
                GroovyPresentationUtil.appendParameterPresentation((GrParameter) parameter, substitutor, TypePresentation.LINK, buffer);
            } else {
                PsiType type = parameter.getType();
                PsiImplUtil.appendTypeString(buffer, substitutor.substitute(type), originalElement);
                buffer.append(" ");
                buffer.append(parameter.getName());
            }
        }
        buffer.append(")");
        final PsiClassType[] referencedTypes = method.getThrowsList().getReferencedTypes();
        if (referencedTypes.length > 0) {
            buffer.append("\nthrows ");
            for (PsiClassType referencedType : referencedTypes) {
                PsiImplUtil.appendTypeString(buffer, referencedType, originalElement);
                buffer.append(", ");
            }
            buffer.delete(buffer.length() - 2, buffer.length());
        }
        return buffer.toString();
    } else if (element instanceof GrTypeDefinition) {
        return generateClassInfo((GrTypeDefinition) element);
    }
    return null;
}
Also used : GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrImplicitVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrImplicitVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrGdkMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrGdkMethod) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) Nullable(org.jetbrains.annotations.Nullable)

Example 98 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GroovyCompletionUtil method isFirstElementAfterPossibleModifiersInVariableDeclaration.

/**
   * return true, if the element is first element after modifiers and there is no type element
   */
public static boolean isFirstElementAfterPossibleModifiersInVariableDeclaration(PsiElement element, boolean acceptParameter) {
    if (element.getParent() instanceof GrTypeDefinitionBody && !(element instanceof PsiComment)) {
        //is first on the line?
        String text = element.getContainingFile().getText();
        int i = CharArrayUtil.shiftBackward(text, element.getTextRange().getStartOffset() - 1, " \t");
        return i >= 0 && (text.charAt(i) == '\n' || text.charAt(i) == '{');
    }
    final PsiElement parent = element.getParent();
    if (!(parent instanceof GrVariable))
        return false;
    if (acceptParameter && parent instanceof GrParameter) {
        return ((GrParameter) parent).getTypeElementGroovy() == null;
    }
    final PsiElement pparent = parent.getParent();
    if (!(pparent instanceof GrVariableDeclaration))
        return false;
    if (((GrVariableDeclaration) pparent).isTuple())
        return false;
    final GrVariableDeclaration variableDeclaration = (GrVariableDeclaration) pparent;
    if (variableDeclaration.getTypeElementGroovy() != null)
        return false;
    return variableDeclaration.getVariables()[0] == parent;
}
Also used : GrTypeDefinitionBody(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinitionBody) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement)

Example 99 with GrParameter

use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.

the class GrMethodParametersFixer method apply.

@Override
public void apply(@NotNull Editor editor, @NotNull GroovySmartEnterProcessor processor, @NotNull PsiElement psiElement) {
    if (psiElement instanceof GrParameterList && psiElement.getParent() instanceof GrMethod) {
        PsiElement rParenth = psiElement.getNextSibling();
        if (rParenth == null)
            return;
        //      [todo] ends with comma
        if (!")".equals(rParenth.getText())) {
            int offset;
            GrParameterList list = (GrParameterList) psiElement;
            final GrParameter[] params = list.getParameters();
            if (params == null || params.length == 0) {
                offset = list.getTextRange().getStartOffset() + 1;
            } else {
                offset = params[params.length - 1].getTextRange().getEndOffset();
            }
            editor.getDocument().insertString(offset, ")");
        }
    }
}
Also used : GrParameterList(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) PsiElement(com.intellij.psi.PsiElement)

Aggregations

GrParameter (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter)99 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)22 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)19 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)19 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)18 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)16 GrParameterList (org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameterList)14 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)13 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)12 ArrayList (java.util.ArrayList)11 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)10 TextRange (com.intellij.openapi.util.TextRange)9 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)9 Nullable (org.jetbrains.annotations.Nullable)8 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)7 Project (com.intellij.openapi.project.Project)6 IncorrectOperationException (com.intellij.util.IncorrectOperationException)6 GroovyFile (org.jetbrains.plugins.groovy.lang.psi.GroovyFile)6 GrField (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField)6