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);
}
}
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;
}
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);
}
}
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;
}
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;
}
}
Aggregations