Search in sources :

Example 1 with NamedArgumentDescriptor

use of org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor in project intellij-community by JetBrains.

the class TestUtils method checkCompletionContains.

public static void checkCompletionContains(JavaCodeInsightTestFixture fixture, String... expectedVariants) {
    LookupElement[] lookupElements = fixture.completeBasic();
    Assert.assertNotNull(lookupElements);
    Set<String> missedVariants = ContainerUtil.newHashSet(expectedVariants);
    for (LookupElement lookupElement : lookupElements) {
        String lookupString = lookupElement.getLookupString();
        missedVariants.remove(lookupString);
        Object object = lookupElement.getObject();
        if (object instanceof ResolveResult) {
            object = ((ResolveResult) object).getElement();
        }
        if (object instanceof PsiMethod) {
            missedVariants.remove(lookupString + "()");
        } else if (object instanceof PsiVariable) {
            missedVariants.remove('@' + lookupString);
        } else if (object instanceof NamedArgumentDescriptor) {
            missedVariants.remove(lookupString + ':');
        }
    }
    if (missedVariants.size() > 0) {
        Assert.assertTrue("Some completion variants are missed " + missedVariants, false);
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) LookupElement(com.intellij.codeInsight.lookup.LookupElement)

Example 2 with NamedArgumentDescriptor

use of org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor in project intellij-community by JetBrains.

the class GroovyReferenceCharFilter method acceptChar.

@Override
@Nullable
public Result acceptChar(char c, int prefixLength, Lookup lookup) {
    final PsiFile psiFile = lookup.getPsiFile();
    if (psiFile != null && !psiFile.getViewProvider().getLanguages().contains(GroovyLanguage.INSTANCE))
        return null;
    LookupElement item = lookup.getCurrentItem();
    if (item == null)
        return null;
    if (Character.isJavaIdentifierPart(c) || c == '\'') {
        return Result.ADD_TO_PREFIX;
    }
    int caret = lookup.getEditor().getCaretModel().getOffset();
    if (c == '.' && prefixLength == 0 && !lookup.isSelectionTouched() && caret > 0 && lookup.getEditor().getDocument().getCharsSequence().charAt(caret - 1) == '.') {
        return Result.HIDE_LOOKUP;
    }
    if (c == ':') {
        PsiFile file = lookup.getPsiFile();
        PsiDocumentManager.getInstance(file.getProject()).commitDocument(lookup.getEditor().getDocument());
        PsiElement element = file.findElementAt(Math.max(caret - 1, 0));
        if (PsiJavaPatterns.psiElement().withParent(PsiJavaPatterns.psiElement(GrReferenceExpression.class).withParent(StandardPatterns.or(PsiJavaPatterns.psiElement(GrCaseLabel.class), PsiJavaPatterns.psiElement(GrConditionalExpression.class)))).accepts(element)) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        if (item.getObject() instanceof NamedArgumentDescriptor && (MapArgumentCompletionProvider.IN_ARGUMENT_LIST_OF_CALL.accepts(element) || MapArgumentCompletionProvider.IN_LABEL.accepts(element))) {
            return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
        }
        return Result.HIDE_LOOKUP;
    }
    if (c == '[' || c == ']' || c == ')' || c == '>')
        return CharFilter.Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '<' && item.getObject() instanceof PsiClass)
        return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
    if (c == '(' && PsiKeyword.RETURN.equals(item.getLookupString())) {
        return Result.HIDE_LOOKUP;
    }
    return null;
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) LookupElement(com.intellij.codeInsight.lookup.LookupElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with NamedArgumentDescriptor

use of org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor in project intellij-community by JetBrains.

the class MapArgumentCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    PsiElement mapOrArgumentList = findMapOrArgumentList(parameters);
    if (mapOrArgumentList == null) {
        return;
    }
    if (isMapKeyCompletion(parameters)) {
        result.stopHere();
    }
    final Map<String, NamedArgumentDescriptor> map = ContainerUtil.newHashMap(calculateNamedArguments(mapOrArgumentList));
    for (GrNamedArgument argument : getSiblingNamedArguments(mapOrArgumentList)) {
        map.remove(argument.getLabelName());
    }
    for (Map.Entry<String, NamedArgumentDescriptor> entry : map.entrySet()) {
        LookupElementBuilder lookup = LookupElementBuilder.create(entry.getValue(), entry.getKey()).withInsertHandler(NamedArgumentInsertHandler.INSTANCE).withTailText(":");
        if (entry.getValue().getPriority() == NamedArgumentDescriptor.Priority.UNLIKELY) {
            lookup = lookup.withItemTextForeground(GroovySyntaxHighlighter.MAP_KEY.getDefaultAttributes().getForegroundColor());
        } else {
            lookup = lookup.withIcon(JetgroovyIcons.Groovy.DynamicProperty);
        }
        LookupElement customized = entry.getValue().customizeLookupElement(lookup);
        result.addElement(customized == null ? lookup : customized);
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) GrNamedArgument(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) HashMap(java.util.HashMap) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement)

Example 4 with NamedArgumentDescriptor

use of org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor in project intellij-community by JetBrains.

the class MapArgumentCompletionProvider method findOtherNamedArgumentsInFile.

@NotNull
private static Map<String, NamedArgumentDescriptor> findOtherNamedArgumentsInFile(PsiElement mapOrArgumentList) {
    final Map<String, NamedArgumentDescriptor> map = new HashMap<>();
    mapOrArgumentList.getContainingFile().accept(new PsiRecursiveElementWalkingVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof GrArgumentLabel) {
                final String name = ((GrArgumentLabel) element).getName();
                if (GroovyNamesUtil.isIdentifier(name)) {
                    map.put(name, NamedArgumentDescriptor.SIMPLE_UNLIKELY);
                }
            }
            super.visitElement(element);
        }
    });
    return map;
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) PsiRecursiveElementWalkingVisitor(com.intellij.psi.PsiRecursiveElementWalkingVisitor) HashMap(java.util.HashMap) GrArgumentLabel(org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrArgumentLabel) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with NamedArgumentDescriptor

use of org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor in project intellij-community by JetBrains.

the class GrKindWeigher method weigh.

@Override
public Comparable weigh(@NotNull LookupElement element, @NotNull CompletionLocation location) {
    final PsiElement position = location.getCompletionParameters().getPosition();
    if (!(position.getContainingFile() instanceof GroovyFileBase))
        return null;
    Object o = element.getObject();
    if (o instanceof ResolveResult) {
        o = ((ResolveResult) o).getElement();
    }
    final PsiElement parent = position.getParent();
    final PsiElement qualifier = parent instanceof GrReferenceElement ? ((GrReferenceElement) parent).getQualifier() : null;
    if (qualifier == null) {
        if (o instanceof NamedArgumentDescriptor) {
            switch(((NamedArgumentDescriptor) o).getPriority()) {
                case ALWAYS_ON_TOP:
                    return NotQualifiedKind.onTop;
                case AS_LOCAL_VARIABLE:
                    return NotQualifiedKind.local;
                default:
                    return NotQualifiedKind.unknown;
            }
        }
        if (o instanceof PsiVariable && !(o instanceof PsiField)) {
            return NotQualifiedKind.local;
        }
        PsiTypeLookupItem item = element.as(PsiTypeLookupItem.CLASS_CONDITION_KEY);
        if (item != null && item.getBracketsCount() > 0) {
            return NotQualifiedKind.arrayType;
        }
        if (isPriorityKeyword(o))
            return NotQualifiedKind.local;
        if (isLightElement(o))
            return NotQualifiedKind.unknown;
        if (o instanceof PsiClass) {
            if (((PsiClass) o).isAnnotationType() && GrMainCompletionProvider.AFTER_AT.accepts(position)) {
                final GrAnnotation annotation = PsiTreeUtil.getParentOfType(position, GrAnnotation.class);
                if (annotation != null) {
                    PsiElement annoParent = annotation.getParent();
                    PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
                    PsiAnnotation.TargetType[] elementTypeFields = GrAnnotationImpl.getApplicableElementTypeFields(ownerToUse);
                    if (AnnotationTargetUtil.findAnnotationTarget((PsiClass) o, elementTypeFields) != null) {
                        return NotQualifiedKind.restrictedClass;
                    }
                }
            }
            if (GrMainCompletionProvider.IN_CATCH_TYPE.accepts(position) && InheritanceUtil.isInheritor((PsiClass) o, CommonClassNames.JAVA_LANG_THROWABLE)) {
                return NotQualifiedKind.restrictedClass;
            }
        }
        if (o instanceof PsiMember) {
            final PsiClass containingClass = ((PsiMember) o).getContainingClass();
            if (isAccessor((PsiMember) o))
                return NotQualifiedKind.accessor;
            if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
                return NotQualifiedKind.unknown;
            if (o instanceof PsiClass)
                return NotQualifiedKind.innerClass;
            if (PsiTreeUtil.isContextAncestor(containingClass, position, false))
                return NotQualifiedKind.currentClassMember;
            return NotQualifiedKind.member;
        }
        return NotQualifiedKind.unknown;
    } else {
        if (o instanceof PsiEnumConstant)
            return QualifiedKind.enumConstant;
        if (isLightElement(o))
            return QualifiedKind.unknown;
        if (o instanceof PsiMember) {
            if (isTrashMethod((PsiMember) o))
                return QualifiedKind.unknown;
            if (isAccessor((PsiMember) o))
                return QualifiedKind.accessor;
            if (isQualifierClassMember((PsiMember) o, qualifier)) {
                return QualifiedKind.currentClassMember;
            }
            if (o instanceof PsiClass && ((PsiClass) o).getContainingClass() == null || o instanceof PsiPackage)
                return QualifiedKind.unknown;
            if (o instanceof PsiClass)
                return QualifiedKind.innerClass;
            return QualifiedKind.member;
        }
        return QualifiedKind.unknown;
    }
}
Also used : NamedArgumentDescriptor(org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor) PsiTypeLookupItem(com.intellij.codeInsight.lookup.PsiTypeLookupItem) GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) GrAnnotation(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.annotation.GrAnnotation) GrReferenceElement(org.jetbrains.plugins.groovy.lang.psi.GrReferenceElement)

Aggregations

NamedArgumentDescriptor (org.jetbrains.plugins.groovy.extensions.NamedArgumentDescriptor)12 GrNamedArgument (org.jetbrains.plugins.groovy.lang.psi.api.statements.arguments.GrNamedArgument)4 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 PsiElement (com.intellij.psi.PsiElement)3 HashMap (java.util.HashMap)3 NotNull (org.jetbrains.annotations.NotNull)3 Map (java.util.Map)2 Nullable (org.jetbrains.annotations.Nullable)2 GrString (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.literals.GrString)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 PsiTypeLookupItem (com.intellij.codeInsight.lookup.PsiTypeLookupItem)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiMethod (com.intellij.psi.PsiMethod)1 PsiRecursiveElementWalkingVisitor (com.intellij.psi.PsiRecursiveElementWalkingVisitor)1 PsiType (com.intellij.psi.PsiType)1 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)1 IElementType (com.intellij.psi.tree.IElementType)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CustomMembersGenerator (org.jetbrains.plugins.groovy.dsl.CustomMembersGenerator)1